internal/refactor/inline: work around channel type misformatting

This change adds parens around the type in T(x) conversions
where T is a receive-only channel type, as previously it
would be misformatted as a receive of a receive.

Updates golang/go#63362

Change-Id: I935b5598d4bc3ea57dd52964e8b02005f5e6ef72
Reviewed-on: https://go-review.googlesource.com/c/tools/+/532576
Reviewed-by: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Alan Donovan 2023-10-04 11:06:14 -04:00
Родитель 0ba9c8439e
Коммит 2be977ecc5
4 изменённых файлов: 22 добавлений и 8 удалений

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

@ -610,10 +610,7 @@ func (st *falconState) expr(e ast.Expr) (res any) { // = types.TypeAndValue | as
// Possible "value out of range". // Possible "value out of range".
kX := st.expr(e.Args[0]) kX := st.expr(e.Args[0])
if kX != nil && isBasic(tv.Type, types.IsConstType) { if kX != nil && isBasic(tv.Type, types.IsConstType) {
conv := &ast.CallExpr{ conv := convert(makeIdent(st.typename(tv.Type)), st.toExpr(kX))
Fun: makeIdent(st.typename(tv.Type)),
Args: []ast.Expr{st.toExpr(kX)},
}
if is[ast.Expr](kX) { if is[ast.Expr](kX) {
st.emit(conv) st.emit(conv)
} }

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

@ -1284,10 +1284,7 @@ next:
// a binding decl or when using the literalization // a binding decl or when using the literalization
// strategy. // strategy.
if len(param.info.Refs) > 0 && !trivialConversion(args[i].typ, params[i].obj) { if len(param.info.Refs) > 0 && !trivialConversion(args[i].typ, params[i].obj) {
arg.expr = &ast.CallExpr{ arg.expr = convert(params[i].fieldType, arg.expr)
Fun: params[i].fieldType, // formatter adds parens as needed
Args: []ast.Expr{arg.expr},
}
logf("param %q: adding explicit %s -> %s conversion around argument", logf("param %q: adding explicit %s -> %s conversion around argument",
param.info.Name, args[i].typ, params[i].obj.Type()) param.info.Name, args[i].typ, params[i].obj.Type())
} }

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

@ -382,6 +382,12 @@ func TestBasics(t *testing.T) {
print(s, s, 0, 0) print(s, s, 0, 0)
}`, }`,
}, },
{
"Workaround for T(x) misformatting (#63362).",
`func f(ch <-chan int) { <-ch }`,
`func _(ch chan int) { f(ch) }`,
`func _(ch chan int) { <-(<-chan int)(ch) }`,
},
}) })
} }

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

@ -103,3 +103,17 @@ func intersects[K comparable, T1, T2 any](x map[K]T1, y map[K]T2) bool {
} }
return false return false
} }
// convert returns syntax for the conversion T(x).
func convert(T, x ast.Expr) *ast.CallExpr {
// The formatter generally adds parens as needed,
// but before go1.22 it had a bug (#63362) for
// channel types that requires this workaround.
if ch, ok := T.(*ast.ChanType); ok && ch.Dir == ast.RECV {
T = &ast.ParenExpr{X: T}
}
return &ast.CallExpr{
Fun: T,
Args: []ast.Expr{x},
}
}