]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: types in method expressions must be instantiated
authorQuim Muntal <quimmuntal@gmail.com>
Sun, 29 Aug 2021 20:57:36 +0000 (22:57 +0200)
committerRobert Griesemer <gri@golang.org>
Mon, 30 Aug 2021 21:51:52 +0000 (21:51 +0000)
Use varType instead of instantiatedOperand to check if the type of a method expressions is instantiated.

This removes the last usage of instantiatedOperand, so it can be deleted.

Fixes #48048

Change-Id: I2b219dafe2bba3603100bb8f25b8ff4e8ef53841
Reviewed-on: https://go-review.googlesource.com/c/go/+/345970
Trust: Robert Griesemer <gri@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/types2/call.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue48048.go2 [new file with mode: 0644]
src/go/types/call.go
src/go/types/testdata/fixedbugs/issue48048.go2 [new file with mode: 0644]

index 0b062b4c94d263d7b9597711faed53394c32a76d..954aa1de207bce3faba662047e43676c35276623 100644 (file)
@@ -479,7 +479,9 @@ func (check *Checker) selector(x *operand, e *syntax.SelectorExpr) {
                goto Error
        }
 
-       check.instantiatedOperand(x)
+       if x.mode == typexpr {
+               x.typ = check.varType(e.X)
+       }
 
        obj, index, indirect = LookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel)
        if obj == nil {
@@ -718,11 +720,3 @@ func (check *Checker) useLHS(arg ...syntax.Expr) {
                }
        }
 }
-
-// instantiatedOperand reports an error of x is an uninstantiated (generic) type and sets x.typ to Typ[Invalid].
-func (check *Checker) instantiatedOperand(x *operand) {
-       if x.mode == typexpr && isGeneric(x.typ) {
-               check.errorf(x, "cannot use generic type %s without instantiation", x.typ)
-               x.typ = Typ[Invalid]
-       }
-}
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48048.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48048.go2
new file mode 100644 (file)
index 0000000..f401330
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright 2021 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 T[P any] struct{}
+
+func (T[_]) A() {}
+
+var _ = (T[int]).A
+var _ = (*T[int]).A
+
+var _ = (T /* ERROR cannot use generic type */).A
+var _ = (*T /* ERROR cannot use generic type */).A
index fdecafb7811455aa031f9739db2d16d7cd7ba861..61534b632896e37eb2f5a7ebe02336a7a9e945e9 100644 (file)
@@ -470,7 +470,9 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
                goto Error
        }
 
-       check.instantiatedOperand(x)
+       if x.mode == typexpr {
+               x.typ = check.varType(e.X)
+       }
 
        obj, index, indirect = LookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel)
        if obj == nil {
@@ -745,11 +747,3 @@ func (check *Checker) useLHS(arg ...ast.Expr) {
                }
        }
 }
-
-// instantiatedOperand reports an error of x is an uninstantiated (generic) type and sets x.typ to Typ[Invalid].
-func (check *Checker) instantiatedOperand(x *operand) {
-       if x.mode == typexpr && isGeneric(x.typ) {
-               check.errorf(x, _Todo, "cannot use generic type %s without instantiation", x.typ)
-               x.typ = Typ[Invalid]
-       }
-}
diff --git a/src/go/types/testdata/fixedbugs/issue48048.go2 b/src/go/types/testdata/fixedbugs/issue48048.go2
new file mode 100644 (file)
index 0000000..f401330
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright 2021 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 T[P any] struct{}
+
+func (T[_]) A() {}
+
+var _ = (T[int]).A
+var _ = (*T[int]).A
+
+var _ = (T /* ERROR cannot use generic type */).A
+var _ = (*T /* ERROR cannot use generic type */).A