]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: consistently use ast/syntax.Unparen (cleanup)
authorRobert Griesemer <gri@golang.org>
Thu, 8 Feb 2024 18:52:19 +0000 (10:52 -0800)
committerGopher Robot <gobot@golang.org>
Thu, 8 Feb 2024 19:30:23 +0000 (19:30 +0000)
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 <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
src/cmd/compile/internal/types2/builtins.go
src/go/types/assignments.go
src/go/types/builtins.go
src/go/types/call.go
src/go/types/expr.go
src/go/types/resolver.go
src/go/types/return.go
src/go/types/stmt.go

index bb89246b7df6474e74bba0be4d3d8a3b39617c69..e32293a9071901a3d48a35a790694c0c49e11dc8 100644 (file)
@@ -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
-       }
-}
index e69f943619d08287a904c2dce0763a526f3879b3..edf8a158d65f07cfcbefde0934bb847d53ab6fb1 100644 (file)
@@ -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,
index ae2bca25f0bc3db4f2c1b75ac86882f6672ed3ee..325a6d67c5cf2b0e97adf6797b457b84feae7914 100644 (file)
@@ -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) }
index cb90a2473617f85eb18774b226cbe419965b9dad..2c55c63d1d75dfa893c848b6da757c60cd8b9401 100644 (file)
@@ -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:
index 95b460c848b6bde2f7019203835206b0561ec403..927cb50d40c3cab4d8e6e5b7acf532b5cb47ad05 100644 (file)
@@ -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
index f828344749b08d73dfd89b7a2d5bfedd686f96cb..d5b0dbf7b25fc4c24b8119b8b7d057dd893b9f14 100644 (file)
@@ -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.
index ee8c41a431d8568657d94b72cb2bde13f6fc61b1..95318e90025450b684098c1a956dc44488f51a06 100644 (file)
@@ -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
                }
 
index 660085d6f2a17d15152d9223738904d2817ab2e7..f16e288ffc085a9683da8d9b9dde817a01aa7845 100644 (file)
@@ -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
                                }
                        }