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 for using a default := p or { T{} } construct in a generic function #22163

Closed
spytheman opened this issue Sep 4, 2024 · 6 comments · Fixed by #22164
Closed

cgen error for using a default := p or { T{} } construct in a generic function #22163

spytheman opened this issue Sep 4, 2024 · 6 comments · Fixed by #22164
Labels
Bug This tag is applied to issues which reports bugs. Option Type Bugs/feature requests, that are related to `?Type`. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@spytheman
Copy link
Member

spytheman commented Sep 4, 2024

V doctor:

V full version: V 0.4.7 f61a4f1.d5c2ebc
OS: linux, Ubuntu 20.04.6 LTS
Processor: 2 cpus, 64bit, little endian, Intel(R) Core(TM) i3-3225 CPU @ 3.30GHz

getwd: /space/v/oo
vexe: /space/v/oo/v
vexe mtime: 2024-09-04 19:52:03

vroot: OK, value: /space/v/oo
VMODULES: OK, value: /home/delian/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.46.0
Git vroot status: weekly.2024.36-8-gd5c2ebce
.git/config present: true

CC version: cc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

What did you do?
./v -g -o vdbg cmd/v && ./vdbg a.v

pub fn f[T](defaults ?T) {
	default := defaults or { T{} }
	dump(default)
}	
		 
f(123)

What did you expect to see?

a compiled program

What did you see instead?

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/a.01J6Z9GC8NRPDKZDBDK9QGBFNR.tmp.c:13299: error: switch expected
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).

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. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Option Type Bugs/feature requests, that are related to `?Type`. labels Sep 4, 2024
@spytheman
Copy link
Member Author

Note that a very similar program works fine:

pub fn f[T](defaults ?T) {
	the_default := defaults or { T{} }
	dump(the_default)
}	
		 
f(123)

@spytheman
Copy link
Member Author

Note also that default is a reserved keyword in C, and it is already in vlib/v/gen/c/cgen.v:23:const c_reserved .

Perhaps it is just a matter of escaping it, in yet another place?

@JalonSolov
Copy link
Contributor

C name collision. default is used in switch statements to specify the catch-all branch (like else in V's match).

Yet another case where all V variables should've been named _v_<whatever> to avoid problems.

@JalonSolov
Copy link
Contributor

We were typing at the same time, you were just faster. :-)

@medvednikov
Copy link
Member

@JalonSolov my goal has been to generate clean readable code. Like it was written by a human.

Every variable starting with v_ would break that. I'd prefer to just handle all reserved c keywords.

@spytheman
Copy link
Member Author

spytheman commented Sep 12, 2024

The reserved C keywords are a limited set, and can be handled.

However, in C programs, there can be global symbols, like function names and variable names, and that set is unlimited. We allow #include, so we get those, and we do not know what they are ahead of time, because we do not parse the C headers ourselves.

Due to that, the V names (that are also user controlled), can still clash with the global names from the C headers, that a V program includes.

The potential for conflicts for variables is minimized, because C allows shadowing of global names with ones from a local scope, and V mostly ensures that it always declares and initializes its own variables.

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. Option Type Bugs/feature requests, that are related to `?Type`. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants