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

cgen: fix smartcast for struct fields(fix #20167) #20171

Merged
merged 1 commit into from
Dec 14, 2023

Conversation

shove70
Copy link
Contributor

@shove70 shove70 commented Dec 14, 2023

  1. Fixed Cgen doesn't unwrap sumtype to another #20167
  2. Add tests.
import x.json2

type Foo = f64 | int | string

fn f(j map[string]json2.Any) {
	println(j['name'] or { return })
	assert (j['name'] or { return } as string) == 'foo'
}

struct Bar {
	foo Foo
}

fn g(bar Bar) {
	f({
		'name':  json2.Any('foo')
		'value': match bar.foo {
			string { bar.foo }
			int { bar.foo }
			f64 { bar.foo }
		}
	})
}

fn main() {
	g(Bar{
		foo: Foo(42)
	})
}

outputs:

foo

@shove70 shove70 changed the title cgen: fix smartcast for struct fields in the match branches(fix #20167) cgen: fix smartcast for struct fields(fix #20167) Dec 14, 2023
Copy link
Member

@spytheman spytheman left a comment

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 f303679 into vlang:master Dec 14, 2023
54 checks passed
@shove70 shove70 deleted the patch-1 branch December 14, 2023 12:30
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.

Cgen doesn't unwrap sumtype to another
2 participants