]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types, types2: don't implicitly modify an argument function's type
authorRobert Griesemer <gri@golang.org>
Mon, 2 Oct 2023 22:47:08 +0000 (15:47 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 3 Oct 2023 21:21:52 +0000 (21:21 +0000)
See the comment in the (very small) fix for a detailed description.
Use the opportunity to introduce a generic clone function which may
be useful elsewhere.

Fixes #63260.

Change-Id: Ic63c6b8bc443011b1a201908254f7d062e1aec71
Reviewed-on: https://go-review.googlesource.com/c/go/+/532157
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/compile/internal/types2/call.go
src/cmd/compile/internal/types2/issues_test.go
src/cmd/compile/internal/types2/predicates.go
src/go/types/call.go
src/go/types/issues_test.go
src/go/types/predicates.go

index 7b0fa10f32da1924eacda3d115975ac0a2ab1a5d..439f51526577fc83c7c73f0e2f982b8f8cc7efb2 100644 (file)
@@ -569,6 +569,13 @@ func (check *Checker) arguments(call *syntax.CallExpr, sig *Signature, targs []T
                for i, arg := range args {
                        // generic arguments cannot have a defined (*Named) type - no need for underlying type below
                        if asig, _ := arg.typ.(*Signature); asig != nil && asig.TypeParams().Len() > 0 {
+                               // The argument type is a generic function signature. This type is
+                               // pointer-identical with (it's copied from) the type of the generic
+                               // function argument and thus the function object.
+                               // Before we change the type (type parameter renaming, below), make
+                               // a clone of it as otherwise we implicitly modify the object's type
+                               // (go.dev/issues/63260).
+                               asig = clone(asig)
                                // Rename type parameters for cases like f(g, g); this gives each
                                // generic function argument a unique type identity (go.dev/issues/59956).
                                // TODO(gri) Consider only doing this if a function argument appears
index bcebc2f2c05361f1a9a1a5104f8e9caf34d4fc2b..32671403c2e9c3a2f133cf7e0f8cd2a2ec7e9442 100644 (file)
@@ -936,3 +936,47 @@ func _()        { f() }
        conf.Error = func(error) {}
        typecheck(src, &conf, nil) // must not panic
 }
+
+func TestIssue63260(t *testing.T) {
+       const src = `
+package p
+
+func _() {
+        use(f[*string])
+}
+
+func use(func()) {}
+
+func f[I *T, T any]() {
+        var v T
+        _ = v
+}`
+
+       info := Info{
+               Defs: make(map[*syntax.Name]Object),
+       }
+       pkg := mustTypecheck(src, nil, &info)
+
+       // get type parameter T in signature of f
+       T := pkg.Scope().Lookup("f").Type().(*Signature).TypeParams().At(1)
+       if T.Obj().Name() != "T" {
+               t.Fatalf("got type parameter %s, want T", T)
+       }
+
+       // get type of variable v in body of f
+       var v Object
+       for name, obj := range info.Defs {
+               if name.Value == "v" {
+                       v = obj
+                       break
+               }
+       }
+       if v == nil {
+               t.Fatal("variable v not found")
+       }
+
+       // type of v and T must be pointer-identical
+       if v.Type() != T {
+               t.Fatalf("types of v and T are not pointer-identical: %p != %p", v.Type().(*TypeParam), T)
+       }
+}
index c48f926bf84350e4625f38627ecaba4b100441d7..11f543f4751092b7e6d4d3d02ab311d8ee8438df 100644 (file)
@@ -533,3 +533,9 @@ func maxType(x, y Type) Type {
        }
        return nil
 }
+
+// clone makes a "flat copy" of *p and returns a pointer to the copy.
+func clone[P *T, T any](p P) P {
+       c := *p
+       return &c
+}
index 18c40629a57e765928d83107fe79cc119eca152a..12f547ea386a670b6e4c33397665087d561f9508 100644 (file)
@@ -571,6 +571,13 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type
                for i, arg := range args {
                        // generic arguments cannot have a defined (*Named) type - no need for underlying type below
                        if asig, _ := arg.typ.(*Signature); asig != nil && asig.TypeParams().Len() > 0 {
+                               // The argument type is a generic function signature. This type is
+                               // pointer-identical with (it's copied from) the type of the generic
+                               // function argument and thus the function object.
+                               // Before we change the type (type parameter renaming, below), make
+                               // a clone of it as otherwise we implicitly modify the object's type
+                               // (go.dev/issues/63260).
+                               asig = clone(asig)
                                // Rename type parameters for cases like f(g, g); this gives each
                                // generic function argument a unique type identity (go.dev/issues/59956).
                                // TODO(gri) Consider only doing this if a function argument appears
index bdc1a388d805f5c4846be3c2ba21ffca645abc8d..5530fceef6d9aca01d9eaaeb5df84f53b417c288 100644 (file)
@@ -946,3 +946,47 @@ func _()        { f() }
        conf.Error = func(error) {}
        typecheck(src, &conf, nil) // must not panic
 }
+
+func TestIssue63260(t *testing.T) {
+       const src = `
+package p
+
+func _() {
+        use(f[*string])
+}
+
+func use(func()) {}
+
+func f[I *T, T any]() {
+        var v T
+        _ = v
+}`
+
+       info := Info{
+               Defs: make(map[*ast.Ident]Object),
+       }
+       pkg := mustTypecheck(src, nil, &info)
+
+       // get type parameter T in signature of f
+       T := pkg.Scope().Lookup("f").Type().(*Signature).TypeParams().At(1)
+       if T.Obj().Name() != "T" {
+               t.Fatalf("got type parameter %s, want T", T)
+       }
+
+       // get type of variable v in body of f
+       var v Object
+       for name, obj := range info.Defs {
+               if name.Name == "v" {
+                       v = obj
+                       break
+               }
+       }
+       if v == nil {
+               t.Fatal("variable v not found")
+       }
+
+       // type of v and T must be pointer-identical
+       if v.Type() != T {
+               t.Fatalf("types of v and T are not pointer-identical: %p != %p", v.Type().(*TypeParam), T)
+       }
+}
index 331a8f69aa245377559aa64eb2baf4d3be208580..01804c69c51cf3f46f9dcc70a877e1b3ae97fc25 100644 (file)
@@ -535,3 +535,9 @@ func maxType(x, y Type) Type {
        }
        return nil
 }
+
+// clone makes a "flat copy" of *p and returns a pointer to the copy.
+func clone[P *T, T any](p P) P {
+       c := *p
+       return &c
+}