golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.7k stars 17.62k forks source link

go/types, types2: missing error for invalid cycle in function declaration #43215

Open findleyr opened 3 years ago

findleyr commented 3 years ago

The following invalid cycle is currently detected by the compiler with the error "typechecking loop involving f".

func f([unsafe.Sizeof(f)]int) {}

https://play.golang.org/p/cfAFJHgsAkX

However no error is detected by go/types, both at master and in the dev.typeparams branch (where there have been some changes to cycle detection in function declarations).

CC @griesemer

griesemer commented 3 years ago

I don't believe this is a bug, and certainly not a release blocker. The spec says:

The functions Alignof and Sizeof take an expression x of any type and return the alignment or size, respectively, of a hypothetical variable v as if v was declared via var v = x.

The size of a variable of function type is independent of the function type and always the same, so arguably this is valid code.

The spec also doesn't explicitly disallow it.

[edited]

Interestingly, we do get a bug (initialization cycle) for this code:

package p
import "unsafe"
var f func([unsafe.Sizeof(f)]int)

which is not an initialization cycle (there is no initialization expression). Again, this shouldn't be a problem for the compiler/type-checker either. In any case, these are not release-blockers.

For reference, gccgo accepts both these cases (possibly accidentally) without error and the function argument is [8]int on a 64-bit platform.

griesemer commented 3 years ago

Re-opening per edited comment above.