1 // Copyright 2012 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.
5 // This file implements typechecking of builtin function calls.
10 "cmd/compile/internal/syntax"
13 . "internal/types/errors"
16 // builtin type-checks a call to the built-in specified by id and
17 // reports whether the call is valid, with *x holding the result;
18 // but x.expr is not set. If the call is invalid, the result is
19 // false, and *x is undefined.
20 func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (_ bool) {
21 argList := call.ArgList
23 // append is the only built-in that permits the use of ... for the last argument
24 bin := predeclaredFuncs[id]
25 if call.HasDots && id != _Append {
26 //check.errorf(call.Ellipsis, invalidOp + "invalid use of ... with built-in %s", bin.name)
29 invalidOp+"invalid use of ... with built-in %s", bin.name)
34 // For len(x) and cap(x) we need to know if x contains any function calls or
35 // receive operations. Save/restore current setting and set hasCallOrRecv to
36 // false for the evaluation of x so that we can check it afterwards.
37 // Note: We must do this _before_ calling exprList because exprList evaluates
39 if id == _Len || id == _Cap {
41 check.hasCallOrRecv = b
42 }(check.hasCallOrRecv)
43 check.hasCallOrRecv = false
46 // Evaluate arguments for built-ins that use ordinary (value) arguments.
47 // For built-ins with special argument handling (make, new, etc.),
48 // evaluation is done by the respective built-in code.
49 var args []*operand // not valid for _Make, _New, _Offsetof, _Trace
53 // check all arguments
54 args = check.exprList(argList)
56 for _, a := range args {
57 if a.mode == invalid {
61 // first argument is always in x
65 case _Make, _New, _Offsetof, _Trace:
66 // arguments require special handling
70 // check argument count
73 if nargs < bin.nargs {
75 } else if !bin.variadic && nargs > bin.nargs {
79 check.errorf(call, WrongArgCount, invalidOp+"%s arguments for %v (expected %d, found %d)", msg, call, bin.nargs, nargs)
86 // append(s S, x ...T) S, where T is the element type of S
87 // spec: "The variadic function append appends zero or more values x to s of type
88 // S, which must be a slice type, and returns the resulting slice, also of type S.
89 // The values x are passed to a parameter of type ...T where T is the element type
90 // of S and the respective parameter passing rules apply."
93 if s, _ := coreType(S).(*Slice); s != nil {
99 cause = "have untyped nil"
101 if u := coreType(S); u != nil {
102 cause = check.sprintf("%s has core type %s", x, u)
104 cause = check.sprintf("%s has no core type", x)
107 cause = check.sprintf("have %s", x)
109 // don't use invalidArg prefix here as it would repeat "argument" in the error message
110 check.errorf(x, InvalidAppend, "first argument to append must be a slice; %s", cause)
114 // spec: "As a special case, append also accepts a first argument assignable
115 // to type []byte with a second argument of string type followed by ... .
116 // This form appends the bytes of the string.
117 if nargs == 2 && call.HasDots {
118 if ok, _ := x.assignableTo(check, NewSlice(universeByte), nil); ok {
120 if t := coreString(y.typ); t != nil && isString(t) {
121 if check.recordTypes() {
122 sig := makeSig(S, S, y.typ)
124 check.recordBuiltinType(call.Fun, sig)
133 // check general case by creating custom signature
134 sig := makeSig(S, S, NewSlice(T)) // []T required for variadic signature
136 check.arguments(call, sig, nil, nil, args, nil, nil) // discard result (we know the result type)
137 // ok to continue even if check.arguments reported errors
141 if check.recordTypes() {
142 check.recordBuiltinType(call.Fun, sig)
149 var val constant.Value
150 switch t := arrayPtrDeref(under(x.typ)).(type) {
152 if isString(t) && id == _Len {
153 if x.mode == constant_ {
155 val = constant.MakeInt64(int64(len(constant.StringVal(x.val))))
163 // spec: "The expressions len(s) and cap(s) are constants
164 // if the type of s is an array or pointer to an array and
165 // the expression s does not contain channel receives or
166 // function calls; in this case s is not evaluated."
167 if !check.hasCallOrRecv {
170 val = constant.MakeInt64(t.len)
172 val = constant.MakeUnknown()
185 if !isTypeParam(x.typ) {
188 if t.typeSet().underIs(func(t Type) bool {
189 switch t := arrayPtrDeref(t).(type) {
191 if isString(t) && id == _Len {
194 case *Array, *Slice, *Chan:
208 // avoid error if underlying type is invalid
209 if under(x.typ) != Typ[Invalid] {
214 check.errorf(x, code, invalidArg+"%s for %s", x, bin.name)
219 // record the signature before changing x.typ
220 if check.recordTypes() && mode != constant_ {
221 check.recordBuiltinType(call.Fun, makeSig(Typ[Int], x.typ))
230 check.verifyVersionf(call.Fun, go1_21, "clear")
232 if !underIs(x.typ, func(u Type) bool {
237 check.errorf(x, InvalidClear, invalidArg+"cannot clear %s: argument must be (or constrained by) map or slice", x)
244 if check.recordTypes() {
245 check.recordBuiltinType(call.Fun, makeSig(nil, x.typ))
250 if !underIs(x.typ, func(u Type) bool {
253 check.errorf(x, InvalidClose, invalidOp+"cannot close non-channel %s", x)
256 if uch.dir == RecvOnly {
257 check.errorf(x, InvalidClose, invalidOp+"cannot close receive-only channel %s", x)
265 if check.recordTypes() {
266 check.recordBuiltinType(call.Fun, makeSig(nil, x.typ))
270 // complex(x, y floatT) complexT
273 // convert or check untyped arguments
275 if isUntyped(x.typ) {
278 if isUntyped(y.typ) {
283 // x and y are typed => nothing to do
285 // only x is untyped => convert to type of y
286 check.convertUntyped(x, y.typ)
288 // only y is untyped => convert to type of x
289 check.convertUntyped(y, x.typ)
291 // x and y are untyped =>
292 // 1) if both are constants, convert them to untyped
293 // floating-point numbers if possible,
294 // 2) if one of them is not constant (possible because
295 // it contains a shift that is yet untyped), convert
296 // both of them to float64 since they must have the
297 // same type to succeed (this will result in an error
298 // because shifts of floats are not permitted)
299 if x.mode == constant_ && y.mode == constant_ {
300 toFloat := func(x *operand) {
301 if isNumeric(x.typ) && constant.Sign(constant.Imag(x.val)) == 0 {
302 x.typ = Typ[UntypedFloat]
308 check.convertUntyped(x, Typ[Float64])
309 check.convertUntyped(y, Typ[Float64])
310 // x and y should be invalid now, but be conservative
314 if x.mode == invalid || y.mode == invalid {
318 // both argument types must be identical
319 if !Identical(x.typ, y.typ) {
320 check.errorf(x, InvalidComplex, invalidOp+"%v (mismatched types %s and %s)", call, x.typ, y.typ)
324 // the argument types must be of floating-point type
325 // (applyTypeFunc never calls f with a type parameter)
326 f := func(typ Type) Type {
327 assert(!isTypeParam(typ))
328 if t, _ := under(typ).(*Basic); t != nil {
331 return Typ[Complex64]
333 return Typ[Complex128]
335 return Typ[UntypedComplex]
340 resTyp := check.applyTypeFunc(f, x, id)
342 check.errorf(x, InvalidComplex, invalidArg+"arguments have type %s, expected floating-point", x.typ)
346 // if both arguments are constants, the result is a constant
347 if x.mode == constant_ && y.mode == constant_ {
348 x.val = constant.BinaryOp(constant.ToFloat(x.val), token.ADD, constant.MakeImag(constant.ToFloat(y.val)))
353 if check.recordTypes() && x.mode != constant_ {
354 check.recordBuiltinType(call.Fun, makeSig(resTyp, x.typ, x.typ))
360 // copy(x, y []T) int
361 dst, _ := coreType(x.typ).(*Slice)
364 src0 := coreString(y.typ)
365 if src0 != nil && isString(src0) {
366 src0 = NewSlice(universeByte)
368 src, _ := src0.(*Slice)
370 if dst == nil || src == nil {
371 check.errorf(x, InvalidCopy, invalidArg+"copy expects slice arguments; found %s and %s", x, y)
375 if !Identical(dst.elem, src.elem) {
376 check.errorf(x, InvalidCopy, invalidArg+"arguments to copy %s and %s have different element types %s and %s", x, y, dst.elem, src.elem)
380 if check.recordTypes() {
381 check.recordBuiltinType(call.Fun, makeSig(Typ[Int], x.typ, y.typ))
388 // map_ must be a map type or a type parameter describing map types.
389 // The key cannot be a type parameter for now.
392 if !underIs(map_, func(u Type) bool {
395 check.errorf(x, InvalidDelete, invalidArg+"%s is not a map", x)
398 if key != nil && !Identical(map_.key, key) {
399 check.errorf(x, InvalidDelete, invalidArg+"maps of %s must have identical key types", x)
409 check.assignment(x, key, "argument to delete")
410 if x.mode == invalid {
415 if check.recordTypes() {
416 check.recordBuiltinType(call.Fun, makeSig(nil, map_, key))
420 // imag(complexT) floatT
421 // real(complexT) floatT
423 // convert or check untyped argument
424 if isUntyped(x.typ) {
425 if x.mode == constant_ {
426 // an untyped constant number can always be considered
427 // as a complex constant
428 if isNumeric(x.typ) {
429 x.typ = Typ[UntypedComplex]
432 // an untyped non-constant argument may appear if
433 // it contains a (yet untyped non-constant) shift
434 // expression: convert it to complex128 which will
435 // result in an error (shift of complex value)
436 check.convertUntyped(x, Typ[Complex128])
437 // x should be invalid now, but be conservative and check
438 if x.mode == invalid {
444 // the argument must be of complex type
445 // (applyTypeFunc never calls f with a type parameter)
446 f := func(typ Type) Type {
447 assert(!isTypeParam(typ))
448 if t, _ := under(typ).(*Basic); t != nil {
455 return Typ[UntypedFloat]
460 resTyp := check.applyTypeFunc(f, x, id)
466 check.errorf(x, code, invalidArg+"argument has type %s, expected complex type", x.typ)
470 // if the argument is a constant, the result is a constant
471 if x.mode == constant_ {
473 x.val = constant.Real(x.val)
475 x.val = constant.Imag(x.val)
481 if check.recordTypes() && x.mode != constant_ {
482 check.recordBuiltinType(call.Fun, makeSig(resTyp, x.typ))
490 // (no argument evaluated yet)
492 T := check.varType(arg0)
493 if T == Typ[Invalid] {
497 var min int // minimum number of arguments
498 switch coreType(T).(type) {
504 check.errorf(arg0, InvalidMake, invalidArg+"cannot make %s: no core type", arg0)
507 check.errorf(arg0, InvalidMake, invalidArg+"cannot make %s; type must be slice, map, or channel", arg0)
510 if nargs < min || min+1 < nargs {
511 check.errorf(call, WrongArgCount, invalidOp+"%v expects %d or %d arguments; found %d", call, min, min+1, nargs)
516 var sizes []int64 // constant integer arguments, if any
517 for _, arg := range argList[1:] {
518 typ, size := check.index(arg, -1) // ok to continue with typ == Typ[Invalid]
519 types = append(types, typ)
521 sizes = append(sizes, size)
524 if len(sizes) == 2 && sizes[0] > sizes[1] {
525 check.error(argList[1], SwappedMakeArgs, invalidArg+"length and capacity swapped")
530 if check.recordTypes() {
531 check.recordBuiltinType(call.Fun, makeSig(x.typ, types...))
537 check.verifyVersionf(call.Fun, go1_21, bin.name)
544 for i, a := range args {
545 if a.mode == invalid {
549 if !allOrdered(a.typ) {
550 check.errorf(a, InvalidMinMaxOperand, invalidArg+"%s cannot be ordered", a)
554 // The first argument is already in x and there's nothing left to do.
556 check.matchTypes(x, a)
557 if x.mode == invalid {
561 if !Identical(x.typ, a.typ) {
562 check.errorf(a, MismatchedTypes, invalidArg+"mismatched types %s (previous argument) and %s (type of %s)", x.typ, a.typ, a.expr)
566 if x.mode == constant_ && a.mode == constant_ {
567 if constant.Compare(a.val, op, x.val) {
576 // If nargs == 1, make sure x.mode is either a value or a constant.
577 if x.mode != constant_ {
581 // Use the final type computed above for all arguments.
582 for _, a := range args {
583 check.updateExprType(a.expr, x.typ, true)
586 if check.recordTypes() && x.mode != constant_ {
587 types := make([]Type, nargs)
588 for i := range types {
591 check.recordBuiltinType(call.Fun, makeSig(x.typ, types...))
596 // (no argument evaluated yet)
597 T := check.varType(argList[0])
598 if T == Typ[Invalid] {
603 x.typ = &Pointer{base: T}
604 if check.recordTypes() {
605 check.recordBuiltinType(call.Fun, makeSig(x.typ, T))
610 // record panic call if inside a function with result parameters
611 // (for use in Checker.isTerminating)
612 if check.sig != nil && check.sig.results.Len() > 0 {
613 // function has result parameters
617 p = make(map[*syntax.CallExpr]bool)
623 check.assignment(x, &emptyInterface, "argument to panic")
624 if x.mode == invalid {
629 if check.recordTypes() {
630 check.recordBuiltinType(call.Fun, makeSig(nil, &emptyInterface))
633 case _Print, _Println:
635 // println(x, y, ...)
638 params = make([]Type, nargs)
639 for i, a := range args {
640 check.assignment(a, nil, "argument to "+predeclaredFuncs[id].name)
641 if a.mode == invalid {
649 if check.recordTypes() {
650 check.recordBuiltinType(call.Fun, makeSig(nil, params...))
654 // recover() interface{}
656 x.typ = &emptyInterface
657 if check.recordTypes() {
658 check.recordBuiltinType(call.Fun, makeSig(x.typ))
662 // unsafe.Add(ptr unsafe.Pointer, len IntegerType) unsafe.Pointer
663 check.verifyVersionf(call.Fun, go1_17, "unsafe.Add")
665 check.assignment(x, Typ[UnsafePointer], "argument to unsafe.Add")
666 if x.mode == invalid {
671 if !check.isValidIndex(y, InvalidUnsafeAdd, "length", true) {
676 x.typ = Typ[UnsafePointer]
677 if check.recordTypes() {
678 check.recordBuiltinType(call.Fun, makeSig(x.typ, x.typ, y.typ))
682 // unsafe.Alignof(x T) uintptr
683 check.assignment(x, nil, "argument to unsafe.Alignof")
684 if x.mode == invalid {
688 if hasVarSize(x.typ, nil) {
690 if check.recordTypes() {
691 check.recordBuiltinType(call.Fun, makeSig(Typ[Uintptr], x.typ))
695 x.val = constant.MakeInt64(check.conf.alignof(x.typ))
696 // result is constant - no need to record signature
701 // unsafe.Offsetof(x T) uintptr, where x must be a selector
702 // (no argument evaluated yet)
704 selx, _ := unparen(arg0).(*syntax.SelectorExpr)
706 check.errorf(arg0, BadOffsetofSyntax, invalidArg+"%s is not a selector expression", arg0)
711 check.expr(nil, x, selx.X)
712 if x.mode == invalid {
716 base := derefStructPtr(x.typ)
717 sel := selx.Sel.Value
718 obj, index, indirect := LookupFieldOrMethod(base, false, check.pkg, sel)
721 check.errorf(x, MissingFieldOrMethod, invalidArg+"%s has no single field %s", base, sel)
724 // TODO(gri) Using derefStructPtr may result in methods being found
725 // that don't actually exist. An error either way, but the error
726 // message is confusing. See: https://play.golang.org/p/al75v23kUy ,
727 // but go/types reports: "invalid argument: x.m is a method value".
728 check.errorf(arg0, InvalidOffsetof, invalidArg+"%s is a method value", arg0)
732 check.errorf(x, InvalidOffsetof, invalidArg+"field %s is embedded via a pointer in %s", sel, base)
736 // TODO(gri) Should we pass x.typ instead of base (and have indirect report if derefStructPtr indirected)?
737 check.recordSelection(selx, FieldVal, base, obj, index, false)
739 // record the selector expression (was bug - go.dev/issue/47895)
742 if x.mode == variable || indirect {
745 check.record(&operand{mode, selx, obj.Type(), nil, 0})
748 // The field offset is considered a variable even if the field is declared before
749 // the part of the struct which is variable-sized. This makes both the rules
750 // simpler and also permits (or at least doesn't prevent) a compiler from re-
751 // arranging struct fields if it wanted to.
752 if hasVarSize(base, nil) {
754 if check.recordTypes() {
755 check.recordBuiltinType(call.Fun, makeSig(Typ[Uintptr], obj.Type()))
758 offs := check.conf.offsetof(base, index)
760 check.errorf(x, TypeTooLarge, "%s is too large", x)
764 x.val = constant.MakeInt64(offs)
765 // result is constant - no need to record signature
770 // unsafe.Sizeof(x T) uintptr
771 check.assignment(x, nil, "argument to unsafe.Sizeof")
772 if x.mode == invalid {
776 if hasVarSize(x.typ, nil) {
778 if check.recordTypes() {
779 check.recordBuiltinType(call.Fun, makeSig(Typ[Uintptr], x.typ))
782 size := check.conf.sizeof(x.typ)
784 check.errorf(x, TypeTooLarge, "%s is too large", x)
788 x.val = constant.MakeInt64(size)
789 // result is constant - no need to record signature
794 // unsafe.Slice(ptr *T, len IntegerType) []T
795 check.verifyVersionf(call.Fun, go1_17, "unsafe.Slice")
797 ptr, _ := under(x.typ).(*Pointer) // TODO(gri) should this be coreType rather than under?
799 check.errorf(x, InvalidUnsafeSlice, invalidArg+"%s is not a pointer", x)
804 if !check.isValidIndex(y, InvalidUnsafeSlice, "length", false) {
809 x.typ = NewSlice(ptr.base)
810 if check.recordTypes() {
811 check.recordBuiltinType(call.Fun, makeSig(x.typ, ptr, y.typ))
815 // unsafe.SliceData(slice []T) *T
816 check.verifyVersionf(call.Fun, go1_20, "unsafe.SliceData")
818 slice, _ := under(x.typ).(*Slice) // TODO(gri) should this be coreType rather than under?
820 check.errorf(x, InvalidUnsafeSliceData, invalidArg+"%s is not a slice", x)
825 x.typ = NewPointer(slice.elem)
826 if check.recordTypes() {
827 check.recordBuiltinType(call.Fun, makeSig(x.typ, slice))
831 // unsafe.String(ptr *byte, len IntegerType) string
832 check.verifyVersionf(call.Fun, go1_20, "unsafe.String")
834 check.assignment(x, NewPointer(universeByte), "argument to unsafe.String")
835 if x.mode == invalid {
840 if !check.isValidIndex(y, InvalidUnsafeString, "length", false) {
846 if check.recordTypes() {
847 check.recordBuiltinType(call.Fun, makeSig(x.typ, NewPointer(universeByte), y.typ))
851 // unsafe.StringData(str string) *byte
852 check.verifyVersionf(call.Fun, go1_20, "unsafe.StringData")
854 check.assignment(x, Typ[String], "argument to unsafe.StringData")
855 if x.mode == invalid {
860 x.typ = NewPointer(universeByte)
861 if check.recordTypes() {
862 check.recordBuiltinType(call.Fun, makeSig(x.typ, Typ[String]))
866 // assert(pred) causes a typechecker error if pred is false.
867 // The result of assert is the value of pred if there is no error.
868 // Note: assert is only available in self-test mode.
869 if x.mode != constant_ || !isBoolean(x.typ) {
870 check.errorf(x, Test, invalidArg+"%s is not a boolean constant", x)
873 if x.val.Kind() != constant.Bool {
874 check.errorf(x, Test, "internal error: value of %s should be a boolean constant", x)
877 if !constant.BoolVal(x.val) {
878 check.errorf(call, Test, "%v failed", call)
879 // compile-time assertion failure - safe to continue
881 // result is constant - no need to record signature
884 // trace(x, y, z, ...) dumps the positions, expressions, and
885 // values of its arguments. The result of trace is the value
886 // of the first argument.
887 // Note: trace is only available in self-test mode.
888 // (no argument evaluated yet)
890 check.dump("%v: trace() without arguments", atPos(call))
896 for _, arg := range argList {
897 check.rawExpr(nil, x1, arg, nil, false) // permit trace for types, e.g.: new(trace(T))
898 check.dump("%v: %s", atPos(x1), x1)
899 x1 = &t // use incoming x only for first argument
901 if x.mode == invalid {
904 // trace is only available in test mode - no need to record signature
910 assert(x.mode != invalid)
914 // hasVarSize reports if the size of type t is variable due to type parameters
915 // or if the type is infinitely-sized due to a cycle for which the type has not
917 func hasVarSize(t Type, seen map[*Named]bool) (varSized bool) {
918 // Cycles are only possible through *Named types.
919 // The seen map is used to detect cycles and track
920 // the results of previously seen types.
921 if named, _ := t.(*Named); named != nil {
922 if v, ok := seen[named]; ok {
926 seen = make(map[*Named]bool)
928 seen[named] = true // possibly cyclic until proven otherwise
930 seen[named] = varSized // record final determination for named
934 switch u := under(t).(type) {
936 return hasVarSize(u.elem, seen)
938 for _, f := range u.fields {
939 if hasVarSize(f.typ, seen) {
944 return isTypeParam(t)
951 // applyTypeFunc applies f to x. If x is a type parameter,
952 // the result is a type parameter constrained by an new
953 // interface bound. The type bounds for that interface
954 // are computed by applying f to each of the type bounds
955 // of x. If any of these applications of f return nil,
956 // applyTypeFunc returns nil.
957 // If x is not a type parameter, the result is f(x).
958 func (check *Checker) applyTypeFunc(f func(Type) Type, x *operand, id builtinId) Type {
959 if tp, _ := x.typ.(*TypeParam); tp != nil {
960 // Test if t satisfies the requirements for the argument
961 // type and collect possible result types at the same time.
963 if !tp.is(func(t *term) bool {
967 if r := f(t.typ); r != nil {
968 terms = append(terms, NewTerm(t.tilde, r))
976 // We can type-check this fine but we're introducing a synthetic
977 // type parameter for the result. It's not clear what the API
978 // implications are here. Report an error for 1.18 (see go.dev/issue/50912),
979 // but continue type-checking.
987 code = InvalidComplex
991 check.softErrorf(x, code, "%s not supported as argument to %s for go1.18 (see go.dev/issue/50937)", x, predeclaredFuncs[id].name)
993 // Construct a suitable new type parameter for the result type.
994 // The type parameter is placed in the current package so export/import
995 // works as expected.
996 tpar := NewTypeName(nopos, check.pkg, tp.obj.name, nil)
997 ptyp := check.newTypeParam(tpar, NewInterfaceType(nil, []Type{NewUnion(terms)})) // assigns type to tpar as a side-effect
998 ptyp.index = tp.index
1006 // makeSig makes a signature for the given argument and result types.
1007 // Default types are used for untyped arguments, and res may be nil.
1008 func makeSig(res Type, args ...Type) *Signature {
1009 list := make([]*Var, len(args))
1010 for i, param := range args {
1011 list[i] = NewVar(nopos, nil, "", Default(param))
1013 params := NewTuple(list...)
1016 assert(!isUntyped(res))
1017 result = NewTuple(NewVar(nopos, nil, "", res))
1019 return &Signature{params: params, results: result}
1022 // arrayPtrDeref returns A if typ is of the form *A and A is an array;
1023 // otherwise it returns typ.
1024 func arrayPtrDeref(typ Type) Type {
1025 if p, ok := typ.(*Pointer); ok {
1026 if a, _ := under(p.base).(*Array); a != nil {
1033 // unparen returns e with any enclosing parentheses stripped.
1034 func unparen(e syntax.Expr) syntax.Expr {
1036 p, ok := e.(*syntax.ParenExpr)