Skip to content

Commit

Permalink
x/tools: eliminate various unparen (et al) helpers
Browse files Browse the repository at this point in the history
This was achieved by annotiating them with //go:fix inline,
running gopls/internal/analysis/gofix/main.go -fix ./...
then deleting them.

Update golang/go#32816

Change-Id: If65dbf8bfcad796ef274d80804daa135e8ccabf9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/648976
Reviewed-by: Jonathan Amsterdam <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Alan Donovan <[email protected]>
Commit-Queue: Alan Donovan <[email protected]>
  • Loading branch information
adonovan authored and gopherbot committed Feb 13, 2025
1 parent d0d86e4 commit 44b61a1
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 47 deletions.
20 changes: 10 additions & 10 deletions go/ssa/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ func (sb *storebuf) emit(fn *Function) {
// literal that may reference parts of the LHS.
func (b *builder) assign(fn *Function, loc lvalue, e ast.Expr, isZero bool, sb *storebuf) {
// Can we initialize it in place?
if e, ok := unparen(e).(*ast.CompositeLit); ok {
if e, ok := ast.Unparen(e).(*ast.CompositeLit); ok {
// A CompositeLit never evaluates to a pointer,
// so if the type of the location is a pointer,
// an &-operation is implied.
Expand Down Expand Up @@ -614,7 +614,7 @@ func (b *builder) assign(fn *Function, loc lvalue, e ast.Expr, isZero bool, sb *
// expr lowers a single-result expression e to SSA form, emitting code
// to fn and returning the Value defined by the expression.
func (b *builder) expr(fn *Function, e ast.Expr) Value {
e = unparen(e)
e = ast.Unparen(e)

tv := fn.info.Types[e]

Expand Down Expand Up @@ -704,7 +704,7 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value {
return y
}
// Call to "intrinsic" built-ins, e.g. new, make, panic.
if id, ok := unparen(e.Fun).(*ast.Ident); ok {
if id, ok := ast.Unparen(e.Fun).(*ast.Ident); ok {
if obj, ok := fn.info.Uses[id].(*types.Builtin); ok {
if v := b.builtin(fn, obj, e.Args, fn.typ(tv.Type), e.Lparen); v != nil {
return v
Expand All @@ -721,7 +721,7 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value {
switch e.Op {
case token.AND: // &X --- potentially escaping.
addr := b.addr(fn, e.X, true)
if _, ok := unparen(e.X).(*ast.StarExpr); ok {
if _, ok := ast.Unparen(e.X).(*ast.StarExpr); ok {
// &*p must panic if p is nil (http://golang.org/s/go12nil).
// For simplicity, we'll just (suboptimally) rely
// on the side effects of a load.
Expand Down Expand Up @@ -1002,7 +1002,7 @@ func (b *builder) setCallFunc(fn *Function, e *ast.CallExpr, c *CallCommon) {
c.pos = e.Lparen

// Is this a method call?
if selector, ok := unparen(e.Fun).(*ast.SelectorExpr); ok {
if selector, ok := ast.Unparen(e.Fun).(*ast.SelectorExpr); ok {
sel := fn.selection(selector)
if sel != nil && sel.kind == types.MethodVal {
obj := sel.obj.(*types.Func)
Expand Down Expand Up @@ -1372,7 +1372,7 @@ func (b *builder) compLit(fn *Function, addr Value, e *ast.CompositeLit, isZero
// An &-operation may be implied:
// map[*struct{}]bool{&struct{}{}: true}
wantAddr := false
if _, ok := unparen(e.Key).(*ast.CompositeLit); ok {
if _, ok := ast.Unparen(e.Key).(*ast.CompositeLit); ok {
wantAddr = isPointerCore(t.Key())
}

Expand Down Expand Up @@ -1547,9 +1547,9 @@ func (b *builder) typeSwitchStmt(fn *Function, s *ast.TypeSwitchStmt, label *lbl
var x Value
switch ass := s.Assign.(type) {
case *ast.ExprStmt: // x.(type)
x = b.expr(fn, unparen(ass.X).(*ast.TypeAssertExpr).X)
x = b.expr(fn, ast.Unparen(ass.X).(*ast.TypeAssertExpr).X)
case *ast.AssignStmt: // y := x.(type)
x = b.expr(fn, unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X)
x = b.expr(fn, ast.Unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X)
}

done := fn.newBasicBlock("typeswitch.done")
Expand Down Expand Up @@ -1667,7 +1667,7 @@ func (b *builder) selectStmt(fn *Function, s *ast.SelectStmt, label *lblock) {
}

case *ast.AssignStmt: // x := <-ch
recv := unparen(comm.Rhs[0]).(*ast.UnaryExpr)
recv := ast.Unparen(comm.Rhs[0]).(*ast.UnaryExpr)
st = &SelectState{
Dir: types.RecvOnly,
Chan: b.expr(fn, recv.X),
Expand All @@ -1678,7 +1678,7 @@ func (b *builder) selectStmt(fn *Function, s *ast.SelectStmt, label *lblock) {
}

case *ast.ExprStmt: // <-ch
recv := unparen(comm.X).(*ast.UnaryExpr)
recv := ast.Unparen(comm.X).(*ast.UnaryExpr)
st = &SelectState{
Dir: types.RecvOnly,
Chan: b.expr(fn, recv.X),
Expand Down
2 changes: 1 addition & 1 deletion go/ssa/emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func emitDebugRef(f *Function, e ast.Expr, v Value, isAddr bool) {
panic("nil")
}
var obj types.Object
e = unparen(e)
e = ast.Unparen(e)
if id, ok := e.(*ast.Ident); ok {
if isBlankIdent(id) {
return
Expand Down
2 changes: 1 addition & 1 deletion go/ssa/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func findNamedFunc(pkg *Package, pos token.Pos) *Function {
// the ssa.Value.)
func (f *Function) ValueForExpr(e ast.Expr) (value Value, isAddr bool) {
if f.debugInfo() { // (opt)
e = unparen(e)
e = ast.Unparen(e)
for _, b := range f.Blocks {
for _, instr := range b.Instrs {
if ref, ok := instr.(*DebugRef); ok {
Expand Down
2 changes: 0 additions & 2 deletions go/ssa/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ func assert(p bool, msg string) {

//// AST utilities

func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) }

// isBlankIdent returns true iff e is an Ident with name "_".
// They have no associated types.Object, and thus no type.
func isBlankIdent(e ast.Expr) bool {
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/golang/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ func collectFreeVars(info *types.Info, file *ast.File, start, end token.Pos, nod
// return value acts as an indicator for where it was defined.
var sel func(n *ast.SelectorExpr) (types.Object, bool)
sel = func(n *ast.SelectorExpr) (types.Object, bool) {
switch x := astutil.Unparen(n.X).(type) {
switch x := ast.Unparen(n.X).(type) {
case *ast.SelectorExpr:
return sel(x)
case *ast.Ident:
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/golang/freesymbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func freeRefs(pkg *types.Package, info *types.Info, file *ast.File, start, end t
for {
suffix = append(suffix, info.Uses[sel.Sel])

switch x := astutil.Unparen(sel.X).(type) {
switch x := ast.Unparen(sel.X).(type) {
case *ast.Ident:
return id(x, suffix)
default:
Expand Down
6 changes: 2 additions & 4 deletions refactor/eg/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func (tr *Transformer) matchExpr(x, y ast.Expr) bool {
if x == nil || y == nil {
return false
}
x = unparen(x)
y = unparen(y)
x = ast.Unparen(x)
y = ast.Unparen(y)

// Is x a wildcard? (a reference to a 'before' parameter)
if xobj, ok := tr.wildcardObj(x); ok {
Expand Down Expand Up @@ -227,8 +227,6 @@ func (tr *Transformer) matchWildcard(xobj *types.Var, y ast.Expr) bool {

// -- utilities --------------------------------------------------------

func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) }

// isRef returns the object referred to by this (possibly qualified)
// identifier, or nil if the node is not a referring identifier.
func isRef(n ast.Node, info *types.Info) types.Object {
Expand Down
4 changes: 2 additions & 2 deletions refactor/rename/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func parseObjectSpec(spec *spec, main string) error {
}

if e, ok := e.(*ast.SelectorExpr); ok {
x := unparen(e.X)
x := ast.Unparen(e.X)

// Strip off star constructor, if any.
if star, ok := x.(*ast.StarExpr); ok {
Expand All @@ -172,7 +172,7 @@ func parseObjectSpec(spec *spec, main string) error {

if x, ok := x.(*ast.SelectorExpr); ok {
// field/method of type e.g. ("encoding/json".Decoder).Decode
y := unparen(x.X)
y := ast.Unparen(x.X)
if pkg := parseImportPath(y); pkg != "" {
spec.pkg = pkg // e.g. "encoding/json"
spec.pkgMember = x.Sel.Name // e.g. "Decoder"
Expand Down
3 changes: 0 additions & 3 deletions refactor/rename/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package rename

import (
"go/ast"
"go/token"
"go/types"
"os"
Expand Down Expand Up @@ -91,8 +90,6 @@ func sameFile(x, y string) bool {
return false
}

func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) }

func is[T any](x any) bool {
_, ok := x.(T)
return ok
Expand Down
38 changes: 16 additions & 22 deletions refactor/satisfy/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ func (f *Finder) exprN(e ast.Expr) types.Type {

case *ast.CallExpr:
// x, err := f(args)
sig := coreType(f.expr(e.Fun)).(*types.Signature)
sig := typeparams.CoreType(f.expr(e.Fun)).(*types.Signature)
f.call(sig, e.Args)

case *ast.IndexExpr:
// y, ok := x[i]
x := f.expr(e.X)
f.assign(f.expr(e.Index), coreType(x).(*types.Map).Key())
f.assign(f.expr(e.Index), typeparams.CoreType(x).(*types.Map).Key())

case *ast.TypeAssertExpr:
// y, ok := x.(T)
Expand Down Expand Up @@ -213,7 +213,7 @@ func (f *Finder) builtin(obj *types.Builtin, sig *types.Signature, args []ast.Ex
f.expr(args[1])
} else {
// append(x, y, z)
tElem := coreType(s).(*types.Slice).Elem()
tElem := typeparams.CoreType(s).(*types.Slice).Elem()
for _, arg := range args[1:] {
f.assign(tElem, f.expr(arg))
}
Expand All @@ -222,7 +222,7 @@ func (f *Finder) builtin(obj *types.Builtin, sig *types.Signature, args []ast.Ex
case "delete":
m := f.expr(args[0])
k := f.expr(args[1])
f.assign(coreType(m).(*types.Map).Key(), k)
f.assign(typeparams.CoreType(m).(*types.Map).Key(), k)

default:
// ordinary call
Expand Down Expand Up @@ -273,7 +273,7 @@ func (f *Finder) assign(lhs, rhs types.Type) {
if types.Identical(lhs, rhs) {
return
}
if !isInterface(lhs) {
if !types.IsInterface(lhs) {
return
}

Expand Down Expand Up @@ -354,7 +354,7 @@ func (f *Finder) expr(e ast.Expr) types.Type {
f.sig = saved

case *ast.CompositeLit:
switch T := coreType(typeparams.Deref(tv.Type)).(type) {
switch T := typeparams.CoreType(typeparams.Deref(tv.Type)).(type) {
case *types.Struct:
for i, elem := range e.Elts {
if kv, ok := elem.(*ast.KeyValueExpr); ok {
Expand Down Expand Up @@ -405,7 +405,7 @@ func (f *Finder) expr(e ast.Expr) types.Type {
// x[i] or m[k] -- index or lookup operation
x := f.expr(e.X)
i := f.expr(e.Index)
if ux, ok := coreType(x).(*types.Map); ok {
if ux, ok := typeparams.CoreType(x).(*types.Map); ok {
f.assign(ux.Key(), i)
}
}
Expand Down Expand Up @@ -440,7 +440,7 @@ func (f *Finder) expr(e ast.Expr) types.Type {
// unsafe call. Treat calls to functions in unsafe like ordinary calls,
// except that their signature cannot be determined by their func obj.
// Without this special handling, f.expr(e.Fun) would fail below.
if s, ok := unparen(e.Fun).(*ast.SelectorExpr); ok {
if s, ok := ast.Unparen(e.Fun).(*ast.SelectorExpr); ok {
if obj, ok := f.info.Uses[s.Sel].(*types.Builtin); ok && obj.Pkg().Path() == "unsafe" {
sig := f.info.Types[e.Fun].Type.(*types.Signature)
f.call(sig, e.Args)
Expand All @@ -449,7 +449,7 @@ func (f *Finder) expr(e ast.Expr) types.Type {
}

// builtin call
if id, ok := unparen(e.Fun).(*ast.Ident); ok {
if id, ok := ast.Unparen(e.Fun).(*ast.Ident); ok {
if obj, ok := f.info.Uses[id].(*types.Builtin); ok {
sig := f.info.Types[id].Type.(*types.Signature)
f.builtin(obj, sig, e.Args)
Expand All @@ -458,7 +458,7 @@ func (f *Finder) expr(e ast.Expr) types.Type {
}

// ordinary call
f.call(coreType(f.expr(e.Fun)).(*types.Signature), e.Args)
f.call(typeparams.CoreType(f.expr(e.Fun)).(*types.Signature), e.Args)
}

case *ast.StarExpr:
Expand Down Expand Up @@ -518,7 +518,7 @@ func (f *Finder) stmt(s ast.Stmt) {
case *ast.SendStmt:
ch := f.expr(s.Chan)
val := f.expr(s.Value)
f.assign(coreType(ch).(*types.Chan).Elem(), val)
f.assign(typeparams.CoreType(ch).(*types.Chan).Elem(), val)

case *ast.IncDecStmt:
f.expr(s.X)
Expand Down Expand Up @@ -622,9 +622,9 @@ func (f *Finder) stmt(s ast.Stmt) {
var I types.Type
switch ass := s.Assign.(type) {
case *ast.ExprStmt: // x.(type)
I = f.expr(unparen(ass.X).(*ast.TypeAssertExpr).X)
I = f.expr(ast.Unparen(ass.X).(*ast.TypeAssertExpr).X)
case *ast.AssignStmt: // y := x.(type)
I = f.expr(unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X)
I = f.expr(ast.Unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X)
}
for _, cc := range s.Body.List {
cc := cc.(*ast.CaseClause)
Expand Down Expand Up @@ -668,7 +668,7 @@ func (f *Finder) stmt(s ast.Stmt) {
var xelem types.Type
// Keys of array, *array, slice, string aren't interesting
// since the RHS key type is just an int.
switch ux := coreType(x).(type) {
switch ux := typeparams.CoreType(x).(type) {
case *types.Chan:
xelem = ux.Elem()
case *types.Map:
Expand All @@ -683,13 +683,13 @@ func (f *Finder) stmt(s ast.Stmt) {
var xelem types.Type
// Values of type strings aren't interesting because
// the RHS value type is just a rune.
switch ux := coreType(x).(type) {
switch ux := typeparams.CoreType(x).(type) {
case *types.Array:
xelem = ux.Elem()
case *types.Map:
xelem = ux.Elem()
case *types.Pointer: // *array
xelem = coreType(typeparams.Deref(ux)).(*types.Array).Elem()
xelem = typeparams.CoreType(typeparams.Deref(ux)).(*types.Array).Elem()
case *types.Slice:
xelem = ux.Elem()
}
Expand All @@ -707,12 +707,6 @@ func (f *Finder) stmt(s ast.Stmt) {

// -- Plundered from golang.org/x/tools/go/ssa -----------------

func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) }

func isInterface(T types.Type) bool { return types.IsInterface(T) }

func coreType(T types.Type) types.Type { return typeparams.CoreType(T) }

func instance(info *types.Info, expr ast.Expr) bool {
var id *ast.Ident
switch x := expr.(type) {
Expand Down

0 comments on commit 44b61a1

Please sign in to comment.