-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/imports: don't add imports that shadow predeclared names
Also, a test. Fixes golang/go#63592 Change-Id: I36cab69cb224f90cc8459c3fced2408486193a1f Reviewed-on: https://go-review.googlesource.com/c/tools/+/557216 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com>
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
gopls/internal/test/marker/testdata/codeaction/import-shadows-builtin.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
This is a regression test for bug #63592 in "organize imports" whereby | ||
the new imports would shadow predeclared names. | ||
|
||
In the original example, the conflict was between predeclared error | ||
type and the unfortunately named package github.com/coreos/etcd/error, | ||
but this example uses a package with the ludicrous name of complex128. | ||
|
||
The new behavior is that we will not attempt to import packages | ||
that shadow predeclared names. (Ideally we would do that only if | ||
the predeclared name is actually referenced in the file, which | ||
complex128 happens to be in this example, but that's a trickier | ||
analysis than the internal/imports package is game for.) | ||
|
||
The name complex127 works as usual. | ||
|
||
-- go.mod -- | ||
module example.com | ||
go 1.18 | ||
|
||
-- complex128/a.go -- | ||
package complex128 | ||
|
||
var V int | ||
|
||
-- complex127/a.go -- | ||
package complex127 | ||
|
||
var V int | ||
|
||
-- main.go -- | ||
package main | ||
|
||
import () //@codeaction("import", "", "source.organizeImports", out) | ||
|
||
func main() { | ||
complex128.V() //@diag("V", re"type complex128 has no field") | ||
complex127.V() //@diag("complex127", re"(undeclared|undefined)") | ||
} | ||
|
||
func _() { | ||
var _ complex128 = 1 + 2i | ||
} | ||
-- @out/main.go -- | ||
package main | ||
|
||
import "example.com/complex127" //@codeaction("import", "", "source.organizeImports", out) | ||
|
||
func main() { | ||
complex128.V() //@diag("V", re"type complex128 has no field") | ||
complex127.V() //@diag("complex127", re"(undeclared|undefined)") | ||
} | ||
|
||
func _() { | ||
var _ complex128 = 1 + 2i | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters