aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/typecheck/typecheck.go
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2020-12-26 01:06:03 -0800
committerMatthew Dempsky <mdempsky@google.com>2020-12-30 04:38:20 +0000
commit0c1a899a6c61dc59032ead0602d1cc6b918f7669 (patch)
tree9a390355d335b7f248b9dadab5b9d8eb8ae58728 /src/cmd/compile/internal/typecheck/typecheck.go
parentf9b67f76a59cb9adf5d04e9b559cda98afb3c6f4 (diff)
downloadgo-0c1a899a6c61dc59032ead0602d1cc6b918f7669.tar.gz
go-0c1a899a6c61dc59032ead0602d1cc6b918f7669.zip
[dev.regabi] cmd/compile: fix defined-pointer method call check
The compiler has logic to check whether we implicitly dereferenced a defined pointer while trying to select a method. However, rather than checking whether there were any implicit dereferences of a defined pointer, it was finding the innermost dereference/selector expression and checking whether that was dereferencing a named pointer. Moreover, it was only checking defined pointer declared in the package block. This CL restructures the code to match go/types and gccgo's behavior. Fixes #43384. Change-Id: I7bddfe2515776d9480eb2c7286023d4c15423888 Reviewed-on: https://go-review.googlesource.com/c/go/+/280392 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> Trust: Robert Griesemer <gri@golang.org> Trust: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/cmd/compile/internal/typecheck/typecheck.go')
-rw-r--r--src/cmd/compile/internal/typecheck/typecheck.go31
1 files changed, 20 insertions, 11 deletions
diff --git a/src/cmd/compile/internal/typecheck/typecheck.go b/src/cmd/compile/internal/typecheck/typecheck.go
index ebdcc4a72e..b79739bfeb 100644
--- a/src/cmd/compile/internal/typecheck/typecheck.go
+++ b/src/cmd/compile/internal/typecheck/typecheck.go
@@ -1328,6 +1328,7 @@ func lookdot(n *ir.SelectorExpr, t *types.Type, dostrcmp int) *types.Field {
// Already in the process of diagnosing an error.
return f2
}
+ orig := n.X
tt := n.X.Type()
types.CalcSize(tt)
rcvr := f2.Type.Recv().Type
@@ -1358,20 +1359,28 @@ func lookdot(n *ir.SelectorExpr, t *types.Type, dostrcmp int) *types.Field {
}
}
- implicit, ll := n.Implicit(), n.X
- for ll != nil && (ll.Op() == ir.ODOT || ll.Op() == ir.ODOTPTR || ll.Op() == ir.ODEREF) {
- switch l := ll.(type) {
+ // Check that we haven't implicitly dereferenced any defined pointer types.
+ for x := n.X; ; {
+ var inner ir.Node
+ implicit := false
+ switch x := x.(type) {
+ case *ir.AddrExpr:
+ inner, implicit = x.X, x.Implicit()
case *ir.SelectorExpr:
- implicit, ll = l.Implicit(), l.X
+ inner, implicit = x.X, x.Implicit()
case *ir.StarExpr:
- implicit, ll = l.Implicit(), l.X
+ inner, implicit = x.X, x.Implicit()
}
- }
- if implicit && ll.Type().IsPtr() && ll.Type().Sym() != nil && ll.Type().Sym().Def != nil && ir.AsNode(ll.Type().Sym().Def).Op() == ir.OTYPE {
- // It is invalid to automatically dereference a named pointer type when selecting a method.
- // Make n.Left == ll to clarify error message.
- n.X = ll
- return nil
+ if !implicit {
+ break
+ }
+ if inner.Type().Sym() != nil && (x.Op() == ir.ODEREF || x.Op() == ir.ODOTPTR) {
+ // Found an implicit dereference of a defined pointer type.
+ // Restore n.X for better error message.
+ n.X = orig
+ return nil
+ }
+ x = inner
}
n.Selection = f2