Skip to content

Commit 61bb3e9

Browse files
committed
internal/refactor/inline: less hacky solution for eliding braces
Joint with Alan Donovan (CL 535455) :) For golang/go#63534 Change-Id: Ia4fdc617edf6699da285f56670a51efac1817834 Reviewed-on: https://go-review.googlesource.com/c/tools/+/534918 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 8a71c39 commit 61bb3e9

File tree

8 files changed

+17
-48
lines changed

8 files changed

+17
-48
lines changed

gopls/internal/regtest/marker/testdata/codeaction/removeparam.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,13 @@ func _() {
145145
Ellipsis()
146146
Ellipsis()
147147
Ellipsis()
148-
149148
var _ []any = []any{1, f(), g()}
150149
Ellipsis()
151-
152150
func(_ ...any) {
153151
Ellipsis()
154152
}(h())
155-
156153
var _ []any = i()
157154
Ellipsis()
158-
159155
}
160156

161157
func f() int
@@ -228,10 +224,8 @@ func f() int
228224
func g() int
229225

230226
func _() {
231-
232227
var x, _ int = f(), g()
233228
effects(x)
234-
235229
{
236230
var x, _ int = f(), g()
237231
effects(x)

gopls/internal/regtest/marker/testdata/codeaction/removeparam_imports.txt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ import (
8989
)
9090

9191
func _() {
92-
9392
var _ c.C = <-b.Chan
9493
b.B(<-b.Chan)
95-
9694
}
9795
-- @b/a/a2.go --
9896
package a
@@ -103,13 +101,10 @@ import (
103101
)
104102

105103
func _() {
106-
107104
var _ c.C = <-b.Chan
108105
b.B(<-b.Chan)
109-
110106
var _ c.C = <-b.Chan
111107
b.B(<-b.Chan)
112-
113108
}
114109
-- @b/a/a3.go --
115110
package a
@@ -120,17 +115,13 @@ import (
120115
)
121116

122117
func _() {
123-
124118
var _ c.C = <-b.Chan
125119
b.B(<-b.Chan)
126-
127120
}
128121

129122
func _() {
130-
131123
var _ c.C = <-b.Chan
132124
b.B(<-b.Chan)
133-
134125
}
135126
-- @b/a/a4.go --
136127
package a
@@ -144,10 +135,8 @@ import (
144135
)
145136

146137
func _() {
147-
148138
var _ c.C = <-Chan
149139
b.B(<-Chan)
150-
151140
}
152141
-- @b/b/b.go --
153142
package b

internal/refactor/inline/inline.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,19 @@ func Inline(logf func(string, ...any), caller *Caller, callee *Callee) ([]byte,
183183
// Precise comment handling would make this a
184184
// non-issue. Formatting wouldn't really need a
185185
// FileSet at all.
186-
mark := out.Len()
187-
if err := format.Node(&out, caller.Fset, res.new); err != nil {
188-
return nil, err
189-
}
190186
if elideBraces {
191-
// Overwrite unnecessary {...} braces with spaces.
192-
// TODO(adonovan): less hacky solution.
193-
out.Bytes()[mark] = ' '
194-
out.Bytes()[out.Len()-1] = ' '
187+
for i, stmt := range res.new.(*ast.BlockStmt).List {
188+
if i > 0 {
189+
out.WriteByte('\n')
190+
}
191+
if err := format.Node(&out, caller.Fset, stmt); err != nil {
192+
return nil, err
193+
}
194+
}
195+
} else {
196+
if err := format.Node(&out, caller.Fset, res.new); err != nil {
197+
return nil, err
198+
}
195199
}
196200
out.Write(caller.Content[end:])
197201
const mode = parser.ParseComments | parser.SkipObjectResolution | parser.AllErrors
@@ -902,9 +906,6 @@ func inline(logf func(string, ...any), caller *Caller, callee *gobCallee) (*resu
902906
// The body may use defer, arbitrary control flow, and
903907
// multiple returns.
904908
//
905-
// TODO(adonovan): omit the braces if the sets of
906-
// names in the two blocks are disjoint.
907-
//
908909
// TODO(adonovan): add a strategy for a 'void tail
909910
// call', i.e. a call statement prior to an (explicit
910911
// or implicit) return.
@@ -942,8 +943,6 @@ func inline(logf func(string, ...any), caller *Caller, callee *gobCallee) (*resu
942943
// - all parameters and result vars can be eliminated
943944
// or replaced by a binding decl,
944945
// - caller ExprStmt is in unrestricted statement context.
945-
//
946-
// If there is only a single statement, the braces are omitted.
947946
if stmt := callStmt(caller.path, true); stmt != nil &&
948947
(!needBindingDecl || bindingDeclStmt != nil) &&
949948
!callee.HasDefer &&
@@ -956,9 +955,6 @@ func inline(logf func(string, ...any), caller *Caller, callee *gobCallee) (*resu
956955
if needBindingDecl {
957956
body.List = prepend(bindingDeclStmt, body.List...)
958957
}
959-
if len(body.List) == 1 { // FIXME do this opt later
960-
repl = body.List[0] // singleton: omit braces
961-
}
962958
res.old = stmt
963959
res.new = repl
964960
return res, nil

internal/refactor/inline/testdata/import-shadow.txtar

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,8 @@ import (
8787
var x b.T
8888

8989
func A(b int) {
90-
9190
b0.One()
92-
b0.Two()
93-
//@ inline(re"F", fresult)
91+
b0.Two() //@ inline(re"F", fresult)
9492
}
9593

9694
-- d/d.go --

internal/refactor/inline/testdata/issue63298.txtar

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ import (
4545
)
4646

4747
func _() {
48-
4948
b.B()
5049
b0.B()
51-
52-
}
50+
}

internal/refactor/inline/testdata/method.txtar

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,8 @@ func (T) h() int { return 1 }
104104

105105
func _() {
106106
var ptr *T
107-
108107
var _ T = *ptr
109-
_ = 1
110-
//@ inline(re"h", h)
108+
_ = 1 //@ inline(re"h", h)
111109
}
112110

113111
-- a/i.go --

internal/refactor/inline/testdata/multistmt-body.txtar

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,8 @@ package a
5454

5555
func _() {
5656
a := 1
57-
5857
z := 1
59-
print(a + 2 + z)
60-
//@ inline(re"f", out2)
58+
print(a + 2 + z) //@ inline(re"f", out2)
6159
}
6260

6361
-- a/a3.go --

internal/refactor/inline/testdata/tailcall.txtar

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ start:
3636
package a
3737

3838
func _() int {
39-
4039
total := 0
4140
start:
4241
for i := 1; i <= 2; i++ {
@@ -47,8 +46,7 @@ start:
4746
return -1
4847
}
4948
}
50-
return total
51-
//@ inline(re"sum", sum)
49+
return total //@ inline(re"sum", sum)
5250
}
5351

5452
func sum(lo, hi int) int {

0 commit comments

Comments
 (0)