Skip to content

Commit

Permalink
frontend: Clean up error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Dec 23, 2024
1 parent ec507db commit 2460bb7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
6 changes: 2 additions & 4 deletions dora-frontend/src/error/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ pub enum ErrorMessage {
EnumExpected,
EnumMismatch(String, String),
EnumVariantExpected,
MatchUncoveredVariant,
MatchUncoveredVariantWithPattern(Vec<String>),
NonExhaustiveMatch(Vec<String>),
MatchUnreachablePattern,
VarNeedsTypeOrExpression,
ParamTypesIncompatible(String, Vec<String>, Vec<String>),
Expand Down Expand Up @@ -345,8 +344,7 @@ impl ErrorMessage {
format!("value of type {} but pattern of type {}.", value, pattern)
}
ErrorMessage::EnumVariantExpected => format!("enum variant expected."),
ErrorMessage::MatchUncoveredVariant => "not all variants are covered.".into(),
ErrorMessage::MatchUncoveredVariantWithPattern(ref patterns) => {
ErrorMessage::NonExhaustiveMatch(ref patterns) => {
let missing = patterns.join(", ");
format!(
"`match` does not cover all possible values. Missing patterns: {}",
Expand Down
56 changes: 44 additions & 12 deletions dora-frontend/src/exhaustiveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn check_match(
sa.report(
file_id,
node.expr.span(),
ErrorMessage::MatchUncoveredVariantWithPattern(patterns),
ErrorMessage::NonExhaustiveMatch(patterns),
);
}
}
Expand Down Expand Up @@ -1251,7 +1251,7 @@ mod tests {
}
",
(4, 23),
ErrorMessage::MatchUncoveredVariantWithPattern(vec!["Foo::C".into()]),
ErrorMessage::NonExhaustiveMatch(vec!["Foo::C".into()]),
);
}

Expand Down Expand Up @@ -1472,7 +1472,7 @@ mod tests {
}
",
(3, 23),
ErrorMessage::MatchUncoveredVariantWithPattern(vec!["false".into()]),
ErrorMessage::NonExhaustiveMatch(vec!["false".into()]),
);

err(
Expand All @@ -1484,7 +1484,7 @@ mod tests {
}
",
(3, 23),
ErrorMessage::MatchUncoveredVariantWithPattern(vec!["true".into()]),
ErrorMessage::NonExhaustiveMatch(vec!["true".into()]),
);
}

Expand Down Expand Up @@ -1539,7 +1539,7 @@ mod tests {
}
",
(3, 23),
ErrorMessage::MatchUncoveredVariantWithPattern(vec!["_".into()]),
ErrorMessage::NonExhaustiveMatch(vec!["_".into()]),
);
}

Expand All @@ -1556,6 +1556,22 @@ mod tests {
");
}

#[test]
fn non_exhaustive_class() {
err(
"
class Foo(Int, Bool)
fn f(v: Foo) {
match v {
Foo(_, true) => {}
}
}
",
(4, 23),
ErrorMessage::NonExhaustiveMatch(vec!["Foo(_, false)".into()]),
);
}

#[test]
fn exhaustive_struct() {
ok("
Expand All @@ -1569,6 +1585,22 @@ mod tests {
");
}

#[test]
fn non_exhaustive_struct() {
err(
"
struct Foo(Int, Bool)
fn f(v: Foo) {
match v {
Foo(_, false) => {}
}
}
",
(4, 23),
ErrorMessage::NonExhaustiveMatch(vec!["Foo(_, true)".into()]),
);
}

#[test]
fn exhaustive_class_named_fields() {
ok("
Expand Down Expand Up @@ -1640,7 +1672,7 @@ mod tests {
}
",
(4, 23),
ErrorMessage::MatchUncoveredVariantWithPattern(vec!["Foo::B".into(), "Foo::C".into()]),
ErrorMessage::NonExhaustiveMatch(vec!["Foo::B".into(), "Foo::C".into()]),
);
}

Expand All @@ -1657,7 +1689,7 @@ mod tests {
}
",
(4, 23),
ErrorMessage::MatchUncoveredVariantWithPattern(vec![
ErrorMessage::NonExhaustiveMatch(vec![
"Foo::C1".into(),
"Foo::C2".into(),
"Foo::C4".into(),
Expand All @@ -1677,7 +1709,7 @@ mod tests {
}
",
(4, 23),
ErrorMessage::MatchUncoveredVariantWithPattern(vec![
ErrorMessage::NonExhaustiveMatch(vec![
"Foo::C1".into(),
"Foo::C2".into(),
"Foo::C6".into(),
Expand Down Expand Up @@ -1799,7 +1831,7 @@ mod tests {
}
",
(4, 23),
ErrorMessage::MatchUncoveredVariantWithPattern(vec!["(Foo::C, Foo::B)".into()]),
ErrorMessage::NonExhaustiveMatch(vec!["(Foo::C, Foo::B)".into()]),
);

err(
Expand All @@ -1815,7 +1847,7 @@ mod tests {
}
",
(4, 23),
ErrorMessage::MatchUncoveredVariantWithPattern(vec!["(Foo::D, false)".into()]),
ErrorMessage::NonExhaustiveMatch(vec!["(Foo::D, false)".into()]),
);
}

Expand Down Expand Up @@ -1888,7 +1920,7 @@ mod tests {
}
",
(5, 23),
ErrorMessage::MatchUncoveredVariantWithPattern(vec!["Foo::A(_)".into()]),
ErrorMessage::NonExhaustiveMatch(vec!["Foo::A(_)".into()]),
);

err(
Expand All @@ -1907,7 +1939,7 @@ mod tests {
}
",
(5, 23),
ErrorMessage::MatchUncoveredVariantWithPattern(vec!["Foo::D(Bar::X, false)".into()]),
ErrorMessage::NonExhaustiveMatch(vec!["Foo::D(Bar::X, false)".into()]),
);
}
}
2 changes: 1 addition & 1 deletion dora-frontend/src/typeck/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1967,7 +1967,7 @@ fn test_enum_match_missing_variants() {
}
",
(4, 19),
ErrorMessage::MatchUncoveredVariantWithPattern(vec!["A::V3".into()]),
ErrorMessage::NonExhaustiveMatch(vec!["A::V3".into()]),
);

err(
Expand Down

0 comments on commit 2460bb7

Please sign in to comment.