Skip to content

Commit af71e42

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/golang: Rename: fix crash in ill-typed redeclaration
+ test Fixes golang/go#70968 Change-Id: I5d446dc3d4bd530a73565a17c12fa875bf25cfd0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/663915 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
1 parent 9fbec96 commit af71e42

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

gopls/internal/golang/rename_check.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -472,14 +472,15 @@ func (r *renamer) checkStructField(from *types.Var) {
472472
// This struct is also a named type.
473473
// We must check for direct (non-promoted) field/field
474474
// and method/field conflicts.
475-
named := r.pkg.TypesInfo().Defs[spec.Name].Type()
476-
prev, indices, _ := types.LookupFieldOrMethod(named, true, r.pkg.Types(), r.to)
477-
if len(indices) == 1 {
478-
r.errorf(from.Pos(), "renaming this field %q to %q",
479-
from.Name(), r.to)
480-
r.errorf(prev.Pos(), "\twould conflict with this %s",
481-
objectKind(prev))
482-
return // skip checkSelections to avoid redundant errors
475+
if tname := r.pkg.TypesInfo().Defs[spec.Name]; tname != nil {
476+
prev, indices, _ := types.LookupFieldOrMethod(tname.Type(), true, r.pkg.Types(), r.to)
477+
if len(indices) == 1 {
478+
r.errorf(from.Pos(), "renaming this field %q to %q",
479+
from.Name(), r.to)
480+
r.errorf(prev.Pos(), "\twould conflict with this %s",
481+
objectKind(prev))
482+
return // skip checkSelections to avoid redundant errors
483+
}
483484
}
484485
} else {
485486
// This struct is not a named type.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Test that an (ill-typed) redeclaration of a name, which causes
2+
types.Info.Defs to lack an entry, doesn't lead to gopls to crash in
3+
renaming. Now, it proceeds with a partial rename.
4+
5+
See golang/go#70968
6+
7+
-- go.mod --
8+
module example.com
9+
go 1.18
10+
11+
-- a/a.go --
12+
package a
13+
14+
type T int //@ diag("T", re"T redeclared")
15+
type T struct { f int } //@ diag("T", re"T redeclared"), rename("f", "g", out)
16+
17+
-- @out/a/a.go --
18+
@@ -4 +4 @@
19+
-type T struct { f int } //@ diag("T", re"T redeclared"), rename("f", "g", out)
20+
+type T struct { g int } //@ diag("T", re"T redeclared"), rename("f", "g", out)

0 commit comments

Comments
 (0)