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 error for Sumtype( f() or { variant_value } ) #22835

Closed
spytheman opened this issue Nov 11, 2024 · 4 comments · Fixed by #22840
Closed

cgen error for Sumtype( f() or { variant_value } ) #22835

spytheman opened this issue Nov 11, 2024 · 4 comments · Fixed by #22840
Assignees
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. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@spytheman
Copy link
Member

spytheman commented Nov 11, 2024

V doctor:

V full version: V 0.4.8 a132d97.ec1f95a
OS: linux, Ubuntu 20.04.6 LTS
Processor: 2 cpus, 64bit, little endian, Intel(R) Core(TM) i3-3225 CPU @ 3.30GHz

getwd: /home/delian/code/v
vexe: /home/delian/code/v/v
vexe mtime: 2024-11-11 17:27:28

vroot: OK, value: /home/delian/code/v
VMODULES: OK, value: /home/delian/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.46.0
Git vroot status: weekly.2024.46-1-gec1f95ac-dirty
.git/config present: true

CC version: cc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
emcc version: N/A
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

What did you do?
./v -g -o vdbg cmd/v && ./vdbg oo.v

type Aa = string | int

fn f() !string {
	return ''
}

y := Aa(f() or { 2 })
println(y)

What did you expect to see?

a compiled program, or a checker error

What did you see instead?

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/oo.01JCE5SGAZ2BEB2ATKXPA41MXY.tmp.c:13771: error: cannot convert 'int' to 'struct string'
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).

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.

Huly®: V_0.6-21280

@spytheman spytheman added Bug This tag is applied to issues which reports bugs. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Checker Bugs/feature requests, that are related to the type checker. labels Nov 11, 2024
@spytheman
Copy link
Member Author

Note, an alternative that compiles is:

type Aa = string | int
fn f() !string { return '' }
y := if x := f() { Aa(x) } else { 2 }
println(y)

@spytheman
Copy link
Member Author

The checker has to produce an error for y := Aa(f() or { 2 }), since the 2 in the or branch is not a string, i.e. the expected type there should come from the return type of f() which is !string -> only a string value is valid as a default value in the or branch.

@spytheman
Copy link
Member Author

spytheman commented Nov 11, 2024

i.e. the goal is that the type of the whole expression f() or { expr } should be string as well, and then the cast to a sumtype value can work properly.

@felipensp felipensp self-assigned this Nov 12, 2024
@felipensp
Copy link
Member

And along this points it should allow foo() or {} and foo() or { foo = 1; }, that is... is_return_used may help us now :-)

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. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants