diff --git a/CHANGELOG.md b/CHANGELOG.md index 4639880ddae..c2f01b8cdf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/compiler-core/src/type_/expression.rs b/compiler-core/src/type_/expression.rs index 5e1f94dd206..cf65a8e6b2b 100644 --- a/compiler-core/src/type_/expression.rs +++ b/compiler-core/src/type_/expression.rs @@ -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 diff --git a/compiler-core/src/type_/tests/errors.rs b/compiler-core/src/type_/tests/errors.rs index 43a438458a7..9d6c6a51b9c 100644 --- a/compiler-core/src/type_/tests/errors.rs +++ b/compiler-core/src/type_/tests/errors.rs @@ -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 +"# + ); +} diff --git a/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__inexhaustive_use_reports_error.snap b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__inexhaustive_use_reports_error.snap new file mode 100644 index 00000000000..e2d47e9d665 --- /dev/null +++ b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__inexhaustive_use_reports_error.snap @@ -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 + │ +2 │ use [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.