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

cgen error, when compiling mut r3 := Zip[int,int]{}, for struct Zip[A,B] { a Iterator[A] b Iterator[B] } #23556

Open
spytheman opened this issue Jan 23, 2025 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. Interface handling Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@spytheman
Copy link
Member

spytheman commented Jan 23, 2025

V version: V 0.4.9 6b0c272, press to see full `v doctor` output
V full version V 0.4.9 bd10e12.6b0c272
OS linux, Ubuntu 20.04.6 LTS
Processor 4 cpus, 64bit, little endian, Intel(R) Core(TM) i3-3225 CPU @ 3.30GHz
Memory 3.3GB/15.05GB
V executable /home/delian/code/v/v
V last modified time 2025-01-23 09:58:28
V home dir OK, value: /home/delian/code/v
VMODULES OK, value: /home/delian/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/delian/code/v
Git version git version 2.47.1
V git status weekly.2025.03-25-g6b0c2722
.git/config present true
cc version cc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
gcc version gcc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
clang version clang version 10.0.0-4ubuntu1
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 (Ubuntu GLIBC 2.31-0ubuntu9.16) 2.31

What did you do?
./v -g -o vdbg cmd/v && ./vdbg /home/delian/r.v && /home/delian/r

pub struct Range[T] {
pub:
	from T
	to T
	inclusive bool
pub mut:	
	step T = T(1)
	i T
mut:	
	started bool
}

pub fn (mut r Range[T]) next[T]() ?T {
	if !r.started {
		r.started = true
		assert r.step > 0
		if r.from > r.to {
			r.step = -r.step
		}
		r.i = r.from
	}
	i := r.i
	next_i := i + r.step
	if r.inclusive {
		if r.step < 0 && i < r.to {
			return none
		}
		if r.step > 0 && i > r.to {
			return none
		}
	} else {
		if r.step < 0 && i <= r.to {
			return none
		}
		if r.step > 0 && i >= r.to {
			return none
		}
	}
	r.i = next_i
	return i
}


pub interface Iterator[T] {
mut:
	next() ?T
}

pub struct Zip[A,B] {
mut:
	a Iterator[A]
	b Iterator[B]
}
pub struct Pair[A,B] {
	a A
	b B
}

pub fn (mut z Zip[A,B]) next[A,B]() ?Pair[A,B] {
	a := z.a.next()?
	b := z.b.next()?
	return Pair[A,B]{a,b}
}

for i in Range{from: 5, to: 10} { println('from 5 to 10, i= ${i}') }
for i in Range{from: 5, to: 10, inclusive: true} { println('from 5 to 10, inclusive, i= ${i}') }
for i in Range{from: 10, to: 5, inclusive: true} { println('from 10 to 5, inclusive, i= ${i}') }
for i in Range{from: 10, to: 5} { println('from 10 to 5, i= ${i}') }

for x in Range{from: 2.0, to: 3.0, step: 0.5} { println('from 2.0 to 3.0, step: 0.5, i= ${x}') }
for x in Range{from: 2.0, to: 3.0, step: 0.5, inclusive: true} { println('from 2.0 to 3.0, step: 0.5, inclusive, i= ${x}') }

mut r1 := Range{from: 5, to: 10}
mut i1 := Iterator[int](r1)
println(i1)
mut r2 := Range{from: 10, to: 5}
mut i2 := Iterator[int](r2)
println(i2)

mut r3 := Zip[int,int]{} // cgen error for main__Iterator_T_int
//r3.a = i1 // checker error: cannot implement interface `Iterator<A>[int]` with a different interface `Iterator[int]`
//r3.b = i2 // checker error: cannot implement interface `Iterator<A>[int]` with a different interface `Iterator[int]`
//dump(r3)
//for x in r3 {
//	println(x)
//}

What did you see?

/home/delian/r.v:80:11: notice: interface field `Zip[int, int].a` must be initialized
   78 | println(i2)
   79 | 
   80 | mut r3 := Zip[int,int]{} // cgen error for main__Iterator_T_int
      |           ~~~~~~~~~~~~~~
   81 | //r3.a = i1 // checker error: cannot implement interface `Iterator<A>[int]` with a different interface `Iterator[int]`
   82 | //r3.b = i2 // checker error: cannot implement interface `Iterator<A>[int]` with a different interface `Iterator[int]`
/home/delian/r.v:80:11: notice: interface field `Zip[int, int].b` must be initialized
   78 | println(i2)
   79 | 
   80 | mut r3 := Zip[int,int]{} // cgen error for main__Iterator_T_int
      |           ~~~~~~~~~~~~~~
   81 | //r3.a = i1 // checker error: cannot implement interface `Iterator<A>[int]` with a different interface `Iterator[int]`
   82 | //r3.b = i2 // checker error: cannot implement interface `Iterator<A>[int]` with a different interface `Iterator[int]`
/home/delian/r.v:80:5: warning: unused variable: `r3`
   78 | println(i2)
   79 | 
   80 | mut r3 := Zip[int,int]{} // cgen error for main__Iterator_T_int
      |     ~~
   81 | //r3.a = i1 // checker error: cannot implement interface `Iterator<A>[int]` with a different interface `Iterator[int]`
   82 | //r3.b = i2 // checker error: cannot implement interface `Iterator<A>[int]` with a different interface `Iterator[int]`
================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/r.01JJ99K99KTPH0SK41R6JFASP5.tmp.c:861: error: struct/union/enum already defined
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).

What did you expect to see?

a compiled program

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.

@spytheman spytheman added Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. Interface handling Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. labels Jan 23, 2025
Copy link

Connected to Huly®: V_0.6-21984

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. Generics[T] Bugs/feature requests, that are related to the V generics. Interface handling Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

No branches or pull requests

1 participant