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
cmd/compile: allow typechecking loops with & #17533
Comments
The current initialization rules disallow this: It's not about how (go/types also disallows this.) |
p.s. if I do:
Then it typechecks, but then dies with:
|
So why can't I use the address of x to initialize x? It seems the |
Same thing: it's an initialization loop per the spec rules. |
We could change the rules I guess, but it general, taking the address of a variable being initialized and using that address during initialization doesn't guarantee that we don't indirect that address and then use the variable during initialization. |
Per the spec: "Dependency analysis does not rely on the actual values of the variables, only on lexical references to them in the source, analyzed transitively." (https://golang.org/ref/spec#Package_initialization) |
Agreed that it seems to be an initialization loop, so it's not allowed by the spec currently. FWIW, it does seem like a bug though that we fail to typecheck "var x = T{&x}" the same as "var x T = T{&x}", but since it's an initialization loop anyway, I'm not sure it's worth fixing that corner case. |
PS: I guess we could change the spec as it would be a backward-compatible change. But it wouldn't catch e.g. the following error anymore:
|
Yes, I guess that makes sense. I'll have to figure out some other way to get those pointers initialized. Maybe some init functions to patch up those links later. |
I've always wanted to make my code depend on linker layout order... |
This code does not compile:
I don't see any obvious reason why this wouldn't be typecheckable.
This comes up when trying to compile descriptors for recursive .proto messages.
The current proto compiler accepts such a message. I'm doing some experiments to optimize the proto-generated code. Part of that is having a descriptor for a
Recursive
message being able to point to itself (to describe itsx
field).@griesemer @mdempsky @dsnet
The text was updated successfully, but these errors were encountered: