]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmd/compile/internal/types2/check.go
cmd/compile/internal/types2: mark gotypesalias as undocumented
[gostls13.git] / src / cmd / compile / internal / types2 / check.go
index 5412e876bd9b5ded4feb428a88cc822d985c3668..381ccd8dcfae81cafba5863181925576596a7f72 100644 (file)
@@ -11,6 +11,7 @@ import (
        "errors"
        "fmt"
        "go/constant"
+       "internal/godebug"
        "internal/goversion"
        . "internal/types/errors"
 )
@@ -21,6 +22,9 @@ var nopos syntax.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
@@ -93,6 +97,12 @@ type actionDesc struct {
 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
        pkg  *Package
@@ -152,9 +162,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)
        }
@@ -164,13 +179,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 syntax.Expr, lhs bool, mode operandMode, typ *Basic, val constant.Value) {
@@ -239,12 +256,13 @@ func NewChecker(conf *Config, pkg *Package, info *Info) *Checker {
        // (previously, pkg.goVersion was mutated here: go.dev/issue/61212)
 
        return &Checker{
-               conf:   conf,
-               ctxt:   conf.Context,
-               pkg:    pkg,
-               Info:   info,
-               objMap: make(map[Object]*declInfo),
-               impMap: make(map[importKey]*Package),
+               enableAlias: gotypesalias.Value() == "1",
+               conf:        conf,
+               ctxt:        conf.Context,
+               pkg:         pkg,
+               Info:        info,
+               objMap:      make(map[Object]*declInfo),
+               impMap:      make(map[importKey]*Package),
        }
 }
 
@@ -285,6 +303,8 @@ func (check *Checker) initFiles(files []*syntax.File) {
        }
 
        for _, file := range check.files {
+               fbase := base(file.Pos())                            // fbase may be nil for tests
+               check.recordFileVersion(fbase, check.conf.GoVersion) // record package version (possibly zero version)
                v, _ := parseGoVersion(file.GoVersion)
                if v.major > 0 {
                        if v.equal(check.version) {
@@ -309,7 +329,8 @@ func (check *Checker) initFiles(files []*syntax.File) {
                        if check.posVers == nil {
                                check.posVers = make(map[*syntax.PosBase]version)
                        }
-                       check.posVers[base(file.Pos())] = v
+                       check.posVers[fbase] = v
+                       check.recordFileVersion(fbase, file.GoVersion) // overwrite package version
                }
        }
 }
@@ -500,7 +521,7 @@ func (check *Checker) recordTypeAndValue(x syntax.Expr, mode operandMode, typ Ty
                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}
@@ -673,3 +694,9 @@ func (check *Checker) recordScope(node syntax.Node, scope *Scope) {
                m[node] = scope
        }
 }
+
+func (check *Checker) recordFileVersion(fbase *syntax.PosBase, version string) {
+       if m := check.FileVersions; m != nil {
+               m[fbase] = version
+       }
+}