Skip to content

Commit

Permalink
[checker] Ban irrefutable patterns in if let
Browse files Browse the repository at this point in the history
  • Loading branch information
SamChou19815 committed Oct 29, 2023
1 parent b418d8b commit c4607ba
Show file tree
Hide file tree
Showing 7 changed files with 404 additions and 94 deletions.
74 changes: 68 additions & 6 deletions crates/samlang-core/src/checker/checker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2588,18 +2588,29 @@ Found 1 error.
);
assert_checks(
heap,
"{ let _ = (t: Test) -> if let {foo, bar as _, fff as _} = t then 1 else 2; }",
"{ let _ = (t: Test2) -> match (t) { Foo(_) -> 1, Bar(s) -> 2 }; }",
&builder.unit_type(),
);
assert_checks(
heap,
"{ let _ = (t: Test2) -> match (t) { Foo(_) -> 1, Bar(s) -> 2 }; }",
"{ let _ = (t: Test2) -> if let Foo(_) = t then 1 else 2; }",
&builder.unit_type(),
);
assert_checks(
assert_errors(
heap,
"{ let _ = (t: Test2) -> if let Foo(_) = t then 1 else 2; }",
"{ let _ = (t: Test) -> if let {foo, bar as _, fff as _} = t then 1 else 2; }",
&builder.unit_type(),
r#"
Error ---------------------------------- DUMMY.sam:1:31-1:56
The pattern is irrefutable.
1| { let _ = (t: Test) -> if let {foo, bar as _, fff as _} = t then 1 else 2; }
^^^^^^^^^^^^^^^^^^^^^^^^^
Found 1 error.
"#,
);
assert_errors(
heap,
Expand Down Expand Up @@ -2747,6 +2758,14 @@ Found 1 error.
"{ let _ = (t: Test) -> if let [a, b, _] = [1, 2] then 1 else 2; }",
&builder.unit_type(),
r#"
Error ---------------------------------- DUMMY.sam:1:31-1:40
The pattern is irrefutable.
1| { let _ = (t: Test) -> if let [a, b, _] = [1, 2] then 1 else 2; }
^^^^^^^^^
Error ---------------------------------- DUMMY.sam:1:38-1:39
Cannot access member of `Pair<int, int>` at index 2.
Expand All @@ -2755,7 +2774,7 @@ Cannot access member of `Pair<int, int>` at index 2.
^
Found 1 error.
Found 2 errors.
"#,
"Test",
true,
Expand All @@ -2767,6 +2786,9 @@ let _ = (t: Test) -> if let [_, bar] = t then 1 else 2;
let _ = (t: Test2) -> if let Foo(_) = t then 1 else 2;
let _ = (t: Test2) -> if let Foo(_, _) = t then 1 else 2;
let _ = (t: Test2) -> if let Foo111(_) = t then 1 else 2;
let _ = (t: Test2) -> if let Foo111(_, a, {bar as baz, b}, [eee, fff]) = t then 1 else 2;
let _ = (t: Test2) -> if let {s} = t then 1 else 2;
let _ = if let F = 1 then 1 else 2;
}"#,
&builder.unit_type(),
r#"
Expand All @@ -2780,6 +2802,14 @@ The pattern does not bind all fields. The following names have not been mentione
^^^^^^^^^^
Error ---------------------------------- DUMMY.sam:1:31-1:41
The pattern is irrefutable.
1| { let _ = (t: Test) -> if let {bar, boo} = t then 1 else 2;
^^^^^^^^^^
Error ---------------------------------- DUMMY.sam:1:32-1:35
Cannot resolve member `bar` on `Test`.
Expand All @@ -2804,6 +2834,14 @@ The pattern does not bind all fields. Expected number of elements: 3, actual num
^^^^^^^^
Error ---------------------------------- DUMMY.sam:2:29-2:37
The pattern is irrefutable.
2| let _ = (t: Test) -> if let [_, bar] = t then 1 else 2;
^^^^^^^^
Error ---------------------------------- DUMMY.sam:2:33-2:36
Cannot access member of `Test` at index 1.
Expand All @@ -2828,7 +2866,31 @@ Cannot resolve member `Foo111` on `Test2`.
^^^^^^
Found 7 errors.
Error ---------------------------------- DUMMY.sam:6:30-6:36
Cannot resolve member `Foo111` on `Test2`.
6| let _ = (t: Test2) -> if let Foo111(_, a, {bar as baz, b}, [eee, fff]) = t then 1 else 2;
^^^^^^
Error ---------------------------------- DUMMY.sam:7:30-7:33
`Test2` is not an instance of a struct class.
7| let _ = (t: Test2) -> if let {s} = t then 1 else 2;
^^^
Error ---------------------------------- DUMMY.sam:8:16-8:17
`int` is not an instance of an enum class.
8| let _ = if let F = 1 then 1 else 2;
^
Found 12 errors.
"#,
"Test2",
false,
Expand Down
Loading

0 comments on commit c4607ba

Please sign in to comment.