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 <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
This commit is contained in:
toad 2024-05-17 14:56:35 +08:00 коммит произвёл Gopher Robot
Родитель 5e43887ecb
Коммит 7522327277
2 изменённых файлов: 67 добавлений и 12 удалений

Просмотреть файл

@ -167,18 +167,20 @@ func (r *renamer) checkInPackageBlock(from types.Object) {
}
}
// Check for conflicts between package block and all file blocks.
for _, f := range r.pkg.Syntax() {
fileScope := r.pkg.TypesInfo().Scopes[f]
b, prev := fileScope.LookupParent(r.to, token.NoPos)
if b == fileScope {
r.errorf(from.Pos(), "renaming this %s %q to %q would conflict", objectKind(from), from.Name(), r.to)
var prevPos token.Pos
if prev != nil {
prevPos = prev.Pos()
// In the declaring package, check for conflicts between the
// package block and all file blocks.
if from.Pkg() == r.pkg.Types() {
for _, f := range r.pkg.Syntax() {
fileScope := r.pkg.TypesInfo().Scopes[f]
if fileScope == nil {
continue // type error? (golang/go#40835)
}
b, prev := fileScope.LookupParent(r.to, token.NoPos)
if b == fileScope {
r.errorf(from.Pos(), "renaming this %s %q to %q would conflict", objectKind(from), from.Name(), r.to)
r.errorf(prev.Pos(), "\twith this %s", objectKind(prev))
return // since checkInPackageBlock would report redundant errors
}
r.errorf(prevPos, "\twith this %s", objectKind(prev))
return // since checkInPackageBlock would report redundant errors
}
}
@ -436,7 +438,6 @@ func (r *renamer) checkLabel(label *types.Label) {
// checkStructField checks that the field renaming will not cause
// conflicts at its declaration, or ambiguity or changes to any selection.
func (r *renamer) checkStructField(from *types.Var) {
// If this is the declaring package, check that the struct
// declaration is free of field conflicts, and field/method
// conflicts.

54
gopls/internal/test/marker/testdata/rename/issue67069.txt поставляемый Normal file
Просмотреть файл

@ -0,0 +1,54 @@
This test verifies spurious pkgname conflicts.
Issue golang/go#67069.
-- go.mod --
module example
go 1.19
-- aa/a.go --
package aa
var cc int //@rename("cc", "aa", CToA)
const C = 0
const D = 0
-- aa/a_test.go --
package aa_test
import "example/aa"
var _ = aa.C //@rename("aa", "bb", AToB)
-- @CToA/aa/a.go --
@@ -3 +3 @@
-var cc int //@rename("cc", "aa", CToA)
+var aa int //@rename("cc", "aa", CToA)
-- @AToB/aa/a_test.go --
@@ -3 +3 @@
-import "example/aa"
+import bb "example/aa"
@@ -5 +5 @@
-var _ = aa.C //@rename("aa", "bb", AToB)
+var _ = bb.C //@rename("aa", "bb", AToB)
-- bb/b.go --
package bb
import "example/aa"
var _ = aa.C
var bb int //@renameerr("bb", "aa", errImportConflict)
-- @errImportConflict --
bb/b.go:6:5: renaming this var "bb" to "aa" would conflict
bb/b.go:3:8: with this imported package name
-- aa/a_internal_test.go --
package aa
var _ = D //@rename("D", "aa", DToA)
-- @DToA/aa/a_internal_test.go --
@@ -3 +3 @@
-var _ = D //@rename("D", "aa", DToA)
+var _ = aa //@rename("D", "aa", DToA)
-- @DToA/aa/a.go --
@@ -5 +5 @@
-const D = 0
+const aa = 0