forked from noir-lang/noir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeric_generics.rs
More file actions
1329 lines (1149 loc) · 31.1 KB
/
Copy pathnumeric_generics.rs
File metadata and controls
1329 lines (1149 loc) · 31.1 KB
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 test_case::test_case;
use crate::tests::{UnstableFeature, assert_no_errors, check_errors, check_errors_using_features};
#[test]
fn numeric_generic_in_function_signature() {
let src = r#"
pub fn foo<let N: u32>(arr: [Field; N]) -> [Field; N] { arr }
"#;
assert_no_errors(src);
}
#[test]
fn numeric_generic_as_struct_field_type_fails() {
let src = r#"
pub struct Foo<let N: u32> {
a: Field,
b: N,
^ Expected type, found numeric generic
~ not a type
}
"#;
check_errors(src);
}
#[test]
fn normal_generic_as_array_length() {
// TODO: improve error location, should be just on N
let src = r#"
pub struct Foo<N> {
a: Field,
b: [Field; N],
^^^^^^^^^^ Type provided when a numeric generic was expected
~~~~~~~~~~ the numeric generic is not of type `u32`
}
"#;
check_errors(src);
}
#[test]
fn struct_array_len() {
let src = r#"
struct Array<T, let N: u32> {
inner: [T; N],
}
impl<T, let N: u32> Array<T, N> {
pub fn len(self) -> u32 {
^^^^ unused variable self
~~~~ unused variable
N as u32
}
}
fn main(xs: [Field; 2]) {
let ys = Array {
inner: xs,
};
assert(ys.len() == 2);
}
"#;
check_errors(src);
}
// Regression for #2540
#[test]
fn for_loop_over_array() {
let src = r#"
fn hello<let N: u32>(_array: [bool; N]) {
for _ in 0..N {}
}
fn main() {
let array: [bool; 2] = [false, true];
hello(array);
}
"#;
assert_no_errors(src);
}
#[test]
fn numeric_generic_as_param_type() {
let src = r#"
pub fn foo<let I: u32>(x: I) -> I {
^ Expected type, found numeric generic
~ not a type
^ Expected type, found numeric generic
~ not a type
let _q: I = 5;
^ Expected type, found numeric generic
~ not a type
x
}
"#;
check_errors(src);
}
#[test]
fn numeric_generic_as_unused_param_type() {
let src = r#"
pub fn foo<let I: u32>(_x: I) { }
^ Expected type, found numeric generic
~ not a type
"#;
check_errors(src);
}
#[test]
fn numeric_generic_as_unused_trait_fn_param_type() {
let src = r#"
trait Foo {
^^^ unused trait Foo
~~~ unused trait
fn foo<let I: u32>(_x: I) { }
^ Expected type, found numeric generic
~ not a type
}
"#;
check_errors(src);
}
#[test]
fn numeric_generic_as_return_type() {
let src = r#"
// std::mem::zeroed() without stdlib
trait Zeroed {
fn zeroed<T>(self) -> T;
}
fn foo<T, let I: Field>(x: T) -> I where T: Zeroed {
^ Expected type, found numeric generic
~ not a type
^^^ unused function foo
~~~ unused function
x.zeroed()
^^^^^^^^ Type annotation needed
~~~~~~~~ Could not determine the type of the generic argument `T` declared on the function `zeroed`
}
"#;
check_errors(src);
}
#[test]
fn numeric_generic_used_in_nested_type_fails() {
let src = r#"
pub struct Foo<let N: u32> {
a: Field,
b: Bar<N>,
}
pub struct Bar<let N: u32> {
inner: N
^ Expected type, found numeric generic
~ not a type
}
"#;
check_errors(src);
}
#[test]
fn normal_generic_used_in_nested_array_length_fail() {
let src = r#"
pub struct Foo<N> {
a: Field,
b: Bar<N>,
^ Type provided when a numeric generic was expected
~ the numeric generic is not of type `u32`
}
pub struct Bar<let N: u32> {
inner: [Field; N]
}
"#;
check_errors(src);
}
#[test]
fn numeric_generic_used_in_nested_type_pass() {
// The order of these structs should not be changed to make sure
// that we are accurately resolving all struct generics before struct fields
let src = r#"
pub struct NestedNumeric<let N: u32> {
a: Field,
b: InnerNumeric<N>
}
pub struct InnerNumeric<let N: u32> {
inner: [u64; N],
}
"#;
assert_no_errors(src);
}
#[test]
fn numeric_generic_used_in_trait() {
// We want to make sure that `N` in `impl<let N: u32, T> Deserialize<N, T>` does
// not trigger `expected type, found numeric generic parameter N` as the trait
// does in fact expect a numeric generic.
let src = r#"
struct MyType<T> {
a: Field,
b: Field,
c: Field,
d: T,
}
impl<let N: u32, T> Deserialize<N, T> for MyType<T> {
fn deserialize(fields: [Field; N], other: T) -> Self {
MyType { a: fields[0], b: fields[1], c: fields[2], d: other }
}
}
trait Deserialize<let N: u32, T> {
fn deserialize(fields: [Field; N], other: T) -> Self;
}
"#;
assert_no_errors(src);
}
#[test]
fn numeric_generic_in_trait_impl_with_extra_impl_generics() {
let src = r#"
trait Default2 {
fn default2() -> Self;
}
struct MyType<T> {
a: Field,
b: Field,
c: Field,
d: T,
}
// Make sure that `T` is placed before `N` as we want to test that the order of the generics is correctly maintained.
// `N` is used first in the trait impl generics (`Deserialize<N> for MyType<T>`).
// We want to make sure that the compiler correctly accounts for that `N` has a numeric kind
// while `T` has a normal kind.
impl<T, let N: u32> Deserialize<N> for MyType<T> where T: Default2 {
fn deserialize(fields: [Field; N]) -> Self {
MyType { a: fields[0], b: fields[1], c: fields[2], d: T::default2() }
}
}
trait Deserialize<let N: u32> {
fn deserialize(fields: [Field; N]) -> Self;
}
"#;
assert_no_errors(src);
}
#[test]
fn numeric_generic_used_in_where_clause() {
let src = r#"
trait Deserialize<let N: u32> {
fn deserialize(fields: [Field; N]) -> Self;
}
pub fn read<T, let N: u32>() -> T where T: Deserialize<N> {
let mut fields: [Field; N] = [0; N];
for i in 0..N {
fields[i] = i as Field + 1;
}
T::deserialize(fields)
}
"#;
assert_no_errors(src);
}
#[test]
fn numeric_generic_used_in_turbofish() {
let src = r#"
pub fn double<let N: u32>() -> u32 {
// Used as an expression
N * 2
}
pub fn double_numeric_generics_test() {
// Example usage of a numeric generic arguments.
assert(double::<9>() == 18);
assert(double::<7 + 8>() == 30);
}
"#;
assert_no_errors(src);
}
#[test]
fn numeric_generic_u16_array_size() {
// TODO: improve the error location
let src = r#"
fn len<let N: u32>(_arr: [Field; N]) -> u32 {
N
}
pub fn foo<let N: u16>() -> u32 {
let fields: [Field; N] = [0; N];
^ The numeric generic is not of type `u32`
~ expected `u32`, found `u16`
^^^^^^^^^^ The numeric generic is not of type `u32`
~~~~~~~~~~ expected `u32`, found `u16`
len(fields)
^^^ Type annotation needed
~~~ Could not determine the value of the generic argument `N` declared on the function `len`
}
"#;
check_errors(src);
}
#[test]
fn numeric_generic_field_larger_than_u32() {
let src = r#"
global A: Field = 4294967297;
fn foo<let A: Field>() { }
fn main() {
let _ = foo::<A>();
}
"#;
assert_no_errors(src);
}
#[test]
fn numeric_generic_field_arithmetic_larger_than_u32() {
let src = r#"
struct Foo<let F: Field> {}
fn size<let F: Field>(_x: Foo<F>) -> Field {
F
}
// 2^32 - 1
global A: Field = 4294967295;
fn foo<let A: Field>() -> Foo<A + A> {
Foo {}
}
fn main() {
let _ = size(foo::<A>());
}
"#;
assert_no_errors(src);
}
#[test]
fn constant_used_with_numeric_generic() {
let src = r#"
struct ValueNote {
value: Field,
}
trait Serialize<let N: u32> {
fn serialize(self) -> [Field; N];
}
impl Serialize<1> for ValueNote {
fn serialize(self) -> [Field; 1] {
[self.value]
}
}
fn main() {
let _ = ValueNote { value: 1 }; // silence ValueNote never constructed warning
}
"#;
assert_no_errors(src);
}
#[test]
fn normal_generic_used_when_numeric_expected_in_where_clause() {
let src = r#"
trait Deserialize<let N: u32> {
fn deserialize(fields: [Field; N]) -> Self;
}
pub fn read<T, N>() -> T where T: Deserialize<N> {
^ Type provided when a numeric generic was expected
~ the numeric generic is not of type `u32`
T::deserialize([0, 1])
}
"#;
check_errors(src);
// TODO: improve the error location for the array (should be on N)
let src = r#"
trait Deserialize<let N: u32> {
fn deserialize(fields: [Field; N]) -> Self;
}
pub fn read<T, N>() -> T where T: Deserialize<N> {
^ Type provided when a numeric generic was expected
~ the numeric generic is not of type `u32`
let mut fields: [Field; N] = [0; N];
^ Type provided when a numeric generic was expected
~ the numeric generic is not of type `u32`
^^^^^^^^^^ Type provided when a numeric generic was expected
~~~~~~~~~~ the numeric generic is not of type `u32`
for i in 0..N {
^ cannot find `N` in this scope
~ not found in this scope
fields[i] = i as Field + 1;
}
T::deserialize(fields)
}
"#;
check_errors(src);
}
#[test]
fn numeric_generics_type_kind_mismatch() {
let src = r#"
fn foo<let N: u32>() -> u16 {
N as u16
}
global J: u16 = 10;
fn bar<let N: u16>() -> u16 {
foo::<J>()
^ The numeric generic is not of type `u32`
~ expected `u32`, found `u16`
}
global M: u16 = 3;
fn main() {
let _ = bar::<M>();
}
"#;
check_errors(src);
}
#[test]
fn numeric_generics_value_kind_mismatch_u32_u64() {
let src = r#"
struct BoundedVec<T, let MaxLen: u32> {
storage: [T; MaxLen],
// can't be compared to MaxLen: u32
// can't be used to index self.storage
len: u64,
}
impl<T, let MaxLen: u32> BoundedVec<T, MaxLen> {
pub fn extend_from_bounded_vec<let Len: u32>(&mut self, _vec: BoundedVec<T, Len>) {
// We do this to avoid an unused variable warning on `self`
let _ = self.len;
for _ in 0..Len { }
}
pub fn push(&mut self, elem: T) {
assert(self.len < MaxLen, "push out of bounds");
^^^^^^^^^^^^^^^^^ Integers must have the same bit width LHS is 64, RHS is 32
self.storage[self.len] = elem;
^^^^^^^^ Indexing arrays and vectors must be done with `u32`, not `u64`
self.len += 1;
}
}
fn main() {
let _ = BoundedVec { storage: [1], len: 1 }; // silence never constructed warning
}
"#;
check_errors(src);
}
#[test]
fn use_non_u32_generic_in_struct() {
let src = r#"
struct S<let N: u8> {}
fn main() {
let _: S<3u8> = S {};
}
"#;
assert_no_errors(src);
}
#[test]
fn use_numeric_generic_in_trait_method() {
let src = r#"
trait Foo {
fn foo<let N: u32>(self, x: [u8; N]) -> Self;
}
struct Bar;
impl Foo for Bar {
fn foo<let N: u32>(self, _x: [u8; N]) -> Self {
self
}
}
fn main() {
let bytes: [u8; 3] = [1,2,3];
let _ = Bar{}.foo(bytes);
}
"#;
assert_no_errors(src);
}
#[test]
fn struct_numeric_generic_in_function() {
let src = r#"
struct Foo {
inner: u64
}
pub fn bar<let N: Foo>() {
^^^ N has a type of Foo. The only supported numeric generic types are integers and `Field`.
~~~ Unsupported numeric generic type
let _ = Foo { inner: 1 }; // silence Foo never constructed warning
}
"#;
check_errors(src);
}
#[test]
fn struct_numeric_generic_in_struct() {
let src = r#"
pub struct Foo {
inner: u64
}
pub struct Bar<let N: Foo> { }
^^^ N has a type of Foo. The only supported numeric generic types are integers and `Field`.
~~~ Unsupported numeric generic type
"#;
check_errors(src);
}
#[test]
fn bool_numeric_generic() {
let src = r#"
pub fn read<let N: bool>() -> Field {
^^^^ N has a type of bool. The only supported numeric generic types are integers and `Field`.
~~~~ Unsupported numeric generic type
if N {
0
} else {
1
}
}
"#;
check_errors(src);
}
#[test]
fn unresolved_numeric_generic_kind_does_not_panic() {
// If the annotated type of a numeric generic fails to resolve, the generic's
// kind ends up as `Kind::Numeric(Type::Error)`. Using the generic in an
// expression previously risked panicking in `check_defaultable_type_variables`
// because `Kind::Numeric(Type::Error)::default_type()` returned `None`.
let src = r#"
pub fn foo<let N: A>() {
^ Could not resolve 'A' in path
^ N has a type of error. The only supported numeric generic types are integers and `Field`.
~ Unsupported numeric generic type
bar(N);
^^^ cannot find `bar` in this scope
~~~ not found in this scope
}
"#;
check_errors(src);
}
#[test]
fn numeric_generic_binary_operation_type_mismatch() {
let src = r#"
pub fn foo<let N: Field>() -> bool {
let mut check: bool = true;
check = N;
^ Cannot assign an expression of type Field to a value of type bool
check
}
"#;
check_errors(src);
}
#[test]
fn bool_generic_as_loop_bound() {
let src = r#"
pub fn read<let N: bool>() {
^^^^ N has a type of bool. The only supported numeric generic types are integers and `Field`.
~~~~ Unsupported numeric generic type
let mut fields = [0; N];
^ The numeric generic is not of type `u32`
~ expected `u32`, found `bool`
for i in 0..N {
^ Expected type Field, found type bool
fields[i] = i + 1;
}
assert(fields[0] == 1);
}
"#;
check_errors(src);
}
/// Regression for CI issue in <https://github.com/noir-lang/noir/pull/10330>
#[test]
fn integer_with_suffix_used_as_type_in_quote() {
let src = "
#[make_bar]
fn main() {
bar([1, 2]);
}
comptime fn make_bar(_f: FunctionDefinition) -> Quoted {
let n = 2u32;
quote {
fn bar(_array: [Field; $n]) {}
}
}
";
assert_no_errors(src);
}
/// Regression for <https://github.com/noir-lang/noir/pull/10330#issuecomment-3499399843>
#[test]
fn integer_with_suffix_used_as_tuple_index() {
let src = "
fn main() {
macro!();
}
comptime fn macro() -> Quoted {
let n = 0u32;
quote {
let tuple = (0u8, 1u16, 2i8);
assert_eq(tuple.$n, 0);
}
}
";
assert_no_errors(src);
}
// Regression for https://github.com/noir-lang/noir/issues/10711
#[test]
fn no_panic_on_numeric_generic_parse_error() {
let src = "
fn foo<let N: >() {
^ Expected a type but found '>'
let _ = N;
}
fn main() { foo::<3>(); }
";
check_errors(src);
}
#[test]
fn regression_10555() {
let src = "
trait Trait {
fn foo<let X: u32>();
fn bar<X>();
fn baz<let X: u32>();
}
impl Trait for i32 {
fn foo<X>() {}
^ Expected type, found numeric generic
~ not a type
fn bar<let X: u32>() {}
^^^^^^ Type provided when a numeric generic was expected
~~~~~~ the numeric generic is not of type `u32`
fn baz<let X: u8>() {}
^^^^^ The numeric generic is not of type `u8`
~~~~~ expected `u8`, found `u32`
}
fn main() {}
";
check_errors(src);
}
#[test]
fn regression_10431_struct_impl() {
let src = r#"
pub struct Foo<T> {
x: T,
}
impl Foo<1> {}
^ Expected type, found numeric generic
~ not a type
fn main() {}
"#;
check_errors(src);
}
#[test]
fn regression_10431_enum_impl() {
let src = r#"
pub enum Foo<T> {
Bar(T),
}
impl Foo<1> {}
^ Expected type, found numeric generic
~ not a type
fn main() {}
"#;
let features = vec![UnstableFeature::Enums];
check_errors_using_features(src, &features);
}
#[test]
fn regression_10431_struct_function_parameter() {
let src = r#"
pub struct Foo<T> {
x: T,
}
pub fn foo(_x: Foo<1>) {}
^ Expected type, found numeric generic
~ not a type
fn main() {}
"#;
check_errors(src);
}
#[test]
fn regression_10431_enum_function_parameter() {
let src = r#"
pub enum Foo<T> {
Bar(T),
}
pub fn foo(_x: Foo<1>) {}
^ Expected type, found numeric generic
~ not a type
fn main() {}
"#;
let features = vec![UnstableFeature::Enums];
check_errors_using_features(src, &features);
}
#[test]
fn regression_10431_struct_function_return() {
let src = r#"
pub struct Foo<T> { }
pub fn foo() -> Foo<1> {
^ Expected type, found numeric generic
~ not a type
Foo {}
^^^ Type annotation needed
~~~ Could not determine the type of the generic argument `T` declared on the struct `Foo`
}
fn main() {}
"#;
check_errors(src);
}
#[test]
fn regression_10431_enum_function_return() {
let src = r#"
pub enum Foo<T> {
Bar(T),
Baz,
}
pub fn foo() -> Foo<1> {
^ Expected type, found numeric generic
~ not a type
Foo::Baz
^^^ Type annotation needed
~~~ Could not determine the type of the generic argument `T` declared on the enum `Foo`
}
fn main() {}
"#;
let features = vec![UnstableFeature::Enums];
check_errors_using_features(src, &features);
}
#[test]
fn regression_10431_struct_trait_impl() {
let src = r#"
pub struct Foo<T> {
x: T,
}
trait Bar {}
impl Bar for Foo<1> {}
^ Expected type, found numeric generic
~ not a type
fn main() {}
"#;
check_errors(src);
}
#[test]
fn regression_10431_enum_trait_impl() {
let src = r#"
pub enum Foo<T> {
Bar(T),
}
trait Bar {}
impl Bar for Foo<1> {}
^ Expected type, found numeric generic
~ not a type
fn main() {}
"#;
let features = vec![UnstableFeature::Enums];
check_errors_using_features(src, &features);
}
#[test]
fn regression_10431_enum() {
let src = r#"
pub enum Foo<T> {
Bar(T),
}
impl Foo<1> {}
^ Expected type, found numeric generic
~ not a type
fn main() {}
"#;
let features = vec![UnstableFeature::Enums];
check_errors_using_features(src, &features);
}
#[test]
fn regression_10431() {
let src = r#"
pub struct Foo<T> {
x: T,
}
impl Foo<1> {}
^ Expected type, found numeric generic
~ not a type
fn main() {}
"#;
check_errors(src);
}
#[test]
fn regression_10431_function_turbofish() {
let src = r#"
fn main() {
foo::<1>();
^ Expected type, found numeric generic
~ not a type
}
fn foo<T>() {}
"#;
check_errors(src);
}
#[test]
fn regression_10431_type_alias() {
let src = r#"
pub type Foo<T> = [T; 0];
pub type Bar = Foo<1>;
^ Expected type, found numeric generic
~ not a type
fn main() { }
"#;
check_errors(src);
}
#[test]
fn numeric_generic_trait_method_via_instance() {
let src = r#"
struct Vector<let N: u32> {
data: [Field; N],
}
trait Process {
fn process(self) -> Field;
}
impl<let N: u32> Process for Vector<N> {
fn process(self) -> Field {
self.data[0]
}
}
fn do_process<let M: u32>(v: Vector<M>) -> Field {
v.process()
}
fn main() {
let v = Vector { data: [1, 2, 3] };
let _ = do_process(v);
}
"#;
assert_no_errors(src);
}
#[test]
fn numeric_generic_static_trait_method_turbofish() {
let src = r#"
struct Matrix<let R: u32, let C: u32> {}
trait Dimensions {
fn rows() -> u32;
fn cols() -> u32;
}
impl<let R: u32, let C: u32> Dimensions for Matrix<R, C> {
fn rows() -> u32 { R }
fn cols() -> u32 { C }
}
fn get_rows<let R: u32, let C: u32>() -> u32 {
Matrix::<R, C>::rows()
}
fn main() {
let _ = get_rows::<3, 4>();
}
"#;
assert_no_errors(src);
}
#[test]
fn generic_fn_trait_method_on_struct_with_numeric_generic() {
let src = r#"
struct Arr<let N: u32> {
inner: [Field; N],
}
trait Len {
fn len(self) -> u32;
}
impl<let N: u32> Len for Arr<N> {
fn len(self) -> u32 { N }
}
fn get_len<let N: u32>(a: Arr<N>) -> u32 {
a.len()
}
fn main() {
let a = Arr { inner: [1, 2, 3] };
assert(get_len(a) == 3);
}
"#;
assert_no_errors(src);
}
#[test]
fn numeric_generic_propagation_chain() {
let src = r#"
fn identity<let N: u32>(arr: [Field; N]) -> [Field; N] {
arr
}
fn wrap_identity<let N: u32>(arr: [Field; N]) -> [Field; N] {
identity(arr)
}
fn double_wrap<let N: u32>(arr: [Field; N]) -> [Field; N] {
wrap_identity(arr)
}
fn main() {
let arr = [1, 2, 3];
let result = double_wrap(arr);
assert(result[0] == 1);
}
"#;
assert_no_errors(src);
}
#[test]
fn trait_with_generic_method_on_numeric_generic_struct() {
let src = r#"
trait Apply {
fn apply<U>(self, f: fn(Field) -> U) -> U;
}
struct Single<let N: u32> {
data: [Field; N],
}
impl<let N: u32> Apply for Single<N> {
fn apply<U>(self, f: fn(Field) -> U) -> U {
f(self.data[0])
}
}
fn main() {
let s = Single { data: [42] };