internal/refactor/inline: add missing spread context (return)

A call f() where f() has tuple type may appear as the
sole operand of a return statement. I forgot this case
in the "reduce spread-context call" strategy.

Plus a test.

Fixes golang/go#63398

Change-Id: Ie851c977c3a2d237feabc95dbed4c50e6a1c96ad
Reviewed-on: https://go-review.googlesource.com/c/tools/+/533176
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
This commit is contained in:
Alan Donovan 2023-10-05 13:35:16 -04:00 коммит произвёл Gopher Robot
Родитель 8954aa7a59
Коммит 0e4fc907c8
2 изменённых файлов: 16 добавлений и 3 удалений

Просмотреть файл

@ -844,23 +844,30 @@ func inline(logf func(string, ...any), caller *Caller, callee *gobCallee) (*resu
// x, y = f()
// or the sole argument to a spread call:
// printf(f())
// or spread return statement:
// return f()
res.old = context
switch context := context.(type) {
case *ast.AssignStmt:
// Inv: the call is in Rhs[0], not Lhs.
// Inv: the call must be in Rhs[0], not Lhs.
assign := shallowCopy(context)
assign.Rhs = results
res.new = assign
case *ast.ValueSpec:
// Inv: the call is in Values[0], not Names.
// Inv: the call must be in Values[0], not Names.
spec := shallowCopy(context)
spec.Values = results
res.new = spec
case *ast.CallExpr:
// Inv: the Call is Args[0], not Fun.
// Inv: the call must be in Args[0], not Fun.
call := shallowCopy(context)
call.Args = results
res.new = call
case *ast.ReturnStmt:
// Inv: the call must be Results[0].
ret := shallowCopy(context)
ret.Results = results
res.new = ret
default:
return nil, fmt.Errorf("internal error: unexpected context %T for spread call", context)
}

Просмотреть файл

@ -617,6 +617,12 @@ func TestSpreadCalls(t *testing.T) {
)
}`,
},
{
"Spread call in return (#63398).",
`func f() (int, error) { return 0, nil }`,
`func _() (int, error) { return f() }`,
`func _() (int, error) { return 0, nil }`,
},
})
}