Skip to content

Commit 7522327

Browse files
tttoadgopherbot
authored andcommitted
gopls/rename: Fix spurious package name conflicts.
Fixes golang/go#67069 Change-Id: I537308c8941a5f2f2c6e10791e75b529574d170b Reviewed-on: https://go-review.googlesource.com/c/tools/+/586336 Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent 5e43887 commit 7522327

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

gopls/internal/golang/rename_check.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,20 @@ func (r *renamer) checkInPackageBlock(from types.Object) {
167167
}
168168
}
169169

170-
// Check for conflicts between package block and all file blocks.
171-
for _, f := range r.pkg.Syntax() {
172-
fileScope := r.pkg.TypesInfo().Scopes[f]
173-
b, prev := fileScope.LookupParent(r.to, token.NoPos)
174-
if b == fileScope {
175-
r.errorf(from.Pos(), "renaming this %s %q to %q would conflict", objectKind(from), from.Name(), r.to)
176-
var prevPos token.Pos
177-
if prev != nil {
178-
prevPos = prev.Pos()
170+
// In the declaring package, check for conflicts between the
171+
// package block and all file blocks.
172+
if from.Pkg() == r.pkg.Types() {
173+
for _, f := range r.pkg.Syntax() {
174+
fileScope := r.pkg.TypesInfo().Scopes[f]
175+
if fileScope == nil {
176+
continue // type error? (golang/go#40835)
177+
}
178+
b, prev := fileScope.LookupParent(r.to, token.NoPos)
179+
if b == fileScope {
180+
r.errorf(from.Pos(), "renaming this %s %q to %q would conflict", objectKind(from), from.Name(), r.to)
181+
r.errorf(prev.Pos(), "\twith this %s", objectKind(prev))
182+
return // since checkInPackageBlock would report redundant errors
179183
}
180-
r.errorf(prevPos, "\twith this %s", objectKind(prev))
181-
return // since checkInPackageBlock would report redundant errors
182184
}
183185
}
184186

@@ -436,7 +438,6 @@ func (r *renamer) checkLabel(label *types.Label) {
436438
// checkStructField checks that the field renaming will not cause
437439
// conflicts at its declaration, or ambiguity or changes to any selection.
438440
func (r *renamer) checkStructField(from *types.Var) {
439-
440441
// If this is the declaring package, check that the struct
441442
// declaration is free of field conflicts, and field/method
442443
// conflicts.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
This test verifies spurious pkgname conflicts.
2+
Issue golang/go#67069.
3+
4+
-- go.mod --
5+
module example
6+
go 1.19
7+
8+
-- aa/a.go --
9+
package aa
10+
11+
var cc int //@rename("cc", "aa", CToA)
12+
const C = 0
13+
const D = 0
14+
15+
-- aa/a_test.go --
16+
package aa_test
17+
18+
import "example/aa"
19+
20+
var _ = aa.C //@rename("aa", "bb", AToB)
21+
-- @CToA/aa/a.go --
22+
@@ -3 +3 @@
23+
-var cc int //@rename("cc", "aa", CToA)
24+
+var aa int //@rename("cc", "aa", CToA)
25+
-- @AToB/aa/a_test.go --
26+
@@ -3 +3 @@
27+
-import "example/aa"
28+
+import bb "example/aa"
29+
@@ -5 +5 @@
30+
-var _ = aa.C //@rename("aa", "bb", AToB)
31+
+var _ = bb.C //@rename("aa", "bb", AToB)
32+
-- bb/b.go --
33+
package bb
34+
35+
import "example/aa"
36+
37+
var _ = aa.C
38+
var bb int //@renameerr("bb", "aa", errImportConflict)
39+
40+
-- @errImportConflict --
41+
bb/b.go:6:5: renaming this var "bb" to "aa" would conflict
42+
bb/b.go:3:8: with this imported package name
43+
-- aa/a_internal_test.go --
44+
package aa
45+
46+
var _ = D //@rename("D", "aa", DToA)
47+
-- @DToA/aa/a_internal_test.go --
48+
@@ -3 +3 @@
49+
-var _ = D //@rename("D", "aa", DToA)
50+
+var _ = aa //@rename("D", "aa", DToA)
51+
-- @DToA/aa/a.go --
52+
@@ -5 +5 @@
53+
-const D = 0
54+
+const aa = 0

0 commit comments

Comments
 (0)