Skip to content

Commit

Permalink
Reintroduce exhaustiveness checking for use (#3880)
Browse files Browse the repository at this point in the history
  • Loading branch information
GearsDatapacks authored and lpil committed Nov 23, 2024
1 parent 402474b commit 9eee754
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@

### Formatter

### Bug fix

## v1.6.2 - 2024-11-23

### Bug fixed

- Fixed a bug where patterns in `use` expressions would not be checked to ensure that
they were exhaustive.
([Surya Rose](https://github.com/GearsDatapacks))

## v1.6.1 - 2024-11-19

### Bug fixed
### Bug fix

- Fixed a bug where `gleam update` would fail to update versions.
([Jason Sipula](https://github.com/SnakeDoc))
6 changes: 2 additions & 4 deletions compiler-core/src/type_/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,10 +1353,8 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
// Do not perform exhaustiveness checking if user explicitly used `let assert ... = ...`.
let exhaustiveness_check = self.check_let_exhaustiveness(location, value.type_(), &pattern);
match (kind, exhaustiveness_check) {
// Generated assignments should be checked before they are generated
(AssignmentKind::Generated, _) => {}
(AssignmentKind::Let, Ok(_)) => {}
(AssignmentKind::Let, Err(e)) => {
(AssignmentKind::Let | AssignmentKind::Generated, Ok(_)) => {}
(AssignmentKind::Let | AssignmentKind::Generated, Err(e)) => {
self.problems.error(e);
}
(AssignmentKind::Assert { location }, Ok(_)) => self
Expand Down
11 changes: 11 additions & 0 deletions compiler-core/src/type_/tests/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2639,3 +2639,14 @@ pub fn main() {
"
);
}

#[test]
// https://github.com/gleam-lang/gleam/issues/3879
fn inexhaustive_use_reports_error() {
assert_error!(
r#"
use [1, 2, 3] <- todo
todo
"#
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: compiler-core/src/type_/tests/errors.rs
expression: "\nuse [1, 2, 3] <- todo\ntodo\n"
---
----- SOURCE CODE

use [1, 2, 3] <- todo
todo


----- ERROR
error: Inexhaustive pattern
┌─ /src/one/two.gleam:2:5
2use [1, 2, 3] <- todo
^^^^^^^^^

This assignment uses a pattern that does not match all possible values. If
one of the other values is used then the assignment will crash.

The missing patterns are:

[]
[_, _, _, _, ..]
[_, _, _]
[_, _]
[_]

Hint: Use a more general pattern or use `let assert` instead.

0 comments on commit 9eee754

Please sign in to comment.