Skip to content

Commit e334696

Browse files
committed
gopls/internal/golang: ignore effects for change signature refactoring
While using the change signature refactoring, I never want it to literalize changed signatures based on its (very coarse) effects analysis. Disable this inlining analysis for the purpose of change signature refactoring. For golang/go#70599 Change-Id: I979c4f5b6be520b67a3441fa6e0c55afe1fe9196 Reviewed-on: https://go-review.googlesource.com/c/tools/+/633255 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 3901733 commit e334696

File tree

7 files changed

+17
-39
lines changed

7 files changed

+17
-39
lines changed

gopls/internal/golang/change_signature.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,11 @@ func rewriteCalls(ctx context.Context, rw signatureRewrite) (map[protocol.Docume
641641
}
642642

643643
post := func(got []byte) []byte { return bytes.ReplaceAll(got, []byte(tag), nil) }
644-
return inlineAllCalls(ctx, logf, rw.snapshot, rw.pkg, rw.pgf, rw.origDecl, calleeInfo, post)
644+
opts := &inline.Options{
645+
Logf: logf,
646+
IgnoreEffects: true,
647+
}
648+
return inlineAllCalls(ctx, rw.snapshot, rw.pkg, rw.pgf, rw.origDecl, calleeInfo, post, opts)
645649
}
646650

647651
// reTypeCheck re-type checks orig with new file contents defined by fileMask.

gopls/internal/golang/inline_all.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import (
4444
//
4545
// The code below notes where are assumptions are made that only hold true in
4646
// the case of parameter removal (annotated with 'Assumption:')
47-
func inlineAllCalls(ctx context.Context, logf func(string, ...any), snapshot *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, origDecl *ast.FuncDecl, callee *inline.Callee, post func([]byte) []byte) (map[protocol.DocumentURI][]byte, error) {
47+
func inlineAllCalls(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, origDecl *ast.FuncDecl, callee *inline.Callee, post func([]byte) []byte, opts *inline.Options) (map[protocol.DocumentURI][]byte, error) {
4848
// Collect references.
4949
var refs []protocol.Location
5050
{
@@ -215,7 +215,7 @@ func inlineAllCalls(ctx context.Context, logf func(string, ...any), snapshot *ca
215215
Call: calls[currentCall],
216216
Content: content,
217217
}
218-
res, err := inline.Inline(caller, callee, &inline.Options{Logf: logf})
218+
res, err := inline.Inline(caller, callee, opts)
219219
if err != nil {
220220
return nil, fmt.Errorf("inlining failed: %v", err)
221221
}
@@ -252,6 +252,10 @@ func inlineAllCalls(ctx context.Context, logf func(string, ...any), snapshot *ca
252252
// anything in the surrounding scope.
253253
//
254254
// TODO(rfindley): improve this.
255+
logf := func(string, ...any) {}
256+
if opts != nil {
257+
logf = opts.Logf
258+
}
255259
tpkg, tinfo, err = reTypeCheck(logf, callInfo.pkg, map[protocol.DocumentURI]*ast.File{uri: file}, true)
256260
if err != nil {
257261
return nil, bug.Errorf("type checking after inlining failed: %v", err)

gopls/internal/test/marker/testdata/codeaction/moveparam.txt

+1-4
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ func b() int { return 2 }
7070
var _ = basic.Foo(2, 1)
7171

7272
// Check that we can refactor a call with effects in a toplevel var decl.
73-
var _ = func() int {
74-
var a int = a()
75-
return basic.Foo(b(), a)
76-
}()
73+
var _ = basic.Foo(b(), a())
7774

7875
func _() {
7976
// check various refactorings in a function body, and comment handling.

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

+2-8
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,10 @@ func _() {
146146
Ellipsis()
147147
Ellipsis()
148148
Ellipsis()
149-
var _ []any = []any{1, f(), g()}
150149
Ellipsis()
151150
func(_ ...any) {
152151
Ellipsis()
153152
}(h())
154-
var _ []any = i()
155153
Ellipsis()
156154
}
157155

@@ -225,12 +223,8 @@ func f() int
225223
func g() int
226224

227225
func _() {
228-
var x, _ int = f(), g()
229-
effects(x)
230-
{
231-
var x, _ int = f(), g()
232-
effects(x)
233-
}
226+
effects(f())
227+
effects(f())
234228
}
235229
-- recursive/recursive.go --
236230
package recursive

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

-10
Original file line numberDiff line numberDiff line change
@@ -67,42 +67,34 @@ package a
6767

6868
import (
6969
"mod.test/b"
70-
"mod.test/c"
7170
)
7271

7372
func _() {
74-
var _ c.C = <-b.Chan
7573
b.B(<-b.Chan)
7674
}
7775

7876
func _() {
79-
var _ c.C = <-b.Chan
8077
b.B(<-b.Chan)
8178
}
8279
-- @b/a/a2.go --
8380
package a
8481

8582
import (
8683
"mod.test/b"
87-
"mod.test/c"
8884
)
8985

9086
func _() {
91-
var _ c.C = <-b.Chan
9287
b.B(<-b.Chan)
93-
var _ c.C = <-b.Chan
9488
b.B(<-b.Chan)
9589
}
9690
-- @b/a/a1.go --
9791
package a
9892

9993
import (
10094
"mod.test/b"
101-
"mod.test/c"
10295
)
10396

10497
func _() {
105-
var _ c.C = <-b.Chan
10698
b.B(<-b.Chan)
10799
}
108100
-- @b/a/a4.go --
@@ -113,11 +105,9 @@ package a
113105
import (
114106
"mod.test/b"
115107
. "mod.test/b"
116-
"mod.test/c"
117108
)
118109

119110
func _() {
120-
var _ c.C = <-Chan
121111
b.B(<-Chan)
122112
}
123113
-- @b/b/b.go --

gopls/internal/test/marker/testdata/codeaction/removeparam_method.txt

+1-6
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,7 @@ import "example.com/rm"
6868

6969
func _() {
7070
x := new(rm.Basic)
71-
var (
72-
t rm.Basic = *x
73-
_ int = sideEffects()
74-
)
75-
t.Foo()
71+
x.Foo()
7672
rm.Basic(1).Foo()
7773
}
7874

@@ -137,7 +133,6 @@ import "example.com/rm"
137133

138134
func _() {
139135
x := rm.Missing{}
140-
var _ int = sideEffects()
141136
x.M(1, 3, 4)
142137
}
143138

gopls/internal/test/marker/testdata/codeaction/removeparam_resolve.txt

+2-8
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,10 @@ func _() {
157157
Ellipsis()
158158
Ellipsis()
159159
Ellipsis()
160-
var _ []any = []any{1, f(), g()}
161160
Ellipsis()
162161
func(_ ...any) {
163162
Ellipsis()
164163
}(h())
165-
var _ []any = i()
166164
Ellipsis()
167165
}
168166

@@ -236,12 +234,8 @@ func f() int
236234
func g() int
237235

238236
func _() {
239-
var x, _ int = f(), g()
240-
effects(x)
241-
{
242-
var x, _ int = f(), g()
243-
effects(x)
244-
}
237+
effects(f())
238+
effects(f())
245239
}
246240
-- recursive/recursive.go --
247241
package recursive

0 commit comments

Comments
 (0)