v icon indicating copy to clipboard operation
v copied to clipboard

build error: comptime `if` in default field value

Open kahsa opened this issue 2 years ago • 1 comments

V version: 0.3.0 d8b0df1 OS: ArchLinux

What did you do?

minimal reproducible example

struct Style{
	color string = $if android { 'red' } $else { 'blue' }
}

struct Foo{
	style Style //= Style{}   // works if uncomment
}

fn new() &Foo{   // works if remove this function
	return &Foo{}
}

What did you expect to see? No error.

What did you see instead?

==================
/usr/lib64/crt1.o: error: Invalid relocation entry [15] '.rela.debug_info' @ 000004f5
tcc: error: file 'crt1.o' not found
/tmp/v_1000/charttest.4118770822571839236.tmp.c:12099: error: function pointer expected
...
==================

kahsa avatar Jul 23 '22 15:07 kahsa

It should be giving an error about an uninitialized field.

Of course, you can easily make the code work by simply embedding the struct instead of making a separate field...

struct Foo {
	Style
}

JalonSolov avatar Jul 23 '22 15:07 JalonSolov

With latest V, the error message is now:

==================
/tmp/v_1000/../../../../../../home/jamie/foo.v:2: error: function pointer expected
...
==================
(Use `v -cg` to print the entire error message)
...

Making the suggested change allows the code to compile cleanly - the embedded Style is initialized at the same time as Foo.

JalonSolov avatar Jan 11 '23 15:01 JalonSolov