-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
tests.rs
1506 lines (1396 loc) · 36.2 KB
/
tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::{fmt::Write, sync::Arc};
use mun_hir_input::WithFixture;
use crate::{
code_model::AssocItem, diagnostics::DiagnosticSink, expr::BodySourceMap, mock::MockDatabase,
HirDisplay, InferenceResult, ModuleDef, Package,
};
#[test]
fn issue_354() {
insta::assert_snapshot!(infer(
r#"
fn value() -> i64 { 6 }
pub fn main() {
let t = 2;
t = loop { break value(); };
}"#),
@r###"
18..23 '{ 6 }': i64
20..21 '6': i64
39..90 '{ ...; }; }': ()
49..50 't': i64
53..54 '2': i64
60..61 't': i64
60..87 't = lo...e(); }': ()
64..87 'loop {...e(); }': i64
69..87 '{ brea...e(); }': never
71..84 'break value()': never
77..82 'value': function value() -> i64
77..84 'value()': i64
"###);
}
#[test]
fn array_element_assignment() {
insta::assert_snapshot!(infer(
r"
fn main() {
let a = [1,2,3,4,5]
a[2] = 4u8
}",
), @r###"
10..52 '{ ... 4u8 }': ()
20..21 'a': [u8]
24..35 '[1,2,3,4,5]': [u8]
25..26 '1': u8
27..28 '2': u8
29..30 '3': u8
31..32 '4': u8
33..34 '5': u8
40..41 'a': [u8]
40..44 'a[2]': u8
40..50 'a[2] = 4u8': ()
42..43 '2': i32
47..50 '4u8': u8
"###);
}
#[test]
fn array_is_place_expr() {
insta::assert_snapshot!(infer(
r"
fn main() {
let a = [1,2,3,4,5]
a = [5,6,7]
a[0] = 0;
[1,2,3][0] = 4
}",
), @r###"
10..86 '{ ... = 4 }': ()
20..21 'a': [i32]
24..35 '[1,2,3,4,5]': [i32]
25..26 '1': i32
27..28 '2': i32
29..30 '3': i32
31..32 '4': i32
33..34 '5': i32
40..41 'a': [i32]
40..51 'a = [5,6,7]': ()
44..51 '[5,6,7]': [i32]
45..46 '5': i32
47..48 '6': i32
49..50 '7': i32
56..57 'a': [i32]
56..60 'a[0]': i32
56..64 'a[0] = 0': ()
58..59 '0': i32
63..64 '0': i32
70..77 '[1,2,3]': [i32]
70..80 '[1,2,3][0]': i32
70..84 '[1,2,3][0] = 4': ()
71..72 '1': i32
73..74 '2': i32
75..76 '3': i32
78..79 '0': i32
83..84 '4': i32
"###);
}
#[test]
fn infer_array_structs() {
insta::assert_snapshot!(infer(
r"
struct Foo;
fn main() -> Foo {
let a = [Foo, Foo, Foo];
a[2]
}",
), @r###"
34..75 '{ ...a[2] }': Foo
44..45 'a': [Foo]
48..63 '[Foo, Foo, Foo]': [Foo]
49..52 'Foo': Foo
54..57 'Foo': Foo
59..62 'Foo': Foo
69..70 'a': [Foo]
69..73 'a[2]': Foo
71..72 '2': i32
"###);
}
#[test]
fn infer_array() {
insta::assert_snapshot!(infer(
r"
fn main() -> u8 {
let b = 5;
let c = 3;
let a = [1,b,4,c];
a[3]
}",
), @r###"
16..81 '{ ...a[3] }': u8
26..27 'b': u8
30..31 '5': u8
41..42 'c': u8
45..46 '3': u8
56..57 'a': [u8]
60..69 '[1,b,4,c]': [u8]
61..62 '1': u8
63..64 'b': u8
65..66 '4': u8
67..68 'c': u8
75..76 'a': [u8]
75..79 'a[3]': u8
77..78 '3': i32
"###);
}
#[test]
fn private_access() {
insta::assert_snapshot!(infer(
r#"
//- /foo.mun
struct Foo {};
struct Bar(i64);
struct Baz;
fn foo() {}
type FooBar = Foo;
pub struct PubFoo {};
pub struct PubBar(i64);
pub struct PubBaz;
pub fn pub_foo() {}
pub type PubFooBar = PubFoo;
pub(super) struct PubSupFoo {};
pub(super) struct PubSupBar(i64);
pub(super) struct PubSupBaz;
pub(super) fn pub_sup_foo() {}
pub(super) type PubSupFooBar = PubSupFoo;
pub(package) struct PubPackageFoo {};
pub(package) struct PubPackageBar(i64);
pub(package) struct PubPackageBaz;
pub(package) fn pub_package_foo() {}
pub(package) type PubPackageFooBar = PubPackageFoo;
//- /bar.mun
fn main() {
let a = package::foo::Foo {}; // private access
let a = package::foo::Bar(3); // private access
let a = package::foo::Baz; // private access
let a = package::foo::FooBar{}}; // private access
let a = super::foo::Foo {}; // private access
let a = super::foo::Bar(3); // private access
let a = super::foo::Baz; // private access
let a = super::foo::FooBar{}; // private access
package::foo::foo(); // private access
super::foo::foo(); // private access
let a = package::foo::PubFoo {};
let a = package::foo::PubBar(3);
let a = package::foo::PubBaz;
let a = package::foo::PubFooBar{};
let a = super::foo::PubFoo {};
let a = super::foo::PubBar(3);
let a = super::foo::PubBaz;
let a = super::foo::PubFooBar{};
package::foo::pub_foo();
super::foo::pub_foo();
let a = package::foo::PubSupFoo {};
let a = package::foo::PubSupBar(3);
let a = package::foo::PubSupBaz;
let a = package::foo::PubSupFooBar{};
let a = super::foo::PubSupFoo {};
let a = super::foo::PubSupBar(3);
let a = super::foo::PubSupBaz;
let a = super::foo::PubSupFooBar{};
package::foo::pub_sup_foo();
super::foo::pub_sup_foo();
let a = package::foo::PubPackageFoo {};
let a = package::foo::PubPackageBar(3);
let a = package::foo::PubPackageBaz;
let a = package::foo::PubPackageFooBar{};
let a = super::foo::PubPackageFoo {};
let a = super::foo::PubPackageBar(3);
let a = super::foo::PubPackageBaz;
let a = super::foo::PubPackageFooBar{};
package::foo::pub_package_foo();
super::foo::pub_package_foo();
}
//- /foo/baz.mun
fn main() {
let a = package::foo::Foo {};
let a = package::foo::Bar(3);
let a = package::foo::Baz;
let a = package::foo::FooBar{};
let a = super::Foo {};
let a = super::Bar(3);
let a = super::Baz;
let a = super::FooBar{};
package::foo::foo();
super::foo();
}
//- /mod.mun
fn main() {
let a = package::foo::Foo {}; // private access
let a = package::foo::Bar(3); // private access
let a = package::foo::Baz; // private access
let a = package::foo::FooBar{}; // private access
let a = foo::Foo {}; // private access
let a = foo::Bar(3); // private access
let a = foo::Baz; // private access
let a = foo::FooBar{}; // private access
package::foo::foo(); // private access
foo::foo(); // private access
let a = package::foo::PubSupFoo {};
let a = package::foo::PubSupBar(3);
let a = package::foo::PubSupBaz;
let a = package::foo::PubSupFooBar{};
let a = foo::PubSupFoo {};
let a = foo::PubSupBar(3);
let a = foo::PubSupBaz;
let a = foo::PubSupFooBar{};
package::foo::pub_sup_foo();
foo::pub_sup_foo();
}
"#),
@r###"
24..41: access of private type
76..93: access of private type
128..145: access of private type
177..197: access of private type
232..240: access of private type
275..283: access of private type
318..326: access of private type
358..369: access of private type
396..413: access of private type
439..447: access of private type
24..41: access of private type
76..93: access of private type
128..145: access of private type
177..197: access of private type
10..812 '{ ...o(); }': ()
20..21 'a': Foo
24..44 'packag...Foo {}': Foo
72..73 'a': Bar
76..93 'packag...o::Bar': ctor Bar(i64) -> Bar
76..96 'packag...Bar(3)': Bar
94..95 '3': i64
124..125 'a': Baz
128..145 'packag...o::Baz': Baz
173..174 'a': FooBar
177..199 'packag...oBar{}': FooBar
228..229 'a': Foo
232..243 'foo::Foo {}': Foo
271..272 'a': Bar
275..283 'foo::Bar': ctor Bar(i64) -> Bar
275..286 'foo::Bar(3)': Bar
284..285 '3': i64
314..315 'a': Baz
318..326 'foo::Baz': Baz
354..355 'a': FooBar
358..371 'foo::FooBar{}': FooBar
396..413 'packag...o::foo': function foo() -> ()
396..415 'packag...:foo()': ()
439..447 'foo::foo': function foo() -> ()
439..449 'foo::foo()': ()
478..479 'a': PubSupFoo
482..508 'packag...Foo {}': PubSupFoo
518..519 'a': PubSupBar
522..545 'packag...SupBar': ctor PubSupBar(i64) -> PubSupBar
522..548 'packag...Bar(3)': PubSupBar
546..547 '3': i64
558..559 'a': PubSupBaz
562..585 'packag...SupBaz': PubSupBaz
595..596 'a': PubSupFooBar
599..627 'packag...oBar{}': PubSupFooBar
638..639 'a': PubSupFoo
642..659 'foo::P...Foo {}': PubSupFoo
669..670 'a': PubSupBar
673..687 'foo::PubSupBar': ctor PubSupBar(i64) -> PubSupBar
673..690 'foo::P...Bar(3)': PubSupBar
688..689 '3': i64
700..701 'a': PubSupBaz
704..718 'foo::PubSupBaz': PubSupBaz
728..729 'a': PubSupFooBar
732..751 'foo::P...oBar{}': PubSupFooBar
758..783 'packag...up_foo': function pub_sup_foo() -> ()
758..785 'packag..._foo()': ()
791..807 'foo::p...up_foo': function pub_sup_foo() -> ()
791..809 'foo::p..._foo()': ()
10..200 '{ ...Bar{}}': ()
20..21 'a': Foo
24..44 'packag...Foo {}': Foo
72..73 'a': Bar
76..93 'packag...o::Bar': ctor Bar(i64) -> Bar
76..96 'packag...Bar(3)': Bar
94..95 '3': i64
124..125 'a': Baz
128..145 'packag...o::Baz': Baz
173..174 'a': FooBar
177..199 'packag...oBar{}': FooBar
53..55 '{}': ()
158..160 '{}': ()
314..316 '{}': ()
507..509 '{}': ()
10..300 '{ ...o(); }': ()
20..21 'a': Foo
24..44 'packag...Foo {}': Foo
54..55 'a': Bar
58..75 'packag...o::Bar': ctor Bar(i64) -> Bar
58..78 'packag...Bar(3)': Bar
76..77 '3': i64
88..89 'a': Baz
92..109 'packag...o::Baz': Baz
119..120 'a': FooBar
123..145 'packag...oBar{}': FooBar
156..157 'a': Foo
160..173 'super::Foo {}': Foo
183..184 'a': Bar
187..197 'super::Bar': ctor Bar(i64) -> Bar
187..200 'super::Bar(3)': Bar
198..199 '3': i64
210..211 'a': Baz
214..224 'super::Baz': Baz
234..235 'a': FooBar
238..253 'super::FooBar{}': FooBar
260..277 'packag...o::foo': function foo() -> ()
260..279 'packag...:foo()': ()
285..295 'super::foo': function foo() -> ()
285..297 'super::foo()': ()
"###);
}
#[test]
fn scoped_path() {
insta::assert_snapshot!(infer(
r"
//- /mod.mun
struct Foo;
fn main() -> self::Foo {
Foo
}
fn bar() -> Foo {
super::Foo // undefined value
}
fn baz() -> Foo {
package::Foo
}
//- /foo.mun
struct Foo;
fn bar() -> Foo {
super::Foo // mismatched type
}
fn baz() -> package::Foo {
super::Foo
}
fn nested() -> self::Foo {
package::foo::Foo
}
"),
@r###"
71..81: undefined value
35..45: mismatched type
29..67: mismatched type
36..47 '{ Foo }': Foo
42..45 'Foo': Foo
65..103 '{ ...alue }': Foo
71..81 'super::Foo': {unknown}
121..141 '{ ...:Foo }': Foo
127..139 'package::Foo': Foo
29..67 '{ ...type }': Foo
35..45 'super::Foo': Foo
94..112 '{ ...:Foo }': Foo
100..110 'super::Foo': Foo
139..164 '{ ...:Foo }': Foo
145..162 'packag...o::Foo': Foo
"###);
}
#[test]
fn comparison_not_implemented_for_struct() {
insta::assert_snapshot!(infer(
r"
struct Foo;
fn main() -> bool {
Foo == Foo
}"),
@r###"
37..47: cannot apply binary operator
31..49 '{ ... Foo }': bool
37..40 'Foo': Foo
37..47 'Foo == Foo': bool
44..47 'Foo': Foo
"###);
}
#[test]
fn infer_literals() {
insta::assert_snapshot!(infer(
r"
fn integer() -> i32 {
0
}
fn large_unsigned_integer() -> u128 {
0
}
fn with_let() -> u16 {
let b = 4;
let a = 4;
a
}
"),
@r###"
20..29 '{ 0 }': i32
26..27 '0': i32
67..76 '{ 0 }': u128
73..74 '0': u128
99..138 '{ ... a }': u16
109..110 'b': i32
113..114 '4': i32
124..125 'a': u16
128..129 '4': u16
135..136 'a': u16
"###);
}
#[test]
fn infer_suffix_literals() {
insta::assert_snapshot!(infer(
r"
fn main(){
123;
123u8;
123u16;
123u32;
123u64;
123u128;
1_000_000_u32;
123i8;
123i16;
123i32;
123i64;
123i128;
1_000_000_i32;
1_000_123.0e-2;
1_000_123.0e-2f32;
1_000_123.0e-2f64;
9999999999999999999999999999999999999999999_f64;
}
fn add(a:u32) -> u32 {
a + 12u32
}
fn errors() {
0b22222; // invalid literal
0b00010_f32; // non-10 base f64
0o71234_f32; // non-10 base f64
1234_foo; // invalid suffix
1234.0_bar; // invalid suffix
9999999999999999999999999999999999999999999; // too large
256_u8; // literal out of range for `u8`
128_i8; // literal out of range for `i8`
12712371237123_u32; // literal out of range `u32`
9999999999999999999999999; // literal out of range `i32`
}
"),
@r###"
358..365: invalid literal value
390..401: binary float literal is not supported
426..437: octal float literal is not supported
462..470: invalid suffix `foo`
494..504: invalid suffix `bar`
528..571: int literal is too large
590..596: literal out of range for `u8`
635..641: literal out of range for `i8`
680..698: literal out of range for `u32`
734..759: literal out of range for `i32`
9..298 '{ ...f64; }': ()
15..18 '123': i32
24..29 '123u8': u8
35..41 '123u16': u16
47..53 '123u32': u32
59..65 '123u64': u64
71..78 '123u128': u128
84..97 '1_000_000_u32': u32
103..108 '123i8': i8
114..120 '123i16': i16
126..132 '123i32': i32
138..144 '123i64': i64
150..157 '123i128': i128
163..176 '1_000_000_i32': i32
182..196 '1_000_123.0e-2': f64
202..219 '1_000_...e-2f32': f32
225..242 '1_000_...e-2f64': f64
248..295 '999999...99_f64': f64
307..308 'a': u32
321..338 '{ ...2u32 }': u32
327..328 'a': u32
327..336 'a + 12u32': u32
331..336 '12u32': u32
352..792 '{ ...i32` }': ()
358..365 '0b22222': i32
390..401 '0b00010_f32': f32
426..437 '0o71234_f32': f32
462..470 '1234_foo': i32
494..504 '1234.0_bar': f64
528..571 '999999...999999': i32
590..596 '256_u8': u8
635..641 '128_i8': i8
680..698 '127123...23_u32': u32
734..759 '999999...999999': i32
"###);
}
#[test]
fn infer_invalid_struct_type() {
insta::assert_snapshot!(infer(
r"
fn main(){
let a = Foo {b: 3};
}"),
@r###"
23..26: undefined type
9..36 '{ ... 3}; }': ()
19..20 'a': {unknown}
23..33 'Foo {b: 3}': {unknown}
31..32 '3': i32
"###);
}
#[test]
fn infer_conditional_return() {
insta::assert_snapshot!(infer(
r#"
fn foo(a:int)->i32 {
if a > 4 {
return 4;
}
a
}
fn bar(a:i32)->i32 {
if a > 4 {
return 4;
} else {
return 1;
}
}
"#),
@r###"
9..12: undefined type
7..8 'a': {unknown}
19..67 '{ ... a }': i32
25..59 'if a >... }': ()
28..29 'a': {unknown}
28..33 'a > 4': bool
32..33 '4': i32
34..59 '{ ... }': never
44..52 'return 4': never
51..52 '4': i32
64..65 'a': {unknown}
76..77 'a': i32
88..161 '{ ... } }': i32
94..159 'if a >... }': i32
97..98 'a': i32
97..102 'a > 4': bool
101..102 '4': i32
103..128 '{ ... }': never
113..121 'return 4': never
120..121 '4': i32
134..159 '{ ... }': never
144..152 'return 1': never
151..152 '1': i32
"###);
}
#[test]
fn infer_return() {
insta::assert_snapshot!(infer(
r#"
fn test()->i32 {
return; // error: mismatched type
return 5;
}
"#),
@r###"
21..27: `return;` in a function whose return type is not `()`
15..70 '{ ...n 5; }': never
21..27 'return': never
59..67 'return 5': never
66..67 '5': i32
"###);
}
#[test]
fn infer_self_param() {
insta::assert_snapshot!(infer(
r#"
struct Foo {
a: i32
}
impl Foo {
fn with_self(self) -> Self {
self
}
}
"#
));
}
#[test]
fn infer_self_field() {
insta::assert_snapshot!(infer(
r#"
struct Foo {
a: i32
}
impl Foo {
fn self_a(self) -> i32 {
self.a
}
fn self_b(self) -> i32 {
self.b // error: attempted to access a non-existent field in a struct.
}
}
"#
));
}
#[test]
fn infer_assoc_function() {
insta::assert_snapshot!(infer(
r#"
struct Foo {
a: i32
}
impl Foo {
fn new() -> Self {
Self { a: 3 }
}
}
fn main() {
let a = Foo::new();
}
"#
));
}
#[test]
fn infer_access_hidden_assoc_function() {
insta::assert_snapshot!(infer(
r#"
//- /foo.mun
pub struct Foo {
a: i32
}
impl Foo {
fn new(){}
}
//- /mod.mun
fn main() {
foo::Foo::new();
}
"#
));
}
#[test]
fn infer_basics() {
insta::assert_snapshot!(infer(
r#"
fn test(a:i32, b:f64, c:never, d:bool) -> bool {
a;
b;
c;
d
}
"#),
@r###"
8..9 'a': i32
15..16 'b': f64
22..23 'c': never
31..32 'd': bool
47..77 '{ ... d }': never
53..54 'a': i32
60..61 'b': f64
67..68 'c': never
74..75 'd': bool
"###);
}
#[test]
fn infer_branching() {
insta::assert_snapshot!(infer(
r#"
fn test() {
let a = if true { 3 } else { 4 }
let b = if true { 3 } // Missing else branch
let c = if true { 3; }
let d = if true { 5 } else if false { 3 } else { 4 }
let e = if true { 5.0 } else { 5 } // Mismatched branches
}
"#),
@r###"
61..74: missing else branch
208..234: mismatched branches
10..260 '{ ...ches }': ()
20..21 'a': i32
24..48 'if tru... { 4 }': i32
27..31 'true': bool
32..37 '{ 3 }': i32
34..35 '3': i32
43..48 '{ 4 }': i32
45..46 '4': i32
57..58 'b': ()
61..74 'if true { 3 }': ()
64..68 'true': bool
69..74 '{ 3 }': i32
71..72 '3': i32
120..121 'c': ()
124..138 'if true { 3; }': ()
127..131 'true': bool
132..138 '{ 3; }': ()
134..135 '3': i32
147..148 'd': i32
151..195 'if tru... { 4 }': i32
154..158 'true': bool
159..164 '{ 5 }': i32
161..162 '5': i32
170..195 'if fal... { 4 }': i32
173..178 'false': bool
179..184 '{ 3 }': i32
181..182 '3': i32
190..195 '{ 4 }': i32
192..193 '4': i32
204..205 'e': f64
208..234 'if tru... { 5 }': f64
211..215 'true': bool
216..223 '{ 5.0 }': f64
218..221 '5.0': f64
229..234 '{ 5 }': i32
231..232 '5': i32
"###);
}
#[test]
fn void_return() {
insta::assert_snapshot!(infer(
r#"
fn bar() {
let a = 3;
}
fn foo(a:i32) {
let c = bar()
}
"#),
@r###"
9..27 '{ ...= 3; }': ()
19..20 'a': i32
23..24 '3': i32
35..36 'a': i32
42..63 '{ ...ar() }': ()
52..53 'c': ()
56..59 'bar': function bar() -> ()
56..61 'bar()': ()
"###);
}
#[test]
fn place_expressions() {
insta::assert_snapshot!(infer(
r#"
fn foo(a:i32) {
a += 3;
3 = 5; // error: invalid left hand side of expression
}
"#),
@r###"
32..33: invalid left hand side of expression
7..8 'a': i32
14..87 '{ ...sion }': ()
20..21 'a': i32
20..26 'a += 3': ()
25..26 '3': i32
32..33 '3': i32
32..37 '3 = 5': ()
36..37 '5': i32
"###);
}
#[test]
fn update_operators() {
insta::assert_snapshot!(infer(
r#"
fn foo(a:i32, b:f64) {
a += 3;
a -= 3;
a *= 3;
a /= 3;
a %= 3;
b += 3.0;
b -= 3.0;
b *= 3.0;
b /= 3.0;
b %= 3.0;
a *= 3.0; // mismatched type
b *= 3; // mismatched type
}
"#),
@r###"
162..165: mismatched type
195..196: mismatched type
7..8 'a': i32
14..15 'b': f64
21..218 '{ ...type }': ()
27..28 'a': i32
27..33 'a += 3': ()
32..33 '3': i32
39..40 'a': i32
39..45 'a -= 3': ()
44..45 '3': i32
51..52 'a': i32
51..57 'a *= 3': ()
56..57 '3': i32
63..64 'a': i32
63..69 'a /= 3': ()
68..69 '3': i32
75..76 'a': i32
75..81 'a %= 3': ()
80..81 '3': i32
87..88 'b': f64
87..95 'b += 3.0': ()
92..95 '3.0': f64
101..102 'b': f64
101..109 'b -= 3.0': ()
106..109 '3.0': f64
115..116 'b': f64
115..123 'b *= 3.0': ()
120..123 '3.0': f64
129..130 'b': f64
129..137 'b /= 3.0': ()
134..137 '3.0': f64
143..144 'b': f64
143..151 'b %= 3.0': ()
148..151 '3.0': f64
157..158 'a': i32
157..165 'a *= 3.0': ()
162..165 '3.0': f64
190..191 'b': f64
190..196 'b *= 3': ()
195..196 '3': i32
"###);
}
#[test]
fn infer_unary_ops() {
insta::assert_snapshot!(infer(
r#"
fn foo(a: i32, b: bool) {
a = -a;
b = !b;
}
"#),
@r###"
7..8 'a': i32
15..16 'b': bool
24..51 '{ ... !b; }': ()
30..31 'a': i32
30..36 'a = -a': ()
34..36 '-a': i32
35..36 'a': i32
42..43 'b': bool
42..48 'b = !b': ()
46..48 '!b': bool
47..48 'b': bool
"###);
}
#[test]
fn invalid_unary_ops() {
insta::assert_snapshot!(infer(
r#"
fn bar(a: f64, b: bool) {
a = !a; // mismatched type
b = -b; // mismatched type
}
"#),
@r###"
35..36: cannot apply unary operator
66..67: cannot apply unary operator
7..8 'a': f64
15..16 'b': bool
24..89 '{ ...type }': ()
30..31 'a': f64
30..36 'a = !a': ()
34..36 '!a': {unknown}
35..36 'a': f64
61..62 'b': bool
61..67 'b = -b': ()
65..67 '-b': {unknown}
66..67 'b': bool
"###);
}
#[test]
fn infer_loop() {
insta::assert_snapshot!(infer(
r#"
fn foo() {
loop {}
}
"#),
@r###"
9..24 '{ loop {} }': never
15..22 'loop {}': never
20..22 '{}': ()
"###);
}
#[test]
fn infer_break() {
insta::assert_snapshot!(infer(
r#"
fn foo()->i32 {
break; // error: not in a loop
loop { break 3; break 3.0; } // error: mismatched type
let a:i32 = loop { break 3.0; } // error: mismatched type