]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: don't crash if comp. literal element type has no core type
authorRobert Griesemer <gri@golang.org>
Wed, 23 Feb 2022 22:26:07 +0000 (14:26 -0800)
committerRobert Griesemer <gri@golang.org>
Thu, 24 Feb 2022 00:04:18 +0000 (00:04 +0000)
Instead, report a suitable error.

Fixes #51335.

Change-Id: Ifce90cb7487b1e99c6b4221c0d43bacc0c39dca8
Reviewed-on: https://go-review.googlesource.com/c/go/+/387676
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue51335.go2 [new file with mode: 0644]
src/go/types/expr.go
src/go/types/testdata/fixedbugs/issue51335.go2 [new file with mode: 0644]

index ac5630dbbb16d2b1fcfc63568c047e0f09bd3b87..c587c40f80f8c5abf21be6ea8335c394d8d7dc77 100644 (file)
@@ -1360,6 +1360,10 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
                        // no composite literal type present - use hint (element type of enclosing type)
                        typ = hint
                        base, _ = deref(coreType(typ)) // *T implies &T{}
+                       if base == nil {
+                               check.errorf(e, "invalid composite literal element type %s: no core type", typ)
+                               goto Error
+                       }
 
                default:
                        // TODO(gri) provide better error messages depending on context
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51335.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue51335.go2
new file mode 100644 (file)
index 0000000..0b5a1af
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+type S1 struct{}
+type S2 struct{}
+
+func _[P *S1|*S2]() {
+       _= []P{{ /* ERROR invalid composite literal element type P: no core type */ }}
+}
+
+func _[P *S1|S1]() {
+       _= []P{{ /* ERROR invalid composite literal element type P: no core type */ }}
+}
index e8038dd178c6c0ddf070ce3153213197fea3d889..9241c243f212ef219c516b4f40ce1197859dd42c 100644 (file)
@@ -1339,6 +1339,10 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
                        // no composite literal type present - use hint (element type of enclosing type)
                        typ = hint
                        base, _ = deref(coreType(typ)) // *T implies &T{}
+                       if base == nil {
+                               check.errorf(e, _InvalidLit, "invalid composite literal element type %s: no core type", typ)
+                               goto Error
+                       }
 
                default:
                        // TODO(gri) provide better error messages depending on context
diff --git a/src/go/types/testdata/fixedbugs/issue51335.go2 b/src/go/types/testdata/fixedbugs/issue51335.go2
new file mode 100644 (file)
index 0000000..0b5a1af
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+type S1 struct{}
+type S2 struct{}
+
+func _[P *S1|*S2]() {
+       _= []P{{ /* ERROR invalid composite literal element type P: no core type */ }}
+}
+
+func _[P *S1|S1]() {
+       _= []P{{ /* ERROR invalid composite literal element type P: no core type */ }}
+}