Skip to content

Commit 2b1f550

Browse files
committed
gopls/internal/analysis/gofix: allow literal array lengths
An array type can be inlined if its length is a literal integer. For golang/go#32816. Change-Id: I80c7f18721c813a0ea7039411ddf8a804b5bf0b5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651655 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 0ffdb82 commit 2b1f550

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

gopls/internal/analysis/gofix/gofix.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,12 @@ func (a *analyzer) findAlias(spec *ast.TypeSpec, declInline bool) {
148148
// type A = [N]int
149149
//
150150
// would result in [5]int, breaking the connection with N.
151-
// TODO(jba): accept type expressions where the array size is a literal integer
152151
for n := range ast.Preorder(spec.Type) {
153152
if ar, ok := n.(*ast.ArrayType); ok && ar.Len != nil {
153+
// Make an exception when the array length is a literal int.
154+
if lit, ok := ast.Unparen(ar.Len).(*ast.BasicLit); ok && lit.Kind == token.INT {
155+
continue
156+
}
154157
a.pass.Reportf(spec.Pos(), "invalid //go:fix inline directive: array types not supported")
155158
return
156159
}

gopls/internal/analysis/gofix/testdata/src/a/a.go

+7
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ type E = map[[Uno]string][]*T // want `invalid //go:fix inline directive: array
129129

130130
var _ E // nothing should happen here
131131

132+
// literal array lengths are OK
133+
//
134+
//go:fix inline
135+
type EL = map[[2]string][]*T // want EL: `goFixInline alias`
136+
137+
var _ EL // want `Type alias EL should be inlined`
138+
132139
//go:fix inline
133140
type F = map[internal.T]T // want F: `goFixInline alias`
134141

gopls/internal/analysis/gofix/testdata/src/a/a.go.golden

+7
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ type E = map[[Uno]string][]*T // want `invalid //go:fix inline directive: array
129129

130130
var _ E // nothing should happen here
131131

132+
// literal array lengths are OK
133+
//
134+
//go:fix inline
135+
type EL = map[[2]string][]*T // want EL: `goFixInline alias`
136+
137+
var _ map[[2]string][]*T // want `Type alias EL should be inlined`
138+
132139
//go:fix inline
133140
type F = map[internal.T]T // want F: `goFixInline alias`
134141

0 commit comments

Comments
 (0)