Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/internal/refactor/inline: missing shadowing check when forming qualified identifiers #62667

Closed
adonovan opened this issue Sep 15, 2023 · 1 comment
Assignees
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@adonovan
Copy link
Member

The inliner fails to detect that the reference to Split in the path.Dir function, which is turned into path.Split in the caller, is shadowed by the parameter name path:

xtools$ go run ./gopls fix -a -d /Users/adonovan/w/xtools/go/buildutil/util.go:#2005 refactor.inline
2023/09/15 17:15:27 inline path.Dir(filepath.ToSlash(filename)) @ /Users/adonovan/w/xtools/go/buildutil/util.go:66:22
2023/09/15 17:15:27 - replace id "Split" @ #42 to "path.Split"
2023/09/15 17:15:27 - replace id "Clean" @ #62 to "path.Clean"
2023/09/15 17:15:27 keeping param "path": argument filepath.ToSlash(filename) is impure
2023/09/15 17:15:27 strategy: literalization
--- /Users/adonovan/w/xtools/go/buildutil/util.go.orig
+++ /Users/adonovan/w/xtools/go/buildutil/util.go
@@ -63,7 +63,10 @@
 	// paths will not use `\` unless the PathSeparator
 	// is also `\`, thus we can rely on filepath.ToSlash for some sanity.
 
-	dirSlash := path.Dir(filepath.ToSlash(filename)) + "/"
+	dirSlash := func(path string) string {
+		dir, _ := path.Split(path)
+		return path.Clean(dir)
+	}(filepath.ToSlash(filename)) + "/"
 
 	// We assume that no source root (GOPATH[i] or GOROOT) contains any other.
 	for _, srcdir := range ctxt.SrcDirs() {
xtools$ 

The fix is to tabulate for each free name all the shadowing definitions (like we do for parameters) and check whether the introduced package name is in that set.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Sep 15, 2023
@gopherbot gopherbot added this to the Unreleased milestone Sep 15, 2023
@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 25, 2023
@adonovan adonovan self-assigned this Sep 26, 2023
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/531455 mentions this issue: internal/refactor/inline: fix pkgname shadowing bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants