forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparam.rs
More file actions
862 lines (762 loc) · 24.5 KB
/
Copy pathparam.rs
File metadata and controls
862 lines (762 loc) · 24.5 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
use rowan::ast::{AstNode, support};
use super::ast_node;
use crate::{FeLang, SyntaxKind as SK, SyntaxToken, ast::Type};
ast_node! {
/// A list of parameters.
/// `(self, a: u256, b: u256)`
pub struct FuncParamList,
SK::FuncParamList,
IntoIterator<Item=FuncParam>,
}
ast_node! {
/// A single parameter.
/// `self`
/// `a: u256`
/// `_ a: u256`
pub struct FuncParam,
SK::FnParam,
}
impl FuncParam {
/// Returns the `ref` keyword for shorthand borrowed receivers (`ref self`).
pub fn ref_token(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::RefKw)
}
/// Returns the `own` keyword for shorthand owned receivers (`own self`).
pub fn own_token(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::OwnKw)
}
/// Returns the `mut` keyword if the parameter is mutable.
pub fn mut_token(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::MutKw)
}
/// Returns `true` if the parameter uses `_` as an argument label (e.g.
/// `_ x: u256`), meaning it doesn't have an argument label at the call site.
pub fn is_label_suppressed(&self) -> bool {
let mut param_names = self.syntax().children_with_tokens().filter_map(|child| {
if let rowan::NodeOrToken::Token(token) = child {
FuncParamName::from_token(token)
} else {
None
}
});
matches!(
(param_names.next(), param_names.next()),
(Some(FuncParamName::Underscore(_)), Some(_))
)
}
/// Returns the name of the parameter.
/// `a` in `_ a: u256`.
pub fn name(&self) -> Option<FuncParamName> {
let mut param_names = self.syntax().children_with_tokens().filter_map(|child| {
if let rowan::NodeOrToken::Token(token) = child {
FuncParamName::from_token(token)
} else {
None
}
});
let first = param_names.next()?;
let second = param_names.next();
// If the label is `_`, the following name token is the parameter name.
// Otherwise, the first token is both the label and the name.
match (first, second) {
(FuncParamName::Underscore(_), Some(second)) => Some(second),
(first, _) => Some(first),
}
}
/// Returns the type of the parameter.
/// `u256` in `a: u256`.
pub fn ty(&self) -> Option<super::Type> {
support::child(self.syntax())
}
}
ast_node! {
/// A list of generic parameters.
/// `<T: Trait, U>`
pub struct GenericParamList,
SK::GenericParamList,
IntoIterator<Item=GenericParam>,
}
ast_node! {
/// A generic parameter.
/// `T`
/// `T: Trait`
/// `const N: usize`
pub struct GenericParam,
SK::TypeGenericParam | SK::ConstGenericParam,
}
impl GenericParam {
/// Returns the specific kind of the generic parameter.
pub fn kind(&self) -> GenericParamKind {
match self.syntax().kind() {
SK::TypeGenericParam => {
GenericParamKind::Type(AstNode::cast(self.syntax().clone()).unwrap())
}
SK::ConstGenericParam => {
GenericParamKind::Const(AstNode::cast(self.syntax().clone()).unwrap())
}
_ => unreachable!(),
}
}
}
/// A generic parameter kind.
/// `Type` is either `T` or `T: Trait`.
/// `Const` is `const N: usize`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, derive_more::From, derive_more::TryInto)]
pub enum GenericParamKind {
Type(TypeGenericParam),
Const(ConstGenericParam),
}
impl GenericParamKind {
pub fn syntax(&self) -> &rowan::SyntaxNode<FeLang> {
match self {
GenericParamKind::Type(param) => param.syntax(),
GenericParamKind::Const(param) => param.syntax(),
}
}
}
ast_node! {
/// `(label1: arg1, arg2, ..)`
pub struct CallArgList,
SK::CallArgList,
IntoIterator<Item=CallArg>,
}
ast_node! {
/// `label1: arg1`
pub struct CallArg,
SK::CallArg,
}
impl CallArg {
/// Returns the label of the argument.
/// `label1` in `label1: arg1`.
pub fn label(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::Ident)
}
/// Returns the expression of the argument.
/// `arg1` in `label1: arg1`.
pub fn expr(&self) -> Option<super::Expr> {
support::child(self.syntax())
}
}
ast_node! {
/// A type generic parameter.
/// `T`
/// `T: Trait`
pub struct TypeGenericParam,
SK::TypeGenericParam,
}
impl TypeGenericParam {
pub fn name(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::Ident)
}
pub fn bounds(&self) -> Option<TypeBoundList> {
support::child(self.syntax())
}
pub fn default_ty(&self) -> Option<Type> {
support::child(self.syntax())
}
}
ast_node! {
/// A const generic parameter.
/// `const N: usize`.
pub struct ConstGenericParam,
SK::ConstGenericParam,
}
impl ConstGenericParam {
/// Returns the name of the const generic parameter.
pub fn name(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::Ident)
}
pub fn const_kw(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::ConstKw)
}
/// Returns the type of the const generic parameter.
pub fn ty(&self) -> Option<super::Type> {
support::child(self.syntax())
}
pub fn default_expr(&self) -> Option<super::Expr> {
support::child(self.syntax())
}
pub fn default_hole(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::Underscore)
}
}
ast_node! {
/// A list of generic arguments.
/// `<T,
pub struct GenericArgList,
SK::GenericArgList,
IntoIterator<Item=GenericArg>,
}
ast_node! {
/// A generic argument.
/// `T`
/// `T: Trait`
/// `{expr}`
/// `lit`
/// `Output = u64`
pub struct GenericArg,
SK::TypeGenericArg | SK::ConstGenericArg | SK::AssocTypeGenericArg,
}
impl GenericArg {
pub fn kind(&self) -> GenericArgKind {
match self.syntax().kind() {
SK::TypeGenericArg => {
GenericArgKind::Type(AstNode::cast(self.syntax().clone()).unwrap())
}
SK::ConstGenericArg => {
GenericArgKind::Const(AstNode::cast(self.syntax().clone()).unwrap())
}
SK::AssocTypeGenericArg => {
GenericArgKind::AssocType(AstNode::cast(self.syntax().clone()).unwrap())
}
_ => unreachable!(),
}
}
}
ast_node! {
pub struct TypeGenericArg,
SK::TypeGenericArg,
}
impl TypeGenericArg {
pub fn ty(&self) -> Option<super::Type> {
support::child(self.syntax())
}
}
ast_node! {
pub struct ConstGenericArg,
SK::ConstGenericArg,
}
impl ConstGenericArg {
pub fn expr(&self) -> Option<super::Expr> {
support::child(self.syntax())
}
pub fn hole_token(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::Underscore)
}
}
ast_node! {
pub struct AssocTypeGenericArg,
SK::AssocTypeGenericArg,
}
impl AssocTypeGenericArg {
pub fn name(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::Ident)
}
pub fn ty(&self) -> Option<super::Type> {
support::child(self.syntax())
}
}
ast_node! {
/// `where T: Trait`
pub struct WhereClause,
SK::WhereClause,
IntoIterator<Item=WherePredicate>,
}
impl WhereClause {
pub fn where_kw(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::WhereKw)
}
}
ast_node! {
/// `T: Trait`
pub struct WherePredicate,
SK::WherePredicate,
}
impl WherePredicate {
/// Returns `T` in `T: Trait`.
pub fn ty(&self) -> Option<super::Type> {
support::child(self.syntax())
}
/// Returns `Trait` in `T: Trait`.
pub fn bounds(&self) -> Option<TypeBoundList> {
support::child(self.syntax())
}
}
// ===== Uses clause AST nodes =====
ast_node! {
/// `uses Ctx` or `uses (Ctx, mut Storage, c: Ctx, f: mut Foo)`
pub struct UsesClause,
SK::UsesClause,
}
impl UsesClause {
/// The `uses` keyword token.
pub fn uses_kw(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::UsesKw)
}
/// The parameter list form: `uses ( .. )`.
pub fn param_list(&self) -> Option<UsesParamList> {
support::child(self.syntax())
}
/// The single-parameter form: `uses Type` or `uses mut Type`.
pub fn param(&self) -> Option<UsesParam> {
support::child(self.syntax())
}
}
ast_node! {
/// A `uses` parameter list.
pub struct UsesParamList,
SK::UsesParamList,
IntoIterator<Item=UsesParam>,
}
ast_node! {
/// A single `uses` parameter.
/// Supports: `Type`, `mut Type`, `name: Type`, `name: mut Type`.
pub struct UsesParam,
SK::UsesParam,
}
impl UsesParam {
/// Returns the `mut` keyword if present.
pub fn mut_token(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::MutKw)
}
/// Returns the name if present (identifier or underscore).
pub fn name(&self) -> Option<UsesParamName> {
let mut param_names = self.syntax().children_with_tokens().filter_map(|child| {
if let rowan::NodeOrToken::Token(token) = child {
UsesParamName::from_token(token)
} else {
None
}
});
let first = param_names.next();
match param_names.next() {
Some(second) => Some(second),
None => first,
}
}
/// The path key of the uses parameter.
pub fn path(&self) -> Option<super::Path> {
support::child(self.syntax())
}
}
pub enum UsesParamName {
/// `name` in `name: Type`
Ident(SyntaxToken),
/// `_` in `_ : Type`.
Underscore(SyntaxToken),
}
impl UsesParamName {
pub fn syntax(&self) -> SyntaxToken {
match self {
UsesParamName::Ident(token) => token,
UsesParamName::Underscore(token) => token,
}
.clone()
}
fn from_token(token: SyntaxToken) -> Option<Self> {
match token.kind() {
SK::Ident => Some(UsesParamName::Ident(token)),
SK::Underscore => Some(UsesParamName::Underscore(token)),
_ => None,
}
}
}
/// A generic argument kind.
/// `Type` is either `Type` or `T: Trait`.
/// `Const` is either `{expr}` or `lit`.
/// `AssocType` is `Output = u64`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, derive_more::From, derive_more::TryInto)]
pub enum GenericArgKind {
Type(TypeGenericArg),
Const(ConstGenericArg),
AssocType(AssocTypeGenericArg),
}
ast_node! {
/// A type bound list.
/// `: Trait + Trait2`
pub struct TypeBoundList,
SK::TypeBoundList,
IntoIterator<Item=TypeBound>,
}
ast_node! {
/// A type bound.
/// `Trait`
/// `Trait<T, U>`
/// `(* -> *) -> *`
pub struct TypeBound,
SK::TypeBound,
}
impl TypeBound {
/// A path of the type bound.
pub fn trait_bound(&self) -> Option<TraitRef> {
support::child(self.syntax())
}
pub fn kind_bound(&self) -> Option<KindBound> {
support::child(self.syntax())
}
}
ast_node! {
pub struct TraitRef,
SK::TraitRef
}
impl TraitRef {
/// A path to the trait.
pub fn path(&self) -> Option<super::Path> {
support::child(self.syntax())
}
/// A generic argument list for the trait.
pub fn generic_args(&self) -> Option<GenericArgList> {
support::child(self.syntax())
}
}
ast_node! {
pub struct KindBound,
SK::KindBoundAbs | SK::KindBoundMono
}
impl KindBound {
pub fn mono(&self) -> Option<KindBoundMono> {
match self.syntax().kind() {
SK::KindBoundMono => Some(KindBoundMono::cast(self.syntax().clone()).unwrap()),
_ => None,
}
}
pub fn abs(&self) -> Option<KindBoundAbs> {
match self.syntax().kind() {
SK::KindBoundAbs => Some(KindBoundAbs::cast(self.syntax().clone()).unwrap()),
_ => None,
}
}
}
ast_node! {
pub struct KindBoundMono,
SK::KindBoundMono,
}
ast_node! {
pub struct KindBoundAbs,
SK::KindBoundAbs,
}
impl KindBoundAbs {
pub fn lhs(&self) -> Option<KindBound> {
support::child(self.syntax())
}
pub fn rhs(&self) -> Option<KindBound> {
support::children(self.syntax()).nth(1)
}
pub fn arrow(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::Arrow)
}
}
#[derive(Debug, Clone)]
pub enum KindBoundVariant {
/// `*`
Mono(KindBoundMono),
/// `KindBound -> KindBound`
Abs(KindBoundAbs),
}
/// A trait for AST nodes that can have generic parameters.
pub trait GenericParamsOwner: AstNode<Language = FeLang> {
/// Returns the generic parameter list of the node.
fn generic_params(&self) -> Option<GenericParamList> {
support::child(self.syntax())
}
}
/// A trait for AST nodes that can have generic arguments.
pub trait GenericArgsOwner: AstNode<Language = FeLang> {
/// Returns the generic argument list of the node.
fn generic_args(&self) -> Option<GenericArgList> {
support::child(self.syntax())
}
}
/// A trait for AST nodes that can have a where clause.
pub trait WhereClauseOwner: AstNode<Language = FeLang> {
/// Returns the where clause of the node.
fn where_clause(&self) -> Option<WhereClause> {
support::child(self.syntax())
}
}
pub enum FuncParamName {
/// `a` in `a: u256`
Ident(SyntaxToken),
/// `self` parameter.
SelfParam(SyntaxToken),
/// `_` parameter.
Underscore(SyntaxToken),
}
impl FuncParamName {
pub fn syntax(&self) -> SyntaxToken {
match self {
FuncParamName::Ident(token) => token,
FuncParamName::SelfParam(token) => token,
FuncParamName::Underscore(token) => token,
}
.clone()
}
fn from_token(token: SyntaxToken) -> Option<Self> {
match token.kind() {
SK::Ident => Some(FuncParamName::Ident(token)),
SK::SelfKw => Some(FuncParamName::SelfParam(token)),
SK::Underscore => Some(FuncParamName::Underscore(token)),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
ast::TypeKind,
lexer::Lexer,
parser::{
Parser, RecoveryMode,
func::FuncScope,
param::{GenericArgListScope, GenericParamListScope, WhereClauseScope},
},
};
use wasm_bindgen_test::wasm_bindgen_test;
fn parse_generic_params(source: &str) -> GenericParamList {
let lexer = Lexer::new(source);
let mut parser = Parser::new(lexer, RecoveryMode::Recover);
parser.parse(GenericParamListScope::default()).unwrap();
GenericParamList::cast(parser.finish_to_node().0).unwrap()
}
fn parse_generic_arg(source: &str) -> GenericArgList {
let lexer = Lexer::new(source);
let mut parser = Parser::new(lexer, RecoveryMode::Recover);
parser.parse(GenericArgListScope::default()).unwrap();
GenericArgList::cast(parser.finish_to_node().0).unwrap()
}
fn parse_where_clause(source: &str) -> WhereClause {
let lexer = Lexer::new(source);
let mut parser = Parser::new(lexer, RecoveryMode::Recover);
parser.parse(WhereClauseScope::default()).unwrap();
WhereClause::cast(parser.finish_to_node().0).unwrap()
}
#[test]
#[wasm_bindgen_test]
fn generic_param() {
let source = r#"<T: Trait + Trait2<X, Y>, U, const N: usize>"#;
let gp = parse_generic_params(source);
let mut params = gp.into_iter();
let GenericParamKind::Type(p1) = params.next().unwrap().kind() else {
panic!("expected type param");
};
assert_eq!(p1.name().unwrap().text(), "T");
let p1_bounds = p1.bounds().unwrap();
let mut p1_bounds = p1_bounds.iter();
assert_eq!(
p1_bounds
.next()
.unwrap()
.trait_bound()
.unwrap()
.path()
.unwrap()
.segments()
.next()
.unwrap()
.ident()
.unwrap()
.text(),
"Trait"
);
let p1_bounds_trait2 = p1_bounds.next().unwrap();
assert_eq!(
p1_bounds_trait2
.trait_bound()
.unwrap()
.path()
.unwrap()
.segments()
.next()
.unwrap()
.ident()
.unwrap()
.text(),
"Trait2"
);
let GenericParamKind::Type(p2) = params.next().unwrap().kind() else {
panic!("expected type param");
};
assert_eq!(p2.name().unwrap().text(), "U");
let GenericParamKind::Const(p3) = params.next().unwrap().kind() else {
panic!("expected const param");
};
assert_eq!(p3.name().unwrap().text(), "N");
assert!(p3.ty().is_some());
}
#[test]
#[wasm_bindgen_test]
fn generic_arg() {
let source = r#"<T, "foo">"#;
let ga = parse_generic_arg(source);
let mut args = ga.iter();
let GenericArgKind::Type(_) = args.next().unwrap().kind() else {
panic!("expected type arg");
};
let GenericArgKind::Const(a2) = args.next().unwrap().kind() else {
panic!("expected const arg");
};
assert!(a2.expr().is_some());
}
#[test]
#[wasm_bindgen_test]
fn generic_arg_with_assoc_type() {
let source = r#"<T, Output = u64>"#;
let ga = parse_generic_arg(source);
let mut args = ga.into_iter();
let GenericArgKind::Type(_) = args.next().unwrap().kind() else {
panic!("expected type arg");
};
let GenericArgKind::AssocType(a2) = args.next().unwrap().kind() else {
panic!("expected associated type arg");
};
assert_eq!(a2.name().unwrap().text(), "Output");
assert!(a2.ty().is_some());
}
#[test]
#[wasm_bindgen_test]
fn where_clause() {
let source = r#"where
T: Trait + Trait2<X, Y>
*U: Trait3
(T, U): Trait4 + Trait5
"#;
let wc = parse_where_clause(source);
let mut count = 0;
for pred in wc {
match count {
0 => {
assert!(matches!(pred.ty().unwrap().kind(), TypeKind::Path(_)));
assert_eq!(pred.bounds().unwrap().iter().count(), 2);
}
1 => {
assert!(matches!(pred.ty().unwrap().kind(), TypeKind::Ptr(_)));
assert_eq!(pred.bounds().unwrap().iter().count(), 1);
}
2 => {
assert!(matches!(pred.ty().unwrap().kind(), TypeKind::Tuple(_)));
assert_eq!(pred.bounds().unwrap().iter().count(), 2);
}
_ => panic!("unexpected predicate"),
}
count += 1;
}
assert!(count == 3);
}
#[test]
#[wasm_bindgen_test]
fn generic_param_with_assoc_type() {
let source = r#"<T: Iterator<Item = i32>>"#;
let gp = parse_generic_params(source);
let mut params = gp.into_iter();
let GenericParamKind::Type(p1) = params.next().unwrap().kind() else {
panic!("expected type param");
};
assert_eq!(p1.name().unwrap().text(), "T");
let p1_bounds = p1.bounds().unwrap();
let mut p1_bounds = p1_bounds.iter();
let bound = p1_bounds.next().unwrap();
let trait_ref = bound.trait_bound().unwrap();
let trait_path = trait_ref.path().unwrap();
assert_eq!(
trait_path
.segments()
.next()
.unwrap()
.ident()
.unwrap()
.text(),
"Iterator"
);
// Check generic args on the trait path's last segment
let last_segment = trait_path.segments().next().unwrap();
let generic_args = last_segment.generic_args().unwrap();
let mut args = generic_args.into_iter();
let GenericArgKind::AssocType(assoc_arg) = args.next().unwrap().kind() else {
panic!("expected associated type arg");
};
assert_eq!(assoc_arg.name().unwrap().text(), "Item");
assert!(assoc_arg.ty().is_some());
}
fn parse_func(source: &str) -> crate::ast::Func {
let lexer = Lexer::new(source);
let mut parser = Parser::new(lexer, RecoveryMode::Recover);
parser.parse(FuncScope::default()).unwrap();
crate::ast::Func::cast(parser.finish_to_node().0).unwrap()
}
fn parse_func_with_errors(source: &str) -> Vec<crate::ParseError> {
let lexer = Lexer::new(source);
let mut parser = Parser::new(lexer, RecoveryMode::Recover);
parser.parse(FuncScope::default()).unwrap();
parser.finish_to_node().1
}
#[test]
#[wasm_bindgen_test]
fn uses_clause_single_type() {
let f = parse_func("fn f() uses Ctx {}");
let uc = f.sig().uses_clause().expect("missing uses clause");
assert!(uc.param_list().is_none());
let p = uc.param().expect("expected single uses param");
assert!(p.mut_token().is_none());
let path = p.path().expect("missing path key");
let seg = path.segments().next().unwrap();
assert_eq!(seg.ident().unwrap().text(), "Ctx");
}
#[test]
#[wasm_bindgen_test]
fn uses_clause_single_mut_type() {
let f = parse_func("fn f() uses mut Ctx {}");
let uc = f.sig().uses_clause().expect("missing uses clause");
let p = uc.param().expect("expected single uses param");
assert!(p.mut_token().is_some());
let path = p.path().expect("missing path key");
let seg = path.segments().next().unwrap();
assert_eq!(seg.ident().unwrap().text(), "Ctx");
}
#[test]
#[wasm_bindgen_test]
fn uses_clause_param_list_variants() {
let f = parse_func("fn f() uses (Ctx, mut Storage, c: Ctx, f: mut Foo) {}");
let uc = f.sig().uses_clause().expect("missing uses clause");
let list = uc.param_list().expect("expected param list");
let params: Vec<_> = list.iter().collect();
assert_eq!(params.len(), 4);
// 0: Ctx
assert!(params[0].mut_token().is_none());
let path0 = params[0].path().expect("missing path");
let seg0 = path0.segments().next().unwrap();
assert_eq!(seg0.ident().unwrap().text(), "Ctx");
// 1: mut Storage
assert!(params[1].mut_token().is_some());
let path1 = params[1].path().expect("missing path");
let seg1 = path1.segments().next().unwrap();
assert_eq!(seg1.ident().unwrap().text(), "Storage");
// 2: c: Ctx
let n = params[2].name().expect("missing name");
assert_eq!(n.syntax().text(), "c");
let path2 = params[2].path().expect("missing path");
let seg2 = path2.segments().next().unwrap();
assert_eq!(seg2.ident().unwrap().text(), "Ctx");
// 3: f: mut Foo
assert!(params[3].mut_token().is_some());
let n = params[3].name().expect("missing name");
assert_eq!(n.syntax().text(), "f");
let path3 = params[3].path().expect("missing path");
let seg3 = path3.segments().next().unwrap();
assert_eq!(seg3.ident().unwrap().text(), "Foo");
}
#[test]
#[wasm_bindgen_test]
fn uses_clause_rejects_legacy_typed_mut_prefix() {
let errors = parse_func_with_errors("fn f() uses (mut x: Foo) {}");
assert!(!errors.is_empty(), "expected parser error");
assert!(errors.iter().any(|err| {
err.msg()
.contains("`uses` typed parameters use `name: mut Type`, not `mut name: Type`")
}));
}
#[test]
#[wasm_bindgen_test]
fn uses_clause_rejects_ref_and_own_modes_for_typed_params() {
let errors = parse_func_with_errors("fn f() uses (x: ref Foo, y: own Foo) {}");
assert!(!errors.is_empty(), "expected parser errors");
assert!(errors.iter().any(|err| {
err.msg()
.contains("typed `uses` parameters only support `mut`; remove `ref`")
}));
assert!(errors.iter().any(|err| {
err.msg()
.contains("typed `uses` parameters only support `mut`; remove `own`")
}));
}
}