aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/noder
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/compile/internal/noder')
-rw-r--r--src/cmd/compile/internal/noder/helpers.go25
-rw-r--r--src/cmd/compile/internal/noder/irgen.go26
2 files changed, 51 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/noder/helpers.go b/src/cmd/compile/internal/noder/helpers.go
index 4c9c6f6cc9..764dcb3f85 100644
--- a/src/cmd/compile/internal/noder/helpers.go
+++ b/src/cmd/compile/internal/noder/helpers.go
@@ -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
+ }
+}
diff --git a/src/cmd/compile/internal/noder/irgen.go b/src/cmd/compile/internal/noder/irgen.go
index ad937eac62..dc69e94924 100644
--- a/src/cmd/compile/internal/noder/irgen.go
+++ b/src/cmd/compile/internal/noder/irgen.go
@@ -6,6 +6,7 @@ package noder
import (
"fmt"
+ "sort"
"cmd/compile/internal/base"
"cmd/compile/internal/dwarfgen"
@@ -63,6 +64,31 @@ func checkFiles(noders []*noder) (posMap, *types2.Package, *types2.Info) {
pkg, err := conf.Check(base.Ctxt.Pkgpath, files, info)
+ // Implementation restriction: we don't allow not-in-heap types to
+ // be used as type arguments (#54765).
+ {
+ type nihTarg struct {
+ pos src.XPos
+ typ types2.Type
+ }
+ var nihTargs []nihTarg
+
+ for name, inst := range info.Instances {
+ for i := 0; i < inst.TypeArgs.Len(); i++ {
+ if targ := inst.TypeArgs.At(i); isNotInHeap(targ) {
+ nihTargs = append(nihTargs, nihTarg{m.makeXPos(name.Pos()), targ})
+ }
+ }
+ }
+ sort.Slice(nihTargs, func(i, j int) bool {
+ ti, tj := nihTargs[i], nihTargs[j]
+ return ti.pos.Before(tj.pos)
+ })
+ for _, targ := range nihTargs {
+ base.ErrorfAt(targ.pos, "cannot use incomplete (or unallocatable) type as a type argument: %v", targ.typ)
+ }
+ }
+
base.ExitIfErrors()
if err != nil {
base.FatalfAt(src.NoXPos, "conf.Check error: %v", err)