Skip to content

reflect: Type.Name and Type.String expose "link name" mangling of type arguments #55924

Open
@mdempsky

Description

@mdempsky

The type parameters proposal (https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#reflection) states:

We do not propose to change the reflect package in any way. When a type or function is instantiated, all of the type parameters will become ordinary non-generic types. The String method of a reflect.Type value of an instantiated type will return the name with the type arguments in square brackets. For example, List[int].

It's ambiguous what "name" should be used. Also, whether only reflect.Type.String should have been affected, or reflect.Type.Name too.

cmd/compile generates two names for each type: a "link name" that corresponds to type identity (so the linker can deduplicate it against other identical types), and a "reflect name" that's simplified somewhat for user presentation via reflect APIs.

However, for instantiated types' reflect names, it currently uses the link names of the type arguments. For example: https://go.dev/play/p/JAP28idHc9z

Can/should we change this behavior?

Some noteworthy distinctions:

  • Reflect names use package name, whereas link names use package path. (reflect.Type.String says either is allowed though.)
  • Link names add "·N" suffixes to locally defined types to disambiguate them, whereas reflect names omit this.
  • Link names include package-path-qualifiers for non-exported struct fields and interface methods.
  • Link names include a = byte to identify embedded fields.

/cc @rsc @ianlancetaylor @griesemer @golang/compiler

Metadata

Metadata

Assignees

Labels

NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Status

In Progress

Relationships

None yet

Development

No branches or pull requests

Issue actions