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

Optional type matches incorrectly in $for field #20918

Closed
jcweaver997 opened this issue Feb 28, 2024 · 0 comments · Fixed by #20952
Closed

Optional type matches incorrectly in $for field #20918

jcweaver997 opened this issue Feb 28, 2024 · 0 comments · Fixed by #20952
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Comptime Features processed during compile time, like $if, $for, $env etc

Comments

@jcweaver997
Copy link

jcweaver997 commented Feb 28, 2024

Describe the bug

Accessing optional field in compile time code causes C error

Reproduction Steps

module main

interface TestInterface {
	test_func() f64
}

struct Struct1 {
	num f64
}

fn (o &Struct1) test_func() f64 {
	return o.num
}

struct Struct2 {
	s1 ?&Struct1
}

fn do_thing[T]() T {
	t := T{}
	$for field in T.fields {
		$if field.typ is TestInterface {
			i := TestInterface(t.$(field.name))
			println('${i.test_func()}')
		}
	}
	return t
}

pub fn main() {
	do_thing[Struct2]()
}

Expected Behavior

no C error

Current Behavior

C error:

PS C:\Users\jcwea\Documents\git\vtest> v run .
==================
C:/Users/jcwea/AppData/Local/Temp/v_0/vtest.01HQPQ5SW9NC7GKNV47M8KWAVT.tmp.c:6995: warning: implicit declaration of function 'tcc_backtrace'
C:/Users/jcwea/AppData/Local/Temp/v_0/vtest.01HQPQ5SW9NC7GKNV47M8KWAVT.tmp.c:13239: error: cannot convert 'struct _option_main__Struct1_ptr' to 'struct main__Struct1 *'
...
==================
(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

The best solution is probably to not match ?T's with $if field.typ is T, thus never calling the code in the $if statement. But we would likely still want to be able to match these optional field types somehow, maybe with a $if field.underlying_typ is T or something similar. I image it could work the same way as unwrapping an alias or a sum type during compile time.

Additional Information/Context

affects vgdextension

V version

V 0.4.4 7df5646

Environment details (OS name and version, etc.)

V full version: V 0.4.4 1733dab.7df5646
OS: windows, Microsoft Windows 11 Pro v26058 64-bit
Processor: 24 cpus, 64bit, little endian, 

getwd: C:\Users\jcwea\Documents\git\vtest
vexe: C:\Users\jcwea\Documents\git\v\v.exe
vexe mtime: 2024-02-27 22:26:52

vroot: OK, value: C:\Users\jcwea\Documents\git\v
VMODULES: OK, value: C:\Users\jcwea\.vmodules
VTMP: OK, value: C:\Users\jcwea\AppData\Local\Temp\v_0

Git version: git version 2.40.0.windows.1
Git vroot status: weekly.2024.09-4-g7df56462
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,
operable program or batch file.

thirdparty/tcc status: thirdparty-windows-amd64 b99a453d

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.

@jcweaver997 jcweaver997 added the Bug This tag is applied to issues which reports bugs. label Feb 28, 2024
@felipensp felipensp self-assigned this Mar 3, 2024
@felipensp felipensp added the Comptime Features processed during compile time, like $if, $for, $env etc label Mar 3, 2024
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. Comptime Features processed during compile time, like $if, $for, $env etc
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants