Skip to content

Commit

Permalink
- 7e87f59: refactor lowering match logic to use optional pattern and …
Browse files Browse the repository at this point in the history
…body expression in match arms
  • Loading branch information
TomerStarkware committed Feb 5, 2024
1 parent 709f0c2 commit 3044bc5
Show file tree
Hide file tree
Showing 2 changed files with 432 additions and 177 deletions.
111 changes: 75 additions & 36 deletions crates/cairo-lang-lowering/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct LoweringDiagnostic {
pub location: Location,
pub kind: LoweringDiagnosticKind,
}

impl DiagnosticEntry for LoweringDiagnostic {
type DbType = dyn SemanticGroup;

Expand All @@ -54,30 +55,10 @@ impl DiagnosticEntry for LoweringDiagnostic {
LoweringDiagnosticKind::DesnappingANonCopyableType { .. } => {
"Cannot desnap a non copyable type.".into()
}
LoweringDiagnosticKind::UnsupportedMatchedType(matched_type) =>
format!("Unsupported matched type. Type: `{}`.", matched_type),
LoweringDiagnosticKind::UnsupportedMatchedValueTuple => "Unsupported matched value. \
Currently, match on tuples only supports enums as tuple members."
.into(),
LoweringDiagnosticKind::UnsupportedMatchArmNotAVariant => {
"Unsupported match arm - not a variant.".into()
}
LoweringDiagnosticKind::UnsupportedMatchArmNotALiteral => {
"Unsupported match arm - not a literal.".into()
}
LoweringDiagnosticKind::UnsupportedMatchArmNonSequential => {
"Unsupported match - numbers must be sequential starting from 0.".into()
}
LoweringDiagnosticKind::UnsupportedMatchArmOrNotSupported => {
"Unsupported match arm - or pattern is not supported in this context".into()
}
LoweringDiagnosticKind::UnsupportedMatchArmNotATuple => {
"Unsupported match arm - not a tuple.".into()
}
LoweringDiagnosticKind::NonExhaustiveMatchFelt252 => {
"Match is non exhaustive - match over a numerical value must have a wildcard card pattern (`_`)."
.into()
}
LoweringDiagnosticKind::MatchError(
match_err
) =>
match_err.format(),
LoweringDiagnosticKind::CannotInlineFunctionThatMightCallItself => {
"Cannot inline a function that might call itself.".into()
}
Expand All @@ -97,8 +78,6 @@ impl DiagnosticEntry for LoweringDiagnostic {
LoweringDiagnosticKind::UnsupportedPattern => {
"Inner patterns are not in this context.".into()
}
LoweringDiagnosticKind::MissingMatchArm(variant) => format!("Missing match arm: `{}` not covered.", variant),
LoweringDiagnosticKind::UnreachableMatchArm => "Unreachable pattern arm.".into(),
LoweringDiagnosticKind::Unsupported => "Unsupported feature.".into(),
}
}
Expand All @@ -122,27 +101,87 @@ impl DiagnosticEntry for LoweringDiagnostic {
}
}

impl MatchError {
fn format(&self) -> String {
match &self.error {
MatchDiagnostic::UnsupportedMatchedType(matched_type) => {
format!("Unsupported matched type. Type: `{}`.", matched_type)
}
MatchDiagnostic::UnsupportedMatchedValueTuple => "Unsupported matched value. \
Currently, match on tuples only \
supports enums as tuple members."
.into(),
MatchDiagnostic::UnsupportedMatchArmNotAVariant => {
"Unsupported pattern - not a variant.".into()
}
MatchDiagnostic::UnsupportedMatchArmNotALiteral => {
"Unsupported match arm - not a literal.".into()
}
MatchDiagnostic::UnsupportedMatchArmNonSequential => {
"Unsupported match - numbers must be sequential starting from 0.".into()
}
MatchDiagnostic::UnsupportedMatchArmNotATuple => {
"Unsupported pattern - not a tuple.".into()
}
MatchDiagnostic::NonExhaustiveMatchFelt252 => "Match is non exhaustive - match over a \
numerical value must have a wildcard \
card pattern (`_`)."
.into(),
MatchDiagnostic::MissingMatchArm(variant) => {
format!("Missing match arm: `{}` not covered.", variant)
}
MatchDiagnostic::UnreachableMatchArm => "Unreachable pattern arm.".into(),
MatchDiagnostic::UnsupportedNumericInLetCondition => {
"Numeric values are not supported in if-let.".into()
}
}
}
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum LoweringDiagnosticKind {
Unreachable { last_statement_ptr: SyntaxStablePtrId },
VariableMoved { inference_error: InferenceError },
VariableNotDropped { drop_err: InferenceError, destruct_err: InferenceError },
MatchError(MatchError),
DesnappingANonCopyableType { inference_error: InferenceError },
UnsupportedMatchedType(String),
UnsupportedMatchedValueTuple,
MissingMatchArm(String),
UnreachableMatchArm,
UnexpectedError,
UnsupportedMatchArmNotAVariant,
UnsupportedMatchArmNotALiteral,
UnsupportedMatchArmNotATuple,
UnsupportedMatchArmNonSequential,
UnsupportedMatchArmOrNotSupported,
NonExhaustiveMatchFelt252,
CannotInlineFunctionThatMightCallItself,
MemberPathLoop,
NoPanicFunctionCycle,
LiteralError(LiteralError),
UnsupportedPattern,
Unsupported,
}

/// Error in a match-like construct.
/// contains which construct the error occurred in and the error itself.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct MatchError {
pub kind: MatchKind,
pub error: MatchDiagnostic,
}

/// The type of branch construct the error occurred in.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum MatchKind {
IfLet,
Match,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum MatchDiagnostic {
/// TODO(TomerStarkware): Get rid of the string and pass the type information directly.
UnsupportedMatchedType(String),
UnsupportedMatchedValueTuple,
UnsupportedMatchArmNotAVariant,
UnsupportedMatchArmNotATuple,

UnreachableMatchArm,
MissingMatchArm(String),

UnsupportedMatchArmNotALiteral,
UnsupportedMatchArmNonSequential,
NonExhaustiveMatchFelt252,
UnsupportedNumericInLetCondition,
}
Loading

0 comments on commit 3044bc5

Please sign in to comment.