Skip to content

cgen: fix infix expr and/or operate unnecessary evaluate (fix #21590) - #21740

Merged
spytheman merged 1 commit into
vlang:masterfrom
yuyi98:fix_if_cond_order
Jun 27, 2024
Merged

spytheman merged 1 commit into
vlang:masterfrom
yuyi98:fix_if_cond_order

Conversation

@yuyi98

@yuyi98 yuyi98 commented Jun 26, 2024

Copy link
Copy Markdown
Member

This PR fix infix expr and/or operate unnecessary evaluate (fix #21590).

  • Fix infix expr and/or operate unnecessary evaluate.
  • Add test.
fn f(s string) !int {
	if s == '' {
		return error('invalid s')
	}
	return s.len
}

fn main() {
	v := ''
	x := v != 'xyz' || f(v)! < f('abc')!
	dump(x)
	assert x

	y := v == 'xyz' && f(v)! < f('abc')!
	dump(y)
	assert !y
}

PS D:\Test\v\tt1> v run .
[.\\tt1.v:11] x: true
[.\\tt1.v:15] y: false

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman
spytheman merged commit d9a300c into vlang:master Jun 27, 2024
@yuyi98
yuyi98 deleted the fix_if_cond_order branch June 27, 2024 06:01
raw-bin pushed a commit to raw-bin/v that referenced this pull request Jul 2, 2024
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.

performance, robustness: don't evaluate expressions in conditions unnecessarily

2 participants