Commit eb4e81a9a4 for golang
commit eb4e81a9a4e5707db15bb3bd30f507e2f5d4e737
Author: Robert Griesemer <gri@golang.org>
Date: Wed Dec 2 13:26:05 2026 -0800
go/types, types2: don't use generic parameter types for untyped composite literals
An untyped composite literal can be used in assignment context, but only
if the target type is known.
An untyped composite literal cannot be passed as an argument to a
generic function parameter, even if type parameters in that parameter
type could be inferred otherwise. Implementation restriction.
While at it, set position information for implicit constraint interfaces
for correct trace output (debugging support).
Fixes #83529.
Change-Id: I7de0d7e3eee1c38ca18d9f37db086b9735638805
Reviewed-on: https://go-review.googlesource.com/c/go/+/868663
Reviewed-by: Mark Freeman <mark@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
diff --git a/src/cmd/compile/internal/types2/call.go b/src/cmd/compile/internal/types2/call.go
index ab4a860049..15a4972293 100644
--- a/src/cmd/compile/internal/types2/call.go
+++ b/src/cmd/compile/internal/types2/call.go
@@ -319,7 +319,14 @@ func (check *Checker) callExpr(x *operand, call *syntax.CallExpr) exprKind {
}
// evaluate arguments
- targetAt := func(i int) *target { return newTarget(sig.argType(i), "function parameter") }
+ targetAt := func(i int) *target {
+ // A target type must be fully known; it cannot be parameterized by the function's type parameters
+ // (type inference comes afterwards). go.dev/issue/#83529
+ if t := sig.argType(i); t != nil && (sig.tparams == nil || !isParameterized(sig.tparams.list(), t)) {
+ return newTarget(t, "function parameter")
+ }
+ return nil
+ }
args, atargs := check.genericExprList(targetAt, call.ArgList)
sig = check.arguments(call, sig, targs, xlist, args, atargs)
@@ -381,7 +388,7 @@ func (check *Checker) exprList(elist []syntax.Expr) (xlist []*operand) {
// genericExprList is like exprList but result operands may be uninstantiated or partially
// instantiated generic functions (where constraint information is insufficient to infer
-// the missing type arguments) for Go 1.21 and later. Additionally, typeAt must return the
+// the missing type arguments) for Go 1.21 and later. Additionally, targetAt must return the
// corresponding target type for each operand, or nil if none exists.
// For each non-generic or uninstantiated generic operand, the corresponding targsList and
// elements do not exist (targsList is nil) or the elements are nil.
diff --git a/src/cmd/compile/internal/types2/decl.go b/src/cmd/compile/internal/types2/decl.go
index a97248dab0..716c2378bd 100644
--- a/src/cmd/compile/internal/types2/decl.go
+++ b/src/cmd/compile/internal/types2/decl.go
@@ -563,7 +563,9 @@ func (check *Checker) bound(x syntax.Expr) Type {
// embed it in an implicit interface so that only interface type-checking
// needs to take care of such type expressions.
if op, _ := x.(*syntax.Operation); op != nil && (op.Op == syntax.Tilde || op.Op == syntax.Or) {
- t := check.typ(&syntax.InterfaceType{MethodList: []*syntax.Field{{Type: x}}})
+ x = &syntax.InterfaceType{MethodList: []*syntax.Field{{Type: x}}}
+ x.SetPos(x.Pos()) // for correct trace output
+ t := check.typ(x)
// mark t as implicit interface if all went well
if t, _ := t.(*Interface); t != nil {
t.implicit = true
diff --git a/src/go/types/call.go b/src/go/types/call.go
index 1b2fa2455f..f42d4a9bc4 100644
--- a/src/go/types/call.go
+++ b/src/go/types/call.go
@@ -320,7 +320,14 @@ func (check *Checker) callExpr(x *operand, call *ast.CallExpr) exprKind {
}
// evaluate arguments
- targetAt := func(i int) *target { return newTarget(sig.argType(i), "function parameter") }
+ targetAt := func(i int) *target {
+ // A target type must be fully known; it cannot be parameterized by the function's type parameters
+ // (type inference comes afterwards). go.dev/issue/#83529
+ if t := sig.argType(i); t != nil && (sig.tparams == nil || !isParameterized(sig.tparams.list(), t)) {
+ return newTarget(t, "function parameter")
+ }
+ return nil
+ }
args, atargs := check.genericExprList(targetAt, call.Args)
sig = check.arguments(call, sig, targs, xlist, args, atargs)
@@ -382,7 +389,7 @@ func (check *Checker) exprList(elist []ast.Expr) (xlist []*operand) {
// genericExprList is like exprList but result operands may be uninstantiated or partially
// instantiated generic functions (where constraint information is insufficient to infer
-// the missing type arguments) for Go 1.21 and later. Additionally, typeAt must return the
+// the missing type arguments) for Go 1.21 and later. Additionally, targetAt must return the
// corresponding target type for each operand, or nil if none exists.
// For each non-generic or uninstantiated generic operand, the corresponding targsList and
// elements do not exist (targsList is nil) or the elements are nil.
diff --git a/src/go/types/decl.go b/src/go/types/decl.go
index b1b0a581fb..c65d39e17a 100644
--- a/src/go/types/decl.go
+++ b/src/go/types/decl.go
@@ -649,7 +649,7 @@ func (check *Checker) bound(x ast.Expr) Type {
wrap = op.Op == token.OR
}
if wrap {
- x = &ast.InterfaceType{Methods: &ast.FieldList{List: []*ast.Field{{Type: x}}}}
+ x = &ast.InterfaceType{Interface: x.Pos() /* for correct trace output */, Methods: &ast.FieldList{List: []*ast.Field{{Type: x}}}}
t := check.typ(x)
// mark t as implicit interface if all went well
if t, _ := t.(*Interface); t != nil {
diff --git a/src/internal/types/testdata/fixedbugs/issue83529.go b/src/internal/types/testdata/fixedbugs/issue83529.go
new file mode 100644
index 0000000000..4b6af4bcdc
--- /dev/null
+++ b/src/internal/types/testdata/fixedbugs/issue83529.go
@@ -0,0 +1,17 @@
+// Copyright 2026 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
+
+func f[T ~[1]int](T) {}
+func _() { f({ /* ERROR "missing type in composite literal" */ }) }
+
+func g[T any](struct{ f T }) {}
+func _(x int) { f({ /* ERROR "missing type in composite literal" */ x}) }
+
+func h[T any](func(T, int)) {}
+func _() {
+ h(hArg)
+}
+func hArg[T any](int, T) {}
diff --git a/src/internal/types/testdata/spec/untyped_lit.go b/src/internal/types/testdata/spec/untyped_lit.go
index 29c77481d4..b624fac844 100644
--- a/src/internal/types/testdata/spec/untyped_lit.go
+++ b/src/internal/types/testdata/spec/untyped_lit.go
@@ -318,6 +318,7 @@ func _() {
func fg[P any](x P) {}
func fgc[P ~struct{ f int }](x P) {}
+func fgd[P any](x struct{ f P }) {}
func _() {
fg[Struct]({})
@@ -326,7 +327,8 @@ func _() {
fg[Map]({"a": 1})
fgc[Struct]({1})
- fgc({f: 1})
+ fgc({ /* ERROR "missing type" */ f: 1})
+ fgd({ /* ERROR "missing type" */ f: int(1)})
}
// 6. Nested composite literals