v icon indicating copy to clipboard operation
v copied to clipboard

Compile error when using `$if` with multiple type cases & generic struct

Open ken0x0a opened this issue 2 years ago • 0 comments

V version: V 0.3.0 7be9963 OS: macos, macOS, 12.5, 21G72

What did you do?

struct Calc<S> {
mut:
	typ S
}
struct TypeA {}
struct TypeB {}
fn (mut c Calc<S>) next<T>(input T) f64 {
	$if S is TypeA || S is TypeB {
		return c.typ.next(input)
	} $else {
		return 99.0
	}
}
fn (mut t TypeA) next<T>(input T) f64 {
	return 10
}
fn (mut t TypeB) next<T>(input T) f64 {
	return 11
}
fn new<S>() Calc<S> {
	$if S is TypeA {
		return Calc<TypeA> { typ: TypeA {} }
	} $else $if S is TypeB {
		return Calc<TypeB> { typ: TypeB {} }
	} $else {
		panic('unknown type $S.name')
	}
}
fn main() {
	{
		mut c := Calc<TypeA> {
			typ: TypeA {}
		}
		assert c.next(100) == 10.0
	}
	{
		mut c := Calc<TypeB> {
			typ: TypeB {}
		}
		assert c.next(100) == 11.0
	}
	println('OK!!')
}

What did you expect to see?

OK!!

What did you see instead?

==================
   ^~~~~~~~~~~~~~~
/tmp/v_502/comptime_generic_multiple_case.14815125428262686736.tmp.c:5758:13: error: implicit declaration of function 'main__TypeA_next_T_int' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
                f64 _t2 = main__TypeA_next_T_int(&c->typ, input);
                          ^
/tmp/v_502/comptime_generic_multiple_case.14815125428262686736.tmp.c:5772:13: error: implicit declaration of function 'main__TypeB_next_T_int' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
                f64 _t2 = main__TypeB_next_T_int(&c->typ, input);
                          ^
1 warning and 2 errors generated.
...
==================
(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

[[Command exited with 1]]

ken0x0a avatar Aug 09 '22 05:08 ken0x0a