"errors"
"fmt"
"go/constant"
+ "internal/godebug"
"internal/goversion"
. "internal/types/errors"
)
// 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
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
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)
}
// 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) {
// (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),
}
}
}
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) {
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
}
}
}
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}
m[node] = scope
}
}
+
+func (check *Checker) recordFileVersion(fbase *syntax.PosBase, version string) {
+ if m := check.FileVersions; m != nil {
+ m[fbase] = version
+ }
+}