]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: don't register interface methods in Info.Types map
authorRobert Griesemer <gri@golang.org>
Tue, 7 Jan 2025 18:15:19 +0000 (10:15 -0800)
committerGopher Robot <gobot@golang.org>
Mon, 3 Feb 2025 16:21:20 +0000 (08:21 -0800)
Methods declared in an interface have a signature and FuncType in the
AST, but they do not express a syntactic function type expression.
Treat them like ordinary function/method declarations and do not record
them in the Info.Types map. This removes an inconsistency in the way
function types are recorded.

Follow-up on CL 640776.

For #70908.

Change-Id: I60848f209b40b008039c014fb8b7b279361487b9
Reviewed-on: https://go-review.googlesource.com/c/go/+/640596
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
src/cmd/compile/internal/types2/interface.go
src/go/types/interface.go

index 4072098e05234d94fe071f02f2d8e38863778f7c..67f5b98a835cd9b82286f907301e3b94448f156e 100644 (file)
@@ -137,17 +137,19 @@ func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType
                name := f.Name.Value
                if name == "_" {
                        check.error(f.Name, BlankIfaceMethod, "methods must have a unique non-blank name")
-                       continue // ignore
+                       continue // ignore method
                }
 
-               typ := check.typ(f.Type)
-               sig, _ := typ.(*Signature)
-               if sig == nil {
-                       if isValid(typ) {
-                               check.errorf(f.Type, InvalidSyntaxTree, "%s is not a method signature", typ)
-                       }
-                       continue // ignore
+               // Type-check method declaration.
+               // Note: Don't call check.typ(f.Type) as that would record
+               // the method incorrectly as a type expression in Info.Types.
+               ftyp, _ := f.Type.(*syntax.FuncType)
+               if ftyp == nil {
+                       check.errorf(f.Type, InvalidSyntaxTree, "%s is not a method signature", f.Type)
+                       continue // ignore method
                }
+               sig := new(Signature)
+               check.funcType(sig, nil, nil, ftyp)
 
                // use named receiver type if available (for better error messages)
                var recvTyp Type = ityp
index 01bbb08e0efe5573f205756b2c1d450913b4d066..e5ca042e7509fc18f166b6b853885e0418c76519 100644 (file)
@@ -176,17 +176,19 @@ func (check *Checker) interfaceType(ityp *Interface, iface *ast.InterfaceType, d
                name := f.Names[0]
                if name.Name == "_" {
                        check.error(name, BlankIfaceMethod, "methods must have a unique non-blank name")
-                       continue // ignore
+                       continue // ignore method
                }
 
-               typ := check.typ(f.Type)
-               sig, _ := typ.(*Signature)
-               if sig == nil {
-                       if isValid(typ) {
-                               check.errorf(f.Type, InvalidSyntaxTree, "%s is not a method signature", typ)
-                       }
-                       continue // ignore
+               // Type-check method declaration.
+               // Note: Don't call check.typ(f.Type) as that would record
+               // the method incorrectly as a type expression in Info.Types.
+               ftyp, _ := f.Type.(*ast.FuncType)
+               if ftyp == nil {
+                       check.errorf(f.Type, InvalidSyntaxTree, "%s is not a method signature", f.Type)
+                       continue // ignore method
                }
+               sig := new(Signature)
+               check.funcType(sig, nil, ftyp)
 
                // The go/parser doesn't accept method type parameters but an ast.FuncType may have them.
                if sig.tparams != nil {