Skip to content

Commit c208b91

Browse files
committed
go/types, types2: clarify Named, Alias, TypeName, Object
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]>
1 parent 165bf24 commit c208b91

File tree

8 files changed

+120
-20
lines changed

8 files changed

+120
-20
lines changed

src/cmd/compile/internal/types2/alias.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,36 @@ import (
1010
)
1111

1212
// An Alias represents an alias type.
13-
// Whether or not Alias types are created is controlled by the
14-
// gotypesalias setting with the GODEBUG environment variable.
15-
// For gotypesalias=1, alias declarations produce an Alias type.
16-
// Otherwise, the alias information is only in the type name,
17-
// which points directly to the actual (aliased) type.
13+
//
14+
// Alias types are created by alias declarations such as:
15+
//
16+
// type A = int
17+
//
18+
// The type on the right-hand side of the declaration can be accessed
19+
// using [Alias.Rhs]. This type may itself be an alias.
20+
// Call [Unalias] to obtain the first non-alias type in a chain of
21+
// alias type declarations.
22+
//
23+
// Like a defined ([Named]) type, an alias type has a name.
24+
// Use the [Alias.Obj] method to access its [TypeName] object.
25+
//
26+
// Historically, Alias types were not materialized so that, in the example
27+
// above, A's type was represented by a Basic (int), not an Alias
28+
// whose [Alias.Rhs] is int. But Go 1.24 allows you to declare an
29+
// alias type with type parameters or arguments:
30+
//
31+
// type Set[K comparable] = map[K]bool
32+
// s := make(Set[String])
33+
//
34+
// and this requires that Alias types be materialized. Use the
35+
// [Alias.TypeParams] and [Alias.TypeArgs] methods to access them.
36+
//
37+
// To ease the transition, the Alias type was introduced in go1.22,
38+
// but the type-checker would not construct values of this type unless
39+
// the GODEBUG=gotypesalias=1 environment variable was provided.
40+
// Starting in go1.23, this variable is enabled by default.
41+
// This setting also causes the predeclared type "any" to be
42+
// represented as an Alias, not a bare [Interface].
1843
type Alias struct {
1944
obj *TypeName // corresponding declared alias object
2045
orig *Alias // original, uninstantiated alias

src/cmd/compile/internal/types2/named.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ import (
9292
// in its "lineage".
9393

9494
// A Named represents a named (defined) type.
95+
//
96+
// A declaration such as:
97+
//
98+
// type S struct { ... }
99+
//
100+
// creates a defined type whose underlying type is a struct,
101+
// and binds this type to the object S, a [TypeName].
102+
// Use [Named.Underlying] to access the underlying type.
103+
// Use [Named.Obj] to obtain the object S.
104+
//
105+
// Before type aliases (Go 1.9), the spec called defined types "named types".
95106
type Named struct {
96107
check *Checker // non-nil during type-checking; nil otherwise
97108
obj *TypeName // corresponding declared object for declared types; see above for instantiated types

src/cmd/compile/internal/types2/object.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ import (
1414
"unicode/utf8"
1515
)
1616

17-
// An Object describes a named language entity such as a package,
18-
// constant, type, variable, function (incl. methods), or label.
19-
// All objects implement the Object interface.
17+
// An Object is a named language entity.
18+
// An Object may be a constant ([Const]), type name ([TypeName]),
19+
// variable or struct field ([Var]), function or method ([Func]),
20+
// imported package ([PkgName]), label ([Label]),
21+
// built-in function ([Builtin]),
22+
// or the predeclared identifier 'nil' ([Nil]).
23+
//
24+
// The environment, which is structured as a tree of Scopes,
25+
// maps each name to the unique Object that it denotes.
2026
type Object interface {
2127
Parent() *Scope // scope in which this object is declared; nil for methods and struct fields
2228
Pos() syntax.Pos // position of object identifier in declaration
@@ -27,6 +33,7 @@ type Object interface {
2733
Id() string // object name if exported, qualified name if not exported (see func Id)
2834

2935
// String returns a human-readable string of the object.
36+
// Use [ObjectString] to control how package names are formatted in the string.
3037
String() string
3138

3239
// order reflects a package-level object's source order: if object
@@ -257,7 +264,11 @@ func (obj *Const) Val() constant.Value { return obj.val }
257264

258265
func (*Const) isDependency() {} // a constant may be a dependency of an initialization expression
259266

260-
// A TypeName represents a name for a (defined or alias) type.
267+
// A TypeName is an [Object] that represents a type with a name:
268+
// a defined type ([Named]),
269+
// an alias type ([Alias]),
270+
// a type parameter ([TypeParam]),
271+
// or a predeclared type such as int or error.
261272
type TypeName struct {
262273
object
263274
}

src/cmd/compile/internal/types2/typeparam.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ var lastID atomic.Uint32
1515
// each call, starting with 1. It may be called concurrently.
1616
func nextID() uint64 { return uint64(lastID.Add(1)) }
1717

18-
// A TypeParam represents a type parameter type.
18+
// A TypeParam represents the type of a type parameter in a generic declaration.
19+
//
20+
// A TypeParam has a name; use the [TypeParam.Obj] method to access
21+
// its [TypeName] object.
1922
type TypeParam struct {
2023
check *Checker // for lazy type bound completion
2124
id uint64 // unique id, for debugging only

src/go/types/alias.go

Lines changed: 30 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/named.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/object.go

Lines changed: 15 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/typeparam.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)