Skip to content

Commit

Permalink
gopls/internal/analysis/gofix: allow literal array lengths
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jba committed Mar 3, 2025
1 parent 0ffdb82 commit 2b1f550
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion gopls/internal/analysis/gofix/gofix.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,12 @@ func (a *analyzer) findAlias(spec *ast.TypeSpec, declInline bool) {
// type A = [N]int
//
// would result in [5]int, breaking the connection with N.
// TODO(jba): accept type expressions where the array size is a literal integer
for n := range ast.Preorder(spec.Type) {
if ar, ok := n.(*ast.ArrayType); ok && ar.Len != nil {
// Make an exception when the array length is a literal int.
if lit, ok := ast.Unparen(ar.Len).(*ast.BasicLit); ok && lit.Kind == token.INT {
continue
}
a.pass.Reportf(spec.Pos(), "invalid //go:fix inline directive: array types not supported")
return
}
Expand Down
7 changes: 7 additions & 0 deletions gopls/internal/analysis/gofix/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ type E = map[[Uno]string][]*T // want `invalid //go:fix inline directive: array

var _ E // nothing should happen here

// literal array lengths are OK
//
//go:fix inline
type EL = map[[2]string][]*T // want EL: `goFixInline alias`

var _ EL // want `Type alias EL should be inlined`

//go:fix inline
type F = map[internal.T]T // want F: `goFixInline alias`

Expand Down
7 changes: 7 additions & 0 deletions gopls/internal/analysis/gofix/testdata/src/a/a.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ type E = map[[Uno]string][]*T // want `invalid //go:fix inline directive: array

var _ E // nothing should happen here

// literal array lengths are OK
//
//go:fix inline
type EL = map[[2]string][]*T // want EL: `goFixInline alias`

var _ map[[2]string][]*T // want `Type alias EL should be inlined`

//go:fix inline
type F = map[internal.T]T // want F: `goFixInline alias`

Expand Down

0 comments on commit 2b1f550

Please sign in to comment.