1
2
3
4
5
6
7 package types2
8
9 import (
10 "cmd/compile/internal/syntax"
11 "fmt"
12 "go/constant"
13 "go/token"
14 . "internal/types/errors"
15 )
16
17
58
59 type opPredicates map[syntax.Operator]func(Type) bool
60
61 var unaryOpPredicates opPredicates
62
63 func init() {
64
65 unaryOpPredicates = opPredicates{
66 syntax.Add: allNumeric,
67 syntax.Sub: allNumeric,
68 syntax.Xor: allInteger,
69 syntax.Not: allBoolean,
70 }
71 }
72
73 func (check *Checker) op(m opPredicates, x *operand, op syntax.Operator) bool {
74 if pred := m[op]; pred != nil {
75 if !pred(x.typ()) {
76 check.errorf(x, UndefinedOp, invalidOp+"operator %s not defined on %s", op, x)
77 return false
78 }
79 } else {
80 check.errorf(x, InvalidSyntaxTree, "unknown operator %s", op)
81 return false
82 }
83 return true
84 }
85
86
87
88 func opPos(x syntax.Expr) syntax.Pos {
89 switch op := x.(type) {
90 case nil:
91 return nopos
92 case *syntax.Operation:
93 return op.Pos()
94 default:
95 return syntax.StartPos(x)
96 }
97 }
98
99
100
101 func opName(x syntax.Expr) string {
102 if e, _ := x.(*syntax.Operation); e != nil {
103 op := int(e.Op)
104 if e.Y == nil {
105 if op < len(op2str1) {
106 return op2str1[op]
107 }
108 } else {
109 if op < len(op2str2) {
110 return op2str2[op]
111 }
112 }
113 }
114 return ""
115 }
116
117 var op2str1 = [...]string{
118 syntax.Xor: "bitwise complement",
119 }
120
121
122 var op2str2 = [...]string{
123 syntax.Add: "addition",
124 syntax.Sub: "subtraction",
125 syntax.Xor: "bitwise XOR",
126 syntax.Mul: "multiplication",
127 syntax.Shl: "shift",
128 }
129
130 func (check *Checker) unary(x *operand, e *syntax.Operation) {
131 check.expr(nil, x, e.X)
132 if !x.isValid() {
133 return
134 }
135
136 op := e.Op
137 switch op {
138 case syntax.And:
139
140
141 if _, ok := syntax.Unparen(e.X).(*syntax.CompositeLit); !ok && x.mode() != variable {
142 check.errorf(x, UnaddressableOperand, invalidOp+"cannot take address of %s", x)
143 x.invalidate()
144 return
145 }
146 x.mode_ = value
147 x.typ_ = &Pointer{base: x.typ()}
148 return
149
150 case syntax.Recv:
151
152 if elem := check.chanElem(x, x, true); elem != nil && check.isComplete(elem) {
153 x.mode_ = commaok
154 x.typ_ = elem
155 check.hasCallOrRecv = true
156 return
157 }
158 x.invalidate()
159 return
160
161 case syntax.Tilde:
162
163 if !allInteger(x.typ()) {
164 check.error(e, UndefinedOp, "cannot use ~ outside of interface or type constraint")
165 x.invalidate()
166 return
167 }
168 check.error(e, UndefinedOp, "cannot use ~ outside of interface or type constraint (use ^ for bitwise complement)")
169 op = syntax.Xor
170 }
171
172 if !check.op(unaryOpPredicates, x, op) {
173 x.invalidate()
174 return
175 }
176
177 if x.mode() == constant_ {
178 if x.val.Kind() == constant.Unknown {
179
180 return
181 }
182 var prec uint
183 if isUnsigned(x.typ()) {
184 prec = uint(check.conf.sizeof(x.typ()) * 8)
185 }
186 x.val = constant.UnaryOp(op2tok[op], x.val, prec)
187 x.expr = e
188 check.overflow(x, opPos(x.expr))
189 return
190 }
191
192 x.mode_ = value
193
194 }
195
196
197
198
199 func (check *Checker) chanElem(pos poser, x *operand, recv bool) Type {
200 u, err := commonUnder(x.typ(), func(t, u Type) *typeError {
201 if u == nil {
202 return typeErrorf("no specific channel type")
203 }
204 ch, _ := u.(*Chan)
205 if ch == nil {
206 return typeErrorf("non-channel %s", t)
207 }
208 if recv && ch.dir == SendOnly {
209 return typeErrorf("send-only channel %s", t)
210 }
211 if !recv && ch.dir == RecvOnly {
212 return typeErrorf("receive-only channel %s", t)
213 }
214 return nil
215 })
216
217 if u != nil {
218 return u.(*Chan).elem
219 }
220
221 cause := err.format(check)
222 if recv {
223 if isTypeParam(x.typ()) {
224 check.errorf(pos, InvalidReceive, invalidOp+"cannot receive from %s: %s", x, cause)
225 } else {
226
227 check.errorf(pos, InvalidReceive, invalidOp+"cannot receive from %s %s", cause, x)
228 }
229 } else {
230 if isTypeParam(x.typ()) {
231 check.errorf(pos, InvalidSend, invalidOp+"cannot send to %s: %s", x, cause)
232 } else {
233
234 check.errorf(pos, InvalidSend, invalidOp+"cannot send to %s %s", cause, x)
235 }
236 }
237 return nil
238 }
239
240 func isShift(op syntax.Operator) bool {
241 return op == syntax.Shl || op == syntax.Shr
242 }
243
244 func isComparison(op syntax.Operator) bool {
245
246 switch op {
247 case syntax.Eql, syntax.Neq, syntax.Lss, syntax.Leq, syntax.Gtr, syntax.Geq:
248 return true
249 }
250 return false
251 }
252
253
254
255
256
257
258
259
260
261
262 func (check *Checker) updateExprType(x syntax.Expr, typ Type, final bool) {
263 old, found := check.untyped[x]
264 if !found {
265 return
266 }
267
268
269 switch x := x.(type) {
270 case *syntax.BadExpr,
271 *syntax.FuncLit,
272 *syntax.CompositeLit,
273 *syntax.IndexExpr,
274 *syntax.SliceExpr,
275 *syntax.AssertExpr,
276 *syntax.ListExpr,
277
278 *syntax.KeyValueExpr,
279 *syntax.ArrayType,
280 *syntax.StructType,
281 *syntax.FuncType,
282 *syntax.InterfaceType,
283 *syntax.MapType,
284 *syntax.ChanType:
285
286
287
288 if debug {
289 check.dump("%v: found old type(%s): %s (new: %s)", atPos(x), x, old.typ, typ)
290 panic("unreachable")
291 }
292 return
293
294 case *syntax.CallExpr:
295
296
297
298
299 case *syntax.Name, *syntax.BasicLit, *syntax.SelectorExpr:
300
301
302
303
304 case *syntax.ParenExpr:
305 check.updateExprType(x.X, typ, final)
306
307
308
309
310
311
312
313
314
315
316
317
318 case *syntax.Operation:
319 if x.Y == nil {
320
321 if x.Op == syntax.Mul {
322
323
324 if debug {
325 panic("unimplemented")
326 }
327 return
328 }
329
330
331
332
333
334 if old.val != nil {
335 break
336 }
337 check.updateExprType(x.X, typ, final)
338 break
339 }
340
341
342 if old.val != nil {
343 break
344 }
345 if isComparison(x.Op) {
346
347
348 } else if isShift(x.Op) {
349
350
351 check.updateExprType(x.X, typ, final)
352 } else {
353
354 check.updateExprType(x.X, typ, final)
355 check.updateExprType(x.Y, typ, final)
356 }
357
358 default:
359 panic("unreachable")
360 }
361
362
363
364 if !final && isUntyped(typ) {
365 old.typ = typ.Underlying().(*Basic)
366 check.untyped[x] = old
367 return
368 }
369
370
371
372 delete(check.untyped, x)
373
374 if old.isLhs {
375
376
377
378 if !allInteger(typ) {
379 check.errorf(x, InvalidShiftOperand, invalidOp+"shifted operand %s (type %s) must be integer", x, typ)
380 return
381 }
382
383
384
385 }
386
387
388
389
390
391
392
393
394 if _, isParen := x.(*syntax.ParenExpr); !isParen && old.val != nil {
395
396 c := operand{old.mode, x, old.typ, old.val, 0}
397 check.convertUntyped(&c, typ)
398 if !c.isValid() {
399 return
400 }
401 }
402
403
404 check.recordTypeAndValue(x, old.mode, typ, old.val)
405 }
406
407
408 func (check *Checker) updateExprVal(x syntax.Expr, val constant.Value) {
409 if info, ok := check.untyped[x]; ok {
410 info.val = val
411 check.untyped[x] = info
412 }
413 }
414
415
416
417
418
419
420
421 func (check *Checker) implicitTypeAndValue(x *operand, target Type) (Type, constant.Value, Code) {
422 if !x.isValid() || x.isTyped() || !isValid(target) {
423 return x.typ(), nil, 0
424 }
425
426
427 if isUntyped(target) {
428
429 if m := maxType(x.typ(), target); m != nil {
430 return m, nil, 0
431 }
432 return nil, nil, InvalidUntypedConversion
433 }
434
435 if x.isNil() {
436 assert(x.isUntyped())
437 if hasNil(target) {
438 if isTypes2 {
439 return target, nil, 0
440 }
441
442 return Typ[UntypedNil], nil, 0
443 }
444 return nil, nil, InvalidUntypedConversion
445 }
446
447 switch u := target.Underlying().(type) {
448 case *Basic:
449 if x.mode() == constant_ {
450 v, code := check.representation(x, u)
451 if code != 0 {
452 return nil, nil, code
453 }
454 return target, v, code
455 }
456
457
458
459
460 switch x.typ().(*Basic).kind {
461 case UntypedBool:
462 if !isBoolean(target) {
463 return nil, nil, InvalidUntypedConversion
464 }
465 case UntypedInt, UntypedRune, UntypedFloat, UntypedComplex:
466 if !isNumeric(target) {
467 return nil, nil, InvalidUntypedConversion
468 }
469 case UntypedString:
470
471
472
473 if !isString(target) {
474 return nil, nil, InvalidUntypedConversion
475 }
476 default:
477 return nil, nil, InvalidUntypedConversion
478 }
479 case *Interface:
480 if isTypeParam(target) {
481 if !underIs(target, func(u Type) bool {
482 if u == nil {
483 return false
484 }
485 t, _, _ := check.implicitTypeAndValue(x, u)
486 return t != nil
487 }) {
488 return nil, nil, InvalidUntypedConversion
489 }
490 break
491 }
492
493
494
495 if !u.Empty() {
496 return nil, nil, InvalidUntypedConversion
497 }
498 return Default(x.typ()), nil, 0
499 default:
500 return nil, nil, InvalidUntypedConversion
501 }
502 return target, nil, 0
503 }
504
505
506 func (check *Checker) comparison(x, y *operand, op syntax.Operator, switchCase bool) {
507
508 if !isValid(x.typ()) || !isValid(y.typ()) {
509 x.invalidate()
510 return
511 }
512
513 if switchCase {
514 op = syntax.Eql
515 }
516
517 errOp := x
518 cause := ""
519
520
521
522 code := MismatchedTypes
523 ok, _ := x.assignableTo(check, y.typ(), nil)
524 if !ok {
525 ok, _ = y.assignableTo(check, x.typ(), nil)
526 }
527 if !ok {
528
529
530
531 errOp = y
532 cause = check.sprintf("mismatched types %s and %s", x.typ(), y.typ())
533 goto Error
534 }
535
536
537 code = UndefinedOp
538 switch op {
539 case syntax.Eql, syntax.Neq:
540
541 switch {
542 case x.isNil() || y.isNil():
543
544 typ := x.typ()
545 if x.isNil() {
546 typ = y.typ()
547 }
548 if !hasNil(typ) {
549
550
551
552
553 errOp = y
554 goto Error
555 }
556
557 case !Comparable(x.typ()):
558 errOp = x
559 cause = check.incomparableCause(x.typ())
560 goto Error
561
562 case !Comparable(y.typ()):
563 errOp = y
564 cause = check.incomparableCause(y.typ())
565 goto Error
566 }
567
568 case syntax.Lss, syntax.Leq, syntax.Gtr, syntax.Geq:
569
570 switch {
571 case !allOrdered(x.typ()):
572 errOp = x
573 goto Error
574 case !allOrdered(y.typ()):
575 errOp = y
576 goto Error
577 }
578
579 default:
580 panic("unreachable")
581 }
582
583
584 if x.mode() == constant_ && y.mode() == constant_ {
585 x.val = constant.MakeBool(constant.Compare(x.val, op2tok[op], y.val))
586
587
588 } else {
589 x.mode_ = value
590
591
592
593
594 check.updateExprType(x.expr, Default(x.typ()), true)
595 check.updateExprType(y.expr, Default(y.typ()), true)
596 }
597
598
599
600 x.typ_ = Typ[UntypedBool]
601 return
602
603 Error:
604
605 if cause == "" {
606 if isTypeParam(x.typ()) || isTypeParam(y.typ()) {
607
608 if !isTypeParam(x.typ()) {
609 errOp = y
610 }
611 cause = check.sprintf("type parameter %s cannot use operator %s", errOp.typ(), op)
612 } else {
613
614 what := compositeKind(errOp.typ())
615 if what == "" {
616 what = check.sprintf("%s", errOp.typ())
617 }
618 cause = check.sprintf("operator %s not defined on %s", op, what)
619 }
620 }
621 if switchCase {
622 check.errorf(x, code, "invalid case %s in switch on %s (%s)", x.expr, y.expr, cause)
623 } else {
624 check.errorf(errOp, code, invalidOp+"%s %s %s (%s)", x.expr, op, y.expr, cause)
625 }
626 x.invalidate()
627 }
628
629
630
631 func (check *Checker) incomparableCause(typ Type) string {
632 switch typ.Underlying().(type) {
633 case *Slice, *Signature, *Map:
634 return compositeKind(typ) + " can only be compared to nil"
635 }
636
637 return comparableType(typ, true, nil).format(check)
638 }
639
640
641 func (check *Checker) shift(x, y *operand, e syntax.Expr, op syntax.Operator) {
642
643
644 var xval constant.Value
645 if x.mode() == constant_ {
646 xval = constant.ToInt(x.val)
647 }
648
649 if allInteger(x.typ()) ||
650 x.isUntyped() && (xval != nil && xval.Kind() == constant.Int || xval == nil && isNumeric(x.typ())) {
651
652
653
654 } else {
655
656 check.errorf(x, InvalidShiftOperand, invalidOp+"shifted operand %s must be integer", x)
657 x.invalidate()
658 return
659 }
660
661
662
663
664
665
666 var yval constant.Value
667 if y.mode() == constant_ {
668
669 yval = constant.ToInt(y.val)
670 if yval.Kind() == constant.Int && constant.Sign(yval) < 0 {
671 check.errorf(y, InvalidShiftCount, invalidOp+"negative shift count %s", y)
672 x.invalidate()
673 return
674 }
675
676 if y.isUntyped() {
677
678
679 check.representable(y, Typ[Uint])
680 if !y.isValid() {
681 x.invalidate()
682 return
683 }
684 }
685 } else {
686
687 switch {
688 case y.isUntyped():
689
690
691
692
693
694 RHStype := Typ[Uint]
695 check.convertUntyped(y, RHStype)
696 if !y.isValid() {
697 x.invalidate()
698 return
699 }
700 fallthrough
701 case allInteger(y.typ()):
702 if !allUnsigned(y.typ()) && !check.verifyVersionf(y, go1_13, invalidOp+"signed shift count %s", y) {
703 x.invalidate()
704 return
705 }
706 default:
707 check.errorf(y, InvalidShiftCount, invalidOp+"shift count %s must be integer", y)
708 x.invalidate()
709 return
710 }
711 }
712
713 if x.mode() == constant_ {
714 if y.mode() == constant_ {
715
716 if x.val.Kind() == constant.Unknown || y.val.Kind() == constant.Unknown {
717 x.val = constant.MakeUnknown()
718
719 if !isInteger(x.typ()) {
720 x.typ_ = Typ[UntypedInt]
721 }
722 return
723 }
724
725 const shiftBound = 1023 - 1 + 52
726 s, ok := constant.Uint64Val(yval)
727 if !ok || s > shiftBound {
728 check.errorf(y, InvalidShiftCount, invalidOp+"invalid shift count %s", y)
729 x.invalidate()
730 return
731 }
732
733
734
735
736 if !isInteger(x.typ()) {
737 x.typ_ = Typ[UntypedInt]
738 }
739
740 x.val = constant.Shift(xval, op2tok[op], uint(s))
741 x.expr = e
742 check.overflow(x, opPos(x.expr))
743 return
744 }
745
746
747 if x.isUntyped() {
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776 lhs := syntax.Unparen(x.expr)
777 if info, found := check.untyped[lhs]; found {
778 info.isLhs = true
779 check.untyped[lhs] = info
780 }
781
782 x.mode_ = value
783 return
784 }
785 }
786
787 x.mode_ = value
788 }
789
790 var binaryOpPredicates opPredicates
791
792 func init() {
793
794 binaryOpPredicates = opPredicates{
795 syntax.Add: allNumericOrString,
796 syntax.Sub: allNumeric,
797 syntax.Mul: allNumeric,
798 syntax.Div: allNumeric,
799 syntax.Rem: allInteger,
800
801 syntax.And: allInteger,
802 syntax.Or: allInteger,
803 syntax.Xor: allInteger,
804 syntax.AndNot: allInteger,
805
806 syntax.AndAnd: allBoolean,
807 syntax.OrOr: allBoolean,
808 }
809 }
810
811
812
813 func isUntypedCompLit(e syntax.Expr) bool {
814 lit, _ := e.(*syntax.CompositeLit)
815 return lit != nil && lit.Type == nil
816 }
817
818
819
820 func (check *Checker) binary(x *operand, e syntax.Expr, lhs, rhs syntax.Expr, op syntax.Operator) {
821 var y operand
822 if isComparison(op) {
823 if l := isUntypedCompLit(lhs); l != isUntypedCompLit(rhs) {
824
825 var T *target
826 if l {
827
828 check.expr(nil, &y, rhs)
829 if y.isValid() {
830 T = newTarget(y.typ(), "comparison")
831 }
832 check.expr(T, x, lhs)
833 } else {
834
835 check.expr(nil, x, lhs)
836 if x.isValid() {
837 T = newTarget(x.typ(), "comparison")
838 }
839 check.expr(T, &y, rhs)
840 }
841 goto checked
842 }
843 }
844
845 check.expr(nil, x, lhs)
846 check.expr(nil, &y, rhs)
847 checked:
848
849 if !x.isValid() {
850 return
851 }
852 if !y.isValid() {
853 x.invalidate()
854 x.expr = y.expr
855 return
856 }
857
858 if isShift(op) {
859 check.shift(x, &y, e, op)
860 return
861 }
862
863 check.matchTypes(x, &y)
864 if !x.isValid() {
865 return
866 }
867
868 if isComparison(op) {
869 check.comparison(x, &y, op, false)
870 return
871 }
872
873 if !Identical(x.typ(), y.typ()) {
874
875
876 if isValid(x.typ()) && isValid(y.typ()) {
877 if e != nil {
878 check.errorf(e, MismatchedTypes, invalidOp+"%s (mismatched types %s and %s)", e, x.typ(), y.typ())
879 } else {
880 check.errorf(x, MismatchedTypes, invalidOp+"%s %s= %s (mismatched types %s and %s)", lhs, op, rhs, x.typ(), y.typ())
881 }
882 }
883 x.invalidate()
884 return
885 }
886
887 if !check.op(binaryOpPredicates, x, op) {
888 x.invalidate()
889 return
890 }
891
892 if op == syntax.Div || op == syntax.Rem {
893
894 if (x.mode() == constant_ || allInteger(x.typ())) && y.mode() == constant_ && constant.Sign(y.val) == 0 {
895 check.error(&y, DivByZero, invalidOp+"division by zero")
896 x.invalidate()
897 return
898 }
899
900
901 if x.mode() == constant_ && y.mode() == constant_ && isComplex(x.typ()) {
902 re, im := constant.Real(y.val), constant.Imag(y.val)
903 re2, im2 := constant.BinaryOp(re, token.MUL, re), constant.BinaryOp(im, token.MUL, im)
904 if constant.Sign(re2) == 0 && constant.Sign(im2) == 0 {
905 check.error(&y, DivByZero, invalidOp+"division by zero")
906 x.invalidate()
907 return
908 }
909 }
910 }
911
912 if x.mode() == constant_ && y.mode() == constant_ {
913
914 if x.val.Kind() == constant.Unknown || y.val.Kind() == constant.Unknown {
915 x.val = constant.MakeUnknown()
916
917 return
918 }
919
920 tok := op2tok[op]
921 if op == syntax.Div && isInteger(x.typ()) {
922 tok = token.QUO_ASSIGN
923 }
924 x.val = constant.BinaryOp(x.val, tok, y.val)
925 x.expr = e
926 check.overflow(x, opPos(x.expr))
927 return
928 }
929
930 x.mode_ = value
931
932 }
933
934
935
936 func (check *Checker) matchTypes(x, y *operand) {
937
938
939
940
941
942
943
944
945
946 mayConvert := func(x, y *operand) bool {
947
948 if x.isTyped() && y.isTyped() {
949 return false
950 }
951
952 if allNumeric(x.typ()) != allNumeric(y.typ()) {
953 return false
954 }
955
956
957
958
959 if isNonTypeParamInterface(x.typ()) || isNonTypeParamInterface(y.typ()) {
960 return true
961 }
962
963 if allBoolean(x.typ()) != allBoolean(y.typ()) {
964 return false
965 }
966
967 if allString(x.typ()) != allString(y.typ()) {
968 return false
969 }
970
971 if x.isNil() {
972 return hasNil(y.typ())
973 }
974 if y.isNil() {
975 return hasNil(x.typ())
976 }
977
978
979 if isPointer(x.typ()) || isPointer(y.typ()) {
980 return false
981 }
982 return true
983 }
984
985 if mayConvert(x, y) {
986 check.convertUntyped(x, y.typ())
987 if !x.isValid() {
988 return
989 }
990 check.convertUntyped(y, x.typ())
991 if !y.isValid() {
992 x.invalidate()
993 return
994 }
995 }
996 }
997
998
999
1000 type exprKind int
1001
1002 const (
1003 conversion exprKind = iota
1004 expression
1005 statement
1006 )
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017 func (check *Checker) rawExpr(T *target, x *operand, e syntax.Expr, allowGeneric bool) exprKind {
1018 if check.conf.Trace {
1019 check.trace(e.Pos(), "-- expr %s", e)
1020 check.indent++
1021 defer func() {
1022 check.indent--
1023 check.trace(e.Pos(), "=> %s", x)
1024 }()
1025 }
1026
1027 kind := check.exprInternal(T, x, e)
1028
1029 if !allowGeneric {
1030 check.nonGeneric(T, x)
1031 }
1032
1033 check.record(x)
1034
1035 return kind
1036 }
1037
1038
1039
1040
1041 func (check *Checker) nonGeneric(T *target, x *operand) {
1042 if !x.isValid() || x.mode() == novalue {
1043 return
1044 }
1045 var what string
1046 switch t := x.typ().(type) {
1047 case *Alias, *Named:
1048 if isGeneric(t) {
1049 what = "type"
1050 }
1051 case *Signature:
1052 if t.tparams != nil {
1053 if enableReverseTypeInference && T.sig() != nil {
1054 check.funcInst(T, x.Pos(), x, nil, true)
1055 return
1056 }
1057 what = "function"
1058 }
1059 }
1060 if what != "" {
1061 check.errorf(x.expr, WrongTypeArgCount, "cannot use generic %s %s without instantiation", what, x.expr)
1062 x.invalidate()
1063 x.typ_ = Typ[Invalid]
1064 }
1065 }
1066
1067
1068
1069
1070 func (check *Checker) exprInternal(T *target, x *operand, e syntax.Expr) exprKind {
1071
1072
1073 x.invalidate()
1074 x.typ_ = Typ[Invalid]
1075
1076 switch e := e.(type) {
1077 case nil:
1078 panic("unreachable")
1079
1080 case *syntax.BadExpr:
1081 goto Error
1082
1083 case *syntax.Name:
1084 check.ident(x, e, false)
1085
1086 case *syntax.DotsType:
1087
1088 check.error(e, InvalidSyntaxTree, "invalid use of ...")
1089 goto Error
1090
1091 case *syntax.BasicLit:
1092 if e.Bad {
1093 goto Error
1094 }
1095 check.basicLit(x, e)
1096 if !x.isValid() {
1097 goto Error
1098 }
1099
1100 case *syntax.FuncLit:
1101 check.funcLit(x, e)
1102 if !x.isValid() {
1103 goto Error
1104 }
1105
1106 case *syntax.CompositeLit:
1107 check.compositeLit(T, x, e)
1108 if !x.isValid() {
1109 goto Error
1110 }
1111
1112 case *syntax.ParenExpr:
1113
1114 kind := check.rawExpr(nil, x, e.X, false)
1115 x.expr = e
1116 return kind
1117
1118 case *syntax.SelectorExpr:
1119 check.selector(x, e, false)
1120
1121 case *syntax.IndexExpr:
1122 if check.indexExpr(x, e) {
1123 if !enableReverseTypeInference {
1124 T = nil
1125 }
1126 check.funcInst(T, e.Pos(), x, e, true)
1127 }
1128 if !x.isValid() {
1129 goto Error
1130 }
1131
1132 case *syntax.SliceExpr:
1133 check.sliceExpr(x, e)
1134 if !x.isValid() {
1135 goto Error
1136 }
1137
1138 case *syntax.AssertExpr:
1139 check.expr(nil, x, e.X)
1140 if !x.isValid() {
1141 goto Error
1142 }
1143
1144 if e.Type == nil {
1145 check.error(e, InvalidSyntaxTree, "invalid use of AssertExpr")
1146 goto Error
1147 }
1148 if isTypeParam(x.typ()) {
1149 check.errorf(x, InvalidAssert, invalidOp+"cannot use type assertion on type parameter value %s", x)
1150 goto Error
1151 }
1152 if _, ok := x.typ().Underlying().(*Interface); !ok {
1153 check.errorf(x, InvalidAssert, invalidOp+"%s is not an interface", x)
1154 goto Error
1155 }
1156 T := check.varType(e.Type)
1157 if !isValid(T) {
1158 goto Error
1159 }
1160
1161 if !check.isComplete(T) {
1162 goto Error
1163 }
1164 check.typeAssertion(e, x, T, false)
1165 x.mode_ = commaok
1166 x.typ_ = T
1167
1168 case *syntax.TypeSwitchGuard:
1169
1170 check.error(e, InvalidSyntaxTree, "use of .(type) outside type switch")
1171 check.use(e.X)
1172 goto Error
1173
1174 case *syntax.CallExpr:
1175 return check.callExpr(x, e)
1176
1177 case *syntax.ListExpr:
1178
1179 check.error(e, InvalidSyntaxTree, "unexpected list of expressions")
1180 goto Error
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202 case *syntax.Operation:
1203 if e.Y == nil {
1204
1205 if e.Op == syntax.Mul {
1206
1207 check.exprOrType(x, e.X, false)
1208 switch x.mode() {
1209 case invalid:
1210 goto Error
1211 case typexpr:
1212 check.validVarType(e.X, x.typ())
1213 x.typ_ = &Pointer{base: x.typ()}
1214 default:
1215 var base Type
1216 if !underIs(x.typ(), func(u Type) bool {
1217 p, _ := u.(*Pointer)
1218 if p == nil {
1219 check.errorf(x, InvalidIndirection, invalidOp+"cannot indirect %s", x)
1220 return false
1221 }
1222 if base != nil && !Identical(p.base, base) {
1223 check.errorf(x, InvalidIndirection, invalidOp+"pointers of %s must have identical base types", x)
1224 return false
1225 }
1226 base = p.base
1227 return true
1228 }) {
1229 goto Error
1230 }
1231
1232 if !check.isComplete(base) {
1233 goto Error
1234 }
1235 x.mode_ = variable
1236 x.typ_ = base
1237 }
1238 break
1239 }
1240
1241 check.unary(x, e)
1242 if !x.isValid() {
1243 goto Error
1244 }
1245 if e.Op == syntax.Recv {
1246 x.expr = e
1247 return statement
1248 }
1249 break
1250 }
1251
1252
1253 check.binary(x, e, e.X, e.Y, e.Op)
1254 if !x.isValid() {
1255 goto Error
1256 }
1257
1258 case *syntax.KeyValueExpr:
1259
1260 check.error(e, InvalidSyntaxTree, "no key:value expected")
1261 goto Error
1262
1263 case *syntax.ArrayType, *syntax.SliceType, *syntax.StructType, *syntax.FuncType,
1264 *syntax.InterfaceType, *syntax.MapType, *syntax.ChanType:
1265 x.mode_ = typexpr
1266 x.typ_ = check.typ(e)
1267
1268
1269
1270
1271
1272
1273 default:
1274 panic(fmt.Sprintf("%s: unknown expression type %T", atPos(e), e))
1275 }
1276
1277
1278 x.expr = e
1279 return expression
1280
1281 Error:
1282 x.invalidate()
1283 x.expr = e
1284 return statement
1285 }
1286
1287
1288 func (check *Checker) typeAssertion(e syntax.Expr, x *operand, T Type, typeSwitch bool) {
1289 var cause string
1290 if check.assertableTo(x.typ(), T, &cause) {
1291 return
1292 }
1293
1294 if typeSwitch {
1295 check.errorf(e, ImpossibleAssert, "impossible type switch case: %s\n\t%s cannot have dynamic type %s %s", e, x, T, cause)
1296 return
1297 }
1298
1299 check.errorf(e, ImpossibleAssert, "impossible type assertion: %s\n\t%s does not implement %s %s", e, T, x.typ(), cause)
1300 }
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310 func (check *Checker) expr(T *target, x *operand, e syntax.Expr) {
1311 check.rawExpr(T, x, e, false)
1312 check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr)
1313 check.singleValue(x)
1314 }
1315
1316
1317 func (check *Checker) genericExpr(T *target, x *operand, e syntax.Expr) {
1318 check.rawExpr(T, x, e, true)
1319 check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr)
1320 check.singleValue(x)
1321 }
1322
1323
1324
1325
1326
1327
1328 func (check *Checker) multiExpr(e syntax.Expr, allowCommaOk bool) (list []*operand, commaOk bool) {
1329 var x operand
1330 check.rawExpr(nil, &x, e, false)
1331 check.exclude(&x, 1<<novalue|1<<builtin|1<<typexpr)
1332
1333 if t, ok := x.typ().(*Tuple); ok && x.isValid() {
1334
1335 list = make([]*operand, t.Len())
1336 for i, v := range t.vars {
1337
1338 dummy := newIdent(startPos(e), nth(i+1, "function result"))
1339 list[i] = &operand{mode_: value, expr: dummy, typ_: v.typ}
1340 }
1341 return
1342 }
1343
1344
1345 list = []*operand{&x}
1346 if allowCommaOk && (x.mode() == mapindex || x.mode() == commaok || x.mode() == commaerr) {
1347 var what string = "ok value of (comma, ok) expression"
1348 var typ Type = Typ[UntypedBool]
1349 if x.mode() == commaerr {
1350 what = "err value of (comma, err) expression"
1351 typ = universeError
1352 }
1353
1354 dummy := newIdent(startPos(e), what)
1355 x2 := &operand{mode_: value, expr: dummy, typ_: typ}
1356 list = append(list, x2)
1357 commaOk = true
1358 }
1359
1360 return
1361 }
1362
1363
1364
1365 func nth(n int, what string) string {
1366 var ext string
1367 switch n {
1368 case 1:
1369 ext = "st"
1370 case 2:
1371 ext = "nd"
1372 case 3:
1373 ext = "rd"
1374 default:
1375 ext = "th"
1376 }
1377 return fmt.Sprintf("%d%s %s", n, ext, what)
1378 }
1379
1380
1381
1382
1383
1384 func (check *Checker) exprOrType(x *operand, e syntax.Expr, allowGeneric bool) {
1385 check.rawExpr(nil, x, e, allowGeneric)
1386 check.exclude(x, 1<<novalue)
1387 check.singleValue(x)
1388 }
1389
1390
1391
1392 func (check *Checker) exclude(x *operand, modeset uint) {
1393 if modeset&(1<<x.mode()) != 0 {
1394 var msg string
1395 var code Code
1396 switch x.mode() {
1397 case novalue:
1398 if modeset&(1<<typexpr) != 0 {
1399 msg = "%s used as value"
1400 } else {
1401 msg = "%s used as value or type"
1402 }
1403 code = TooManyValues
1404 case builtin:
1405 msg = "%s must be called"
1406 code = UncalledBuiltin
1407 case typexpr:
1408 msg = "%s is not an expression"
1409 code = NotAnExpr
1410 default:
1411 panic("unreachable")
1412 }
1413 check.errorf(x, code, msg, x)
1414 x.invalidate()
1415 }
1416 }
1417
1418
1419 func (check *Checker) singleValue(x *operand) {
1420 if x.mode() == value {
1421
1422 if t, ok := x.typ().(*Tuple); ok {
1423 assert(t.Len() != 1)
1424 check.errorf(x, TooManyValues, "multiple-value %s in single-value context", x)
1425 x.invalidate()
1426 }
1427 }
1428 }
1429
1430
1431 var op2tok = [...]token.Token{
1432 syntax.Def: token.ILLEGAL,
1433 syntax.Not: token.NOT,
1434 syntax.Recv: token.ILLEGAL,
1435
1436 syntax.OrOr: token.LOR,
1437 syntax.AndAnd: token.LAND,
1438
1439 syntax.Eql: token.EQL,
1440 syntax.Neq: token.NEQ,
1441 syntax.Lss: token.LSS,
1442 syntax.Leq: token.LEQ,
1443 syntax.Gtr: token.GTR,
1444 syntax.Geq: token.GEQ,
1445
1446 syntax.Add: token.ADD,
1447 syntax.Sub: token.SUB,
1448 syntax.Or: token.OR,
1449 syntax.Xor: token.XOR,
1450
1451 syntax.Mul: token.MUL,
1452 syntax.Div: token.QUO,
1453 syntax.Rem: token.REM,
1454 syntax.And: token.AND,
1455 syntax.AndNot: token.AND_NOT,
1456 syntax.Shl: token.SHL,
1457 syntax.Shr: token.SHR,
1458 }
1459
View as plain text