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

C error when init struct fields with both local match and function with result #23661

Open
jorgeluismireles opened this issue Feb 7, 2025 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@jorgeluismireles
Copy link

jorgeluismireles commented Feb 7, 2025

Describe the bug

We initialize a struct with two fields. Field one with a local match and field two with a function returning a result. This combination can't be compiled.

Reproduction Steps

struct Bauds {
	standard bool
	bauds    u32
}

fn max(v u32) ! u32 {
	if v > 38400 {
		return error('too fast')
	}
	return v
}

fn new_bauds(bauds u32) ! &Bauds {
	return &Bauds{
		standard: match bauds {
			300,600,1200,2400,4800,9600,14400,19200,38400 { true }
			else { false }
		}
		bauds: max(bauds)!
	}
}

println(new_bauds(310)!)

Expected Behavior

No C error

Current Behavior

Running code...
Can't run code. The server returned an error:
/tmp/v_60000/../../../../../../box/code.v:15: error: ')' expected (got "_result_u32")
builder error: 
==================
C error found. It should never happen, when compiling pure V code.

Possible Solution

Workarounds:

  • Precalculate the first field with match before init.
  • Propagate the error of the second field before init.

Additional Information/Context

No response

V version

V 0.4.9 b91bbad

Environment details (OS name and version, etc.)

https://play.vlang.io/p/6e15c8929d

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.

@jorgeluismireles jorgeluismireles added the Bug This tag is applied to issues which reports bugs. label Feb 7, 2025
Copy link

Connected to Huly®: V_0.6-22082

@felipensp felipensp added the Unit: cgen Bugs/feature requests, that are related to the default C generating backend. label Feb 9, 2025
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. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

No branches or pull requests

2 participants