-
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.
gopls/internal/lsp/source: fix renaming instantiated fields
Correctly associate instantiated functions and fields with their origin during renaming. Fixes golang/go#61640 Change-Id: I819ffe303a2b1c35810d5b3c2d71fa5f4231a0c4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/518897 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> gopls-CI: kokoro <[email protected]>
- Loading branch information
Showing
5 changed files
with
109 additions
and
18 deletions.
There are no files selected for viewing
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,26 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build !go1.19 | ||
// +build !go1.19 | ||
|
||
package source | ||
|
||
import "go/types" | ||
|
||
// containsOrigin reports whether the provided object set contains an object | ||
// with the same origin as the provided obj (which may be a synthetic object | ||
// created during instantiation). | ||
func containsOrigin(objSet map[types.Object]bool, obj types.Object) bool { | ||
if obj == nil { | ||
return objSet[obj] | ||
} | ||
// In Go 1.18, we can't use the types.Var.Origin and types.Func.Origin methods. | ||
for target := range objSet { | ||
if target.Pkg() == obj.Pkg() && target.Pos() == obj.Pos() && target.Name() == obj.Name() { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,33 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build go1.19 | ||
// +build go1.19 | ||
|
||
package source | ||
|
||
import "go/types" | ||
|
||
// containsOrigin reports whether the provided object set contains an object | ||
// with the same origin as the provided obj (which may be a synthetic object | ||
// created during instantiation). | ||
func containsOrigin(objSet map[types.Object]bool, obj types.Object) bool { | ||
objOrigin := origin(obj) | ||
for target := range objSet { | ||
if origin(target) == objOrigin { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func origin(obj types.Object) types.Object { | ||
switch obj := obj.(type) { | ||
case *types.Var: | ||
return obj.Origin() | ||
case *types.Func: | ||
return obj.Origin() | ||
} | ||
return obj | ||
} |
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
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
47 changes: 47 additions & 0 deletions
47
gopls/internal/regtest/marker/testdata/rename/issue61640.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,47 @@ | ||
This test verifies that gopls can rename instantiated fields. | ||
|
||
-- flags -- | ||
-min_go=go1.18 | ||
|
||
-- a.go -- | ||
package a | ||
|
||
// This file is adapted from the example in the issue. | ||
|
||
type builder[S ~[]int] struct { | ||
elements S //@rename("elements", elements2, OneToTwo) | ||
} | ||
|
||
type BuilderImpl[S ~[]int] struct{ builder[S] } | ||
|
||
func NewBuilderImpl[S ~[]int](name string) *BuilderImpl[S] { | ||
impl := &BuilderImpl[S]{ | ||
builder[S]{ | ||
elements: S{}, | ||
}, | ||
} | ||
|
||
_ = impl.elements | ||
return impl | ||
} | ||
-- @OneToTwo/a.go -- | ||
package a | ||
|
||
// This file is adapted from the example in the issue. | ||
|
||
type builder[S ~[]int] struct { | ||
elements2 S //@rename("elements", elements2, OneToTwo) | ||
} | ||
|
||
type BuilderImpl[S ~[]int] struct{ builder[S] } | ||
|
||
func NewBuilderImpl[S ~[]int](name string) *BuilderImpl[S] { | ||
impl := &BuilderImpl[S]{ | ||
builder[S]{ | ||
elements2: S{}, | ||
}, | ||
} | ||
|
||
_ = impl.elements2 | ||
return impl | ||
} |