diff --git a/dora-frontend/src/error/msg.rs b/dora-frontend/src/error/msg.rs index 04e6d64ed..1fd30d273 100644 --- a/dora-frontend/src/error/msg.rs +++ b/dora-frontend/src/error/msg.rs @@ -51,8 +51,7 @@ pub enum ErrorMessage { EnumExpected, EnumMismatch(String, String), EnumVariantExpected, - MatchUncoveredVariant, - MatchUncoveredVariantWithPattern(Vec), + NonExhaustiveMatch(Vec), MatchUnreachablePattern, VarNeedsTypeOrExpression, ParamTypesIncompatible(String, Vec, Vec), @@ -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: {}", diff --git a/dora-frontend/src/exhaustiveness.rs b/dora-frontend/src/exhaustiveness.rs index abfb67a80..b9a067214 100644 --- a/dora-frontend/src/exhaustiveness.rs +++ b/dora-frontend/src/exhaustiveness.rs @@ -105,7 +105,7 @@ fn check_match( sa.report( file_id, node.expr.span(), - ErrorMessage::MatchUncoveredVariantWithPattern(patterns), + ErrorMessage::NonExhaustiveMatch(patterns), ); } } @@ -1251,7 +1251,7 @@ mod tests { } ", (4, 23), - ErrorMessage::MatchUncoveredVariantWithPattern(vec!["Foo::C".into()]), + ErrorMessage::NonExhaustiveMatch(vec!["Foo::C".into()]), ); } @@ -1472,7 +1472,7 @@ mod tests { } ", (3, 23), - ErrorMessage::MatchUncoveredVariantWithPattern(vec!["false".into()]), + ErrorMessage::NonExhaustiveMatch(vec!["false".into()]), ); err( @@ -1484,7 +1484,7 @@ mod tests { } ", (3, 23), - ErrorMessage::MatchUncoveredVariantWithPattern(vec!["true".into()]), + ErrorMessage::NonExhaustiveMatch(vec!["true".into()]), ); } @@ -1539,7 +1539,7 @@ mod tests { } ", (3, 23), - ErrorMessage::MatchUncoveredVariantWithPattern(vec!["_".into()]), + ErrorMessage::NonExhaustiveMatch(vec!["_".into()]), ); } @@ -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(" @@ -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(" @@ -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()]), ); } @@ -1657,7 +1689,7 @@ mod tests { } ", (4, 23), - ErrorMessage::MatchUncoveredVariantWithPattern(vec![ + ErrorMessage::NonExhaustiveMatch(vec![ "Foo::C1".into(), "Foo::C2".into(), "Foo::C4".into(), @@ -1677,7 +1709,7 @@ mod tests { } ", (4, 23), - ErrorMessage::MatchUncoveredVariantWithPattern(vec![ + ErrorMessage::NonExhaustiveMatch(vec![ "Foo::C1".into(), "Foo::C2".into(), "Foo::C6".into(), @@ -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( @@ -1815,7 +1847,7 @@ mod tests { } ", (4, 23), - ErrorMessage::MatchUncoveredVariantWithPattern(vec!["(Foo::D, false)".into()]), + ErrorMessage::NonExhaustiveMatch(vec!["(Foo::D, false)".into()]), ); } @@ -1888,7 +1920,7 @@ mod tests { } ", (5, 23), - ErrorMessage::MatchUncoveredVariantWithPattern(vec!["Foo::A(_)".into()]), + ErrorMessage::NonExhaustiveMatch(vec!["Foo::A(_)".into()]), ); err( @@ -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()]), ); } } diff --git a/dora-frontend/src/typeck/tests.rs b/dora-frontend/src/typeck/tests.rs index 398a9dff3..f8a4cd5c6 100644 --- a/dora-frontend/src/typeck/tests.rs +++ b/dora-frontend/src/typeck/tests.rs @@ -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(