Skip to content

Commit

Permalink
Fix tuple pattern matching issue where some elements would violate ca…
Browse files Browse the repository at this point in the history
…ps. (#2658)

When pattern-matching tuples that have the same number of elements,
ponyc was treating the entire match as violating capabilities when
only one of the elements was violating. In truth we should be able
to recognize that tuples are distinct from the other elements.

This patch fixes `is_tuple_match_tuple` to return MATCHTYPE_REJECT
as soon as any pairwise element comparison returns MATCHTYPE_REJECT.

The fix was pinpointed by @Praetonus when I discussed this issue with
him.
  • Loading branch information
jemc committed Apr 19, 2018
1 parent 10cef01 commit 0051f75
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/libponyc/type/matchtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ static matchtype_t is_tuple_match_tuple(ast_t* operand, ast_t* pattern,
break;
}

if(ok == MATCHTYPE_DENY)
if(ok != MATCHTYPE_ACCEPT)
break;

operand_child = ast_sibling(operand_child);
Expand Down
6 changes: 6 additions & 0 deletions test/libponyc/matchtype.cc
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ TEST_F(MatchTypeTest, Tuples)
"interface Test\n"
" fun z(c1: C1, c2: C2, c3: C3, t1: T1, t2: T2,\n"
" c1c1: (C1, C1), c1c2: (C1, C2), c1c3: (C1, C3),\n"
" c2i1: (C2, I1), c3i1tag: (C3, I1 tag),\n"
" t1t2: (T1, T2), i1: I1, i2: I2)";

TEST_COMPILE(src);
Expand All @@ -328,6 +329,11 @@ TEST_F(MatchTypeTest, Tuples)
ASSERT_EQ(
MATCHTYPE_REJECT, is_matchtype(type_of("i2"), type_of("c1c1"), NULL, &opt));

ASSERT_EQ(
MATCHTYPE_REJECT, is_matchtype(type_of("c2i1"), type_of("c3i1tag"), NULL, &opt));
ASSERT_EQ(
MATCHTYPE_REJECT, is_matchtype(type_of("c3i1tag"), type_of("c2i1"), NULL, &opt));

// We can't make types with don't cares in as the type of a parameter. Modify
// t1t2 instead.
ast_t* t1t2 = type_of("t1t2");
Expand Down

0 comments on commit 0051f75

Please sign in to comment.