]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/ast: add Unparen(Expr) helper
authorAlan Donovan <adonovan@google.com>
Tue, 16 May 2023 17:33:15 +0000 (13:33 -0400)
committerAlan Donovan <adonovan@google.com>
Thu, 20 Jul 2023 20:19:51 +0000 (20:19 +0000)
Fixes #60061

Change-Id: If1bc03f9367620c9ea8702bfd4648020d5ab52ea
Reviewed-on: https://go-review.googlesource.com/c/go/+/495315
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

api/next/60061.txt [new file with mode: 0644]
src/go/ast/ast.go
src/go/parser/parser.go
src/go/parser/resolver.go
src/go/printer/testdata/parser.go
src/go/types/builtins.go

diff --git a/api/next/60061.txt b/api/next/60061.txt
new file mode 100644 (file)
index 0000000..3e497ad
--- /dev/null
@@ -0,0 +1 @@
+pkg go/ast, func Unparen(Expr) Expr #60061
index c43905261050a179e23509160a219be80cf87393..be7c72d13a536a1872b2672a09f0034a9bf383a1 100644 (file)
@@ -1110,3 +1110,14 @@ func generator(file *File) (string, bool) {
        }
        return "", false
 }
+
+// Unparen returns the expression with any enclosing parentheses removed.
+func Unparen(e Expr) Expr {
+       for {
+               paren, ok := e.(*ParenExpr)
+               if !ok {
+                       return e
+               }
+               e = paren.X
+       }
+}
index e1d941eff31d3bb50fd971bb33f0e26bee5cbd41..7d8f727b0c93e2ae09621b3c0f1b6142ca826450 100644 (file)
@@ -1654,14 +1654,6 @@ func (p *parser) parseLiteralValue(typ ast.Expr) ast.Expr {
        return &ast.CompositeLit{Type: typ, Lbrace: lbrace, Elts: elts, Rbrace: rbrace}
 }
 
-// If x is of the form (T), unparen returns unparen(T), otherwise it returns x.
-func unparen(x ast.Expr) ast.Expr {
-       if p, isParen := x.(*ast.ParenExpr); isParen {
-               x = unparen(p.X)
-       }
-       return x
-}
-
 func (p *parser) parsePrimaryExpr(x ast.Expr) ast.Expr {
        if p.trace {
                defer un(trace(p, "PrimaryExpr"))
@@ -1706,7 +1698,7 @@ func (p *parser) parsePrimaryExpr(x ast.Expr) ast.Expr {
                case token.LBRACE:
                        // operand may have returned a parenthesized complit
                        // type; accept it but complain if we have a complit
-                       t := unparen(x)
+                       t := ast.Unparen(x)
                        // determine if '{' belongs to a composite literal or a block statement
                        switch t.(type) {
                        case *ast.BadExpr, *ast.Ident, *ast.SelectorExpr:
@@ -1949,7 +1941,7 @@ func (p *parser) parseSimpleStmt(mode int) (ast.Stmt, bool) {
 
 func (p *parser) parseCallExpr(callType string) *ast.CallExpr {
        x := p.parseRhs() // could be a conversion: (some type)(x)
-       if t := unparen(x); t != x {
+       if t := ast.Unparen(x); t != x {
                p.error(x.Pos(), fmt.Sprintf("expression in %s must not be parenthesized", callType))
                x = t
        }
index f8ff618eba3a8eacb153dd4c2f1873c94d32bee5..1539dcd5c7122e455b04d11ac07d46b45a06d4aa 100644 (file)
@@ -234,7 +234,7 @@ func (r *resolver) walkExprs(list []ast.Expr) {
 
 func (r *resolver) walkLHS(list []ast.Expr) {
        for _, expr := range list {
-               expr := unparen(expr)
+               expr := ast.Unparen(expr)
                if _, ok := expr.(*ast.Ident); !ok && expr != nil {
                        ast.Walk(r, expr)
                }
index bb06c8dd424b0b62ae67c55c85cec974fe7e5d67..11795b42802a50cf6fbcaddc95cfc656ec37703f 100644 (file)
@@ -1127,7 +1127,7 @@ func (p *parser) parseLiteralValue(typ ast.Expr) ast.Expr {
 
 // checkExpr checks that x is an expression (and not a type).
 func (p *parser) checkExpr(x ast.Expr) ast.Expr {
-       switch t := unparen(x).(type) {
+       switch t := ast.Unparen(x).(type) {
        case *ast.BadExpr:
        case *ast.Ident:
        case *ast.BasicLit:
@@ -1200,18 +1200,10 @@ func deref(x ast.Expr) ast.Expr {
        return x
 }
 
-// If x is of the form (T), unparen returns unparen(T), otherwise it returns x.
-func unparen(x ast.Expr) ast.Expr {
-       if p, isParen := x.(*ast.ParenExpr); isParen {
-               x = unparen(p.X)
-       }
-       return x
-}
-
 // checkExprOrType checks that x is an expression or a type
 // (and not a raw type such as [...]T).
 func (p *parser) checkExprOrType(x ast.Expr) ast.Expr {
-       switch t := unparen(x).(type) {
+       switch t := ast.Unparen(x).(type) {
        case *ast.ParenExpr:
                panic("unreachable")
        case *ast.UnaryExpr:
index 7795f2552dde3d847b51c1ea4347283539d5467e..4aee3979d003a2b3962aef908d0372574b7c2e0c 100644 (file)
@@ -1029,13 +1029,4 @@ func arrayPtrDeref(typ Type) Type {
        return typ
 }
 
-// unparen returns e with any enclosing parentheses stripped.
-func unparen(e ast.Expr) ast.Expr {
-       for {
-               p, ok := e.(*ast.ParenExpr)
-               if !ok {
-                       return e
-               }
-               e = p.X
-       }
-}
+func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) }