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

checker, cgen: fix match branches return type ref mismatch, when return type exists interface or sumtype(fix #16203) #19806

Merged
merged 2 commits into from
Nov 13, 2023

Conversation

shove70
Copy link
Contributor

@shove70 shove70 commented Nov 8, 2023

  1. Fixed Interface with string instance accessing underlying value missing * dereference (incompatible operand types) error #16203
  2. Add tests.

Because smartcast adds one level of ref to the interface type, special handling is required in the return type of the match expr branch.

interface Any {}

fn (any Any) echo() {
	r := match any {
		string { any }
		else { 'unsupported' }
	}
	println(r)
}

Any('hallo').echo()

outputs:

hallo

@shove70 shove70 closed this Nov 8, 2023
@shove70 shove70 changed the title checker, cgen: fix branches of match return both sumtype and non-sumtype(fix #16203) checker, cgen: fix branches of match return both interface and non-interface(fix #16203) Nov 8, 2023
@shove70 shove70 reopened this Nov 8, 2023
@shove70 shove70 marked this pull request as ready for review November 8, 2023 09:40
vlib/v/tests/match_test.v Outdated Show resolved Hide resolved
vlib/v/tests/match_test.v Outdated Show resolved Hide resolved
vlib/v/tests/match_test.v Outdated Show resolved Hide resolved
@shove70
Copy link
Contributor Author

shove70 commented Nov 9, 2023

Ok, I'll rearrange it.

I just noticed that sumtype has some C happen issues too:

type Any = int | string

fn (any Any) echo() {
	a := 'abc'
	r := match any {
		string { &any }
		else { a }
	}
	println(r)
}

Any('hallo').echo()
Running code...
Can't run code. The server returned an error:
/tmp/v_60000/../../../../../../box/code.v:5: error: type mismatch in conditional expression (have 'struct string *' and 'struct string')
builder error: 
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.
type Any = int | string

fn (any Any) echo() {
	a := 'abc'
	r := match any {
		string { any }
		else { &a }
	}
	println(r)
}

Any('hallo').echo()
Running code...
Can't run code. The server returned an error:
/tmp/v_60000/../../../../../../box/code.v:5: error: type mismatch in conditional expression (have 'struct string' and 'struct string *')
builder error: 
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.

Looks like the sumtype and interface match issues need to be handled together, and I will

@spytheman
Copy link
Member

Thank you @shove70 .

@shove70 shove70 changed the title checker, cgen: fix branches of match return both interface and non-interface(fix #16203) checker, cgen: fix match branches return type ref mismatch, when return type exists interface or sumtype(fix #16203) Nov 9, 2023
@shove70
Copy link
Contributor Author

shove70 commented Nov 9, 2023

Thank you @shove70 .

done, review it again when you're free, 🎉 @spytheman

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.

Good work.

@spytheman spytheman merged commit c9df064 into vlang:master Nov 13, 2023
54 checks passed
@shove70 shove70 deleted the match_ptr branch November 13, 2023 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants