Skip to content

Commit 2be977e

Browse files
committed
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 0ba9c84 commit 2be977e

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

internal/refactor/inline/falcon.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -610,10 +610,7 @@ func (st *falconState) expr(e ast.Expr) (res any) { // = types.TypeAndValue | as
610610
// Possible "value out of range".
611611
kX := st.expr(e.Args[0])
612612
if kX != nil && isBasic(tv.Type, types.IsConstType) {
613-
conv := &ast.CallExpr{
614-
Fun: makeIdent(st.typename(tv.Type)),
615-
Args: []ast.Expr{st.toExpr(kX)},
616-
}
613+
conv := convert(makeIdent(st.typename(tv.Type)), st.toExpr(kX))
617614
if is[ast.Expr](kX) {
618615
st.emit(conv)
619616
}

internal/refactor/inline/inline.go

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

internal/refactor/inline/inline_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,12 @@ func TestBasics(t *testing.T) {
382382
print(s, s, 0, 0)
383383
}`,
384384
},
385+
{
386+
"Workaround for T(x) misformatting (#63362).",
387+
`func f(ch <-chan int) { <-ch }`,
388+
`func _(ch chan int) { f(ch) }`,
389+
`func _(ch chan int) { <-(<-chan int)(ch) }`,
390+
},
385391
})
386392
}
387393

internal/refactor/inline/util.go

+14
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,17 @@ func intersects[K comparable, T1, T2 any](x map[K]T1, y map[K]T2) bool {
103103
}
104104
return false
105105
}
106+
107+
// convert returns syntax for the conversion T(x).
108+
func convert(T, x ast.Expr) *ast.CallExpr {
109+
// The formatter generally adds parens as needed,
110+
// but before go1.22 it had a bug (#63362) for
111+
// channel types that requires this workaround.
112+
if ch, ok := T.(*ast.ChanType); ok && ch.Dir == ast.RECV {
113+
T = &ast.ParenExpr{X: T}
114+
}
115+
return &ast.CallExpr{
116+
Fun: T,
117+
Args: []ast.Expr{x},
118+
}
119+
}

0 commit comments

Comments
 (0)