cmd/compile: reject not-in-heap types as type arguments

After running the types2 type checker, walk info.Instances to reject
any not-in-heap type arguments. This is feasible to check using the
types2 API now, thanks to #46731.

Fixes #54765.

Change-Id: Idd2acc124d102d5a76f128f13c21a6e593b6790b
Reviewed-on: https://go-review.googlesource.com/c/go/+/427235
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Keith Randall <khr@google.com>
This commit is contained in:
Matthew Dempsky 2022-08-31 15:48:35 -07:00
parent e4b624eae5
commit ca634fa2c5
3 changed files with 79 additions and 0 deletions

View file

@ -256,3 +256,28 @@ func isTypeParam(t types2.Type) bool {
_, ok := t.(*types2.TypeParam)
return ok
}
// isNotInHeap reports whether typ is or contains an element of type
// runtime/internal/sys.NotInHeap.
func isNotInHeap(typ types2.Type) bool {
if named, ok := typ.(*types2.Named); ok {
if obj := named.Obj(); obj.Name() == "nih" && obj.Pkg().Path() == "runtime/internal/sys" {
return true
}
typ = named.Underlying()
}
switch typ := typ.(type) {
case *types2.Array:
return isNotInHeap(typ.Elem())
case *types2.Struct:
for i := 0; i < typ.NumFields(); i++ {
if isNotInHeap(typ.Field(i).Type()) {
return true
}
}
return false
default:
return false
}
}