Skip to content

Commit 44b61a1

Browse files
adonovangopherbot
authored andcommitted
x/tools: eliminate various unparen (et al) helpers
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]>
1 parent d0d86e4 commit 44b61a1

File tree

10 files changed

+34
-47
lines changed

10 files changed

+34
-47
lines changed

go/ssa/builder.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ func (sb *storebuf) emit(fn *Function) {
559559
// literal that may reference parts of the LHS.
560560
func (b *builder) assign(fn *Function, loc lvalue, e ast.Expr, isZero bool, sb *storebuf) {
561561
// Can we initialize it in place?
562-
if e, ok := unparen(e).(*ast.CompositeLit); ok {
562+
if e, ok := ast.Unparen(e).(*ast.CompositeLit); ok {
563563
// A CompositeLit never evaluates to a pointer,
564564
// so if the type of the location is a pointer,
565565
// an &-operation is implied.
@@ -614,7 +614,7 @@ func (b *builder) assign(fn *Function, loc lvalue, e ast.Expr, isZero bool, sb *
614614
// expr lowers a single-result expression e to SSA form, emitting code
615615
// to fn and returning the Value defined by the expression.
616616
func (b *builder) expr(fn *Function, e ast.Expr) Value {
617-
e = unparen(e)
617+
e = ast.Unparen(e)
618618

619619
tv := fn.info.Types[e]
620620

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

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

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

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

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

16801680
case *ast.ExprStmt: // <-ch
1681-
recv := unparen(comm.X).(*ast.UnaryExpr)
1681+
recv := ast.Unparen(comm.X).(*ast.UnaryExpr)
16821682
st = &SelectState{
16831683
Dir: types.RecvOnly,
16841684
Chan: b.expr(fn, recv.X),

go/ssa/emit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func emitDebugRef(f *Function, e ast.Expr, v Value, isAddr bool) {
8181
panic("nil")
8282
}
8383
var obj types.Object
84-
e = unparen(e)
84+
e = ast.Unparen(e)
8585
if id, ok := e.(*ast.Ident); ok {
8686
if isBlankIdent(id) {
8787
return

go/ssa/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func findNamedFunc(pkg *Package, pos token.Pos) *Function {
153153
// the ssa.Value.)
154154
func (f *Function) ValueForExpr(e ast.Expr) (value Value, isAddr bool) {
155155
if f.debugInfo() { // (opt)
156-
e = unparen(e)
156+
e = ast.Unparen(e)
157157
for _, b := range f.Blocks {
158158
for _, instr := range b.Instrs {
159159
if ref, ok := instr.(*DebugRef); ok {

go/ssa/util.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ func assert(p bool, msg string) {
3535

3636
//// AST utilities
3737

38-
func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) }
39-
4038
// isBlankIdent returns true iff e is an Ident with name "_".
4139
// They have no associated types.Object, and thus no type.
4240
func isBlankIdent(e ast.Expr) bool {

gopls/internal/golang/extract.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ func collectFreeVars(info *types.Info, file *ast.File, start, end token.Pos, nod
12291229
// return value acts as an indicator for where it was defined.
12301230
var sel func(n *ast.SelectorExpr) (types.Object, bool)
12311231
sel = func(n *ast.SelectorExpr) (types.Object, bool) {
1232-
switch x := astutil.Unparen(n.X).(type) {
1232+
switch x := ast.Unparen(n.X).(type) {
12331233
case *ast.SelectorExpr:
12341234
return sel(x)
12351235
case *ast.Ident:

gopls/internal/golang/freesymbols.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ func freeRefs(pkg *types.Package, info *types.Info, file *ast.File, start, end t
342342
for {
343343
suffix = append(suffix, info.Uses[sel.Sel])
344344

345-
switch x := astutil.Unparen(sel.X).(type) {
345+
switch x := ast.Unparen(sel.X).(type) {
346346
case *ast.Ident:
347347
return id(x, suffix)
348348
default:

refactor/eg/match.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func (tr *Transformer) matchExpr(x, y ast.Expr) bool {
3232
if x == nil || y == nil {
3333
return false
3434
}
35-
x = unparen(x)
36-
y = unparen(y)
35+
x = ast.Unparen(x)
36+
y = ast.Unparen(y)
3737

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

228228
// -- utilities --------------------------------------------------------
229229

230-
func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) }
231-
232230
// isRef returns the object referred to by this (possibly qualified)
233231
// identifier, or nil if the node is not a referring identifier.
234232
func isRef(n ast.Node, info *types.Info) types.Object {

refactor/rename/spec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func parseObjectSpec(spec *spec, main string) error {
155155
}
156156

157157
if e, ok := e.(*ast.SelectorExpr); ok {
158-
x := unparen(e.X)
158+
x := ast.Unparen(e.X)
159159

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

173173
if x, ok := x.(*ast.SelectorExpr); ok {
174174
// field/method of type e.g. ("encoding/json".Decoder).Decode
175-
y := unparen(x.X)
175+
y := ast.Unparen(x.X)
176176
if pkg := parseImportPath(y); pkg != "" {
177177
spec.pkg = pkg // e.g. "encoding/json"
178178
spec.pkgMember = x.Sel.Name // e.g. "Decoder"

refactor/rename/util.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package rename
66

77
import (
8-
"go/ast"
98
"go/token"
109
"go/types"
1110
"os"
@@ -91,8 +90,6 @@ func sameFile(x, y string) bool {
9190
return false
9291
}
9392

94-
func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) }
95-
9693
func is[T any](x any) bool {
9794
_, ok := x.(T)
9895
return ok

refactor/satisfy/find.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ func (f *Finder) exprN(e ast.Expr) types.Type {
126126

127127
case *ast.CallExpr:
128128
// x, err := f(args)
129-
sig := coreType(f.expr(e.Fun)).(*types.Signature)
129+
sig := typeparams.CoreType(f.expr(e.Fun)).(*types.Signature)
130130
f.call(sig, e.Args)
131131

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

137137
case *ast.TypeAssertExpr:
138138
// y, ok := x.(T)
@@ -213,7 +213,7 @@ func (f *Finder) builtin(obj *types.Builtin, sig *types.Signature, args []ast.Ex
213213
f.expr(args[1])
214214
} else {
215215
// append(x, y, z)
216-
tElem := coreType(s).(*types.Slice).Elem()
216+
tElem := typeparams.CoreType(s).(*types.Slice).Elem()
217217
for _, arg := range args[1:] {
218218
f.assign(tElem, f.expr(arg))
219219
}
@@ -222,7 +222,7 @@ func (f *Finder) builtin(obj *types.Builtin, sig *types.Signature, args []ast.Ex
222222
case "delete":
223223
m := f.expr(args[0])
224224
k := f.expr(args[1])
225-
f.assign(coreType(m).(*types.Map).Key(), k)
225+
f.assign(typeparams.CoreType(m).(*types.Map).Key(), k)
226226

227227
default:
228228
// ordinary call
@@ -273,7 +273,7 @@ func (f *Finder) assign(lhs, rhs types.Type) {
273273
if types.Identical(lhs, rhs) {
274274
return
275275
}
276-
if !isInterface(lhs) {
276+
if !types.IsInterface(lhs) {
277277
return
278278
}
279279

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

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

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

460460
// ordinary call
461-
f.call(coreType(f.expr(e.Fun)).(*types.Signature), e.Args)
461+
f.call(typeparams.CoreType(f.expr(e.Fun)).(*types.Signature), e.Args)
462462
}
463463

464464
case *ast.StarExpr:
@@ -518,7 +518,7 @@ func (f *Finder) stmt(s ast.Stmt) {
518518
case *ast.SendStmt:
519519
ch := f.expr(s.Chan)
520520
val := f.expr(s.Value)
521-
f.assign(coreType(ch).(*types.Chan).Elem(), val)
521+
f.assign(typeparams.CoreType(ch).(*types.Chan).Elem(), val)
522522

523523
case *ast.IncDecStmt:
524524
f.expr(s.X)
@@ -622,9 +622,9 @@ func (f *Finder) stmt(s ast.Stmt) {
622622
var I types.Type
623623
switch ass := s.Assign.(type) {
624624
case *ast.ExprStmt: // x.(type)
625-
I = f.expr(unparen(ass.X).(*ast.TypeAssertExpr).X)
625+
I = f.expr(ast.Unparen(ass.X).(*ast.TypeAssertExpr).X)
626626
case *ast.AssignStmt: // y := x.(type)
627-
I = f.expr(unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X)
627+
I = f.expr(ast.Unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X)
628628
}
629629
for _, cc := range s.Body.List {
630630
cc := cc.(*ast.CaseClause)
@@ -668,7 +668,7 @@ func (f *Finder) stmt(s ast.Stmt) {
668668
var xelem types.Type
669669
// Keys of array, *array, slice, string aren't interesting
670670
// since the RHS key type is just an int.
671-
switch ux := coreType(x).(type) {
671+
switch ux := typeparams.CoreType(x).(type) {
672672
case *types.Chan:
673673
xelem = ux.Elem()
674674
case *types.Map:
@@ -683,13 +683,13 @@ func (f *Finder) stmt(s ast.Stmt) {
683683
var xelem types.Type
684684
// Values of type strings aren't interesting because
685685
// the RHS value type is just a rune.
686-
switch ux := coreType(x).(type) {
686+
switch ux := typeparams.CoreType(x).(type) {
687687
case *types.Array:
688688
xelem = ux.Elem()
689689
case *types.Map:
690690
xelem = ux.Elem()
691691
case *types.Pointer: // *array
692-
xelem = coreType(typeparams.Deref(ux)).(*types.Array).Elem()
692+
xelem = typeparams.CoreType(typeparams.Deref(ux)).(*types.Array).Elem()
693693
case *types.Slice:
694694
xelem = ux.Elem()
695695
}
@@ -707,12 +707,6 @@ func (f *Finder) stmt(s ast.Stmt) {
707707

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

710-
func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) }
711-
712-
func isInterface(T types.Type) bool { return types.IsInterface(T) }
713-
714-
func coreType(T types.Type) types.Type { return typeparams.CoreType(T) }
715-
716710
func instance(info *types.Info, expr ast.Expr) bool {
717711
var id *ast.Ident
718712
switch x := expr.(type) {

0 commit comments

Comments
 (0)