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

go/types: add Info.PkgName method #62037

Closed
adonovan opened this issue Aug 15, 2023 · 13 comments
Closed

go/types: add Info.PkgName method #62037

adonovan opened this issue Aug 15, 2023 · 13 comments

Comments

@adonovan
Copy link
Member

adonovan commented Aug 15, 2023

Any time a client of go/types needs to find the types.PkgName (or just the name) defined by an import, they must write logic equivalent to this:

// PkgName returns the local package name defined by the specified import.
func (info *types.Info) PkgName(imp *ast.ImportSpec) (*types.PkgName, bool) {
	var obj types.Object
	if imp.Name != nil {
		obj = info.Defs[imp.Name]
	} else {
		obj = info.Implicits[imp]
	}
	pkgname, ok := obj.(*types.PkgName)
	return pkgname, ok
}

I propose that we add this method to types.Info.

@gopherbot gopherbot added this to the Proposal milestone Aug 15, 2023
@adonovan adonovan moved this to Incoming in Proposals Aug 15, 2023
@findleyr
Copy link
Contributor

findleyr commented Aug 15, 2023

Makes sense to me, in general, though I think we should discuss the name/signature.

The proposed API is fine, but perhaps inconsistent with the existing methods. Currently, types.Info has two methods: ObjectOf and TypeOf, both of which accept an argument from the go/ast domain and return a possibly nil go/types result. Therefore, would a more consistent API be something like this?

// ImportOf returns the local package name defined by the specified import, or nil if not found.
func (info *types.Info) ImportOf(imp *ast.ImportSpec) *types.PkgName

@adonovan
Copy link
Member Author

Fair point about nil (vs bool). How about a name of PkgNameOf?

@findleyr
Copy link
Contributor

findleyr commented Aug 15, 2023

PkgNameOf SGTM. Indeed, it is the most natural name since ObjectOf returns an Object, and TypeOf returns a Type.

@adonovan
Copy link
Member Author

I found at least 5 copies of this function in x/tools:

