From 7731fd9cd344d387fa0e95ec7ae8f8dfcfb5df5f Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 8 Feb 2024 10:52:19 -0800 Subject: [PATCH] go/types, types2: consistently use ast/syntax.Unparen (cleanup) This further reduces the differences between go/types and types2. Change-Id: I5ed0f621e1d64cd65b6a3e8eaca9926a1ccb5794 Reviewed-on: https://go-review.googlesource.com/c/go/+/562776 Reviewed-by: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Gopher Robot Auto-Submit: Robert Griesemer Reviewed-by: Alan Donovan --- src/cmd/compile/internal/types2/builtins.go | 11 ----------- src/go/types/assignments.go | 8 ++++---- src/go/types/builtins.go | 4 +--- src/go/types/call.go | 2 +- src/go/types/expr.go | 2 +- src/go/types/resolver.go | 4 ++-- src/go/types/return.go | 2 +- src/go/types/stmt.go | 4 ++-- 8 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/cmd/compile/internal/types2/builtins.go b/src/cmd/compile/internal/types2/builtins.go index bb89246b7d..e32293a907 100644 --- a/src/cmd/compile/internal/types2/builtins.go +++ b/src/cmd/compile/internal/types2/builtins.go @@ -1034,14 +1034,3 @@ func arrayPtrDeref(typ Type) Type { } return typ } - -// unparen returns e with any enclosing parentheses stripped. -func unparen(e syntax.Expr) syntax.Expr { - for { - p, ok := e.(*syntax.ParenExpr) - if !ok { - return e - } - e = p.X - } -} diff --git a/src/go/types/assignments.go b/src/go/types/assignments.go index e69f943619..edf8a158d6 100644 --- a/src/go/types/assignments.go +++ b/src/go/types/assignments.go @@ -169,7 +169,7 @@ func (check *Checker) initVar(lhs *Var, x *operand, context string) { // and Typ[Invalid] if it is an invalid lhs expression. func (check *Checker) lhsVar(lhs ast.Expr) Type { // Determine if the lhs is a (possibly parenthesized) identifier. - ident, _ := unparen(lhs).(*ast.Ident) + ident, _ := ast.Unparen(lhs).(*ast.Ident) // Don't evaluate lhs if it is the blank identifier. if ident != nil && ident.Name == "_" { @@ -325,7 +325,7 @@ func (check *Checker) assignError(rhs []ast.Expr, l, r int) { rhs0 := rhs[0] if len(rhs) == 1 { - if call, _ := unparen(rhs0).(*ast.CallExpr); call != nil { + if call, _ := ast.Unparen(rhs0).(*ast.CallExpr); call != nil { check.errorf(rhs0, WrongAssignCount, "assignment mismatch: %s but %s returns %s", vars, call.Fun, vals) return } @@ -366,7 +366,7 @@ func (check *Checker) initVars(lhs []*Var, orig_rhs []ast.Expr, returnStmt ast.S // error message don't handle it as n:n mapping below. isCall := false if r == 1 { - _, isCall = unparen(orig_rhs[0]).(*ast.CallExpr) + _, isCall = ast.Unparen(orig_rhs[0]).(*ast.CallExpr) } // If we have a n:n mapping from lhs variable to rhs expression, @@ -445,7 +445,7 @@ func (check *Checker) assignVars(lhs, orig_rhs []ast.Expr) { // error message don't handle it as n:n mapping below. isCall := false if r == 1 { - _, isCall = unparen(orig_rhs[0]).(*ast.CallExpr) + _, isCall = ast.Unparen(orig_rhs[0]).(*ast.CallExpr) } // If we have a n:n mapping from lhs variable to rhs expression, diff --git a/src/go/types/builtins.go b/src/go/types/builtins.go index ae2bca25f0..325a6d67c5 100644 --- a/src/go/types/builtins.go +++ b/src/go/types/builtins.go @@ -705,7 +705,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b // unsafe.Offsetof(x T) uintptr, where x must be a selector // (no argument evaluated yet) arg0 := argList[0] - selx, _ := unparen(arg0).(*ast.SelectorExpr) + selx, _ := ast.Unparen(arg0).(*ast.SelectorExpr) if selx == nil { check.errorf(arg0, BadOffsetofSyntax, invalidArg+"%s is not a selector expression", arg0) check.use(arg0) @@ -1033,5 +1033,3 @@ func arrayPtrDeref(typ Type) Type { } return typ } - -func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) } diff --git a/src/go/types/call.go b/src/go/types/call.go index cb90a24736..2c55c63d1d 100644 --- a/src/go/types/call.go +++ b/src/go/types/call.go @@ -995,7 +995,7 @@ func (check *Checker) useN(args []ast.Expr, lhs bool) bool { func (check *Checker) use1(e ast.Expr, lhs bool) bool { var x operand x.mode = value // anything but invalid - switch n := unparen(e).(type) { + switch n := ast.Unparen(e).(type) { case nil: // nothing to do case *ast.Ident: diff --git a/src/go/types/expr.go b/src/go/types/expr.go index 95b460c848..927cb50d40 100644 --- a/src/go/types/expr.go +++ b/src/go/types/expr.go @@ -134,7 +134,7 @@ func (check *Checker) unary(x *operand, e *ast.UnaryExpr) { case token.AND: // spec: "As an exception to the addressability // requirement x may also be a composite literal." - if _, ok := unparen(e.X).(*ast.CompositeLit); !ok && x.mode != variable { + if _, ok := ast.Unparen(e.X).(*ast.CompositeLit); !ok && x.mode != variable { check.errorf(x, UnaddressableOperand, invalidOp+"cannot take address of %s", x) x.mode = invalid return diff --git a/src/go/types/resolver.go b/src/go/types/resolver.go index f828344749..d5b0dbf7b2 100644 --- a/src/go/types/resolver.go +++ b/src/go/types/resolver.go @@ -561,7 +561,7 @@ func (check *Checker) resolveBaseTypeName(seenPtr bool, typ ast.Expr, fileScopes for { // Note: this differs from types2, but is necessary. The syntax parser // strips unnecessary parens. - typ = unparen(typ) + typ = ast.Unparen(typ) // check if we have a pointer type if pexpr, _ := typ.(*ast.StarExpr); pexpr != nil { @@ -570,7 +570,7 @@ func (check *Checker) resolveBaseTypeName(seenPtr bool, typ ast.Expr, fileScopes return false, nil } ptr = true - typ = unparen(pexpr.X) // continue with pointer base type + typ = ast.Unparen(pexpr.X) // continue with pointer base type } // typ must be a name, or a C.name cgo selector. diff --git a/src/go/types/return.go b/src/go/types/return.go index ee8c41a431..95318e9002 100644 --- a/src/go/types/return.go +++ b/src/go/types/return.go @@ -29,7 +29,7 @@ func (check *Checker) isTerminating(s ast.Stmt, label string) bool { case *ast.ExprStmt: // calling the predeclared (possibly parenthesized) panic() function is terminating - if call, ok := unparen(s.X).(*ast.CallExpr); ok && check.isPanic[call] { + if call, ok := ast.Unparen(s.X).(*ast.CallExpr); ok && check.isPanic[call] { return true } diff --git a/src/go/types/stmt.go b/src/go/types/stmt.go index 660085d6f2..f16e288ffc 100644 --- a/src/go/types/stmt.go +++ b/src/go/types/stmt.go @@ -270,7 +270,7 @@ L: // isNil reports whether the expression e denotes the predeclared value nil. func (check *Checker) isNil(e ast.Expr) bool { // The only way to express the nil value is by literally writing nil (possibly in parentheses). - if name, _ := unparen(e).(*ast.Ident); name != nil { + if name, _ := ast.Unparen(e).(*ast.Ident); name != nil { _, ok := check.lookup(name.Name).(*Nil) return ok } @@ -779,7 +779,7 @@ func (check *Checker) stmt(ctxt stmtContext, s ast.Stmt) { // if present, rhs must be a receive operation if rhs != nil { - if x, _ := unparen(rhs).(*ast.UnaryExpr); x != nil && x.Op == token.ARROW { + if x, _ := ast.Unparen(rhs).(*ast.UnaryExpr); x != nil && x.Op == token.ARROW { valid = true } } -- 2.48.1