-
Notifications
You must be signed in to change notification settings - Fork 18.1k
proposal: go/types: add HasTypeName interface #66890
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
Comments
Change https://go.dev/cl/604476 mentions this issue: |
Updates #65855 Updates #66890 Change-Id: I167c9de818049cae02f0d99f8e0fb4017e07bea9 Reviewed-on: https://go-review.googlesource.com/c/go/+/604476 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
I note that we have an unexported function // hasName reports whether t has a name. This includes
// predeclared types, defined types, and type parameters.
// hasName may be called with types that are not fully set up.
func hasName(t Type) bool {
switch Unalias(t).(type) {
case *Basic, *Named, *TypeParam:
return true
}
return false
} Per the spec: "Predeclared types, defined types, and type parameters are called named types." Maybe the interface should just be called "HasName". Having the |
In the branch where I was playing with this I ended up using the name TypeWithName to make clear that the interface is a restriction of Type; otherwise it's a bit unclear what kind of thing it is. |
My 2 cents: |
In the spec, a named type has a very specific meaning; specifically, an alias type may not be a named type. This interface is about whether a type has an |
This proposal has been added to the active column of the proposals project |
@griesemer notes that this is almost but not quite a "named type" from the spec (an alias is only a named type if it aliases a named type). We'll need to rewrite the comment to be careful about that. The fact that it doesn't correspond exactly to the spec seems to be okay. Given that, |
Based on the discussion above, this proposal seems like a likely accept. The proposal is to add the following to package // HasTypeName abstracts the four kinds of types that have declared names:
// basic types ([*Basic]), defined types ([*Named]), aliases ([*Alias]), and
// type parameters ([*TypeParam]),
//
// Note that the Go spec considers all of these "named types", with the exception
// of alias types, which are only "named types" if their target is a named type.
type HasTypeName interface {
Obj() *TypeName
} |
Unfortunately I just discovered that #71886 is infeasible, so we need to revert the comment to the original wording to exclude |
Maybe this should be a function instead? func TypeNameFor(Type) *TypeName |
This proposal has been added to the active column of the proposals project |
Yeah, that would work. I looked at x/tools and 5 out of 7 of the places where we define this interface would be satisfied with this helper. |
Change https://go.dev/cl/672975 mentions this issue: |
I added // TypeNameFor returns the type name symbol for the specified type, if
// it is a [*types.Alias], [*types.Named], [*types.TypeParam], or a
// [*types.Basic] representing a type.
//
// For all other types, and for Basic types representing a builtin,
// constant, or nil, it returns nil. Be careful not to convert the
// resulting nil pointer to a [types.Object]!
//
// If t is the type of a constant, it may be an "untyped" type, which
// has no TypeName. To access the name of such types (e.g. "untyped
// int"), use [types.Basic.Name].
func TypeNameFor(t types.Type) *types.TypeName {
switch t := t.(type) {
case *types.Alias:
return t.Obj()
case *types.Named:
return t.Obj()
case *types.TypeParam:
return t.Obj()
case *types.Basic:
// See issues #71886 and #66890 for some history.
if tname, ok := types.Universe.Lookup(t.Name()).(*types.TypeName); ok {
return tname
}
}
return nil
} |
This function eliminates most of the places where we had reinvented this wheel: type HasTypeName interface { Obj() *TypeName } Updates golang/go#66890 Updates golang/go#71886 Change-Id: I70d3c8141b20efb1ba0e02fc846d8b38b8f3a23c Reviewed-on: https://go-review.googlesource.com/c/tools/+/672975 Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Commit-Queue: Alan Donovan <[email protected]>
This proposal has been declined as retracted. |
During the types.Alias work, there was a recurring need (7 times in x/tools; @findleyr is adding an eighth) for this interface:
Of course users can easily define it for themselves, but adding it to
go/types
provides a good place to hang additional documentation.Related:
@findleyr @griesemer
The text was updated successfully, but these errors were encountered: