Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect detection of exhaustive matches for structural equality comparisons on some primitives. #2342

Merged
merged 2 commits into from
Nov 17, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libponyc/expr/match.c
Original file line number Diff line number Diff line change
@@ -111,7 +111,7 @@ static ast_t* is_match_exhaustive(pass_opt_t* opt, ast_t* expr_type,
for(ast_t* c = ast_child(cases); c != NULL; c = ast_sibling(c))
{
AST_GET_CHILDREN(c, case_expr, guard, case_body);
ast_t* pattern_type = ast_type(c);
ast_t* case_type = ast_type(case_expr);

// if any case is a `_` we have an exhaustive match
// and we can shortcut here
@@ -133,7 +133,7 @@ static ast_t* is_match_exhaustive(pass_opt_t* opt, ast_t* expr_type,
continue;

// It counts, so add this pattern type to our running union type.
ast_add(cases_union_type, pattern_type);
ast_add(cases_union_type, case_type);

// If our cases types union is a supertype of the match expression type,
// then the match must be exhaustive, because all types are covered by cases.
43 changes: 43 additions & 0 deletions test/libponyc/sugar_expr.cc
Original file line number Diff line number Diff line change
@@ -972,6 +972,49 @@ TEST_F(SugarExprTest, MatchExhaustiveUnreachableCases)
TEST_ERRORS_1(src, "match contains unreachable cases");
}

TEST_F(SugarExprTest, MatchExhaustiveEquatableOfSubType)
{
const char* src =
"interface Equatable[T]\n"
" fun eq(that: T): Bool => this is that\n"
"primitive X is Equatable[Dimension]\n"
"primitive Y is Equatable[Dimension]\n"
"primitive Z is Equatable[Dimension]\n"
"\n"
"type Dimension is ( X | Y | Z )\n"
"\n"
"primitive Foo\n"
" fun apply(p: Dimension) =>\n"
" let s: String =\n"
" match p\n"
" | X => \"x\"\n"
" | Y => \"y\"\n"
" | Z => \"z\"\n"
" end";
TEST_COMPILE(src);
}

TEST_F(SugarExprTest, MatchNonExhaustiveSubsetOfEquatableOfSubType)
{
const char* src =
"interface Equatable[T]\n"
" fun eq(that: T): Bool => this is that\n"
"primitive X is Equatable[Dimension]\n"
"primitive Y is Equatable[Dimension]\n"
"primitive Z is Equatable[Dimension]\n"
"\n"
"type Dimension is ( X | Y | Z )\n"
"\n"
"primitive Foo\n"
" fun apply(p: Dimension): String =>\n"
" match p\n"
" | X => \"x\"\n"
" | Y => \"y\"\n"
" end";
TEST_ERRORS_1(src, "function body isn't the result type");

}

TEST_F(SugarExprTest, MatchStructuralEqualityOnIncompatibleUnion)
{
// From issue #2110