]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: rename definedType to declaredType and clarify docs
authorMark Freeman <mark@golang.org>
Wed, 13 Aug 2025 18:55:50 +0000 (14:55 -0400)
committerGopher Robot <gobot@golang.org>
Thu, 13 Nov 2025 20:54:47 +0000 (12:54 -0800)
declaredType seems a better name for this function because it is reached
when processing a (Named or Alias) type declaration. In both cases, the
fromRHS field (rather than the underlying field) will be set.

Change-Id: Ibb1cc338e3b0632dc63f9cf2fd9d64cef6f1aaa5
Reviewed-on: https://go-review.googlesource.com/c/go/+/695955
Auto-Submit: Mark Freeman <markfreeman@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/decl.go
src/cmd/compile/internal/types2/typexpr.go
src/go/types/decl.go
src/go/types/typexpr.go

index 91d2492a532ebef35ffe9d36fe83e0a2f5f73a5e..8f196ece613bfd3ed770bd2073dc7d7819002a42 100644 (file)
@@ -532,7 +532,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *TypeN
                                check.collectTypeParams(&alias.tparams, tdecl.TParamList)
                        }
 
-                       rhs = check.definedType(tdecl.Type, obj)
+                       rhs = check.declaredType(tdecl.Type, obj)
                        assert(rhs != nil)
 
                        alias.fromRHS = rhs
@@ -576,7 +576,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *TypeN
                check.collectTypeParams(&named.tparams, tdecl.TParamList)
        }
 
-       rhs = check.definedType(tdecl.Type, obj)
+       rhs = check.declaredType(tdecl.Type, obj)
        assert(rhs != nil)
        named.fromRHS = rhs
 
