"go/ast"
"go/constant"
"go/internal/typeparams"
+ . "internal/types/errors"
"strings"
)
// If an error occurred, x.mode is set to invalid.
// For the meaning of def, see Checker.definedType, below.
// If wantType is set, the identifier e is expected to denote a type.
-func (check *Checker) ident(x *operand, e *ast.Ident, def *Named, wantType bool) {
+func (check *Checker) ident(x *operand, e *ast.Ident, def *TypeName, wantType bool) {
x.mode = invalid
x.expr = e
x.mode = typexpr
x.typ = tpar
} else {
- check.error(e, _InvalidBlank, "cannot use _ as value or type")
+ check.error(e, InvalidBlank, "cannot use _ as value or type")
}
} else {
- check.errorf(e, _UndeclaredName, "undefined: %s", e.Name)
+ check.errorf(e, UndeclaredName, "undefined: %s", e.Name)
}
return
case universeAny, universeComparable:
- if !check.allowVersion(check.pkg, 1, 18) {
- check.versionErrorf(e, "go1.18", "predeclared %s", e.Name)
+ if !check.verifyVersionf(e, go1_18, "predeclared %s", e.Name) {
return // avoid follow-on errors
}
}
// a cycle which needs to be reported). Otherwise we can skip the
// call and avoid a possible cycle error in favor of the more
// informative "not a type/value" error that this function's caller
- // will issue (see issue #25790).
+ // will issue (see go.dev/issue/25790).
typ := obj.Type()
if _, gotType := obj.(*TypeName); typ == nil || gotType && wantType {
check.objDecl(obj, def)
switch obj := obj.(type) {
case *PkgName:
- check.errorf(e, _InvalidPkgUse, "use of package %s not in selector", obj.name)
+ check.errorf(e, InvalidPkgUse, "use of package %s not in selector", obj.name)
return
case *Const:
check.addDeclDep(obj)
- if typ == Typ[Invalid] {
+ if !isValid(typ) {
return
}
if obj == universeIota {
if check.iota == nil {
- check.errorf(e, _InvalidIota, "cannot use iota outside constant declaration")
+ check.error(e, InvalidIota, "cannot use iota outside constant declaration")
return
}
x.val = check.iota
x.mode = constant_
case *TypeName:
- if check.isBrokenAlias(obj) {
- check.errorf(e, _InvalidDeclCycle, "invalid use of type alias %s in recursive type (see issue #50729)", obj.name)
+ if !check.enableAlias && check.isBrokenAlias(obj) {
+ check.errorf(e, InvalidDeclCycle, "invalid use of type alias %s in recursive type (see go.dev/issue/50729)", obj.name)
return
}
x.mode = typexpr
obj.used = true
}
check.addDeclDep(obj)
- if typ == Typ[Invalid] {
+ if !isValid(typ) {
return
}
x.mode = variable
tset := computeInterfaceTypeSet(check, e.Pos(), t) // TODO(gri) is this the correct position?
if !tset.IsMethodSet() {
if tset.comparable {
- check.softErrorf(e, _MisplacedConstraintIface, "cannot use type %s outside a type constraint: interface is (or embeds) comparable", typ)
+ check.softErrorf(e, MisplacedConstraintIface, "cannot use type %s outside a type constraint: interface is (or embeds) comparable", typ)
} else {
- check.softErrorf(e, _MisplacedConstraintIface, "cannot use type %s outside a type constraint: interface contains type constraints", typ)
+ check.softErrorf(e, MisplacedConstraintIface, "cannot use type %s outside a type constraint: interface contains type constraints", typ)
}
}
}
}
// definedType is like typ but also accepts a type name def.
-// If def != nil, e is the type specification for the defined type def, declared
-// in a type declaration, and def.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 *Named) Type {
+// 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 {
typ := check.typInternal(e, def)
assert(isTyped(typ))
if isGeneric(typ) {
- check.errorf(e, _WrongTypeArgCount, "cannot use generic type %s without instantiation", typ)
+ check.errorf(e, WrongTypeArgCount, "cannot use generic type %s without instantiation", typ)
typ = Typ[Invalid]
}
check.recordTypeAndValue(e, typexpr, typ, nil)
func (check *Checker) genericType(e ast.Expr, cause *string) Type {
typ := check.typInternal(e, nil)
assert(isTyped(typ))
- if typ != Typ[Invalid] && !isGeneric(typ) {
+ if isValid(typ) && !isGeneric(typ) {
if cause != nil {
*cause = check.sprintf("%s is not a generic type", typ)
}
// typInternal drives type checking of types.
// Must only be called by definedType or genericType.
-func (check *Checker) typInternal(e0 ast.Expr, def *Named) (T Type) {
- if trace {
+func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
+ if check.conf._Trace {
check.trace(e0.Pos(), "-- type %s", e0)
check.indent++
defer func() {
switch x.mode {
case typexpr:
typ := x.typ
- def.setUnderlying(typ)
+ setDefType(def, typ)
return typ
case invalid:
// ignore - error reported before
case novalue:
- check.errorf(&x, _NotAType, "%s used as type", &x)
+ check.errorf(&x, NotAType, "%s used as type", &x)
default:
- check.errorf(&x, _NotAType, "%s is not a type", &x)
+ check.errorf(&x, NotAType, "%s is not a type", &x)
}
case *ast.SelectorExpr:
var x operand
- check.selector(&x, e, def)
+ check.selector(&x, e, def, true)
switch x.mode {
case typexpr:
typ := x.typ
- def.setUnderlying(typ)
+ setDefType(def, typ)
return typ
case invalid:
// ignore - error reported before
case novalue:
- check.errorf(&x, _NotAType, "%s used as type", &x)
+ check.errorf(&x, NotAType, "%s used as type", &x)
default:
- check.errorf(&x, _NotAType, "%s is not a type", &x)
+ check.errorf(&x, NotAType, "%s is not a type", &x)
}
case *ast.IndexExpr, *ast.IndexListExpr:
ix := typeparams.UnpackIndexExpr(e)
- if !check.allowVersion(check.pkg, 1, 18) {
- check.softErrorf(inNode(e, ix.Lbrack), _UnsupportedFeature, "type instantiation requires go1.18 or later")
- }
+ check.verifyVersionf(inNode(e, ix.Lbrack), go1_18, "type instantiation")
return check.instantiatedType(ix, def)
case *ast.ParenExpr:
case *ast.ArrayType:
if e.Len == nil {
typ := new(Slice)
- def.setUnderlying(typ)
+ setDefType(def, typ)
typ.elem = check.varType(e.Elt)
return typ
}
typ := new(Array)
- def.setUnderlying(typ)
+ setDefType(def, typ)
// Provide a more specific error when encountering a [...] array
// rather than leaving it to the handling of the ... expression.
if _, ok := e.Len.(*ast.Ellipsis); ok {
- check.error(e.Len, _BadDotDotDotSyntax, "invalid use of [...] array (outside a composite literal)")
+ check.error(e.Len, BadDotDotDotSyntax, "invalid use of [...] array (outside a composite literal)")
typ.len = -1
} else {
typ.len = check.arrayLength(e.Len)
case *ast.Ellipsis:
// dots are handled explicitly where they are legal
// (array composite literals and parameter lists)
- check.error(e, _InvalidDotDotDot, "invalid use of '...'")
+ check.error(e, InvalidDotDotDot, "invalid use of '...'")
check.use(e.Elt)
case *ast.StructType:
typ := new(Struct)
- def.setUnderlying(typ)
+ setDefType(def, typ)
check.structType(typ, e)
return typ
case *ast.StarExpr:
typ := new(Pointer)
typ.base = Typ[Invalid] // avoid nil base in invalid recursive type declaration
- def.setUnderlying(typ)
+ setDefType(def, typ)
typ.base = check.varType(e.X)
return typ
case *ast.FuncType:
typ := new(Signature)
- def.setUnderlying(typ)
+ setDefType(def, typ)
check.funcType(typ, nil, e)
return typ
case *ast.InterfaceType:
typ := check.newInterface()
- def.setUnderlying(typ)
+ setDefType(def, typ)
check.interfaceType(typ, e, def)
return typ
case *ast.MapType:
typ := new(Map)
- def.setUnderlying(typ)
+ setDefType(def, typ)
typ.key = check.varType(e.Key)
typ.elem = check.varType(e.Value)
// function, map, or slice."
//
// Delay this check because it requires fully setup types;
- // it is safe to continue in any case (was issue 6667).
+ // it is safe to continue in any case (was go.dev/issue/6667).
check.later(func() {
if !Comparable(typ.key) {
var why string
if isTypeParam(typ.key) {
why = " (missing comparable constraint)"
}
- check.errorf(e.Key, _IncomparableMapKey, "invalid map key type %s%s", typ.key, why)
+ check.errorf(e.Key, IncomparableMapKey, "invalid map key type %s%s", typ.key, why)
}
}).describef(e.Key, "check map key %s", typ.key)
case *ast.ChanType:
typ := new(Chan)
- def.setUnderlying(typ)
+ setDefType(def, typ)
dir := SendRecv
switch e.Dir {
case ast.RECV:
dir = RecvOnly
default:
- check.invalidAST(e, "unknown channel direction %d", e.Dir)
+ check.errorf(e, InvalidSyntaxTree, "unknown channel direction %d", e.Dir)
// ok to continue
}
return typ
default:
- check.errorf(e0, _NotAType, "%s is not a type", e0)
+ check.errorf(e0, NotAType, "%s is not a type", e0)
check.use(e0)
}
typ := Typ[Invalid]
- def.setUnderlying(typ)
+ setDefType(def, typ)
return typ
}
-func (check *Checker) instantiatedType(ix *typeparams.IndexExpr, def *Named) (res Type) {
- if trace {
+func setDefType(def *TypeName, typ Type) {
+ if def != nil {
+ switch t := def.typ.(type) {
+ case *Alias:
+ // t.fromRHS should always be set, either to an invalid type
+ // in the beginning, or to typ in certain cyclic declarations.
+ if t.fromRHS != Typ[Invalid] && t.fromRHS != typ {
+ panic(sprintf(nil, nil, true, "t.fromRHS = %s, typ = %s\n", t.fromRHS, typ))
+ }
+ t.fromRHS = typ
+ case *Basic:
+ assert(t == Typ[Invalid])
+ case *Named:
+ t.underlying = typ
+ default:
+ panic(fmt.Sprintf("unexpected type %T", t))
+ }
+ }
+}
+
+func (check *Checker) instantiatedType(ix *typeparams.IndexExpr, def *TypeName) (res Type) {
+ if check.conf._Trace {
check.trace(ix.Pos(), "-- instantiating type %s with %s", ix.X, ix.Indices)
check.indent++
defer func() {
var cause string
gtyp := check.genericType(ix.X, &cause)
if cause != "" {
- check.invalidOp(ix.Orig, _NotAGenericType, "%s (%s)", ix.Orig, cause)
+ check.errorf(ix.Orig, NotAGenericType, invalidOp+"%s (%s)", ix.Orig, cause)
}
- if gtyp == Typ[Invalid] {
+ if !isValid(gtyp) {
return gtyp // error already reported
}
- orig, _ := gtyp.(*Named)
+ orig := asNamed(gtyp)
if orig == nil {
panic(fmt.Sprintf("%v: cannot instantiate %v", ix.Pos(), gtyp))
}
// evaluate arguments
targs := check.typeList(ix.Indices)
if targs == nil {
- def.setUnderlying(Typ[Invalid]) // avoid errors later due to lazy instantiation
+ setDefType(def, Typ[Invalid]) // avoid errors later due to lazy instantiation
return Typ[Invalid]
}
// create the instance
- inst := check.instance(ix.Pos(), orig, targs, nil, check.context()).(*Named)
- def.setUnderlying(inst)
+ inst := asNamed(check.instance(ix.Pos(), orig, targs, nil, check.context()))
+ setDefType(def, inst)
// orig.tparams may not be set up, so we need to do expansion later.
check.later(func() {
if i < len(ix.Indices) {
pos = ix.Indices[i].Pos()
}
- check.softErrorf(atPos(pos), _InvalidTypeArg, err.Error())
+ check.softErrorf(atPos(pos), InvalidTypeArg, err.Error())
} else {
check.mono.recordInstance(check.pkg, ix.Pos(), inst.TypeParams().list(), inst.TypeArgs().list(), ix.Indices)
}
if name, _ := e.(*ast.Ident); name != nil {
obj := check.lookup(name.Name)
if obj == nil {
- check.errorf(name, _InvalidArrayLen, "undefined %s for array length", name.Name)
+ check.errorf(name, InvalidArrayLen, "undefined array length %s or missing type constraint", name.Name)
return -1
}
if _, ok := obj.(*Const); !ok {
- check.errorf(name, _InvalidArrayLen, "invalid array length %s", name.Name)
+ check.errorf(name, InvalidArrayLen, "invalid array length %s", name.Name)
return -1
}
}
var x operand
- check.expr(&x, e)
+ check.expr(nil, &x, e)
if x.mode != constant_ {
if x.mode != invalid {
- check.errorf(&x, _InvalidArrayLen, "array length %s must be constant", &x)
+ check.errorf(&x, InvalidArrayLen, "array length %s must be constant", &x)
}
return -1
}
if n, ok := constant.Int64Val(val); ok && n >= 0 {
return n
}
- check.errorf(&x, _InvalidArrayLen, "invalid array length %s", &x)
- return -1
}
}
}
- check.errorf(&x, _InvalidArrayLen, "array length %s must be integer", &x)
+ var msg string
+ if isInteger(x.typ) {
+ msg = "invalid array length %s"
+ } else {
+ msg = "array length %s must be integer"
+ }
+ check.errorf(&x, InvalidArrayLen, msg, &x)
return -1
}
res := make([]Type, len(list)) // res != nil even if len(list) == 0
for i, x := range list {
t := check.varType(x)
- if t == Typ[Invalid] {
+ if !isValid(t) {
res = nil
}
if res != nil {