Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated match code for sumtypes with embedded members causes C errors #22481

Open
edam opened this issue Oct 10, 2024 · 0 comments
Open

Generated match code for sumtypes with embedded members causes C errors #22481

edam opened this issue Oct 10, 2024 · 0 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@edam
Copy link
Member

edam commented Oct 10, 2024

Describe the bug

When using match on a sumtype to access a member present in all variants, but where that member is in an embedded struct in one variant, the C code is generated incorrectly and appends the embedded struct name when accessing all variants of the sumtype.

Reproduction Steps

type SumType = StructA | StructB

struct StructA {
	member string
}

struct StructB {
	StructA
}

fn get_member(st SumType) string {
	match st {
		StructA, StructB {
			return st.member
		}
	}
}

Expected Behavior

Code should complie

Current Behavior

Compiler causes C error:

================== C compilation error (from cc): ==============
cc: /tmp/v_503/bug-sumtype-embedded-member.01J9VN69CT4FPD3NPZB806NEXK.tmp.c:13452:37: error: no member named 'StructA' in 'struct main__StructA'
cc:                 string _t1 = (*st._main__StructA).StructA.member;
cc:                              ~~~~~~~~~~~~~~~~~~~~ ^
cc: 1 error generated.
================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
builder error:
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https:/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .

Possible Solution

None

Additional Information/Context

Generated C code at fault:

// TypeDecl
VV_LOCAL_SYMBOL string main__get_member(main__SumType st) {
	if (st._typ == 102 /* main.StructA */) {
		string _t1 = (*st._main__StructA).StructA.member; // WRONG -- StructA has no StructA member!
		return _t1;
	}
	else if (st._typ == 103 /* main.StructB */) {
		string _t2 = (*st._main__StructB).StructA.member;
		return _t2;
	}
	
	return (string){.str=(byteptr)"", .is_lit=1};
}

V version

V 0.4.8 51b471b

Environment details (OS name and version, etc.)

MacOS

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@edam edam added the Bug This tag is applied to issues which reports bugs. label Oct 10, 2024
@edam edam changed the title Generated get_member() for sumtypes with embedded members causes C errors Generated match code for sumtypes with embedded members causes C errors Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

1 participant