Source file
src/go/types/builtins.go
1
2
3
4
5
6
7
8
9
10 package types
11
12 import (
13 "go/ast"
14 "go/constant"
15 "go/token"
16 . "internal/types/errors"
17 )
18
19
20
21
22
23 func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ bool) {
24 argList := call.Args
25
26
27 bin := predeclaredFuncs[id]
28 if hasDots(call) && id != _Append {
29 check.errorf(dddErrPos(call),
30 InvalidDotDotDot,
31 invalidOp+"invalid use of ... with built-in %s", bin.name)
32 check.use(argList...)
33 return
34 }
35
36
37
38
39
40
41 if id == _Len || id == _Cap {
42 defer func(b bool) {
43 check.hasCallOrRecv = b
44 }(check.hasCallOrRecv)
45 check.hasCallOrRecv = false
46 }
47
48
49
50
51 var args []*operand
52 var nargs int
53 switch id {
54 default:
55
56 args = check.exprList(argList)
57 nargs = len(args)
58 for _, a := range args {
59 if a.mode == invalid {
60 return
61 }
62 }
63
64 if nargs > 0 {
65 *x = *args[0]
66 }
67 case _Make, _New, _Offsetof, _Trace:
68
69 nargs = len(argList)
70 }
71
72
73 {
74 msg := ""
75 if nargs < bin.nargs {
76 msg = "not enough"
77 } else if !bin.variadic && nargs > bin.nargs {
78 msg = "too many"
79 }
80 if msg != "" {
81 check.errorf(argErrPos(call), WrongArgCount, invalidOp+"%s arguments for %v (expected %d, found %d)", msg, call, bin.nargs, nargs)
82 return
83 }
84 }
85
86 switch id {
87 case _Append:
88
89
90
91
92
93
94
95
96
97
98
99 var sig *Signature
100 if nargs == 2 && hasDots(call) {
101 if ok, _ := x.assignableTo(check, NewSlice(universeByte), nil); ok {
102 y := args[1]
103 hasString := false
104 typeset(y.typ, func(_, u Type) bool {
105 if s, _ := u.(*Slice); s != nil && Identical(s.elem, universeByte) {
106 return true
107 }
108 if isString(u) {
109 hasString = true
110 return true
111 }
112 y = nil
113 return false
114 })
115 if y != nil && hasString {
116
117 sig = makeSig(x.typ, x.typ, y.typ)
118 sig.variadic = true
119 }
120 }
121 }
122
123
124 if sig == nil {
125
126
127 E, err := sliceElem(x)
128 if err != nil {
129 check.errorf(x, InvalidAppend, "invalid append: %s", err.format(check))
130 return
131 }
132
133 sig = makeSig(x.typ, x.typ, NewSlice(E))
134 sig.variadic = true
135 check.arguments(call, sig, nil, nil, args, nil)
136
137 }
138
139 if check.recordTypes() {
140 check.recordBuiltinType(call.Fun, sig)
141 }
142 x.mode = value
143
144
145 case _Cap, _Len:
146
147
148 mode := invalid
149 var val constant.Value
150 switch t := arrayPtrDeref(under(x.typ)).(type) {
151 case *Basic:
152 if isString(t) && id == _Len {
153 if x.mode == constant_ {
154 mode = constant_
155 val = constant.MakeInt64(int64(len(constant.StringVal(x.val))))
156 } else {
157 mode = value
158 }
159 }
160
161 case *Array:
162 mode = value
163
164
165
166
167 if !check.hasCallOrRecv {
168 mode = constant_
169 if t.len >= 0 {
170 val = constant.MakeInt64(t.len)
171 } else {
172 val = constant.MakeUnknown()
173 }
174 }
175
176 case *Slice, *Chan:
177 mode = value
178
179 case *Map:
180 if id == _Len {
181 mode = value
182 }
183
184 case *Interface:
185 if !isTypeParam(x.typ) {
186 break
187 }
188 if underIs(x.typ, func(u Type) bool {
189 switch t := arrayPtrDeref(u).(type) {
190 case *Basic:
191 if isString(t) && id == _Len {
192 return true
193 }
194 case *Array, *Slice, *Chan:
195 return true
196 case *Map:
197 if id == _Len {
198 return true
199 }
200 }
201 return false
202 }) {
203 mode = value
204 }
205 }
206
207 if mode == invalid {
208
209 if isValid(under(x.typ)) {
210 code := InvalidCap
211 if id == _Len {
212 code = InvalidLen
213 }
214 check.errorf(x, code, invalidArg+"%s for built-in %s", x, bin.name)
215 }
216 return
217 }
218
219
220 if check.recordTypes() && mode != constant_ {
221 check.recordBuiltinType(call.Fun, makeSig(Typ[Int], x.typ))
222 }
223
224 x.mode = mode
225 x.typ = Typ[Int]
226 x.val = val
227
228 case _Clear:
229
230 check.verifyVersionf(call.Fun, go1_21, "clear")
231
232 if !underIs(x.typ, func(u Type) bool {
233 switch u.(type) {
234 case *Map, *Slice:
235 return true
236 }
237 check.errorf(x, InvalidClear, invalidArg+"cannot clear %s: argument must be (or constrained by) map or slice", x)
238 return false
239 }) {
240 return
241 }
242
243 x.mode = novalue
244 if check.recordTypes() {
245 check.recordBuiltinType(call.Fun, makeSig(nil, x.typ))
246 }
247
248 case _Close:
249
250 if !underIs(x.typ, func(u Type) bool {
251 uch, _ := u.(*Chan)
252 if uch == nil {
253 check.errorf(x, InvalidClose, invalidOp+"cannot close non-channel %s", x)
254 return false
255 }
256 if uch.dir == RecvOnly {
257 check.errorf(x, InvalidClose, invalidOp+"cannot close receive-only channel %s", x)
258 return false
259 }
260 return true
261 }) {
262 return
263 }
264 x.mode = novalue
265 if check.recordTypes() {
266 check.recordBuiltinType(call.Fun, makeSig(nil, x.typ))
267 }
268
269 case _Complex:
270
271 y := args[1]
272
273
274 d := 0
275 if isUntyped(x.typ) {
276 d |= 1
277 }
278 if isUntyped(y.typ) {
279 d |= 2
280 }
281 switch d {
282 case 0:
283
284 case 1:
285
286 check.convertUntyped(x, y.typ)
287 case 2:
288
289 check.convertUntyped(y, x.typ)
290 case 3:
291
292
293
294
295
296
297
298
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]
303 }
304 }
305 toFloat(x)
306 toFloat(y)
307 } else {
308 check.convertUntyped(x, Typ[Float64])
309 check.convertUntyped(y, Typ[Float64])
310
311
312 }
313 }
314 if x.mode == invalid || y.mode == invalid {
315 return
316 }
317
318
319 if !Identical(x.typ, y.typ) {
320 check.errorf(x, InvalidComplex, invalidOp+"%v (mismatched types %s and %s)", call, x.typ, y.typ)
321 return
322 }
323
324
325
326 f := func(typ Type) Type {
327 assert(!isTypeParam(typ))
328 if t, _ := under(typ).(*Basic); t != nil {
329 switch t.kind {
330 case Float32:
331 return Typ[Complex64]
332 case Float64:
333 return Typ[Complex128]
334 case UntypedFloat:
335 return Typ[UntypedComplex]
336 }
337 }
338 return nil
339 }
340 resTyp := check.applyTypeFunc(f, x, id)
341 if resTyp == nil {
342 check.errorf(x, InvalidComplex, invalidArg+"arguments have type %s, expected floating-point", x.typ)
343 return
344 }
345
346
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)))
349 } else {
350 x.mode = value
351 }
352
353 if check.recordTypes() && x.mode != constant_ {
354 check.recordBuiltinType(call.Fun, makeSig(resTyp, x.typ, x.typ))
355 }
356
357 x.typ = resTyp
358
359 case _Copy:
360
361
362
363
364
365
366
367
368
369
370 y := args[1]
371 var special bool
372 if ok, _ := x.assignableTo(check, NewSlice(universeByte), nil); ok {
373 special = true
374 typeset(y.typ, func(_, u Type) bool {
375 if s, _ := u.(*Slice); s != nil && Identical(s.elem, universeByte) {
376 return true
377 }
378 if isString(u) {
379 return true
380 }
381 special = false
382 return false
383 })
384 }
385
386
387 if !special {
388
389
390 dstE, err := sliceElem(x)
391 if err != nil {
392 check.errorf(x, InvalidCopy, "invalid copy: %s", err.format(check))
393 return
394 }
395 srcE, err := sliceElem(y)
396 if err != nil {
397
398 if !allString(y.typ) {
399 check.errorf(y, InvalidCopy, "invalid copy: %s", err.format(check))
400 return
401 }
402 srcE = universeByte
403 }
404 if !Identical(dstE, srcE) {
405 check.errorf(x, InvalidCopy, "invalid copy: arguments %s and %s have different element types %s and %s", x, y, dstE, srcE)
406 return
407 }
408 }
409
410 if check.recordTypes() {
411 check.recordBuiltinType(call.Fun, makeSig(Typ[Int], x.typ, y.typ))
412 }
413 x.mode = value
414 x.typ = Typ[Int]
415
416 case _Delete:
417
418
419
420 map_ := x.typ
421 var key Type
422 if !underIs(map_, func(u Type) bool {
423 map_, _ := u.(*Map)
424 if map_ == nil {
425 check.errorf(x, InvalidDelete, invalidArg+"%s is not a map", x)
426 return false
427 }
428 if key != nil && !Identical(map_.key, key) {
429 check.errorf(x, InvalidDelete, invalidArg+"maps of %s must have identical key types", x)
430 return false
431 }
432 key = map_.key
433 return true
434 }) {
435 return
436 }
437
438 *x = *args[1]
439 check.assignment(x, key, "argument to delete")
440 if x.mode == invalid {
441 return
442 }
443
444 x.mode = novalue
445 if check.recordTypes() {
446 check.recordBuiltinType(call.Fun, makeSig(nil, map_, key))
447 }
448
449 case _Imag, _Real:
450
451
452
453
454 if isUntyped(x.typ) {
455 if x.mode == constant_ {
456
457
458 if isNumeric(x.typ) {
459 x.typ = Typ[UntypedComplex]
460 }
461 } else {
462
463
464
465
466 check.convertUntyped(x, Typ[Complex128])
467
468 if x.mode == invalid {
469 return
470 }
471 }
472 }
473
474
475
476 f := func(typ Type) Type {
477 assert(!isTypeParam(typ))
478 if t, _ := under(typ).(*Basic); t != nil {
479 switch t.kind {
480 case Complex64:
481 return Typ[Float32]
482 case Complex128:
483 return Typ[Float64]
484 case UntypedComplex:
485 return Typ[UntypedFloat]
486 }
487 }
488 return nil
489 }
490 resTyp := check.applyTypeFunc(f, x, id)
491 if resTyp == nil {
492 code := InvalidImag
493 if id == _Real {
494 code = InvalidReal
495 }
496 check.errorf(x, code, invalidArg+"argument has type %s, expected complex type", x.typ)
497 return
498 }
499
500
501 if x.mode == constant_ {
502 if id == _Real {
503 x.val = constant.Real(x.val)
504 } else {
505 x.val = constant.Imag(x.val)
506 }
507 } else {
508 x.mode = value
509 }
510
511 if check.recordTypes() && x.mode != constant_ {
512 check.recordBuiltinType(call.Fun, makeSig(resTyp, x.typ))
513 }
514
515 x.typ = resTyp
516
517 case _Make:
518
519
520
521 arg0 := argList[0]
522 T := check.varType(arg0)
523 if !isValid(T) {
524 return
525 }
526
527 u, err := commonUnder(T, func(_, u Type) *typeError {
528 switch u.(type) {
529 case *Slice, *Map, *Chan:
530 return nil
531 case nil:
532 return typeErrorf("no specific type")
533 default:
534 return typeErrorf("type must be slice, map, or channel")
535 }
536 })
537 if err != nil {
538 check.errorf(arg0, InvalidMake, invalidArg+"cannot make %s: %s", arg0, err.format(check))
539 return
540 }
541
542 var min int
543 switch u.(type) {
544 case *Slice:
545 min = 2
546 case *Map, *Chan:
547 min = 1
548 default:
549
550 panic("unreachable")
551 }
552 if nargs < min || min+1 < nargs {
553 check.errorf(call, WrongArgCount, invalidOp+"%v expects %d or %d arguments; found %d", call, min, min+1, nargs)
554 return
555 }
556
557 types := []Type{T}
558 var sizes []int64
559 for _, arg := range argList[1:] {
560 typ, size := check.index(arg, -1)
561 types = append(types, typ)
562 if size >= 0 {
563 sizes = append(sizes, size)
564 }
565 }
566 if len(sizes) == 2 && sizes[0] > sizes[1] {
567 check.error(argList[1], SwappedMakeArgs, invalidArg+"length and capacity swapped")
568
569 }
570 x.mode = value
571 x.typ = T
572 if check.recordTypes() {
573 check.recordBuiltinType(call.Fun, makeSig(x.typ, types...))
574 }
575
576 case _Max, _Min:
577
578
579 check.verifyVersionf(call.Fun, go1_21, "built-in %s", bin.name)
580
581 op := token.LSS
582 if id == _Max {
583 op = token.GTR
584 }
585
586 for i, a := range args {
587 if a.mode == invalid {
588 return
589 }
590
591 if !allOrdered(a.typ) {
592 check.errorf(a, InvalidMinMaxOperand, invalidArg+"%s cannot be ordered", a)
593 return
594 }
595
596
597 if i > 0 {
598 check.matchTypes(x, a)
599 if x.mode == invalid {
600 return
601 }
602
603 if !Identical(x.typ, a.typ) {
604 check.errorf(a, MismatchedTypes, invalidArg+"mismatched types %s (previous argument) and %s (type of %s)", x.typ, a.typ, a.expr)
605 return
606 }
607
608 if x.mode == constant_ && a.mode == constant_ {
609 if constant.Compare(a.val, op, x.val) {
610 *x = *a
611 }
612 } else {
613 x.mode = value
614 }
615 }
616 }
617
618
619 if x.mode != constant_ {
620 x.mode = value
621
622 check.assignment(x, &emptyInterface, "argument to built-in "+bin.name)
623 if x.mode == invalid {
624 return
625 }
626 }
627
628
629 for _, a := range args {
630 check.updateExprType(a.expr, x.typ, true)
631 }
632
633 if check.recordTypes() && x.mode != constant_ {
634 types := make([]Type, nargs)
635 for i := range types {
636 types[i] = x.typ
637 }
638 check.recordBuiltinType(call.Fun, makeSig(x.typ, types...))
639 }
640
641 case _New:
642
643
644 T := check.varType(argList[0])
645 if !isValid(T) {
646 return
647 }
648
649 x.mode = value
650 x.typ = &Pointer{base: T}
651 if check.recordTypes() {
652 check.recordBuiltinType(call.Fun, makeSig(x.typ, T))
653 }
654
655 case _Panic:
656
657
658
659 if check.sig != nil && check.sig.results.Len() > 0 {
660
661 p := check.isPanic
662 if p == nil {
663
664 p = make(map[*ast.CallExpr]bool)
665 check.isPanic = p
666 }
667 p[call] = true
668 }
669
670 check.assignment(x, &emptyInterface, "argument to panic")
671 if x.mode == invalid {
672 return
673 }
674
675 x.mode = novalue
676 if check.recordTypes() {
677 check.recordBuiltinType(call.Fun, makeSig(nil, &emptyInterface))
678 }
679
680 case _Print, _Println:
681
682
683 var params []Type
684 if nargs > 0 {
685 params = make([]Type, nargs)
686 for i, a := range args {
687 check.assignment(a, nil, "argument to built-in "+predeclaredFuncs[id].name)
688 if a.mode == invalid {
689 return
690 }
691 params[i] = a.typ
692 }
693 }
694
695 x.mode = novalue
696 if check.recordTypes() {
697 check.recordBuiltinType(call.Fun, makeSig(nil, params...))
698 }
699
700 case _Recover:
701
702 x.mode = value
703 x.typ = &emptyInterface
704 if check.recordTypes() {
705 check.recordBuiltinType(call.Fun, makeSig(x.typ))
706 }
707
708 case _Add:
709
710 check.verifyVersionf(call.Fun, go1_17, "unsafe.Add")
711
712 check.assignment(x, Typ[UnsafePointer], "argument to unsafe.Add")
713 if x.mode == invalid {
714 return
715 }
716
717 y := args[1]
718 if !check.isValidIndex(y, InvalidUnsafeAdd, "length", true) {
719 return
720 }
721
722 x.mode = value
723 x.typ = Typ[UnsafePointer]
724 if check.recordTypes() {
725 check.recordBuiltinType(call.Fun, makeSig(x.typ, x.typ, y.typ))
726 }
727
728 case _Alignof:
729
730 check.assignment(x, nil, "argument to unsafe.Alignof")
731 if x.mode == invalid {
732 return
733 }
734
735 if hasVarSize(x.typ, nil) {
736 x.mode = value
737 if check.recordTypes() {
738 check.recordBuiltinType(call.Fun, makeSig(Typ[Uintptr], x.typ))
739 }
740 } else {
741 x.mode = constant_
742 x.val = constant.MakeInt64(check.conf.alignof(x.typ))
743
744 }
745 x.typ = Typ[Uintptr]
746
747 case _Offsetof:
748
749
750 arg0 := argList[0]
751 selx, _ := ast.Unparen(arg0).(*ast.SelectorExpr)
752 if selx == nil {
753 check.errorf(arg0, BadOffsetofSyntax, invalidArg+"%s is not a selector expression", arg0)
754 check.use(arg0)
755 return
756 }
757
758 check.expr(nil, x, selx.X)
759 if x.mode == invalid {
760 return
761 }
762
763 base := derefStructPtr(x.typ)
764 sel := selx.Sel.Name
765 obj, index, indirect := lookupFieldOrMethod(base, false, check.pkg, sel, false)
766 switch obj.(type) {
767 case nil:
768 check.errorf(x, MissingFieldOrMethod, invalidArg+"%s has no single field %s", base, sel)
769 return
770 case *Func:
771
772
773
774
775 check.errorf(arg0, InvalidOffsetof, invalidArg+"%s is a method value", arg0)
776 return
777 }
778 if indirect {
779 check.errorf(x, InvalidOffsetof, invalidArg+"field %s is embedded via a pointer in %s", sel, base)
780 return
781 }
782
783
784 check.recordSelection(selx, FieldVal, base, obj, index, false)
785
786
787 {
788 mode := value
789 if x.mode == variable || indirect {
790 mode = variable
791 }
792 check.record(&operand{mode, selx, obj.Type(), nil, 0})
793 }
794
795
796
797
798
799 if hasVarSize(base, nil) {
800 x.mode = value
801 if check.recordTypes() {
802 check.recordBuiltinType(call.Fun, makeSig(Typ[Uintptr], obj.Type()))
803 }
804 } else {
805 offs := check.conf.offsetof(base, index)
806 if offs < 0 {
807 check.errorf(x, TypeTooLarge, "%s is too large", x)
808 return
809 }
810 x.mode = constant_
811 x.val = constant.MakeInt64(offs)
812
813 }
814 x.typ = Typ[Uintptr]
815
816 case _Sizeof:
817
818 check.assignment(x, nil, "argument to unsafe.Sizeof")
819 if x.mode == invalid {
820 return
821 }
822
823 if hasVarSize(x.typ, nil) {
824 x.mode = value
825 if check.recordTypes() {
826 check.recordBuiltinType(call.Fun, makeSig(Typ[Uintptr], x.typ))
827 }
828 } else {
829 size := check.conf.sizeof(x.typ)
830 if size < 0 {
831 check.errorf(x, TypeTooLarge, "%s is too large", x)
832 return
833 }
834 x.mode = constant_
835 x.val = constant.MakeInt64(size)
836
837 }
838 x.typ = Typ[Uintptr]
839
840 case _Slice:
841
842 check.verifyVersionf(call.Fun, go1_17, "unsafe.Slice")
843
844 u, _ := commonUnder(x.typ, nil)
845 ptr, _ := u.(*Pointer)
846 if ptr == nil {
847 check.errorf(x, InvalidUnsafeSlice, invalidArg+"%s is not a pointer", x)
848 return
849 }
850
851 y := args[1]
852 if !check.isValidIndex(y, InvalidUnsafeSlice, "length", false) {
853 return
854 }
855
856 x.mode = value
857 x.typ = NewSlice(ptr.base)
858 if check.recordTypes() {
859 check.recordBuiltinType(call.Fun, makeSig(x.typ, ptr, y.typ))
860 }
861
862 case _SliceData:
863
864 check.verifyVersionf(call.Fun, go1_20, "unsafe.SliceData")
865
866 u, _ := commonUnder(x.typ, nil)
867 slice, _ := u.(*Slice)
868 if slice == nil {
869 check.errorf(x, InvalidUnsafeSliceData, invalidArg+"%s is not a slice", x)
870 return
871 }
872
873 x.mode = value
874 x.typ = NewPointer(slice.elem)
875 if check.recordTypes() {
876 check.recordBuiltinType(call.Fun, makeSig(x.typ, slice))
877 }
878
879 case _String:
880
881 check.verifyVersionf(call.Fun, go1_20, "unsafe.String")
882
883 check.assignment(x, NewPointer(universeByte), "argument to unsafe.String")
884 if x.mode == invalid {
885 return
886 }
887
888 y := args[1]
889 if !check.isValidIndex(y, InvalidUnsafeString, "length", false) {
890 return
891 }
892
893 x.mode = value
894 x.typ = Typ[String]
895 if check.recordTypes() {
896 check.recordBuiltinType(call.Fun, makeSig(x.typ, NewPointer(universeByte), y.typ))
897 }
898
899 case _StringData:
900
901 check.verifyVersionf(call.Fun, go1_20, "unsafe.StringData")
902
903 check.assignment(x, Typ[String], "argument to unsafe.StringData")
904 if x.mode == invalid {
905 return
906 }
907
908 x.mode = value
909 x.typ = NewPointer(universeByte)
910 if check.recordTypes() {
911 check.recordBuiltinType(call.Fun, makeSig(x.typ, Typ[String]))
912 }
913
914 case _Assert:
915
916
917
918 if x.mode != constant_ || !isBoolean(x.typ) {
919 check.errorf(x, Test, invalidArg+"%s is not a boolean constant", x)
920 return
921 }
922 if x.val.Kind() != constant.Bool {
923 check.errorf(x, Test, "internal error: value of %s should be a boolean constant", x)
924 return
925 }
926 if !constant.BoolVal(x.val) {
927 check.errorf(call, Test, "%v failed", call)
928
929 }
930
931
932 case _Trace:
933
934
935
936
937
938 if nargs == 0 {
939 check.dump("%v: trace() without arguments", call.Pos())
940 x.mode = novalue
941 break
942 }
943 var t operand
944 x1 := x
945 for _, arg := range argList {
946 check.rawExpr(nil, x1, arg, nil, false)
947 check.dump("%v: %s", x1.Pos(), x1)
948 x1 = &t
949 }
950 if x.mode == invalid {
951 return
952 }
953
954
955 default:
956 panic("unreachable")
957 }
958
959 assert(x.mode != invalid)
960 return true
961 }
962
963
964
965 func sliceElem(x *operand) (Type, *typeError) {
966 var E Type
967 var err *typeError
968 typeset(x.typ, func(_, u Type) bool {
969 s, _ := u.(*Slice)
970 if s == nil {
971 if x.isNil() {
972
973
974 err = typeErrorf("argument must be a slice; have untyped nil")
975 } else {
976 err = typeErrorf("argument must be a slice; have %s", x)
977 }
978 return false
979 }
980 if E == nil {
981 E = s.elem
982 } else if !Identical(E, s.elem) {
983 err = typeErrorf("mismatched slice element types %s and %s in %s", E, s.elem, x)
984 return false
985 }
986 return true
987 })
988 if err != nil {
989 return nil, err
990 }
991 return E, nil
992 }
993
994
995
996
997 func hasVarSize(t Type, seen map[*Named]bool) (varSized bool) {
998
999
1000
1001 if named := asNamed(t); named != nil {
1002 if v, ok := seen[named]; ok {
1003 return v
1004 }
1005 if seen == nil {
1006 seen = make(map[*Named]bool)
1007 }
1008 seen[named] = true
1009 defer func() {
1010 seen[named] = varSized
1011 }()
1012 }
1013
1014 switch u := under(t).(type) {
1015 case *Array:
1016 return hasVarSize(u.elem, seen)
1017 case *Struct:
1018 for _, f := range u.fields {
1019 if hasVarSize(f.typ, seen) {
1020 return true
1021 }
1022 }
1023 case *Interface:
1024 return isTypeParam(t)
1025 case *Named, *Union:
1026 panic("unreachable")
1027 }
1028 return false
1029 }
1030
1031
1032
1033
1034
1035
1036
1037
1038 func (check *Checker) applyTypeFunc(f func(Type) Type, x *operand, id builtinId) Type {
1039 if tp, _ := Unalias(x.typ).(*TypeParam); tp != nil {
1040
1041
1042 var terms []*Term
1043 if !tp.is(func(t *term) bool {
1044 if t == nil {
1045 return false
1046 }
1047 if r := f(t.typ); r != nil {
1048 terms = append(terms, NewTerm(t.tilde, r))
1049 return true
1050 }
1051 return false
1052 }) {
1053 return nil
1054 }
1055
1056
1057
1058
1059
1060 var code Code
1061 switch id {
1062 case _Real:
1063 code = InvalidReal
1064 case _Imag:
1065 code = InvalidImag
1066 case _Complex:
1067 code = InvalidComplex
1068 default:
1069 panic("unreachable")
1070 }
1071 check.softErrorf(x, code, "%s not supported as argument to built-in %s for go1.18 (see go.dev/issue/50937)", x, predeclaredFuncs[id].name)
1072
1073
1074
1075
1076 tpar := NewTypeName(nopos, check.pkg, tp.obj.name, nil)
1077 ptyp := check.newTypeParam(tpar, NewInterfaceType(nil, []Type{NewUnion(terms)}))
1078 ptyp.index = tp.index
1079
1080 return ptyp
1081 }
1082
1083 return f(x.typ)
1084 }
1085
1086
1087
1088 func makeSig(res Type, args ...Type) *Signature {
1089 list := make([]*Var, len(args))
1090 for i, param := range args {
1091 list[i] = NewParam(nopos, nil, "", Default(param))
1092 }
1093 params := NewTuple(list...)
1094 var result *Tuple
1095 if res != nil {
1096 assert(!isUntyped(res))
1097 result = NewTuple(newVar(ResultVar, nopos, nil, "", res))
1098 }
1099 return &Signature{params: params, results: result}
1100 }
1101
1102
1103
1104 func arrayPtrDeref(typ Type) Type {
1105 if p, ok := Unalias(typ).(*Pointer); ok {
1106 if a, _ := under(p.base).(*Array); a != nil {
1107 return a
1108 }
1109 }
1110 return typ
1111 }
1112
View as plain text