1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 "internal/types/errors"
14 "cmd/compile/internal/base"
15 "cmd/compile/internal/ir"
16 "cmd/compile/internal/types"
20 func tcShift(n, l, r ir.Node) (ir.Node, ir.Node, *types.Type) {
21 if l.Type() == nil || r.Type() == nil {
25 r = DefaultLit(r, types.Types[types.TUINT])
28 base.Errorf("invalid operation: %v (shift count type %v, must be integer)", n, r.Type())
32 if t != nil && t.Kind() != types.TIDEAL && !t.IsInteger() {
33 base.Errorf("invalid operation: %v (shift of type %v)", n, t)
37 // no DefaultLit for left
38 // the outer context gives the type
40 if (l.Type() == types.UntypedFloat || l.Type() == types.UntypedComplex) && r.Op() == ir.OLITERAL {
46 // tcArith typechecks operands of a binary arithmetic expression.
47 // The result of tcArith MUST be assigned back to original operands,
48 // t is the type of the expression, and should be set by the caller. e.g:
50 // n.X, n.Y, t = tcArith(n, op, n.X, n.Y)
52 func tcArith(n ir.Node, op ir.Op, l, r ir.Node) (ir.Node, ir.Node, *types.Type) {
53 l, r = defaultlit2(l, r, false)
54 if l.Type() == nil || r.Type() == nil {
58 if t.Kind() == types.TIDEAL {
62 if n.Op().IsCmp() && t.Kind() != types.TIDEAL && !types.Identical(l.Type(), r.Type()) {
63 // comparison is okay as long as one side is
64 // assignable to the other. convert so they have
67 // the only conversion that isn't a no-op is concrete == interface.
68 // in that case, check comparability of the concrete type.
69 // The conversion allocates, so only do it if the concrete type is huge.
71 if r.Type().Kind() != types.TBLANK {
72 aop, _ = Assignop(l.Type(), r.Type())
74 if r.Type().IsInterface() && !l.Type().IsInterface() && !types.IsComparable(l.Type()) {
75 base.Errorf("invalid operation: %v (operator %v not defined on %s)", n, op, typekind(l.Type()))
79 types.CalcSize(l.Type())
80 if r.Type().IsInterface() == l.Type().IsInterface() || l.Type().Size() >= 1<<16 {
81 l = ir.NewConvExpr(base.Pos, aop, r.Type(), l)
90 if !converted && l.Type().Kind() != types.TBLANK {
91 aop, _ = Assignop(r.Type(), l.Type())
93 if l.Type().IsInterface() && !r.Type().IsInterface() && !types.IsComparable(r.Type()) {
94 base.Errorf("invalid operation: %v (operator %v not defined on %s)", n, op, typekind(r.Type()))
98 types.CalcSize(r.Type())
99 if r.Type().IsInterface() == l.Type().IsInterface() || r.Type().Size() >= 1<<16 {
100 r = ir.NewConvExpr(base.Pos, aop, l.Type(), r)
109 if t.Kind() != types.TIDEAL && !types.Identical(l.Type(), r.Type()) {
110 l, r = defaultlit2(l, r, true)
111 if l.Type() == nil || r.Type() == nil {
114 if l.Type().IsInterface() == r.Type().IsInterface() || aop == 0 {
115 base.Errorf("invalid operation: %v (mismatched types %v and %v)", n, l.Type(), r.Type())
120 if t.Kind() == types.TIDEAL {
121 t = mixUntyped(l.Type(), r.Type())
123 if dt := defaultType(t); !okfor[op][dt.Kind()] {
124 base.Errorf("invalid operation: %v (operator %v not defined on %s)", n, op, typekind(t))
128 // okfor allows any array == array, map == map, func == func.
129 // restrict to slice/map/func == nil and nil == slice/map/func.
130 if l.Type().IsArray() && !types.IsComparable(l.Type()) {
131 base.Errorf("invalid operation: %v (%v cannot be compared)", n, l.Type())
135 if l.Type().IsSlice() && !ir.IsNil(l) && !ir.IsNil(r) {
136 base.Errorf("invalid operation: %v (slice can only be compared to nil)", n)
140 if l.Type().IsMap() && !ir.IsNil(l) && !ir.IsNil(r) {
141 base.Errorf("invalid operation: %v (map can only be compared to nil)", n)
145 if l.Type().Kind() == types.TFUNC && !ir.IsNil(l) && !ir.IsNil(r) {
146 base.Errorf("invalid operation: %v (func can only be compared to nil)", n)
150 if l.Type().IsStruct() {
151 if f := types.IncomparableField(l.Type()); f != nil {
152 base.Errorf("invalid operation: %v (struct containing %v cannot be compared)", n, f.Type)
160 // The result of tcCompLit MUST be assigned back to n, e.g.
162 // n.Left = tcCompLit(n.Left)
163 func tcCompLit(n *ir.CompLitExpr) (res ir.Node) {
164 if base.EnableTrace && base.Flag.LowerT {
165 defer tracePrint("tcCompLit", n)(&res)
176 base.AssertfAt(t != nil, n.Pos(), "missing type in composite literal")
180 base.Errorf("invalid composite literal type %v", t)
184 typecheckarraylit(t.Elem(), t.NumElem(), n.List, "array literal")
185 n.SetOp(ir.OARRAYLIT)
188 length := typecheckarraylit(t.Elem(), -1, n.List, "slice literal")
189 n.SetOp(ir.OSLICELIT)
193 for i3, l := range n.List {
195 if l.Op() != ir.OKEY {
197 base.Errorf("missing key in map literal")
204 l.Key = AssignConv(r, t.Key(), "map key")
208 l.Value = AssignConv(r, t.Elem(), "map value")
214 // Need valid field offsets for Xoffset below.
218 if len(n.List) != 0 && nokeys(n.List) {
219 // simple list of variables
221 for i, n1 := range ls {
225 if i >= t.NumFields() {
227 base.Errorf("too many values in %v", n)
236 // Do the test for assigning to unexported fields.
237 // But if this is an instantiated function, then
238 // the function has already been typechecked. In
239 // that case, don't do the test, since it can fail
240 // for the closure structs created in
241 // walkClosure(), because the instantiated
242 // function is compiled as if in the source
243 // package of the generic function.
244 if !(ir.CurFunc != nil && strings.Index(ir.CurFunc.Nname.Sym().Name, "[") >= 0) {
245 if s != nil && !types.IsExported(s.Name) && s.Pkg != types.LocalPkg {
246 base.Errorf("implicit assignment of unexported field '%s' in %v literal", s.Name, t)
249 // No pushtype allowed here. Must name fields for that.
250 n1 = AssignConv(n1, f.Type, "field value")
251 ls[i] = ir.NewStructKeyExpr(base.Pos, f, n1)
253 if len(ls) < t.NumFields() {
254 base.Errorf("too few values in %v", n)
257 hash := make(map[string]bool)
261 for i, n := range ls {
264 sk, ok := n.(*ir.StructKeyExpr)
266 kv, ok := n.(*ir.KeyExpr)
269 base.Errorf("mixture of field:value and value initializers")
276 sk = tcStructLitKey(t, kv)
281 fielddup(sk.Sym().Name, hash)
284 // No pushtype allowed here. Tried and rejected.
285 sk.Value = Expr(sk.Value)
286 sk.Value = AssignConv(sk.Value, sk.Field.Type, "field value")
291 n.SetOp(ir.OSTRUCTLIT)
297 // tcStructLitKey typechecks an OKEY node that appeared within a
299 func tcStructLitKey(typ *types.Type, kv *ir.KeyExpr) *ir.StructKeyExpr {
304 // An OXDOT uses the Sym field to hold
305 // the field to the right of the dot,
306 // so s will be non-nil, but an OXDOT
307 // is never a valid struct literal key.
308 if sym == nil || sym.Pkg != types.LocalPkg || key.Op() == ir.OXDOT || sym.IsBlank() {
309 base.Errorf("invalid field name %v in struct initializer", key)
313 if f := Lookdot1(nil, sym, typ, typ.Fields(), 0); f != nil {
314 return ir.NewStructKeyExpr(kv.Pos(), f, kv.Value)
317 if ci := Lookdot1(nil, sym, typ, typ.Fields(), 2); ci != nil { // Case-insensitive lookup.
319 base.Errorf("unknown field '%v' in struct literal of type %v (but does have %v)", sym, typ, ci.Sym)
320 } else if nonexported(sym) && sym.Name == ci.Sym.Name { // Ensure exactness before the suggestion.
321 base.Errorf("cannot refer to unexported field '%v' in struct literal of type %v", sym, typ)
323 base.Errorf("unknown field '%v' in struct literal of type %v", sym, typ)
329 p, _ := dotpath(sym, typ, &f, true)
330 if p == nil || f.IsMethod() {
331 base.Errorf("unknown field '%v' in struct literal of type %v", sym, typ)
335 // dotpath returns the parent embedded types in reverse order.
337 for ei := len(p) - 1; ei >= 0; ei-- {
338 ep = append(ep, p[ei].field.Sym.Name)
340 ep = append(ep, sym.Name)
341 base.Errorf("cannot use promoted field %v in struct literal of type %v", strings.Join(ep, "."), typ)
345 // tcConv typechecks an OCONV node.
346 func tcConv(n *ir.ConvExpr) ir.Node {
347 types.CheckSize(n.Type()) // ensure width is calculated for backend
349 n.X = convlit1(n.X, n.Type(), true, nil)
351 if t == nil || n.Type() == nil {
355 op, why := Convertop(n.X.Op() == ir.OLITERAL, t, n.Type())
357 // Due to //go:nointerface, we may be stricter than types2 here (#63333).
358 base.ErrorfAt(n.Pos(), errors.InvalidConversion, "cannot convert %L to type %v%s", n.X, n.Type(), why)
366 if t.Kind() == n.Type().Kind() {
368 case types.TFLOAT32, types.TFLOAT64, types.TCOMPLEX64, types.TCOMPLEX128:
369 // Floating point casts imply rounding and
370 // so the conversion must be kept.
375 // do not convert to []byte literal. See CL 125796.
376 // generated code and compiler memory footprint is better without it.
381 if n.X.Op() == ir.OLITERAL {
382 return stringtoruneslit(n)
386 if t.Elem() != types.ByteType && t.Elem() != types.Types[types.TUINT8] {
387 // If t is a slice of a user-defined byte type B (not uint8
388 // or byte), then add an extra CONVNOP from []B to []byte, so
389 // that the call to slicebytetostring() added in walk will
390 // typecheck correctly.
391 n.X = ir.NewConvExpr(n.X.Pos(), ir.OCONVNOP, types.NewSlice(types.ByteType), n.X)
396 if t.Elem() != types.RuneType && t.Elem() != types.Types[types.TINT32] {
397 // If t is a slice of a user-defined rune type B (not uint32
398 // or rune), then add an extra CONVNOP from []B to []rune, so
399 // that the call to slicerunetostring() added in walk will
400 // typecheck correctly.
401 n.X = ir.NewConvExpr(n.X.Pos(), ir.OCONVNOP, types.NewSlice(types.RuneType), n.X)
409 // DotField returns a field selector expression that selects the
410 // index'th field of the given expression, which must be of struct or
411 // pointer-to-struct type.
412 func DotField(pos src.XPos, x ir.Node, index int) *ir.SelectorExpr {
413 op, typ := ir.ODOT, x.Type()
415 op, typ = ir.ODOTPTR, typ.Elem()
418 base.FatalfAt(pos, "DotField of non-struct: %L", x)
421 // TODO(mdempsky): This is the backend's responsibility.
424 field := typ.Field(index)
425 return dot(pos, field.Type, op, x, field)
428 func dot(pos src.XPos, typ *types.Type, op ir.Op, x ir.Node, selection *types.Field) *ir.SelectorExpr {
429 n := ir.NewSelectorExpr(pos, op, x, selection.Sym)
430 n.Selection = selection
436 // XDotMethod returns an expression representing the field selection
437 // x.sym. If any implicit field selection are necessary, those are
439 func XDotField(pos src.XPos, x ir.Node, sym *types.Sym) *ir.SelectorExpr {
440 n := Expr(ir.NewSelectorExpr(pos, ir.OXDOT, x, sym)).(*ir.SelectorExpr)
441 if n.Op() != ir.ODOT && n.Op() != ir.ODOTPTR {
442 base.FatalfAt(pos, "unexpected result op: %v (%v)", n.Op(), n)
447 // XDotMethod returns an expression representing the method value
448 // x.sym (i.e., x is a value, not a type). If any implicit field
449 // selection are necessary, those are inserted too.
451 // If callee is true, the result is an ODOTMETH/ODOTINTER, otherwise
453 func XDotMethod(pos src.XPos, x ir.Node, sym *types.Sym, callee bool) *ir.SelectorExpr {
454 n := ir.NewSelectorExpr(pos, ir.OXDOT, x, sym)
456 n = Callee(n).(*ir.SelectorExpr)
457 if n.Op() != ir.ODOTMETH && n.Op() != ir.ODOTINTER {
458 base.FatalfAt(pos, "unexpected result op: %v (%v)", n.Op(), n)
461 n = Expr(n).(*ir.SelectorExpr)
462 if n.Op() != ir.OMETHVALUE {
463 base.FatalfAt(pos, "unexpected result op: %v (%v)", n.Op(), n)
469 // tcDot typechecks an OXDOT or ODOT node.
470 func tcDot(n *ir.SelectorExpr, top int) ir.Node {
471 if n.Op() == ir.OXDOT {
472 n = AddImplicitDots(n)
481 n.X = DefaultLit(n.X, nil)
485 base.UpdateErrorDot(ir.Line(n), fmt.Sprint(n.X), fmt.Sprint(n))
490 if n.X.Op() == ir.OTYPE {
491 base.FatalfAt(n.Pos(), "use NewMethodExpr to construct OMETHEXPR")
494 if t.IsPtr() && !t.Elem().IsInterface() {
505 base.Errorf("cannot refer to blank field or method")
510 if Lookdot(n, t, 0) == nil {
511 // Legitimate field or method lookup failed, try to explain the error
513 case t.IsEmptyInterface():
514 base.Errorf("%v undefined (type %v is interface with no methods)", n, n.X.Type())
516 case t.IsPtr() && t.Elem().IsInterface():
517 // Pointer to interface is almost always a mistake.
518 base.Errorf("%v undefined (type %v is pointer to interface, not interface)", n, n.X.Type())
520 case Lookdot(n, t, 1) != nil:
521 // Field or method matches by name, but it is not exported.
522 base.Errorf("%v undefined (cannot refer to unexported field or method %v)", n, n.Sel)
525 if mt := Lookdot(n, t, 2); mt != nil && visible(mt.Sym) { // Case-insensitive lookup.
526 base.Errorf("%v undefined (type %v has no field or method %v, but does have %v)", n, n.X.Type(), n.Sel, mt.Sym)
528 base.Errorf("%v undefined (type %v has no field or method %v)", n, n.X.Type(), n.Sel)
535 if (n.Op() == ir.ODOTINTER || n.Op() == ir.ODOTMETH) && top&ctxCallee == 0 {
536 n.SetOp(ir.OMETHVALUE)
537 n.SetType(NewMethodType(n.Type(), nil))
542 // tcDotType typechecks an ODOTTYPE node.
543 func tcDotType(n *ir.TypeAssertExpr) ir.Node {
545 n.X = DefaultLit(n.X, nil)
552 if !t.IsInterface() {
553 base.Errorf("invalid type assertion: %v (non-interface type %v on left)", n, t)
558 base.AssertfAt(n.Type() != nil, n.Pos(), "missing type: %v", n)
560 if n.Type() != nil && !n.Type().IsInterface() {
561 why := ImplementsExplain(n.Type(), t)
563 base.Fatalf("impossible type assertion:\n\t%s", why)
571 // tcITab typechecks an OITAB node.
572 func tcITab(n *ir.UnaryExpr) ir.Node {
579 if !t.IsInterface() {
580 base.Fatalf("OITAB of %v", t)
582 n.SetType(types.NewPtr(types.Types[types.TUINTPTR]))
586 // tcIndex typechecks an OINDEX node.
587 func tcIndex(n *ir.IndexExpr) ir.Node {
589 n.X = DefaultLit(n.X, nil)
590 n.X = implicitstar(n.X)
592 n.Index = Expr(n.Index)
595 if t == nil || r.Type() == nil {
601 base.Errorf("invalid operation: %v (type %v does not support indexing)", n, t)
605 case types.TSTRING, types.TARRAY, types.TSLICE:
606 n.Index = indexlit(n.Index)
608 n.SetType(types.ByteType)
615 } else if t.IsSlice() {
619 if n.Index.Type() != nil && !n.Index.Type().IsInteger() {
620 base.Errorf("non-integer %s index %v", why, n.Index)
624 if !n.Bounded() && ir.IsConst(n.Index, constant.Int) {
626 if constant.Sign(x) < 0 {
627 base.Errorf("invalid %s index %v (index must be non-negative)", why, n.Index)
628 } else if t.IsArray() && constant.Compare(x, token.GEQ, constant.MakeInt64(t.NumElem())) {
629 base.Errorf("invalid array index %v (out of bounds for %d-element array)", n.Index, t.NumElem())
630 } else if ir.IsConst(n.X, constant.String) && constant.Compare(x, token.GEQ, constant.MakeInt64(int64(len(ir.StringVal(n.X))))) {
631 base.Errorf("invalid string index %v (out of bounds for %d-byte string)", n.Index, len(ir.StringVal(n.X)))
632 } else if ir.ConstOverflow(x, types.Types[types.TINT]) {
633 base.Errorf("invalid %s index %v (index too large)", why, n.Index)
638 n.Index = AssignConv(n.Index, t.Key(), "map index")
640 n.SetOp(ir.OINDEXMAP)
646 // tcLenCap typechecks an OLEN or OCAP node.
647 func tcLenCap(n *ir.UnaryExpr) ir.Node {
649 n.X = DefaultLit(n.X, nil)
650 n.X = implicitstar(n.X)
659 if n.Op() == ir.OLEN {
660 ok = okforlen[t.Kind()]
662 ok = okforcap[t.Kind()]
665 base.Errorf("invalid argument %L for %v", l, n.Op())
670 n.SetType(types.Types[types.TINT])
674 // tcUnsafeData typechecks an OUNSAFESLICEDATA or OUNSAFESTRINGDATA node.
675 func tcUnsafeData(n *ir.UnaryExpr) ir.Node {
677 n.X = DefaultLit(n.X, nil)
686 if n.Op() == ir.OUNSAFESLICEDATA {
693 if t.Kind() != kind {
694 base.Errorf("invalid argument %L for %v", l, n.Op())
699 if kind == types.TSTRING {
704 n.SetType(types.NewPtr(t))
708 // tcRecv typechecks an ORECV node.
709 func tcRecv(n *ir.UnaryExpr) ir.Node {
711 n.X = DefaultLit(n.X, nil)
719 base.Errorf("invalid operation: %v (receive from non-chan type %v)", n, t)
724 if !t.ChanDir().CanRecv() {
725 base.Errorf("invalid operation: %v (receive from send-only type %v)", n, t)
734 // tcSPtr typechecks an OSPTR node.
735 func tcSPtr(n *ir.UnaryExpr) ir.Node {
742 if !t.IsSlice() && !t.IsString() {
743 base.Fatalf("OSPTR of %v", t)
746 n.SetType(types.NewPtr(types.Types[types.TUINT8]))
748 n.SetType(types.NewPtr(t.Elem()))
753 // tcSlice typechecks an OSLICE or OSLICE3 node.
754 func tcSlice(n *ir.SliceExpr) ir.Node {
755 n.X = DefaultLit(Expr(n.X), nil)
756 n.Low = indexlit(Expr(n.Low))
757 n.High = indexlit(Expr(n.High))
758 n.Max = indexlit(Expr(n.Max))
759 hasmax := n.Op().IsSlice3()
765 if l.Type().IsArray() {
766 if !ir.IsAddressable(n.X) {
767 base.Errorf("invalid operation %v (slice of unaddressable value)", n)
773 addr.SetImplicit(true)
781 base.Errorf("invalid operation %v (3-index slice of string)", n)
786 n.SetOp(ir.OSLICESTR)
787 } else if t.IsPtr() && t.Elem().IsArray() {
789 n.SetType(types.NewSlice(tp.Elem()))
790 types.CalcSize(n.Type())
792 n.SetOp(ir.OSLICE3ARR)
794 n.SetOp(ir.OSLICEARR)
796 } else if t.IsSlice() {
799 base.Errorf("cannot slice %v (type %v)", l, t)
804 if n.Low != nil && !checksliceindex(l, n.Low, tp) {
808 if n.High != nil && !checksliceindex(l, n.High, tp) {
812 if n.Max != nil && !checksliceindex(l, n.Max, tp) {
816 if !checksliceconst(n.Low, n.High) || !checksliceconst(n.Low, n.Max) || !checksliceconst(n.High, n.Max) {
823 // tcSliceHeader typechecks an OSLICEHEADER node.
824 func tcSliceHeader(n *ir.SliceHeaderExpr) ir.Node {
825 // Errors here are Fatalf instead of Errorf because only the compiler
826 // can construct an OSLICEHEADER node.
827 // Components used in OSLICEHEADER that are supplied by parsed source code
828 // have already been typechecked in e.g. OMAKESLICE earlier.
831 base.Fatalf("no type specified for OSLICEHEADER")
835 base.Fatalf("invalid type %v for OSLICEHEADER", n.Type())
838 if n.Ptr == nil || n.Ptr.Type() == nil || !n.Ptr.Type().IsUnsafePtr() {
839 base.Fatalf("need unsafe.Pointer for OSLICEHEADER")
843 n.Len = DefaultLit(Expr(n.Len), types.Types[types.TINT])
844 n.Cap = DefaultLit(Expr(n.Cap), types.Types[types.TINT])
846 if ir.IsConst(n.Len, constant.Int) && ir.Int64Val(n.Len) < 0 {
847 base.Fatalf("len for OSLICEHEADER must be non-negative")
850 if ir.IsConst(n.Cap, constant.Int) && ir.Int64Val(n.Cap) < 0 {
851 base.Fatalf("cap for OSLICEHEADER must be non-negative")
854 if ir.IsConst(n.Len, constant.Int) && ir.IsConst(n.Cap, constant.Int) && constant.Compare(n.Len.Val(), token.GTR, n.Cap.Val()) {
855 base.Fatalf("len larger than cap for OSLICEHEADER")
861 // tcStringHeader typechecks an OSTRINGHEADER node.
862 func tcStringHeader(n *ir.StringHeaderExpr) ir.Node {
865 base.Fatalf("no type specified for OSTRINGHEADER")
869 base.Fatalf("invalid type %v for OSTRINGHEADER", n.Type())
872 if n.Ptr == nil || n.Ptr.Type() == nil || !n.Ptr.Type().IsUnsafePtr() {
873 base.Fatalf("need unsafe.Pointer for OSTRINGHEADER")
877 n.Len = DefaultLit(Expr(n.Len), types.Types[types.TINT])
879 if ir.IsConst(n.Len, constant.Int) && ir.Int64Val(n.Len) < 0 {
880 base.Fatalf("len for OSTRINGHEADER must be non-negative")
886 // tcStar typechecks an ODEREF node, which may be an expression or a type.
887 func tcStar(n *ir.StarExpr, top int) ir.Node {
888 n.X = typecheck(n.X, ctxExpr|ctxType)
896 // TODO(mdempsky): Remove (along with ctxType above) once I'm
897 // confident this code path isn't needed any more.
898 if l.Op() == ir.OTYPE {
899 base.Fatalf("unexpected type in deref expression: %v", l)
903 if top&(ctxExpr|ctxStmt) != 0 {
904 base.Errorf("invalid indirect of %L", n.X)
908 base.Errorf("%v is not a type", l)
916 // tcUnaryArith typechecks a unary arithmetic expression.
917 func tcUnaryArith(n *ir.UnaryExpr) ir.Node {
925 if !okfor[n.Op()][defaultType(t).Kind()] {
926 base.Errorf("invalid operation: %v (operator %v not defined on %s)", n, n.Op(), typekind(t))