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

cmd/compile: incorrect error for function conversion based on cyclic definition (esoteric) #25305

Open
griesemer opened this issue May 9, 2018 · 5 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@griesemer
Copy link
Contributor

I'm only reporting this for future use should we ever get to a more thorough rethink of the type checker. This is a completely esoteric test case: https://play.golang.org/p/JjZZGCopERA

The error message is "cannot convert nil to type F" but F is clearly a function and thus nil can be converted into that function (of whatever type). The problem here is the type cycle; the conversion looks at a type that is not yet fully set up.

This example is primarily interesting as a test case.

@griesemer griesemer added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 9, 2018
@griesemer griesemer added this to the Unplanned milestone May 9, 2018
@griesemer griesemer self-assigned this May 9, 2018
@griesemer
Copy link
Contributor Author

For reference: go/types (accidentally) handles this correctly, and the following code type-checks w/o errors:

package p

import "unsafe"

type F func([unsafe.Sizeof(F(nil))]int) int

var f F
var _ = f([8]int{})

@cznic
Copy link
Contributor

cznic commented May 9, 2018

go/types (accidentally) handles this correctly

Why accidentally? The type in question has computable size. IMO that means it's not a cyclic type. It just refers to itself like type T *T does, for example.

@griesemer
Copy link
Contributor Author

@cznic I'm fairly sure it's not on purpose that it works. There's a reason why the compiler complains; we go through great lengths to detect such cycles, even if they are "obviously" not cyclic to a human reader.

The type in question has a computable size only because we know it's a function, but the function may not be set up yet (not even partially) as we are in the definition of that very function. It's non-trivial to get right which is why go/types and the compilers have various cycle-related issues, mostly esoteric and not relevant for real code (otherwise they would have shown up a long time ago).

But let's not discuss this here. It would be a very long discussion. The reason I am collecting these is in order to come up with a model of what we "should" accept and what we can safely refuse (even if a human reader could determine a type). Notably the spec remains silent on the subject exactly because it's tricky to specify.

@cznic
Copy link
Contributor

cznic commented May 9, 2018

@griesemer
Copy link
Contributor Author

For reference: gccgo handles this code w/o problems.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants