Skip to content

Commit

Permalink
internal/refactor/inline: work around channel type misformatting
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
adonovan committed Oct 5, 2023
1 parent 0ba9c84 commit 2be977e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
5 changes: 1 addition & 4 deletions internal/refactor/inline/falcon.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,7 @@ func (st *falconState) expr(e ast.Expr) (res any) { // = types.TypeAndValue | as
// Possible "value out of range".
kX := st.expr(e.Args[0])
if kX != nil && isBasic(tv.Type, types.IsConstType) {
conv := &ast.CallExpr{
Fun: makeIdent(st.typename(tv.Type)),
Args: []ast.Expr{st.toExpr(kX)},
}
conv := convert(makeIdent(st.typename(tv.Type)), st.toExpr(kX))
if is[ast.Expr](kX) {
st.emit(conv)
}
Expand Down
5 changes: 1 addition & 4 deletions internal/refactor/inline/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -1284,10 +1284,7 @@ next:
// a binding decl or when using the literalization
// strategy.
if len(param.info.Refs) > 0 && !trivialConversion(args[i].typ, params[i].obj) {
arg.expr = &ast.CallExpr{
Fun: params[i].fieldType, // formatter adds parens as needed
Args: []ast.Expr{arg.expr},
}
arg.expr = convert(params[i].fieldType, arg.expr)
logf("param %q: adding explicit %s -> %s conversion around argument",
param.info.Name, args[i].typ, params[i].obj.Type())
}
Expand Down
6 changes: 6 additions & 0 deletions internal/refactor/inline/inline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ func TestBasics(t *testing.T) {
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) }`,
},
})
}

Expand Down
14 changes: 14 additions & 0 deletions internal/refactor/inline/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,17 @@ func intersects[K comparable, T1, T2 any](x map[K]T1, y map[K]T2) bool {
}
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},
}
}

0 comments on commit 2be977e

Please sign in to comment.