Skip to content

Commit

Permalink
internal/refactor/inline: fix condition for splice assignment strategy
Browse files Browse the repository at this point in the history
In the assign statements strategy, if there are no nontrivial result
conversions, we can replace a call with its result expression regardless
of the AssignStmt.Tok.

For golang/go#70599

Change-Id: Ifa2b615c17d58e3bbc708b9ecf98530858915564
Reviewed-on: https://go-review.googlesource.com/c/tools/+/632937
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
findleyr committed Dec 3, 2024
1 parent 557f540 commit 01e0b05
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
This test reproduces various bugs encountered while bug-bashing on the movement
refactoring.

TODO(rfindley): fix these bugs.
This test checks the fixes for bugs encountered while bug-bashing on the
movement refactoring.

-- go.mod --
module example.com
Expand Down Expand Up @@ -74,8 +72,7 @@ func Short(y, x int) (int, int) { //@codeaction("x", "refactor.rewrite.moveParam
}

func _() {
var x, y int
x, y = Short(1, 0)
x, y := Short(1, 0)
_, _ = x, y
}

Expand Down
5 changes: 2 additions & 3 deletions internal/refactor/inline/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -3266,10 +3266,9 @@ func (st *state) assignStmts(callerStmt *ast.AssignStmt, returnOperands []ast.Ex
//
// This works as long as we don't need to write any additional type
// information.
if callerStmt.Tok == token.ASSIGN && // LHS types already determined before call
len(nonTrivial) == 0 { // no non-trivial conversions to worry about
if len(nonTrivial) == 0 { // no non-trivial conversions to worry about

logf("substrategy: slice assignment")
logf("substrategy: splice assignment")
return []ast.Stmt{&ast.AssignStmt{
Lhs: lhs,
Tok: callerStmt.Tok,
Expand Down
62 changes: 62 additions & 0 deletions internal/refactor/inline/testdata/assignment-splice.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
This test checks the splice assignment substrategy.

-- go.mod --
module testdata

go 1.20

-- a.go --
package a

func a() (int32, string) {
return b()
}

func b() (int32, string) {
return 0, "a"
}

func c() (int, chan<- int) {
return 0, make(chan int) // nontrivial conversion
}

-- a1.go --
package a

func _() {
x, y := a() //@ inline(re"a", a1)
}
-- a1 --
package a

func _() {
x, y := b() //@ inline(re"a", a1)
}
-- a2.go --
package a

func _() {
var x, y any
x, y = a() //@ inline(re"a", a2)
}
-- a2 --
package a

func _() {
var x, y any
x, y = b() //@ inline(re"a", a2)
}
-- a3.go --
package a

func _() {
var y chan<- int
x, y := c() //@ inline(re"c", a3)
}
-- a3 --
package a

func _() {
var y chan<- int
x, y := 0, make(chan int) //@ inline(re"c", a3)
}
7 changes: 3 additions & 4 deletions internal/refactor/inline/testdata/assignment.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ Basic tests of inlining a call on the RHS of an assignment.

-- go.mod --
module testdata
go 1.12

go 1.20

-- a/a1.go --
package a
Expand Down Expand Up @@ -104,13 +105,11 @@ package a

import (
"testdata/b"
"testdata/c"
)

func _() {
var y int
var x c.C
x, y = b.B1() //@ inline(re"B", b2)
x, y := b.B1() //@ inline(re"B", b2)
_, _ = x, y
}
-- b3 --
Expand Down

0 comments on commit 01e0b05

Please sign in to comment.