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 check.recordTypeAndValue(x, invalid, Typ[Invalid], nil)
381 return
382 }
383
384
385
386 }
387
388
389
390
391
392
393
394
395 if _, isParen := x.(*syntax.ParenExpr); !isParen && old.val != nil {
396
397 c := operand{old.mode, x, old.typ, old.val, 0}
398 check.convertUntyped(&c, typ)
399 if !c.isValid() {
400 check.recordTypeAndValue(x, invalid, Typ[Invalid], nil)
401 return
402 }
403 }
404
405
406 check.recordTypeAndValue(x, old.mode, typ, old.val)
407 }
408
409
410 func (check *Checker) updateExprVal(x syntax.Expr, val constant.Value) {
411 if info, ok := check.untyped[x]; ok {
412 info.val = val
413 check.untyped[x] = info
414 }
415 }
416
417
418
419
420
421
422
423 func (check *Checker) implicitTypeAndValue(x *operand, target Type) (Type, constant.Value, Code) {
424 if !x.isValid() || x.isTyped() || !isValid(target) {
425 return x.typ(), nil, 0
426 }
427
428
429 if isUntyped(target) {
430
431 if m := maxType(x.typ(), target); m != nil {
432 return m, nil, 0
433 }
434 return nil, nil, InvalidUntypedConversion
435 }
436
437 if x.isNil() {
438 assert(x.isUntyped())
439 if hasNil(target) {
440 if isTypes2 {
441 return target, nil, 0
442 }
443
444 return Typ[UntypedNil], nil, 0
445 }
446 return nil, nil, InvalidUntypedConversion
447 }
448
449 switch u := target.Underlying().(type) {
450 case *Basic:
451 if x.mode() == constant_ {
452 v, code := check.representation(x, u)
453 if code != 0 {
454 return nil, nil, code
455 }
456 return target, v, code
457 }
458
459
460
461
462 switch x.typ().(*Basic).kind {
463 case UntypedBool:
464 if !isBoolean(target) {
465 return nil, nil, InvalidUntypedConversion
466 }
467 case UntypedInt, UntypedRune, UntypedFloat, UntypedComplex:
468 if !isNumeric(target) {
469 return nil, nil, InvalidUntypedConversion
470 }
471 case UntypedString:
472
473
474
475 if !isString(target) {
476 return nil, nil, InvalidUntypedConversion
477 }
478 default:
479 return nil, nil, InvalidUntypedConversion
480 }
481 case *Interface:
482 if isTypeParam(target) {
483 if !underIs(target, func(u Type) bool {
484 if u == nil {
485 return false
486 }
487 t, _, _ := check.implicitTypeAndValue(x, u)
488 return t != nil
489 }) {
490 return nil, nil, InvalidUntypedConversion
491 }
492 break
493 }
494
495
496
497 if !u.Empty() {
498 return nil, nil, InvalidUntypedConversion
499 }
500 return Default(x.typ()), nil, 0
501 default:
502 return nil, nil, InvalidUntypedConversion
503 }
504 return target, nil, 0
505 }
506
507
508 func (check *Checker) comparison(x, y *operand, op syntax.Operator, switchCase bool) {
509
510 if !isValid(x.typ()) || !isValid(y.typ()) {
511 x.invalidate()
512 return
513 }
514
515 if switchCase {
516 op = syntax.Eql
517 }
518
519 errOp := x
520 cause := ""
521
522
523
524 code := MismatchedTypes
525 ok, _ := x.assignableTo(check, y.typ(), nil)
526 if !ok {
527 ok, _ = y.assignableTo(check, x.typ(), nil)
528 }
529 if !ok {
530
531
532
533 errOp = y
534 cause = check.sprintf("mismatched types %s and %s", x.typ(), y.typ())
535 goto Error
536 }
537
538
539 code = UndefinedOp
540 switch op {
541 case syntax.Eql, syntax.Neq:
542
543 switch {
544 case x.isNil() || y.isNil():
545
546 typ := x.typ()
547 if x.isNil() {
548 typ = y.typ()
549 }
550 if !hasNil(typ) {
551
552
553
554
555 errOp = y
556 goto Error
557 }
558
559 case !Comparable(x.typ()):
560 errOp = x
561 cause = check.incomparableCause(x.typ())
562 goto Error
563
564 case !Comparable(y.typ()):
565 errOp = y
566 cause = check.incomparableCause(y.typ())
567 goto Error
568 }
569
570 case syntax.Lss, syntax.Leq, syntax.Gtr, syntax.Geq:
571
572 switch {
573 case !allOrdered(x.typ()):
574 errOp = x
575 goto Error
576 case !allOrdered(y.typ()):
577 errOp = y
578 goto Error
579 }
580
581 default:
582 panic("unreachable")
583 }
584
585
586 if x.mode() == constant_ && y.mode() == constant_ {
587 x.val = constant.MakeBool(constant.Compare(x.val, op2tok[op], y.val))
588
589
590 } else {
591 x.mode_ = value
592
593
594
595
596 check.updateExprType(x.expr, Default(x.typ()), true)
597 check.updateExprType(y.expr, Default(y.typ()), true)
598 }
599
600
601
602 x.typ_ = Typ[UntypedBool]
603 return
604
605 Error:
606
607 if cause == "" {
608 if isTypeParam(x.typ()) || isTypeParam(y.typ()) {
609
610 if !isTypeParam(x.typ()) {
611 errOp = y
612 }
613 cause = check.sprintf("type parameter %s cannot use operator %s", errOp.typ(), op)
614 } else {
615
616 what := compositeKind(errOp.typ())
617 if what == "" {
618 what = check.sprintf("%s", errOp.typ())
619 }
620 cause = check.sprintf("operator %s not defined on %s", op, what)
621 }
622 }
623 if switchCase {
624 check.errorf(x, code, "invalid case %s in switch on %s (%s)", x.expr, y.expr, cause)
625 } else {
626 check.errorf(errOp, code, invalidOp+"%s %s %s (%s)", x.expr, op, y.expr, cause)
627 }
628 x.invalidate()
629 }
630
631
632
633 func (check *Checker) incomparableCause(typ Type) string {
634 switch typ.Underlying().(type) {
635 case *Slice, *Signature, *Map:
636 return compositeKind(typ) + " can only be compared to nil"
637 }
638
639 return comparableType(typ, true, nil).format(check)
640 }
641
642
643 func (check *Checker) shift(x, y *operand, e syntax.Expr, op syntax.Operator) {
644
645
646 var xval constant.Value
647 if x.mode() == constant_ {
648 xval = constant.ToInt(x.val)
649 }
650
651 if allInteger(x.typ()) ||
652 x.isUntyped() && (xval != nil && xval.Kind() == constant.Int || xval == nil && isNumeric(x.typ())) {
653
654
655
656 } else {
657
658 check.errorf(x, InvalidShiftOperand, invalidOp+"shifted operand %s must be integer", x)
659 x.invalidate()
660 return
661 }
662
663
664
665
666
667
668 var yval constant.Value
669 if y.mode() == constant_ {
670
671 yval = constant.ToInt(y.val)
672 if yval.Kind() == constant.Int && constant.Sign(yval) < 0 {
673 check.errorf(y, InvalidShiftCount, invalidOp+"negative shift count %s", y)
674 x.invalidate()
675 return
676 }
677
678 if y.isUntyped() {
679
680
681 check.representable(y, Typ[Uint])
682 if !y.isValid() {
683 x.invalidate()
684 return
685 }
686 }
687 } else {
688
689 switch {
690 case y.isUntyped():
691
692
693
694
695
696 RHStype := Typ[Uint]
697 check.convertUntyped(y, RHStype)
698 if !y.isValid() {
699 x.invalidate()
700 return
701 }
702 fallthrough
703 case allInteger(y.typ()):
704 if !allUnsigned(y.typ()) && !check.verifyVersionf(y, go1_13, invalidOp+"signed shift count %s", y) {
705 x.invalidate()
706 return
707 }
708 default:
709 check.errorf(y, InvalidShiftCount, invalidOp+"shift count %s must be integer", y)
710 x.invalidate()
711 return
712 }
713 }
714
715 if x.mode() == constant_ {
716 if y.mode() == constant_ {
717
718 if x.val.Kind() == constant.Unknown || y.val.Kind() == constant.Unknown {
719 x.val = constant.MakeUnknown()
720
721 if !isInteger(x.typ()) {
722 x.typ_ = Typ[UntypedInt]
723 }
724 return
725 }
726
727 const shiftBound = 1023 - 1 + 52
728 s, ok := constant.Uint64Val(yval)
729 if !ok || s > shiftBound {
730 check.errorf(y, InvalidShiftCount, invalidOp+"invalid shift count %s", y)
731 x.invalidate()
732 return
733 }
734
735
736
737
738 if !isInteger(x.typ()) {
739 x.typ_ = Typ[UntypedInt]
740 }
741
742 x.val = constant.Shift(xval, op2tok[op], uint(s))
743 x.expr = e
744 check.overflow(x, opPos(x.expr))
745 return
746 }
747
748
749 if x.isUntyped() {
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
777
778 lhs := syntax.Unparen(x.expr)
779 if info, found := check.untyped[lhs]; found {
780 info.isLhs = true
781 check.untyped[lhs] = info
782 }
783
784 x.mode_ = value
785 return
786 }
787 }
788
789 x.mode_ = value
790 }
791
792 var binaryOpPredicates opPredicates
793
794 func init() {
795
796 binaryOpPredicates = opPredicates{
797 syntax.Add: allNumericOrString,
798 syntax.Sub: allNumeric,
799 syntax.Mul: allNumeric,
800 syntax.Div: allNumeric,
801 syntax.Rem: allInteger,
802
803 syntax.And: allInteger,
804 syntax.Or: allInteger,
805 syntax.Xor: allInteger,
806 syntax.AndNot: allInteger,
807
808 syntax.AndAnd: allBoolean,
809 syntax.OrOr: allBoolean,
810 }
811 }
812
813
814
815 func isUntypedCompLit(e syntax.Expr) bool {
816 lit, _ := e.(*syntax.CompositeLit)
817 return lit != nil && lit.Type == nil
818 }
819
820
821
822 func (check *Checker) binary(x *operand, e syntax.Expr, lhs, rhs syntax.Expr, op syntax.Operator) {
823 var y operand
824 if isComparison(op) {
825 if l := isUntypedCompLit(lhs); l != isUntypedCompLit(rhs) {
826
827 var T *target
828 if l {
829
830 check.expr(nil, &y, rhs)
831 if y.isValid() {
832 T = newTarget(y.typ(), "comparison")
833 }
834 check.expr(T, x, lhs)
835 } else {
836
837 check.expr(nil, x, lhs)
838 if x.isValid() {
839 T = newTarget(x.typ(), "comparison")
840 }
841 check.expr(T, &y, rhs)
842 }
843 goto checked
844 }
845 }
846
847 check.expr(nil, x, lhs)
848 check.expr(nil, &y, rhs)
849 checked:
850
851 if !x.isValid() {
852 return
853 }
854 if !y.isValid() {
855 x.invalidate()
856 x.expr = y.expr
857 return
858 }
859
860 if isShift(op) {
861 check.shift(x, &y, e, op)
862 return
863 }
864
865 check.matchTypes(x, &y)
866 if !x.isValid() {
867 return
868 }
869
870 if isComparison(op) {
871 check.comparison(x, &y, op, false)
872 return
873 }
874
875 if !Identical(x.typ(), y.typ()) {
876
877
878 if isValid(x.typ()) && isValid(y.typ()) {
879 if e != nil {
880 check.errorf(e, MismatchedTypes, invalidOp+"%s (mismatched types %s and %s)", e, x.typ(), y.typ())
881 } else {
882 check.errorf(x, MismatchedTypes, invalidOp+"%s %s= %s (mismatched types %s and %s)", lhs, op, rhs, x.typ(), y.typ())
883 }
884 }
885 x.invalidate()
886 return
887 }
888
889 if !check.op(binaryOpPredicates, x, op) {
890 x.invalidate()
891 return
892 }
893
894 if op == syntax.Div || op == syntax.Rem {
895
896 if (x.mode() == constant_ || allInteger(x.typ())) && y.mode() == constant_ && constant.Sign(y.val) == 0 {
897 check.error(&y, DivByZero, invalidOp+"division by zero")
898 x.invalidate()
899 return
900 }
901
902
903 if x.mode() == constant_ && y.mode() == constant_ && isComplex(x.typ()) {
904 re, im := constant.Real(y.val), constant.Imag(y.val)
905 re2, im2 := constant.BinaryOp(re, token.MUL, re), constant.BinaryOp(im, token.MUL, im)
906 if constant.Sign(re2) == 0 && constant.Sign(im2) == 0 {
907 check.error(&y, DivByZero, invalidOp+"division by zero")
908 x.invalidate()
909 return
910 }
911 }
912 }
913
914 if x.mode() == constant_ && y.mode() == constant_ {
915
916 if x.val.Kind() == constant.Unknown || y.val.Kind() == constant.Unknown {
917 x.val = constant.MakeUnknown()
918
919 return
920 }
921
922 tok := op2tok[op]
923 if op == syntax.Div && isInteger(x.typ()) {
924 tok = token.QUO_ASSIGN
925 }
926 x.val = constant.BinaryOp(x.val, tok, y.val)
927 x.expr = e
928 check.overflow(x, opPos(x.expr))
929 return
930 }
931
932 x.mode_ = value
933
934 }
935
936
937
938 func (check *Checker) matchTypes(x, y *operand) {
939
940
941
942
943
944
945
946
947
948 mayConvert := func(x, y *operand) bool {
949
950 if x.isTyped() && y.isTyped() {
951 return false
952 }
953
954 if allNumeric(x.typ()) != allNumeric(y.typ()) {
955 return false
956 }
957
958
959
960
961 if isNonTypeParamInterface(x.typ()) || isNonTypeParamInterface(y.typ()) {
962 return true
963 }
964
965 if allBoolean(x.typ()) != allBoolean(y.typ()) {
966 return false
967 }
968
969 if allString(x.typ()) != allString(y.typ()) {
970 return false
971 }
972
973 if x.isNil() {
974 return hasNil(y.typ())
975 }
976 if y.isNil() {
977 return hasNil(x.typ())
978 }
979
980
981 if isPointer(x.typ()) || isPointer(y.typ()) {
982 return false
983 }
984 return true
985 }
986
987 if mayConvert(x, y) {
988 check.convertUntyped(x, y.typ())
989 if !x.isValid() {
990 return
991 }
992 check.convertUntyped(y, x.typ())
993 if !y.isValid() {
994 x.invalidate()
995 return
996 }
997 }
998 }
999
1000
1001
1002 type exprKind int
1003
1004 const (
1005 conversion exprKind = iota
1006 expression
1007 statement
1008 )
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019 func (check *Checker) rawExpr(T *target, x *operand, e syntax.Expr, allowGeneric bool) exprKind {
1020 if check.conf.Trace {
1021 check.trace(e.Pos(), "-- expr %s", e)
1022 check.indent++
1023 defer func() {
1024 check.indent--
1025 check.trace(e.Pos(), "=> %s", x)
1026 }()
1027 }
1028
1029 kind := check.exprInternal(T, x, e)
1030
1031 if !allowGeneric {
1032 check.nonGeneric(T, x)
1033 }
1034
1035 check.record(x)
1036
1037 return kind
1038 }
1039
1040
1041
1042
1043 func (check *Checker) nonGeneric(T *target, x *operand) {
1044 if !x.isValid() || x.mode() == novalue {
1045 return
1046 }
1047 var what string
1048 switch t := x.typ().(type) {
1049 case *Alias, *Named:
1050 if isGeneric(t) {
1051 what = "type"
1052 }
1053 case *Signature:
1054 if t.tparams != nil {
1055 if enableReverseTypeInference && T.sig() != nil {
1056 check.funcInst(T, x.Pos(), x, nil, true)
1057 return
1058 }
1059 what = "function"
1060 }
1061 }
1062 if what != "" {
1063 check.errorf(x.expr, WrongTypeArgCount, "cannot use generic %s %s without instantiation", what, x.expr)
1064 x.invalidate()
1065 x.typ_ = Typ[Invalid]
1066 }
1067 }
1068
1069
1070
1071
1072 func (check *Checker) exprInternal(T *target, x *operand, e syntax.Expr) exprKind {
1073
1074
1075 x.invalidate()
1076 x.typ_ = Typ[Invalid]
1077
1078 switch e := e.(type) {
1079 case nil:
1080 panic("unreachable")
1081
1082 case *syntax.BadExpr:
1083 goto Error
1084
1085 case *syntax.Name:
1086 check.ident(x, e, false)
1087
1088 case *syntax.DotsType:
1089
1090 check.error(e, InvalidSyntaxTree, "invalid use of ...")
1091 goto Error
1092
1093 case *syntax.BasicLit:
1094 if e.Bad {
1095 goto Error
1096 }
1097 check.basicLit(x, e)
1098 if !x.isValid() {
1099 goto Error
1100 }
1101
1102 case *syntax.FuncLit:
1103 check.funcLit(x, e)
1104 if !x.isValid() {
1105 goto Error
1106 }
1107
1108 case *syntax.CompositeLit:
1109 check.compositeLit(T, x, e)
1110 if !x.isValid() {
1111 goto Error
1112 }
1113
1114 case *syntax.ParenExpr:
1115
1116 kind := check.rawExpr(nil, x, e.X, false)
1117 x.expr = e
1118 return kind
1119
1120 case *syntax.SelectorExpr:
1121 check.selector(x, e, false)
1122
1123 case *syntax.IndexExpr:
1124 if check.indexExpr(x, e) {
1125 if !enableReverseTypeInference {
1126 T = nil
1127 }
1128 check.funcInst(T, e.Pos(), x, e, true)
1129 }
1130 if !x.isValid() {
1131 goto Error
1132 }
1133
1134 case *syntax.SliceExpr:
1135 check.sliceExpr(x, e)
1136 if !x.isValid() {
1137 goto Error
1138 }
1139
1140 case *syntax.AssertExpr:
1141 check.expr(nil, x, e.X)
1142 if !x.isValid() {
1143 check.use(e.Type)
1144 goto Error
1145 }
1146
1147 if e.Type == nil {
1148 check.error(e, InvalidSyntaxTree, "invalid use of AssertExpr")
1149 goto Error
1150 }
1151 if isTypeParam(x.typ()) {
1152 check.errorf(x, InvalidAssert, invalidOp+"cannot use type assertion on type parameter value %s", x)
1153 check.use(e.Type)
1154 goto Error
1155 }
1156 if _, ok := x.typ().Underlying().(*Interface); !ok {
1157 check.errorf(x, InvalidAssert, invalidOp+"%s is not an interface", x)
1158 check.use(e.Type)
1159 goto Error
1160 }
1161 T := check.varType(e.Type)
1162 if !isValid(T) {
1163 goto Error
1164 }
1165
1166 if !check.isComplete(T) {
1167 goto Error
1168 }
1169 check.typeAssertion(e, x, T, false)
1170 x.mode_ = commaok
1171 x.typ_ = T
1172
1173 case *syntax.TypeSwitchGuard:
1174
1175 check.error(e, InvalidSyntaxTree, "use of .(type) outside type switch")
1176 check.use(e.X)
1177 goto Error
1178
1179 case *syntax.CallExpr:
1180 return check.callExpr(x, e)
1181
1182 case *syntax.ListExpr:
1183
1184 check.error(e, InvalidSyntaxTree, "unexpected list of expressions")
1185 goto Error
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207 case *syntax.Operation:
1208 if e.Y == nil {
1209
1210 if e.Op == syntax.Mul {
1211
1212 check.exprOrType(x, e.X, false)
1213 switch x.mode() {
1214 case invalid:
1215 goto Error
1216 case typexpr:
1217 check.validVarType(e.X, x.typ())
1218 x.typ_ = &Pointer{base: x.typ()}
1219 default:
1220 var base Type
1221 if !underIs(x.typ(), func(u Type) bool {
1222 p, _ := u.(*Pointer)
1223 if p == nil {
1224 check.errorf(x, InvalidIndirection, invalidOp+"cannot indirect %s", x)
1225 return false
1226 }
1227 if base != nil && !Identical(p.base, base) {
1228 check.errorf(x, InvalidIndirection, invalidOp+"pointers of %s must have identical base types", x)
1229 return false
1230 }
1231 base = p.base
1232 return true
1233 }) {
1234 goto Error
1235 }
1236
1237 if !check.isComplete(base) {
1238 goto Error
1239 }
1240 x.mode_ = variable
1241 x.typ_ = base
1242 }
1243 break
1244 }
1245
1246 check.unary(x, e)
1247 if !x.isValid() {
1248 goto Error
1249 }
1250 if e.Op == syntax.Recv {
1251 x.expr = e
1252 return statement
1253 }
1254 break
1255 }
1256
1257
1258 check.binary(x, e, e.X, e.Y, e.Op)
1259 if !x.isValid() {
1260 goto Error
1261 }
1262
1263 case *syntax.KeyValueExpr:
1264
1265 check.error(e, InvalidSyntaxTree, "no key:value expected")
1266 goto Error
1267
1268 case *syntax.ArrayType, *syntax.SliceType, *syntax.StructType, *syntax.FuncType,
1269 *syntax.InterfaceType, *syntax.MapType, *syntax.ChanType:
1270 x.mode_ = typexpr
1271 x.typ_ = check.typ(e)
1272
1273
1274
1275
1276
1277
1278 default:
1279 panic(fmt.Sprintf("%s: unknown expression type %T", atPos(e), e))
1280 }
1281
1282
1283 x.expr = e
1284 return expression
1285
1286 Error:
1287 x.invalidate()
1288 x.expr = e
1289 return statement
1290 }
1291
1292
1293 func (check *Checker) typeAssertion(e syntax.Expr, x *operand, T Type, typeSwitch bool) {
1294 var cause string
1295 if check.assertableTo(x.typ(), T, &cause) {
1296 return
1297 }
1298
1299 if typeSwitch {
1300 check.errorf(e, ImpossibleAssert, "impossible type switch case: %s\n\t%s cannot have dynamic type %s %s", e, x, T, cause)
1301 return
1302 }
1303
1304 check.errorf(e, ImpossibleAssert, "impossible type assertion: %s\n\t%s does not implement %s %s", e, T, x.typ(), cause)
1305 }
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315 func (check *Checker) expr(T *target, x *operand, e syntax.Expr) {
1316 check.rawExpr(T, x, e, false)
1317 check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr)
1318 check.singleValue(x)
1319 }
1320
1321
1322 func (check *Checker) genericExpr(T *target, x *operand, e syntax.Expr) {
1323 check.rawExpr(T, x, e, true)
1324 check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr)
1325 check.singleValue(x)
1326 }
1327
1328
1329
1330
1331
1332
1333 func (check *Checker) multiExpr(e syntax.Expr, allowCommaOk bool) (list []*operand, commaOk bool) {
1334 var x operand
1335 check.rawExpr(nil, &x, e, false)
1336 check.exclude(&x, 1<<novalue|1<<builtin|1<<typexpr)
1337
1338 if t, ok := x.typ().(*Tuple); ok && x.isValid() {
1339
1340 list = make([]*operand, t.Len())
1341 for i, v := range t.vars {
1342
1343 dummy := newIdent(startPos(e), nth(i+1, "function result"))
1344 list[i] = &operand{mode_: value, expr: dummy, typ_: v.typ}
1345 }
1346 return
1347 }
1348
1349
1350 list = []*operand{&x}
1351 if allowCommaOk && (x.mode() == mapindex || x.mode() == commaok || x.mode() == commaerr) {
1352 var what string = "ok value of (comma, ok) expression"
1353 var typ Type = Typ[UntypedBool]
1354 if x.mode() == commaerr {
1355 what = "err value of (comma, err) expression"
1356 typ = universeError
1357 }
1358
1359 dummy := newIdent(startPos(e), what)
1360 x2 := &operand{mode_: value, expr: dummy, typ_: typ}
1361 list = append(list, x2)
1362 commaOk = true
1363 }
1364
1365 return
1366 }
1367
1368
1369
1370 func nth(n int, what string) string {
1371 var ext string
1372 switch n {
1373 case 1:
1374 ext = "st"
1375 case 2:
1376 ext = "nd"
1377 case 3:
1378 ext = "rd"
1379 default:
1380 ext = "th"
1381 }
1382 return fmt.Sprintf("%d%s %s", n, ext, what)
1383 }
1384
1385
1386
1387
1388
1389 func (check *Checker) exprOrType(x *operand, e syntax.Expr, allowGeneric bool) {
1390 check.rawExpr(nil, x, e, allowGeneric)
1391 check.exclude(x, 1<<novalue)
1392 check.singleValue(x)
1393 }
1394
1395
1396
1397 func (check *Checker) exclude(x *operand, modeset uint) {
1398 if modeset&(1<<x.mode()) != 0 {
1399 var msg string
1400 var code Code
1401 switch x.mode() {
1402 case novalue:
1403 if modeset&(1<<typexpr) != 0 {
1404 msg = "%s used as value"
1405 } else {
1406 msg = "%s used as value or type"
1407 }
1408 code = TooManyValues
1409 case builtin:
1410 msg = "%s must be called"
1411 code = UncalledBuiltin
1412 case typexpr:
1413 msg = "%s is not an expression"
1414 code = NotAnExpr
1415 default:
1416 panic("unreachable")
1417 }
1418 check.errorf(x, code, msg, x)
1419 x.invalidate()
1420 }
1421 }
1422
1423
1424 func (check *Checker) singleValue(x *operand) {
1425 if x.mode() == value {
1426
1427 if t, ok := x.typ().(*Tuple); ok {
1428 assert(t.Len() != 1)
1429 check.errorf(x, TooManyValues, "multiple-value %s in single-value context", x)
1430 x.invalidate()
1431 }
1432 }
1433 }
1434
1435
1436 var op2tok = [...]token.Token{
1437 syntax.Def: token.ILLEGAL,
1438 syntax.Not: token.NOT,
1439 syntax.Recv: token.ILLEGAL,
1440
1441 syntax.OrOr: token.LOR,
1442 syntax.AndAnd: token.LAND,
1443
1444 syntax.Eql: token.EQL,
1445 syntax.Neq: token.NEQ,
1446 syntax.Lss: token.LSS,
1447 syntax.Leq: token.LEQ,
1448 syntax.Gtr: token.GTR,
1449 syntax.Geq: token.GEQ,
1450
1451 syntax.Add: token.ADD,
1452 syntax.Sub: token.SUB,
1453 syntax.Or: token.OR,
1454 syntax.Xor: token.XOR,
1455
1456 syntax.Mul: token.MUL,
1457 syntax.Div: token.QUO,
1458 syntax.Rem: token.REM,
1459 syntax.And: token.AND,
1460 syntax.AndNot: token.AND_NOT,
1461 syntax.Shl: token.SHL,
1462 syntax.Shr: token.SHR,
1463 }
1464
View as plain text