Commit ce991fa819 for golang
commit ce991fa8197ced012816b86d9f0f7c752c8b4701
Author: Guido Iaquinti <guido.iaquinti@gmail.com>
Date: Wed Dec 2 20:21:40 2026 +0000
go/types, types2: fix order-dependent variadic check in NewSignatureType
For a variadic signature, NewSignatureType requires the last parameter
to be a slice, a string, or a type parameter whose type set has a
common slice type or mixes []byte and string types. The loop that
checks the type set used S == nil both before the first term and after
an unsupported term. An unsupported leading term therefore left S nil,
and the next term overwrote it. As a result, a type parameter
constrained by ~int | ~[]byte was accepted, while the same constraint
written as ~[]byte | ~int was rejected.
The order dependency was introduced by CL 655695 in Go 1.25, which
replaced the core type check with this loop. Go 1.24 rejected both
orders.
Fixes #83583.
Change-Id: Ida3fe65ef6af78f961fd6bf8acba621dd08d96ca
GitHub-Last-Rev: 375912e8f7cf29930f7a4e0e10e001b3e70ee2d6
GitHub-Pull-Request: golang/go#83584
Reviewed-on: https://go-review.googlesource.com/c/go/+/869241
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Mark Freeman <mark@golang.org>
diff --git a/src/cmd/compile/internal/types2/issues_test.go b/src/cmd/compile/internal/types2/issues_test.go
index 4b12d29174..7192456c72 100644
--- a/src/cmd/compile/internal/types2/issues_test.go
+++ b/src/cmd/compile/internal/types2/issues_test.go
@@ -670,13 +670,14 @@ func TestIssue55030(t *testing.T) {
makeSig(NewTypeParam(P, NewInterfaceType(nil, []Type{NewSlice(Typ[Int])})), true)
}
- // P where P's type set contains strings and []byte
+ // P where P's type set contains strings and []byte, in either order
{
t1 := NewTerm(true, Typ[String]) // ~string
t2 := NewTerm(false, NewSlice(Typ[Byte])) // []byte
- u := NewUnion([]*Term{t1, t2}) // ~string | []byte
- P := NewTypeName(nopos, nil, "P", nil) // [P ~string | []byte]
- makeSig(NewTypeParam(P, NewInterfaceType(nil, []Type{u})), true)
+ for _, terms := range [][]*Term{{t1, t2}, {t2, t1}} {
+ P := NewTypeName(nopos, nil, "P", nil) // [P ~string | []byte], [P []byte | ~string]
+ makeSig(NewTypeParam(P, NewInterfaceType(nil, []Type{NewUnion(terms)})), true)
+ }
}
// makeSig must panic for the following (example) types:
@@ -694,6 +695,17 @@ func TestIssue55030(t *testing.T) {
P := NewTypeName(nopos, nil, "P", nil) // [P any]
makeSig(NewTypeParam(P, NewInterfaceType(nil, []Type{Typ[Int]})), false)
}
+
+ // P where P's type set contains a slice type and a non-slice,
+ // non-string type, in either order
+ {
+ t1 := NewTerm(true, Typ[Int]) // ~int
+ t2 := NewTerm(true, NewSlice(Typ[Byte])) // ~[]byte
+ for _, terms := range [][]*Term{{t1, t2}, {t2, t1}} {
+ P := NewTypeName(nopos, nil, "P", nil) // [P ~int | ~[]byte], [P ~[]byte | ~int]
+ makeSig(NewTypeParam(P, NewInterfaceType(nil, []Type{NewUnion(terms)})), false)
+ }
+ }
}
func TestIssue51093(t *testing.T) {
diff --git a/src/cmd/compile/internal/types2/signature.go b/src/cmd/compile/internal/types2/signature.go
index f3214c203f..859d5a253d 100644
--- a/src/cmd/compile/internal/types2/signature.go
+++ b/src/cmd/compile/internal/types2/signature.go
@@ -90,12 +90,13 @@ func NewSignatureType(recv *Var, recvTypeParams, typeParams []*TypeParam, params
// know that it is dealing with the append special case.)
s, _ = t.Underlying().(*Slice)
}
- if S == nil {
- S = s
- } else if s == nil || !Identical(S, s) {
+ if s == nil || S != nil && !Identical(S, s) {
+ // t is not a slice or string type, or its slice
+ // type differs from the one of a preceding term
S = nil
break
}
+ S = s
}
if S == nil {
panic(fmt.Sprintf("got %s, want variadic parameter of slice or string type", last))
diff --git a/src/go/types/issues_test.go b/src/go/types/issues_test.go
index 956b6fda07..1ae98ebfce 100644
--- a/src/go/types/issues_test.go
+++ b/src/go/types/issues_test.go
@@ -680,13 +680,14 @@ func TestIssue55030(t *testing.T) {
makeSig(NewTypeParam(P, NewInterfaceType(nil, []Type{NewSlice(Typ[Int])})), true)
}
- // P where P's type set contains strings and []byte
+ // P where P's type set contains strings and []byte, in either order
{
t1 := NewTerm(true, Typ[String]) // ~string
t2 := NewTerm(false, NewSlice(Typ[Byte])) // []byte
- u := NewUnion([]*Term{t1, t2}) // ~string | []byte
- P := NewTypeName(nopos, nil, "P", nil) // [P ~string | []byte]
- makeSig(NewTypeParam(P, NewInterfaceType(nil, []Type{u})), true)
+ for _, terms := range [][]*Term{{t1, t2}, {t2, t1}} {
+ P := NewTypeName(nopos, nil, "P", nil) // [P ~string | []byte], [P []byte | ~string]
+ makeSig(NewTypeParam(P, NewInterfaceType(nil, []Type{NewUnion(terms)})), true)
+ }
}
// makeSig must panic for the following (example) types:
@@ -704,6 +705,17 @@ func TestIssue55030(t *testing.T) {
P := NewTypeName(nopos, nil, "P", nil) // [P any]
makeSig(NewTypeParam(P, NewInterfaceType(nil, []Type{Typ[Int]})), false)
}
+
+ // P where P's type set contains a slice type and a non-slice,
+ // non-string type, in either order
+ {
+ t1 := NewTerm(true, Typ[Int]) // ~int
+ t2 := NewTerm(true, NewSlice(Typ[Byte])) // ~[]byte
+ for _, terms := range [][]*Term{{t1, t2}, {t2, t1}} {
+ P := NewTypeName(nopos, nil, "P", nil) // [P ~int | ~[]byte], [P ~[]byte | ~int]
+ makeSig(NewTypeParam(P, NewInterfaceType(nil, []Type{NewUnion(terms)})), false)
+ }
+ }
}
func TestIssue51093(t *testing.T) {
diff --git a/src/go/types/signature.go b/src/go/types/signature.go
index dd921ae87f..f5f5444bbf 100644
--- a/src/go/types/signature.go
+++ b/src/go/types/signature.go
@@ -103,12 +103,13 @@ func NewSignatureType(recv *Var, recvTypeParams, typeParams []*TypeParam, params
// know that it is dealing with the append special case.)
s, _ = t.Underlying().(*Slice)
}
- if S == nil {
- S = s
- } else if s == nil || !Identical(S, s) {
+ if s == nil || S != nil && !Identical(S, s) {
+ // t is not a slice or string type, or its slice
+ // type differs from the one of a preceding term
S = nil
break
}
+ S = s
}
if S == nil {
panic(fmt.Sprintf("got %s, want variadic parameter of slice or string type", last))