]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/go/types/check.go
go/types, types2: implement Alias proposal (export API)
[gostls13.git] / src / go / types / check.go
index f3ad85dff913d6510a9a773ff32c4e2d056d2478..0feea6dfeba678815a8559497e643152439a79a1 100644 (file)
@@ -12,6 +12,7 @@ import (
        "go/ast"
        "go/constant"
        "go/token"
+       "internal/godebug"
        "internal/goversion"
        . "internal/types/errors"
 )
@@ -22,6 +23,9 @@ var nopos token.Pos
 // debugging/development support
 const debug = false // leave on during development
 
+// gotypesalias controls the use of Alias types
+var gotypesalias = godebug.New("gotypesalias")
+
 // exprInfo stores information about an untyped expression.
 type exprInfo struct {
        isLhs bool // expression is lhs operand of a shift with delayed type-check
@@ -90,21 +94,27 @@ type actionDesc struct {
 }
 
 // A Checker maintains the state of the type checker.
-// It must be created with NewChecker.
+// It must be created with [NewChecker].
 type Checker struct {
        // package information
        // (initialized by NewChecker, valid for the life-time of checker)
+
+       // If EnableAlias is set, alias declarations produce an Alias type.
+       // Otherwise the alias information is only in the type name, which
+       // points directly to the actual (aliased) type.
+       enableAlias bool
+
        conf *Config
        ctxt *Context // context for de-duplicating instances
        fset *token.FileSet
        pkg  *Package
        *Info
-       version version                 // accepted language version
-       posVers map[*token.File]version // maps files to versions (may be nil)
-       nextID  uint64                  // unique Id for type parameters (first valid Id is 1)
-       objMap  map[Object]*declInfo    // maps package-level objects and (non-interface) methods to declaration info
-       impMap  map[importKey]*Package  // maps (import path, source directory) to (complete or fake) package
-       valids  instanceLookup          // valid *Named (incl. instantiated) types per the validType check
+       version version                // accepted language version
+       posVers map[token.Pos]version  // maps file start positions to versions (may be nil)
+       nextID  uint64                 // unique Id for type parameters (first valid Id is 1)
+       objMap  map[Object]*declInfo   // maps package-level objects and (non-interface) methods to declaration info
+       impMap  map[importKey]*Package // maps (import path, source directory) to (complete or fake) package
+       valids  instanceLookup         // valid *Named (incl. instantiated) types per the validType check
 
        // pkgPathMap maps package names to the set of distinct import paths we've
        // seen for that name, anywhere in the import graph. It is used for
@@ -154,9 +164,14 @@ func (check *Checker) addDeclDep(to Object) {
        from.addDep(to)
 }
 
+// Note: The following three alias-related functions are only used
+//       when Alias types are not enabled.
+
 // brokenAlias records that alias doesn't have a determined type yet.
 // It also sets alias.typ to Typ[Invalid].
+// Not used if check.enableAlias is set.
 func (check *Checker) brokenAlias(alias *TypeName) {
+       assert(!check.enableAlias)
        if check.brokenAliases == nil {
                check.brokenAliases = make(map[*TypeName]bool)
        }
@@ -166,13 +181,15 @@ func (check *Checker) brokenAlias(alias *TypeName) {
 
 // validAlias records that alias has the valid type typ (possibly Typ[Invalid]).
 func (check *Checker) validAlias(alias *TypeName, typ Type) {
+       assert(!check.enableAlias)
        delete(check.brokenAliases, alias)
        alias.typ = typ
 }
 
 // isBrokenAlias reports whether alias doesn't have a determined type yet.
 func (check *Checker) isBrokenAlias(alias *TypeName) bool {
-       return alias.typ == Typ[Invalid] && check.brokenAliases[alias]
+       assert(!check.enableAlias)
+       return check.brokenAliases[alias]
 }
 
 func (check *Checker) rememberUntyped(e ast.Expr, lhs bool, mode operandMode, typ *Basic, val constant.Value) {
@@ -221,8 +238,8 @@ func (check *Checker) needsCleanup(c cleaner) {
        check.cleaners = append(check.cleaners, c)
 }
 
-// NewChecker returns a new Checker instance for a given package.
-// Package files may be added incrementally via checker.Files.
+// NewChecker returns a new [Checker] instance for a given package.
+// [Package] files may be added incrementally via checker.Files.
 func NewChecker(conf *Config, fset *token.FileSet, pkg *Package, info *Info) *Checker {
        // make sure we have a configuration
        if conf == nil {
@@ -241,13 +258,14 @@ func NewChecker(conf *Config, fset *token.FileSet, pkg *Package, info *Info) *Ch
        // (previously, pkg.goVersion was mutated here: go.dev/issue/61212)
 
        return &Checker{
-               conf:   conf,
-               ctxt:   conf.Context,
-               fset:   fset,
-               pkg:    pkg,
-               Info:   info,
-               objMap: make(map[Object]*declInfo),
-               impMap: make(map[importKey]*Package),
+               enableAlias: gotypesalias.Value() == "1",
+               conf:        conf,
+               ctxt:        conf.Context,
+               fset:        fset,
+               pkg:         pkg,
+               Info:        info,
+               objMap:      make(map[Object]*declInfo),
+               impMap:      make(map[importKey]*Package),
        }
 }
 
@@ -287,11 +305,10 @@ func (check *Checker) initFiles(files []*ast.File) {
                }
        }
 
+       // collect file versions
        for _, file := range check.files {
-               tfile := check.fset.File(file.FileStart)
-               check.recordFileVersion(tfile, check.version) // record package version (possibly zero version)
-               v, _ := parseGoVersion(file.GoVersion)
-               if v.major > 0 {
+               check.recordFileVersion(file, check.conf.GoVersion) // record package version (possibly zero version)
+               if v, _ := parseGoVersion(file.GoVersion); v.major > 0 {
                        if v.equal(check.version) {
                                continue
                        }
@@ -312,20 +329,14 @@ func (check *Checker) initFiles(files []*ast.File) {
                                continue
                        }
                        if check.posVers == nil {
-                               check.posVers = make(map[*token.File]version)
+                               check.posVers = make(map[token.Pos]version)
                        }
-                       check.posVers[tfile] = v
-                       check.recordFileVersion(tfile, v) // overwrite package version
+                       check.posVers[file.FileStart] = v
+                       check.recordFileVersion(file, file.GoVersion) // overwrite package version
                }
        }
 }
 
-// A posVers records that the file starting at pos declares the Go version vers.
-type posVers struct {
-       pos  token.Pos
-       vers version
-}
-
 // A bailout panic is used for early termination.
 type bailout struct{}
 
@@ -512,7 +523,7 @@ func (check *Checker) recordTypeAndValue(x ast.Expr, mode operandMode, typ Type,
                assert(val != nil)
                // We check allBasic(typ, IsConstType) here as constant expressions may be
                // recorded as type parameters.
-               assert(typ == Typ[Invalid] || allBasic(typ, IsConstType))
+               assert(!isValid(typ) || allBasic(typ, IsConstType))
        }
        if m := check.Types; m != nil {
                m[x] = TypeAndValue{mode, typ, val}
@@ -640,8 +651,8 @@ func (check *Checker) recordScope(node ast.Node, scope *Scope) {
        }
 }
 
-func (check *Checker) recordFileVersion(tfile *token.File, v version) {
-       if m := check._FileVersions; m != nil {
-               m[tfile] = _Version{v.major, v.minor}
+func (check *Checker) recordFileVersion(file *ast.File, version string) {
+       if m := check.FileVersions; m != nil {
+               m[file] = version
        }
 }