index 8601ce627768e4fa0dfbcc9ede8c13c6f9079851..303f782ac4917cb194617d529bf1f83847402231 100644 (file)
@@ -16,7 +16,7 @@ import (
 
 // ident type-checks identifier e and initializes x with the value or type of e.
 // If an error occurred, x.mode is set to invalid.
-// For the meaning of def, see Checker.definedType, below.
+// For the meaning of def, see Checker.declaredType, below.
 // If wantType is set, the identifier e is expected to denote a type.
 func (check *Checker) ident(x *operand, e *syntax.Name, def *TypeName, wantType bool) {
        x.mode = invalid
@@ -149,14 +149,14 @@ func (check *Checker) ident(x *operand, e *syntax.Name, def *TypeName, wantType
 // typ type-checks the type expression e and returns its type, or Typ[Invalid].
 // The type must not be an (uninstantiated) generic type.
 func (check *Checker) typ(e syntax.Expr) Type {
-       return check.definedType(e, nil)
+       return check.declaredType(e, nil)
 }
 
 // varType type-checks the type expression e and returns its type, or Typ[Invalid].
 // The type must not be an (uninstantiated) generic type and it must not be a
 // constraint interface.
 func (check *Checker) varType(e syntax.Expr) Type {
-       typ := check.definedType(e, nil)
+       typ := check.declaredType(e, nil)
        check.validVarType(e, typ)
        return typ
 }
@@ -187,11 +187,11 @@ func (check *Checker) validVarType(e syntax.Expr, typ Type) {
        }).describef(e, "check var type %s", typ)
 }
 
-// definedType is like typ but also accepts a type name def.
-// If def != nil, e is the type specification for the type named def, declared
-// in a type declaration, and def.typ.underlying will be set to the type of e
-// before any components of e are type-checked.
-func (check *Checker) definedType(e syntax.Expr, def *TypeName) Type {
+// declaredType is like typ but also accepts a type name def.
+// If def != nil, e is the type specification for the [Alias] or [Named] type
+// named def, and def.typ.fromRHS will be set to the [Type] of e immediately
+// after its creation.
+func (check *Checker) declaredType(e syntax.Expr, def *TypeName) Type {
        typ := check.typInternal(e, def)
        assert(isTyped(typ))
        if isGeneric(typ) {
@@ -230,7 +230,7 @@ func goTypeName(typ Type) string {
 }
 
 // typInternal drives type checking of types.
-// Must only be called by definedType or genericType.
+// Must only be called by declaredType or genericType.
 func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
        if check.conf.Trace {
                check.trace(e0.Pos(), "-- type %s", e0)
@@ -296,7 +296,7 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
        case *syntax.ParenExpr:
                // Generic types must be instantiated before they can be used in any form.
                // Consequently, generic types cannot be parenthesized.
-               return check.definedType(e.X, def)
+               return check.declaredType(e.X, def)
 
        case *syntax.ArrayType:
                typ := new(Array)
index 2dab5cf7b94381b0bd4e7d81d9b5f6ddc5ba6dd3..c35edd8afa4a128fb73f47ec5cde8b71e42e6d8f 100644 (file)
@@ -607,7 +607,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *TypeName
                                check.collectTypeParams(&alias.tparams, tdecl.TypeParams)
                        }
 
-                       rhs = check.definedType(tdecl.Type, obj)
+                       rhs = check.declaredType(tdecl.Type, obj)
                        assert(rhs != nil)
 
                        alias.fromRHS = rhs
@@ -658,7 +658,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *TypeName
                check.collectTypeParams(&named.tparams, tdecl.TypeParams)
        }
 
-       rhs = check.definedType(tdecl.Type, obj)
+       rhs = check.declaredType(tdecl.Type, obj)
        assert(rhs != nil)
        named.fromRHS = rhs
 
index 88ec4b77fca7e9c3fb8ddfbf42af5972dd30295d..b44fe4d768655cb646579db7756b7c719d236a33 100644 (file)
@@ -16,7 +16,7 @@ import (
 
 // ident type-checks identifier e and initializes x with the value or type of e.
 // If an error occurred, x.mode is set to invalid.
-// For the meaning of def, see Checker.definedType, below.
+// For the meaning of def, see Checker.declaredType, below.
 // If wantType is set, the identifier e is expected to denote a type.
 func (check *Checker) ident(x *operand, e *ast.Ident, def *TypeName, wantType bool) {
        x.mode = invalid
@@ -148,14 +148,14 @@ func (check *Checker) ident(x *operand, e *ast.Ident, def *TypeName, wantType bo
 // typ type-checks the type expression e and returns its type, or Typ[Invalid].
 // The type must not be an (uninstantiated) generic type.
 func (check *Checker) typ(e ast.Expr) Type {
-       return check.definedType(e, nil)
+       return check.declaredType(e, nil)
 }
 
 // varType type-checks the type expression e and returns its type, or Typ[Invalid].
 // The type must not be an (uninstantiated) generic type and it must not be a
 // constraint interface.
 func (check *Checker) varType(e ast.Expr) Type {
-       typ := check.definedType(e, nil)
+       typ := check.declaredType(e, nil)
        check.validVarType(e, typ)
        return typ
 }
@@ -185,11 +185,11 @@ func (check *Checker) validVarType(e ast.Expr, typ Type) {
        }).describef(e, "check var type %s", typ)
 }
 
-// definedType is like typ but also accepts a type name def.
-// If def != nil, e is the type specification for the type named def, declared
-// in a type declaration, and def.typ.underlying will be set to the type of e
-// before any components of e are type-checked.
-func (check *Checker) definedType(e ast.Expr, def *TypeName) Type {
+// declaredType is like typ but also accepts a type name def.
+// If def != nil, e is the type specification for the [Alias] or [Named] type
+// named def, and def.typ.fromRHS will be set to the [Type] of e immediately
+// after its creation.
+func (check *Checker) declaredType(e ast.Expr, def *TypeName) Type {
        typ := check.typInternal(e, def)
        assert(isTyped(typ))
        if isGeneric(typ) {
@@ -228,7 +228,7 @@ func goTypeName(typ Type) string {
 }
 
 // typInternal drives type checking of types.
-// Must only be called by definedType or genericType.
+// Must only be called by declaredType or genericType.
 func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
        if check.conf._Trace {
                check.trace(e0.Pos(), "-- type %s", e0)
@@ -295,7 +295,7 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
        case *ast.ParenExpr:
                // Generic types must be instantiated before they can be used in any form.
                // Consequently, generic types cannot be parenthesized.
-               return check.definedType(e.X, def)
+               return check.declaredType(e.X, def)
 
        case *ast.ArrayType:
                if e.Len == nil {