You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: incorrect invalid field error in generic code #50148
package a
type S[T any] struct {
}
func (b *S[T]) build() *X[T] {
return &X[T]{f:0}
}
type X[T any] struct {
f int
}
b.go:
package b
import "./a"
func B() {
var x a.S[int]
_ = x
}
main.go:
package main
import "./b"
func main() {
b.B()
}
This should compile successfully. Instead we get the error:
a.go:7:16: invalid field name a.f in struct initializer
This is an error from the old typechecker when importing code (from b into main, I think). The error occurs in the phase where we're building more instantiations after inlining.
This example is reduced from an example generated by my generifier. It was originally package html_test importing package html importing a generified version of package string.
This error occurs as part of typechecking a new generic instantiation, with the generic definition being in a separate package. It's unhappy about typechecking a unexported field reference in a package other than the one being compiled.
A similar error does not occur in 1.17, because during inlining we don't ever typecheck imported code - we just deserialize the types that were serialized on export.
A similar error does not occur in 1.16, because the type checker is happy to typecheck unexported field references in other packages. The error case was added here (as part of 1.17 development): https://go-review.googlesource.com/c/go/+/277713@mdempsky
So I think we're ok allowing the typechecker to pass in this situation, as it used to 1.16 and earlier.
I do want to understand more about why we're calling the typechecker here. I don't think it should be necessary, which would probably be a better fix if it works out.
Activity
gopherbot commentedon Dec 14, 2021
Change https://golang.org/cl/371554 mentions this issue:
cmd/compile: allow field keys from different packagesrandall77 commentedon Dec 14, 2021
This error occurs as part of typechecking a new generic instantiation, with the generic definition being in a separate package. It's unhappy about typechecking a unexported field reference in a package other than the one being compiled.
A similar error does not occur in 1.17, because during inlining we don't ever typecheck imported code - we just deserialize the types that were serialized on export.
A similar error does not occur in 1.16, because the type checker is happy to typecheck unexported field references in other packages. The error case was added here (as part of 1.17 development): https://go-review.googlesource.com/c/go/+/277713 @mdempsky
So I think we're ok allowing the typechecker to pass in this situation, as it used to 1.16 and earlier.
I do want to understand more about why we're calling the typechecker here. I don't think it should be necessary, which would probably be a better fix if it works out.
mdempsky commentedon Dec 14, 2021
Removing the
types.LocalPkgcheck seems reasonable.cmd/compile: don't re-typecheck while importing