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

Uncaught type checking error with options and pointers #19555

Closed
SlashScreen opened this issue Oct 12, 2023 · 0 comments · Fixed by #20195
Closed

Uncaught type checking error with options and pointers #19555

SlashScreen opened this issue Oct 12, 2023 · 0 comments · Fixed by #20195
Labels
Bug This tag is applied to issues which reports bugs. Build V build error on any OS/CPU architecture. Option Type Bugs/feature requests, that are related to `?Type`. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@SlashScreen
Copy link
Contributor

SlashScreen commented Oct 12, 2023

Describe the bug

When running this code, I get a compiler error with the C backend.

error: cannot convert 'struct _option_main__MyType' to 'unsigned char'

Reproduction Steps

run this code:

module main
import rand

struct MyType {
	foo string
}

struct TypeWithOption {
	maybe ?&MyType
}

fn main() {
	println("trying...")
	x := rand.int()

	ptr := if x > 0 {
		?MyType(MyType{foo:"bar"})
	} else {
		?MyType(none)
	}

	container := TypeWithOption{maybe:ptr} // Expects ?&MyType, not ?MyType. Sema doesn't catch this, I guess.
	println("success!")
	println(container)
}

Expected Behavior

Printout:

trying...
success!
TypeWithOption{
    maybe: &Option(MyType{
        foo: 'bar'
    })
}

Current Behavior

==================
C:/Users/talle/AppData/Local/Temp/v_0/main.8318066459009658785.tmp.c:15503: warning: assignment from incompatible pointer type
C:/Users/talle/AppData/Local/Temp/v_0/main.8318066459009658785.tmp.c:16821: error: cannot convert 'struct _option_main__MyType' to 'unsigned char'
...
==================
(Use `v -cg` to print the entire error message)

builder error:
==================
C error. This should never happen.

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

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

Possible Solution

It isn't seeing that the struct accepts a ?&MyType, not a ?MyType. Sema might not catch this type discrepancy.

Additional Information/Context

No response

V version

V 0.4.2 11e67a1

Environment details (OS name and version, etc.)

Windows 11
V originally installed from Scoop, but updated using v up.

Note

You can vote for this issue using the 👍 reaction. More votes increase the issue's priority for developers.

Take into account that only the 👍 reaction counts as a vote.
Only reactions to the issue itself will be counted as votes, not comments.

@SlashScreen SlashScreen added the Bug This tag is applied to issues which reports bugs. label Oct 12, 2023
@ArtemkaKun ArtemkaKun added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Option Type Bugs/feature requests, that are related to `?Type`. Build V build error on any OS/CPU architecture. labels Oct 12, 2023
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. Build V build error on any OS/CPU architecture. Option Type Bugs/feature requests, that are related to `?Type`. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants