Skip to content

Commit fc82fb2

Browse files
committed
internal/lsp: return error when renaming within an import spec
Since renaming an identifier within an import spec is not yet supported, return an error when this is encountered. These idents from the import spec have a nil declaration object. Import paths that contain '.' or '/' are caught by the valid identifier check avoiding the crash, but import paths such as "fmt" are not as fmt is a valid identifier. This change checks if i.decl.obj is nil and returns an error if it is to avoid the crash. Fixes golang/go#33768 Change-Id: I4e757b42bedffd648fc821590e4a383826200dc3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/191163 Run-TryBot: Suzy Mueller <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Cottrell <[email protected]>
1 parent 65e3620 commit fc82fb2

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

internal/lsp/source/rename.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ func (i *IdentifierInfo) Rename(ctx context.Context, newName string) (map[span.U
4343
if i.Name == newName {
4444
return nil, errors.Errorf("old and new names are the same: %s", newName)
4545
}
46+
// If the object declaration is nil, assume it is an import spec and return an error.
47+
// TODO(suzmue): support renaming of identifiers in an import spec.
48+
if i.decl.obj == nil {
49+
return nil, errors.Errorf("renaming import %q not supported", i.Name)
50+
}
4651
if !isValidIdentifier(i.Name) {
4752
return nil, errors.Errorf("invalid identifier to rename: %q", i.Name)
4853
}

internal/lsp/testdata/rename/a/random.go.golden

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package a
44

55
import (
66
lg "log"
7-
"fmt"
7+
"fmt" //@rename("fmt", "fmty")
88
f2 "fmt"
99
)
1010

@@ -49,7 +49,7 @@ package a
4949

5050
import (
5151
lg "log"
52-
"fmt"
52+
"fmt" //@rename("fmt", "fmty")
5353
fmt2 "fmt"
5454
)
5555

@@ -88,13 +88,15 @@ func sw() {
8888
}
8989
}
9090

91+
-- fmty-rename --
92+
renaming import "fmt" not supported
9193
-- format-rename --
9294
random.go:
9395
package a
9496

9597
import (
9698
lg "log"
97-
format "fmt"
99+
format "fmt" //@rename("fmt", "fmty")
98100
f2 "fmt"
99101
)
100102

@@ -139,7 +141,7 @@ package a
139141

140142
import (
141143
"log"
142-
"fmt"
144+
"fmt" //@rename("fmt", "fmty")
143145
f2 "fmt"
144146
)
145147

@@ -184,7 +186,7 @@ package a
184186

185187
import (
186188
lg "log"
187-
"fmt"
189+
"fmt" //@rename("fmt", "fmty")
188190
f2 "fmt"
189191
)
190192

@@ -229,7 +231,7 @@ package a
229231

230232
import (
231233
lg "log"
232-
"fmt"
234+
"fmt" //@rename("fmt", "fmty")
233235
f2 "fmt"
234236
)
235237

@@ -274,7 +276,7 @@ package a
274276

275277
import (
276278
lg "log"
277-
"fmt"
279+
"fmt" //@rename("fmt", "fmty")
278280
f2 "fmt"
279281
)
280282

@@ -319,7 +321,7 @@ package a
319321

320322
import (
321323
lg "log"
322-
"fmt"
324+
"fmt" //@rename("fmt", "fmty")
323325
f2 "fmt"
324326
)
325327

@@ -364,7 +366,7 @@ package a
364366

365367
import (
366368
lg "log"
367-
"fmt"
369+
"fmt" //@rename("fmt", "fmty")
368370
f2 "fmt"
369371
)
370372

@@ -409,7 +411,7 @@ package a
409411

410412
import (
411413
lg "log"
412-
"fmt"
414+
"fmt" //@rename("fmt", "fmty")
413415
f2 "fmt"
414416
)
415417

@@ -454,7 +456,7 @@ package a
454456

455457
import (
456458
lg "log"
457-
"fmt"
459+
"fmt" //@rename("fmt", "fmty")
458460
f2 "fmt"
459461
)
460462

internal/lsp/testdata/rename/a/random.go.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package a
22

33
import (
44
lg "log"
5-
"fmt"
5+
"fmt" //@rename("fmt", "fmty")
66
f2 "fmt"
77
)
88

internal/lsp/tests/tests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const (
3838
ExpectedTypeDefinitionsCount = 2
3939
ExpectedHighlightsCount = 2
4040
ExpectedReferencesCount = 5
41-
ExpectedRenamesCount = 17
41+
ExpectedRenamesCount = 18
4242
ExpectedSymbolsCount = 1
4343
ExpectedSignaturesCount = 21
4444
ExpectedLinksCount = 4

0 commit comments

Comments
 (0)