Skip to content

Commit

Permalink
[checker][pattern-matching] Infra support for generating counter exam…
Browse files Browse the repository at this point in the history
…ples (#1124)
  • Loading branch information
SamChou19815 authored Oct 30, 2023
1 parent 3f51b53 commit cddaf7d
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 129 deletions.
36 changes: 36 additions & 0 deletions crates/samlang-core/src/ast/reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub(crate) enum Description {
NominalType { name: PStr, type_args: Vec<Description> },
FunctionType(Vec<Description>, Box<Description>),
TypeParameter(PStr, Option<Box<Description>>),
WildcardPattern,
TuplePattern(Vec<Description>),
VariantPattern(PStr, Vec<Description>),
OrPattern(Vec<Description>),
}

impl Description {
Expand Down Expand Up @@ -59,6 +63,24 @@ impl Description {
Self::TypeParameter(name, Some(bound)) => {
format!("{} : {}", name.as_str(heap), bound.pretty_print(heap))
}
Self::WildcardPattern => "_".to_string(),
Self::TuplePattern(elements) => {
format!("({})", elements.iter().map(|e| e.pretty_print(heap)).join(", "))
}
Self::VariantPattern(tag, elements) => {
if elements.is_empty() {
tag.as_str(heap).to_string()
} else {
format!(
"{}({})",
tag.as_str(heap),
elements.iter().map(|e| e.pretty_print(heap)).join(", ")
)
}
}
Self::OrPattern(elements) => {
elements.iter().map(|e| e.pretty_print(heap)).join(" | ").to_string()
}
}
}
}
Expand Down Expand Up @@ -145,6 +167,20 @@ mod tests {
Description::TypeParameter(PStr::UPPER_A, Some(Box::new(Description::IntType)))
.pretty_print(heap)
);
assert_eq!(
"A | B((_, _))",
Description::OrPattern(vec![
Description::VariantPattern(PStr::UPPER_A, vec![]),
Description::VariantPattern(
PStr::UPPER_B,
vec![Description::TuplePattern(vec![
Description::WildcardPattern,
Description::WildcardPattern
])]
)
])
.pretty_print(heap)
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/samlang-core/src/checker/main_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ fn check_match(
});
}
if !unused_mappings.is_empty() {
cx.error_set.report_non_exhausive_match_error(
cx.error_set.report_non_exhausive_match_for_match_expr_error(
expression.common.loc,
unused_mappings.keys().copied().collect(),
);
Expand Down
Loading

0 comments on commit cddaf7d

Please sign in to comment.