@@ -4,8 +4,8 @@ use std::fmt::Display;
4
4
use thiserror:: Error ;
5
5
6
6
use crate :: ast:: {
7
- Ident , ItemVisibility , Path , Pattern , Recoverable , Statement , StatementKind ,
8
- UnresolvedTraitConstraint , UnresolvedType , UnresolvedTypeData , Visibility ,
7
+ Ident , ItemVisibility , Path , Pattern , Statement , StatementKind , UnresolvedTraitConstraint ,
8
+ UnresolvedType , UnresolvedTypeData , Visibility ,
9
9
} ;
10
10
use crate :: node_interner:: {
11
11
ExprId , InternedExpressionKind , InternedStatementKind , QuotedTypeId , TypeId ,
@@ -26,6 +26,7 @@ pub enum ExpressionKind {
26
26
Index ( Box < IndexExpression > ) ,
27
27
Call ( Box < CallExpression > ) ,
28
28
MethodCall ( Box < MethodCallExpression > ) ,
29
+ Constrain ( ConstrainExpression ) ,
29
30
Constructor ( Box < ConstructorExpression > ) ,
30
31
MemberAccess ( Box < MemberAccessExpression > ) ,
31
32
Cast ( Box < CastExpression > ) ,
@@ -226,24 +227,6 @@ impl ExpressionKind {
226
227
}
227
228
}
228
229
229
- impl Recoverable for ExpressionKind {
230
- fn error ( _: Span ) -> Self {
231
- ExpressionKind :: Error
232
- }
233
- }
234
-
235
- impl Recoverable for Expression {
236
- fn error ( span : Span ) -> Self {
237
- Expression :: new ( ExpressionKind :: Error , span)
238
- }
239
- }
240
-
241
- impl Recoverable for Option < Expression > {
242
- fn error ( span : Span ) -> Self {
243
- Some ( Expression :: new ( ExpressionKind :: Error , span) )
244
- }
245
- }
246
-
247
230
#[ derive( Debug , Eq , Clone ) ]
248
231
pub struct Expression {
249
232
pub kind : ExpressionKind ,
@@ -600,6 +583,55 @@ impl BlockExpression {
600
583
}
601
584
}
602
585
586
+ #[ derive( Debug , PartialEq , Eq , Clone ) ]
587
+ pub struct ConstrainExpression {
588
+ pub kind : ConstrainKind ,
589
+ pub arguments : Vec < Expression > ,
590
+ pub span : Span ,
591
+ }
592
+
593
+ impl Display for ConstrainExpression {
594
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
595
+ match self . kind {
596
+ ConstrainKind :: Assert | ConstrainKind :: AssertEq => write ! (
597
+ f,
598
+ "{}({})" ,
599
+ self . kind,
600
+ vecmap( & self . arguments, |arg| arg. to_string( ) ) . join( ", " )
601
+ ) ,
602
+ ConstrainKind :: Constrain => {
603
+ write ! ( f, "constrain {}" , & self . arguments[ 0 ] )
604
+ }
605
+ }
606
+ }
607
+ }
608
+
609
+ #[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
610
+ pub enum ConstrainKind {
611
+ Assert ,
612
+ AssertEq ,
613
+ Constrain ,
614
+ }
615
+
616
+ impl ConstrainKind {
617
+ pub fn required_arguments_count ( & self ) -> usize {
618
+ match self {
619
+ ConstrainKind :: Assert | ConstrainKind :: Constrain => 1 ,
620
+ ConstrainKind :: AssertEq => 2 ,
621
+ }
622
+ }
623
+ }
624
+
625
+ impl Display for ConstrainKind {
626
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
627
+ match self {
628
+ ConstrainKind :: Assert => write ! ( f, "assert" ) ,
629
+ ConstrainKind :: AssertEq => write ! ( f, "assert_eq" ) ,
630
+ ConstrainKind :: Constrain => write ! ( f, "constrain" ) ,
631
+ }
632
+ }
633
+ }
634
+
603
635
impl Display for Expression {
604
636
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
605
637
self . kind . fmt ( f)
@@ -616,6 +648,7 @@ impl Display for ExpressionKind {
616
648
Index ( index) => index. fmt ( f) ,
617
649
Call ( call) => call. fmt ( f) ,
618
650
MethodCall ( call) => call. fmt ( f) ,
651
+ Constrain ( constrain) => constrain. fmt ( f) ,
619
652
Cast ( cast) => cast. fmt ( f) ,
620
653
Infix ( infix) => infix. fmt ( f) ,
621
654
If ( if_expr) => if_expr. fmt ( f) ,
0 commit comments