Skip to content

Commit fa71b88

Browse files
fruno-bulaxlpil
authored andcommitted
✨ Improve ordering of missing patterns
1 parent 4f5a542 commit fa71b88

16 files changed

+29
-31
lines changed

compiler-core/src/exhaustiveness/missing_patterns.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::{CompileCaseResult, Decision, FallbackCheck, RuntimeCheck, Variable, printer::Printer};
22
use crate::type_::environment::Environment;
33
use ecow::EcoString;
4-
use itertools::Itertools;
5-
use std::collections::{HashMap, HashSet};
4+
use indexmap::IndexSet;
5+
use std::collections::HashMap;
66

77
/// Returns a list of patterns not covered by the match expression.
88
pub fn missing_patterns(
@@ -12,11 +12,8 @@ pub fn missing_patterns(
1212
let subjects = &result.compiled_case.subject_variables;
1313
let mut generator = MissingPatternsGenerator::new(subjects, environment);
1414
generator.add_missing_patterns(&result.compiled_case.tree);
15-
let mut missing = generator.missing.into_iter().collect_vec();
1615

17-
// Sorting isn't necessary, but it makes it a bit easier to write tests.
18-
missing.sort();
19-
missing
16+
generator.missing.into_iter().collect()
2017
}
2118

2219
#[derive(Debug, Clone)]
@@ -67,7 +64,7 @@ impl Term {
6764
struct MissingPatternsGenerator<'a, 'env> {
6865
subjects: &'a Vec<Variable>,
6966
terms: Vec<Term>,
70-
missing: HashSet<EcoString>,
67+
missing: IndexSet<EcoString>,
7168
environment: &'a Environment<'env>,
7269
printer: Printer<'a>,
7370
}
@@ -77,7 +74,7 @@ impl<'a, 'env> MissingPatternsGenerator<'a, 'env> {
7774
MissingPatternsGenerator {
7875
subjects,
7976
terms: vec![],
80-
missing: HashSet::new(),
77+
missing: IndexSet::new(),
8178
environment,
8279
printer: Printer::new(&environment.names),
8380
}
@@ -113,6 +110,7 @@ impl<'a, 'env> MissingPatternsGenerator<'a, 'env> {
113110
_ = mapping.insert(step.variable().id, index);
114111
}
115112
let pattern = self.print_terms(mapping);
113+
116114
_ = self.missing.insert(pattern);
117115
}
118116

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__action__add_missing_patterns_bool.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn main(bool: Bool) {
1414

1515
pub fn main(bool: Bool) {
1616
case bool {
17-
False -> todo
1817
True -> todo
18+
False -> todo
1919
}
2020
}

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__action__add_missing_patterns_inline.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn main(a: Bool) {
1414

1515
pub fn main(a: Bool) {
1616
let value = case a {
17-
False -> todo
1817
True -> todo
18+
False -> todo
1919
}
2020
}

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__action__add_missing_patterns_list.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ pub fn main() {
2020
case list {
2121
[a, b, c, 4 as d] -> d
2222
[] -> todo
23-
[_, _, _, _, _, ..] -> todo
24-
[_, _, _, _] -> todo
25-
[_, _, _] -> todo
26-
[_, _] -> todo
2723
[_] -> todo
24+
[_, _] -> todo
25+
[_, _, _] -> todo
26+
[_, _, _, _] -> todo
27+
[_, _, _, _, _, ..] -> todo
2828
}
2929
}

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__action__add_missing_patterns_multi.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn main(a: Bool) {
1818
pub fn main(a: Bool) {
1919
let b = 1
2020
case a, b {
21-
False, _ -> todo
2221
True, _ -> todo
22+
False, _ -> todo
2323
}
2424
}

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__action__inexhaustive_let_to_case_multi_variables.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ pub fn main() {
1414
let #(var1, var2, var4) = case [1, 2, 3, 4] {
1515
[var1, var2, _var3, var4] -> #(var1, var2, var4)
1616
[] -> todo
17-
[_, _, _, _, _, ..] -> todo
18-
[_, _, _] -> todo
19-
[_, _] -> todo
2017
[_] -> todo
18+
[_, _] -> todo
19+
[_, _, _] -> todo
20+
[_, _, _, _, _, ..] -> todo
2121
}
2222
}

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__inexhaustive_use_reports_error.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ one of the other values is used then the assignment will crash.
2121
The missing patterns are:
2222

2323
[]
24-
[_, _, _, _, ..]
25-
[_, _, _]
26-
[_, _]
2724
[_]
25+
[_, _]
26+
[_, _, _]
27+
[_, _, _, _, ..]
2828

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

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__exhaustiveness__custom_2.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ is run on one of the values without a pattern then it will crash.
3535

3636
The missing patterns are:
3737

38-
Three(Three(_))
3938
Three(Two)
39+
Three(Three(_))

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__exhaustiveness__empty_case_of_bool.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ is run on one of the values without a pattern then it will crash.
2121

2222
The missing patterns are:
2323

24-
False
2524
True
25+
False

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__exhaustiveness__empty_case_of_multi_pattern.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ is run on one of the values without a pattern then it will crash.
2121

2222
The missing patterns are:
2323

24-
Error(_), _
2524
Ok(_), _
25+
Error(_), _

0 commit comments

Comments
 (0)