1
- use crate :: {
2
- engine_threading:: DebugWithEngines ,
3
- language:: parsed:: { Expression , ExpressionKind } ,
4
- } ;
5
- use sway_types:: { span:: Span , Spanned } ;
1
+ use crate :: engine_threading:: DebugWithEngines ;
2
+ use sway_types:: { span:: Span , Ident , Spanned } ;
6
3
7
4
/// Describes a fixed length for types that need it, e.g., [crate::TypeInfo::Array].
8
5
///
@@ -22,26 +19,20 @@ pub struct Length(pub LengthExpression);
22
19
#[ derive( Debug , Clone ) ]
23
20
pub enum LengthExpression {
24
21
Literal { val : usize , span : Span } ,
25
- AmbiguousVariableExpression { inner : Expression } ,
22
+ AmbiguousVariableExpression { ident : Ident } ,
26
23
}
27
24
28
25
impl PartialOrd for Length {
29
26
fn partial_cmp ( & self , other : & Self ) -> Option < std:: cmp:: Ordering > {
30
27
match ( & self . 0 , & other. 0 ) {
31
28
(
32
- LengthExpression :: Literal { val : l_val , .. } ,
33
- LengthExpression :: Literal { val : r_val , .. } ,
34
- ) => l_val . partial_cmp ( r_val ) ,
29
+ LengthExpression :: Literal { val : l , .. } ,
30
+ LengthExpression :: Literal { val : r , .. } ,
31
+ ) => l . partial_cmp ( r ) ,
35
32
(
36
- LengthExpression :: AmbiguousVariableExpression { inner : l_inner } ,
37
- LengthExpression :: AmbiguousVariableExpression { inner : r_inner } ,
38
- ) => match ( & l_inner. kind , & r_inner. kind ) {
39
- (
40
- ExpressionKind :: AmbiguousVariableExpression ( l) ,
41
- ExpressionKind :: AmbiguousVariableExpression ( r) ,
42
- ) => l. partial_cmp ( r) ,
43
- _ => None ,
44
- } ,
33
+ LengthExpression :: AmbiguousVariableExpression { ident : l } ,
34
+ LengthExpression :: AmbiguousVariableExpression { ident : r } ,
35
+ ) => l. partial_cmp ( r) ,
45
36
_ => None ,
46
37
}
47
38
}
@@ -52,32 +43,11 @@ impl Eq for LengthExpression {}
52
43
impl PartialEq for LengthExpression {
53
44
fn eq ( & self , other : & Self ) -> bool {
54
45
match ( self , other) {
46
+ ( Self :: Literal { val : l, .. } , Self :: Literal { val : r, .. } ) => l == r,
55
47
(
56
- Self :: Literal {
57
- val : l_val,
58
- span : l_span,
59
- } ,
60
- Self :: Literal {
61
- val : r_val,
62
- span : r_span,
63
- } ,
64
- ) => l_val == r_val && l_span == r_span,
65
- (
66
- Self :: AmbiguousVariableExpression {
67
- inner :
68
- Expression {
69
- kind : ExpressionKind :: AmbiguousVariableExpression ( l_ident) ,
70
- ..
71
- } ,
72
- } ,
73
- Self :: AmbiguousVariableExpression {
74
- inner :
75
- Expression {
76
- kind : ExpressionKind :: AmbiguousVariableExpression ( r_ident) ,
77
- ..
78
- } ,
79
- } ,
80
- ) => l_ident == r_ident,
48
+ Self :: AmbiguousVariableExpression { ident : l } ,
49
+ Self :: AmbiguousVariableExpression { ident : r } ,
50
+ ) => l == r,
81
51
_ => false ,
82
52
}
83
53
}
@@ -88,12 +58,7 @@ impl std::hash::Hash for LengthExpression {
88
58
core:: mem:: discriminant ( self ) . hash ( state) ;
89
59
match self {
90
60
LengthExpression :: Literal { val, .. } => val. hash ( state) ,
91
- LengthExpression :: AmbiguousVariableExpression { inner } => match & inner. kind {
92
- crate :: language:: parsed:: ExpressionKind :: AmbiguousVariableExpression (
93
- base_ident,
94
- ) => base_ident. hash ( state) ,
95
- _ => unreachable ! ( ) ,
96
- } ,
61
+ LengthExpression :: AmbiguousVariableExpression { ident } => ident. hash ( state) ,
97
62
}
98
63
}
99
64
}
@@ -130,7 +95,7 @@ impl Spanned for Length {
130
95
fn span ( & self ) -> Span {
131
96
match & self . 0 {
132
97
LengthExpression :: Literal { span, .. } => span. clone ( ) ,
133
- LengthExpression :: AmbiguousVariableExpression { inner , .. } => inner . span ( ) ,
98
+ LengthExpression :: AmbiguousVariableExpression { ident , .. } => ident . span ( ) ,
134
99
}
135
100
}
136
101
}
@@ -139,12 +104,9 @@ impl DebugWithEngines for Length {
139
104
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > , _engines : & crate :: Engines ) -> std:: fmt:: Result {
140
105
match & self . 0 {
141
106
LengthExpression :: Literal { val, .. } => write ! ( f, "{val}" ) ,
142
- LengthExpression :: AmbiguousVariableExpression { inner } => match & inner. kind {
143
- ExpressionKind :: AmbiguousVariableExpression ( base_ident) => {
144
- write ! ( f, "{}" , base_ident. as_str( ) )
145
- }
146
- _ => unreachable ! ( ) ,
147
- } ,
107
+ LengthExpression :: AmbiguousVariableExpression { ident } => {
108
+ write ! ( f, "{}" , ident. as_str( ) )
109
+ }
148
110
}
149
111
}
150
112
}
0 commit comments