v icon indicating copy to clipboard operation
v copied to clipboard

Generics compile failure

Open pd-giz-dave opened this issue 11 months ago • 1 comments

Describe the bug

Example code fails to compile. This may be a bug or a misunderstanding, but I found it unexpected and very confusing.

Reproduction Steps

Attempt to compile this:

struct Type1 {
	item f32
}
struct Type2 {
	item int
}
struct GenericThing[T] {
	part1 T
	part2 T
}
struct AnotherGenericThing[T] {
	part3 T
}
pub fn (thing GenericThing[T]) weird() AnotherGenericThing[int] {
	/* fails to compile
	 */
	$if T is Type2 {
		return AnotherGenericThing[int]{thing.part1.item + thing.part2.item}
	} $else $if T is Type1 {
		return AnotherGenericThing[int]{int(thing.part1.item + thing.part2.item)}
	} $else {
		$compile_error('unrecognised type')
	}
	/* compiles OK
	result := thing.part1.item + thing.part2.item
	$if T is Type2 {
		return AnotherGenericThing[int]{result}
	} $else $if T is Type1 {
		return AnotherGenericThing[int]{int(result)}
	} $else {
		$compile_error('unrecognised type')
	}
	 */
}
fn main() {
	thing1 := GenericThing{Type1{1},Type1{2}}
	result1 := thing1.weird()
	println(result1)
	thing2 := GenericThing{Type2{1},Type2{2}}
	result2 := thing2.weird()
	println(result2)
}

Expected Behavior

Expect it to compile and run without having to introduce an intermediate variable.

Current Behavior

Compile fails with a very confusing error:

compiler_bugs/main.v:20:51: error: `int` has no property `item`
   18 |         return AnotherGenericThing[int]{thing.part1.item + thing.part2.item}
   19 |     } $else $if T is Type1 {
   20 |         return AnotherGenericThing[int]{int(thing.part1.item + thing.part2.item)}
      |                                                         ~~~~
   21 |     } $else {
   22 |         $compile_error('unrecognised type')

Possible Solution

Introduce an intermediate variable.

Additional Information/Context

No response

V version

V 0.4.10 0b7a20d

Environment details (OS name and version, etc.)

v doctor
|V full version      |V 0.4.10 537605a.0b7a20d
|:-------------------|:-------------------
|OS                  |linux, "EndeavourOS Linux"
|Processor           |16 cpus, 64bit, little endian, AMD Ryzen 7 5800H with Radeon Graphics
|Memory              |7.73GB/30.74GB
|                    |
|V executable        |/home/dave/v/v
|V last modified time|2025-05-13 10:37:37
|                    |
|V home dir          |OK, value: /home/dave/v
|VMODULES            |OK, value: /home/dave/.vmodules
|VTMP                |OK, value: /tmp/v_1000
|Current working dir |OK, value: /home/dave/precious/fellsafe/v
|                    |
|Git version         |git version 2.49.0
|V git status        |weekly.2025.16-137-g0b7a20d9
|.git/config present |true
|                    |
|cc version          |cc (GCC) 15.1.1 20250425
|gcc version         |gcc (GCC) 15.1.1 20250425
|clang version       |clang version 19.1.7
|tcc version         |tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
|tcc git status      |thirdparty-linux-amd64 0134e9b9
|emcc version        |N/A
|glibc version       |ldd (GNU libc) 2.41

[!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.

pd-giz-dave avatar May 13 '25 10:05 pd-giz-dave

Connected to Huly®: V_0.6-22840

huly-for-github[bot] avatar May 13 '25 10:05 huly-for-github[bot]