Skip to content

Commit 4b448c8

Browse files
committed
simpler length expression
1 parent 3d75513 commit 4b448c8

File tree

5 files changed

+42
-72
lines changed

5 files changed

+42
-72
lines changed

sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2909,18 +2909,23 @@ fn expr_to_length(
29092909
expr: Expr,
29102910
) -> Result<Length, ErrorEmitted> {
29112911
let span = expr.span();
2912-
Ok(match &expr {
2913-
Expr::Literal(..) => Length::literal(expr_to_usize(context, handler, expr)?, Some(span)),
2912+
match &expr {
2913+
Expr::Literal(..) => Ok(Length::literal(
2914+
expr_to_usize(context, handler, expr)?,
2915+
Some(span),
2916+
)),
29142917
_ => {
29152918
let expr = expr_to_expression(context, handler, engines, expr)?;
2916-
Length(match expr.kind {
2917-
ExpressionKind::AmbiguousVariableExpression(_) => {
2918-
LengthExpression::AmbiguousVariableExpression { inner: expr }
2919+
match expr.kind {
2920+
ExpressionKind::AmbiguousVariableExpression(ident) => {
2921+
Ok(Length(LengthExpression::AmbiguousVariableExpression {
2922+
ident,
2923+
}))
29192924
}
2920-
_ => todo!(),
2921-
})
2925+
_ => Err(handler.emit_err(CompileError::LengthExpressionNotSupported { span })),
2926+
}
29222927
}
2923-
})
2928+
}
29242929
}
29252930

29262931
fn expr_to_numeric_length(

sway-core/src/type_system/ast_elements/length.rs

Lines changed: 18 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
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};
63

74
/// Describes a fixed length for types that need it, e.g., [crate::TypeInfo::Array].
85
///
@@ -22,26 +19,20 @@ pub struct Length(pub LengthExpression);
2219
#[derive(Debug, Clone)]
2320
pub enum LengthExpression {
2421
Literal { val: usize, span: Span },
25-
AmbiguousVariableExpression { inner: Expression },
22+
AmbiguousVariableExpression { ident: Ident },
2623
}
2724

2825
impl PartialOrd for Length {
2926
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
3027
match (&self.0, &other.0) {
3128
(
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),
3532
(
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),
4536
_ => None,
4637
}
4738
}
@@ -52,32 +43,11 @@ impl Eq for LengthExpression {}
5243
impl PartialEq for LengthExpression {
5344
fn eq(&self, other: &Self) -> bool {
5445
match (self, other) {
46+
(Self::Literal { val: l, .. }, Self::Literal { val: r, .. }) => l == r,
5547
(
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,
8151
_ => false,
8252
}
8353
}
@@ -88,12 +58,7 @@ impl std::hash::Hash for LengthExpression {
8858
core::mem::discriminant(self).hash(state);
8959
match self {
9060
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),
9762
}
9863
}
9964
}
@@ -130,7 +95,7 @@ impl Spanned for Length {
13095
fn span(&self) -> Span {
13196
match &self.0 {
13297
LengthExpression::Literal { span, .. } => span.clone(),
133-
LengthExpression::AmbiguousVariableExpression { inner, .. } => inner.span(),
98+
LengthExpression::AmbiguousVariableExpression { ident, .. } => ident.span(),
13499
}
135100
}
136101
}
@@ -139,12 +104,9 @@ impl DebugWithEngines for Length {
139104
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, _engines: &crate::Engines) -> std::fmt::Result {
140105
match &self.0 {
141106
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+
}
148110
}
149111
}
150112
}

sway-core/src/type_system/info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,8 @@ impl DisplayWithEngines for TypeInfo {
608608
Array(elem_ty, length) => {
609609
let l = match &length.0 {
610610
LengthExpression::Literal { val, .. } => format!("{val}"),
611-
LengthExpression::AmbiguousVariableExpression { inner } => {
612-
inner.span.as_str().to_string()
611+
LengthExpression::AmbiguousVariableExpression { ident } => {
612+
ident.as_str().to_string()
613613
}
614614
};
615615
format!("[{}; {l}]", engines.help_out(elem_ty))

sway-core/src/type_system/unify/unify_check.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,13 @@ impl<'a> UnifyCheck<'a> {
283283
} else {
284284
match (&l1.0, &r1.0) {
285285
(
286-
LengthExpression::Literal { val: l_val, .. },
287-
LengthExpression::Literal { val: r_val, .. },
288-
) => l_val == r_val,
286+
LengthExpression::Literal { val: l, .. },
287+
LengthExpression::Literal { val: r, .. },
288+
) => l == r,
289289
(
290-
LengthExpression::AmbiguousVariableExpression { inner: l_inner },
291-
LengthExpression::AmbiguousVariableExpression { inner: r_inner },
292-
) => l_inner.span.as_str() == r_inner.span.as_str(),
290+
LengthExpression::AmbiguousVariableExpression { ident: l },
291+
LengthExpression::AmbiguousVariableExpression { ident: r },
292+
) => l == r,
293293
_ => false,
294294
}
295295
};

sway-error/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ impl fmt::Display for InterfaceName {
5050
// provided identifier.
5151
#[derive(Error, Debug, Clone, PartialEq, Eq, Hash)]
5252
pub enum CompileError {
53+
#[error("This expression is not supported as lengths.")]
54+
LengthExpressionNotSupported { span: Span },
5355
#[error(
5456
"This needs \"{feature}\" to be enabled, but it is currently disabled. For more details go to {url}."
5557
)]
@@ -1069,6 +1071,7 @@ impl Spanned for CompileError {
10691071
fn span(&self) -> Span {
10701072
use CompileError::*;
10711073
match self {
1074+
LengthExpressionNotSupported { span } => span.clone(),
10721075
FeatureIsDisabled { span, .. } => span.clone(),
10731076
ModuleDepGraphEvaluationError { .. } => Span::dummy(),
10741077
ModuleDepGraphCyclicReference { .. } => Span::dummy(),

0 commit comments

Comments
 (0)