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

v.gen.c: fix error of if expr (fix #7337) #11182

Merged
merged 1 commit into from
Aug 15, 2021

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Aug 14, 2021

This PR fix error of if expr (fix #7337).

  • Fix error of if expr.
  • Add test.
fn main() {
	mut a := true
	b := a && if true {
		a = false
		true
	} else {
		false
	}
	println(b)
	assert b == true
}

PS D:\Test\v\tt1> v -autofree run .
true

generated codes:

VV_LOCAL_SYMBOL void main__main(void) {
	bool a = true;
	bool _t1 = a;
	bool _t2; /* if prepend */
	if (true) {
		a = false;
		_t2 = true;
	} else {
		_t2 = false;
	}
		bool b =  _t1 &&  _t2;
	string _t3 = b ? _SLIT("true") : _SLIT("false"); println(_t3); string_free(&_t3);
	;
}

@spytheman
Copy link
Member

spytheman commented Aug 14, 2021

That inverts the normal ordering. Try for example:

mut a := false

=> the inner if branches will still be executed, while normally, if x() && y() { } means that depending on the return value of x(), y() is short circuited and not evaluated.

@yuyi98
Copy link
Member Author

yuyi98 commented Aug 14, 2021

That's true. For now, it's just an alternative to the ternary operator solution, there are some special cases.
Normally this would be a redundant execution, but the result should be correct.

@medvednikov medvednikov merged commit d3cf53e into vlang:master Aug 15, 2021
@yuyi98 yuyi98 deleted the fix_if_expr_in_infix branch August 16, 2021 04:01
@danieldaeschle
Copy link
Member

I know nim solved this. It's possible to inspect their C generation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

result should be true, with autofree result is false
4 participants