cmd/guru/describe.go
623-		if n.Name != nil {
624-			obj = qpos.info.Defs[n.Name]
625-		} else {
626:			obj = qpos.info.Implicits[n]
627-		}
628-		pkgname, _ := obj.(*types.PkgName)
629-		if pkgname == nil {

go/analysis/passes/pkgfact/pkgfact.go
120-}
121-
122-func imported(info *types.Info, spec *ast.ImportSpec) *types.Package {
123:	obj, ok := info.Implicits[spec]
124-	if !ok {
125-		obj = info.Defs[spec.Name] // renaming import
126-	}

go/analysis/passes/cgocall/cgocall.go
371-
372-// TODO(adonovan): make this a library function or method of Info.
373-func imported(info *types.Info, spec *ast.ImportSpec) *types.Package {
374:	obj, ok := info.Implicits[spec]
375-	if !ok {
376-		obj = info.Defs[spec.Name] // renaming import
377-	}

gopls/internal/lsp/source/highlight.go
473-	if imp.Name != nil {
474-		obj = info.Defs[imp.Name]
475-	} else {
476:		obj = info.Implicits[imp]
477-	}
478-	pkgname, ok := obj.(*types.PkgName)
479-	return pkgname, ok

internal/refactor/inline/inline.go
2088-	if imp.Name != nil {
2089-		obj = info.Defs[imp.Name]
2090-	} else {
2091:		obj = info.Implicits[imp]
2092-	}
2093-	pkgname, ok := obj.(*types.PkgName)
2094-	return pkgname, ok

@rsc rsc moved this from Incoming to Active in Proposals Oct 27, 2023
@rsc
Copy link
Contributor

rsc commented Oct 27, 2023

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@adonovan adonovan changed the title proposal: go/types: add Info.ImportedPkgName method proposal: go/types: add Info.PkgName method Nov 1, 2023
@rsc
Copy link
Contributor

rsc commented Nov 2, 2023

Have all remaining concerns about this proposal been addressed?

In go/types, add a new method:

// PkgName returns the local package name defined by the import.
func (info *types.Info) PkgName(imp *ast.ImportSpec)

@griesemer
Copy link
Contributor

For consistency in naming with the existing functions, and per the comment above, this should probably be:

// PkgNameOf returns the local package name defined by the import,
// or nil if it is not found.
//
// Precondition: the Defs and Implicts maps are populated.
func (info *types.Info) PkgNameOf(imp *ast.ImportSpec) *PkgName

@rsc
Copy link
Contributor

rsc commented Nov 10, 2023

Based on the discussion above, this proposal seems like a likely accept.
— rsc for the proposal review group

In go/types, add a new method:

// PkgNameOf returns the local package name defined by the import.
func (info *types.Info) PkgNameOf(imp *ast.ImportSpec)

@rsc rsc moved this from Active to Likely Accept in Proposals Nov 10, 2023
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/541575 mentions this issue: go/types, types2: implement Info.PkgNameOf

gopherbot pushed a commit that referenced this issue Nov 11, 2023
For #62037.

Change-Id: I354f6417232708278d3f2b2d5ea41ff48e08d6b6
Reviewed-on: https://go-review.googlesource.com/c/go/+/541575
Reviewed-by: Alan Donovan <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
Auto-Submit: Robert Griesemer <[email protected]>
Run-TryBot: Robert Griesemer <[email protected]>
aimuz added a commit to aimuz/go that referenced this issue Nov 12, 2023
The decl.go file in cmd/compile/internal/noder has been removed as its
only contained function, pkgNameOf, is now redundant. This change updates
the references of this function to use the method PkgNameOf directly from
the *types2.Info struct.

For golang#62037
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/541738 mentions this issue: cmd/compile: remove redundant function pkgNameOf

gopherbot pushed a commit that referenced this issue Nov 13, 2023
Replace calls to pkgNameOf with calls to types2.Info.PkgNameOf.
Delete function pkgNameOf and file decl.go which are not needed anymore.

For #62037

Change-Id: Ib8a0411cc9eb9fdd42ee6e73c23deed2daaf73d5
GitHub-Last-Rev: 3c8928f
GitHub-Pull-Request: #64075
Reviewed-on: https://go-review.googlesource.com/c/go/+/541738
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Keith Randall <[email protected]>
Run-TryBot: Robert Griesemer <[email protected]>
Run-TryBot: qiulaidongfeng <[email protected]>
Auto-Submit: Robert Griesemer <[email protected]>
Reviewed-by: Keith Randall <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
@rsc rsc moved this from Likely Accept to Accepted in Proposals Nov 16, 2023
@rsc
Copy link
Contributor

rsc commented Nov 16, 2023

No change in consensus, so accepted. 🎉
This issue now tracks the work of implementing the proposal.
— rsc for the proposal review group

In go/types, add a new method:

// PkgNameOf returns the local package name defined by the import.
func (info *types.Info) PkgNameOf(imp *ast.ImportSpec)

@rsc rsc changed the title proposal: go/types: add Info.PkgName method go/types: add Info.PkgName method Nov 16, 2023
@rsc rsc modified the milestones: Proposal, Backlog Nov 16, 2023
@griesemer
Copy link
Contributor

This has already been implemented and was submitted. Closing.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/546975 mentions this issue: doc: add release note for go/types/PkgNameOf

gopherbot pushed a commit that referenced this issue Dec 5, 2023
For #62037.

Change-Id: Id1d02f88205e5ea62662e78c8313731ec9e55b1e
Reviewed-on: https://go-review.googlesource.com/c/go/+/546975
Reviewed-by: Alan Donovan <[email protected]>
TryBot-Bypass: Robert Griesemer <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
Auto-Submit: Robert Griesemer <[email protected]>
ezz-no pushed a commit to ezz-no/go-ezzno that referenced this issue Feb 18, 2024
For golang#62037.

Change-Id: Id1d02f88205e5ea62662e78c8313731ec9e55b1e
Reviewed-on: https://go-review.googlesource.com/c/go/+/546975
Reviewed-by: Alan Donovan <[email protected]>
TryBot-Bypass: Robert Griesemer <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
Auto-Submit: Robert Griesemer <[email protected]>
@dmitshur dmitshur modified the milestones: Backlog, Go1.22 May 9, 2024
@aclements aclements removed this from Proposals Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants