Skip to content

Commit ac1f741

Browse files
committed
clippy/fmt
1 parent a7e025d commit ac1f741

File tree

6 files changed

+263
-195
lines changed

6 files changed

+263
-195
lines changed

crates/hir-analysis/src/ty/decision_tree.rs

Lines changed: 89 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ pub enum DecisionTree<'db> {
2525
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2626
pub struct Occurrence(pub Vec<usize>);
2727

28+
impl Default for Occurrence {
29+
fn default() -> Self {
30+
Self::new()
31+
}
32+
}
33+
2834
impl Occurrence {
2935
pub fn new() -> Self {
3036
Self(vec![])
3137
}
32-
38+
3339
pub fn child(&self, index: usize) -> Self {
3440
let mut path = self.0.clone();
3541
path.push(index);
@@ -53,40 +59,51 @@ fn build_decision_tree_with_arms<'db>(
5359
if matrix.nrows() == 0 {
5460
panic!("Cannot build decision tree from empty matrix");
5561
}
56-
62+
5763
if matrix.ncols() == 0 || is_all_wildcards(matrix, 0) {
5864
return DecisionTree::Leaf {
5965
arm_index: arm_indices[0],
6066
bindings: vec![],
6167
};
6268
}
63-
69+
6470
// For now, always select column 0
6571
let occurrence = Occurrence::new();
6672
let ty = matrix.first_column_ty();
6773
let sigma_set = matrix.sigma_set();
68-
74+
6975
let mut branches = vec![];
7076
for ctor in sigma_set.into_iter() {
71-
let (specialized_matrix, specialized_arms) = specialize_matrix_with_arms(db, matrix, &arm_indices, ctor, true);
77+
let (specialized_matrix, specialized_arms) =
78+
specialize_matrix_with_arms(db, matrix, &arm_indices, ctor, true);
7279
if specialized_matrix.nrows() > 0 {
7380
let subtree = build_decision_tree_with_arms(db, &specialized_matrix, specialized_arms);
7481
branches.push((ctor, subtree));
7582
}
7683
}
77-
84+
7885
let sigma_set = matrix.sigma_set(); // Recreate since we consumed it
7986
let default = if !sigma_set.is_complete(db, ty) {
80-
let (specialized_matrix, specialized_arms) = specialize_matrix_with_arms(db, matrix, &arm_indices, ConstructorKind::Tuple(ty), false);
87+
let (specialized_matrix, specialized_arms) = specialize_matrix_with_arms(
88+
db,
89+
matrix,
90+
&arm_indices,
91+
ConstructorKind::Tuple(ty),
92+
false,
93+
);
8194
if specialized_matrix.nrows() > 0 {
82-
Some(Box::new(build_decision_tree_with_arms(db, &specialized_matrix, specialized_arms)))
95+
Some(Box::new(build_decision_tree_with_arms(
96+
db,
97+
&specialized_matrix,
98+
specialized_arms,
99+
)))
83100
} else {
84101
None
85102
}
86103
} else {
87104
None
88105
};
89-
106+
90107
DecisionTree::Switch {
91108
occurrence,
92109
branches,
@@ -103,29 +120,29 @@ fn specialize_matrix_with_arms<'db>(
103120
) -> (PatternMatrix<'db>, Vec<usize>) {
104121
let mut new_rows = Vec::new();
105122
let mut new_arms = Vec::new();
106-
123+
107124
for (row_idx, row) in matrix.rows.iter().enumerate() {
108125
let specialized_rows = if is_constructor {
109126
row.phi_specialize(db, ctor)
110127
} else {
111128
row.d_specialize(db)
112129
};
113-
130+
114131
for specialized_row in specialized_rows {
115132
new_rows.push(specialized_row);
116133
new_arms.push(arm_indices[row_idx]);
117134
}
118135
}
119-
136+
120137
(PatternMatrix::new(new_rows), new_arms)
121138
}
122139

123140
/// Check if the first row is all wildcards
124-
fn is_all_wildcards<'db>(matrix: &PatternMatrix<'db>, row: usize) -> bool {
141+
fn is_all_wildcards(matrix: &PatternMatrix<'_>, row: usize) -> bool {
125142
if row >= matrix.nrows() {
126143
return false;
127144
}
128-
145+
129146
matrix.rows[row].inner.iter().all(|pat| pat.is_wildcard())
130147
}
131148

@@ -137,10 +154,10 @@ mod tests {
137154
fn test_occurrence_creation() {
138155
let occ = Occurrence::new();
139156
assert_eq!(occ.0, vec![]);
140-
157+
141158
let child = occ.child(0);
142159
assert_eq!(child.0, vec![0]);
143-
160+
144161
let grandchild = child.child(1);
145162
assert_eq!(grandchild.0, vec![0, 1]);
146163
}
@@ -151,7 +168,7 @@ mod tests {
151168
let tuple_first = root.child(0);
152169
let tuple_second = root.child(1);
153170
let nested = tuple_first.child(0).child(1);
154-
171+
155172
assert_eq!(root.0, vec![]);
156173
assert_eq!(tuple_first.0, vec![0]);
157174
assert_eq!(tuple_second.0, vec![1]);
@@ -164,9 +181,12 @@ mod tests {
164181
arm_index: 0,
165182
bindings: vec![("x".to_string(), Occurrence::new())],
166183
};
167-
184+
168185
match leaf {
169-
DecisionTree::Leaf { arm_index, bindings } => {
186+
DecisionTree::Leaf {
187+
arm_index,
188+
bindings,
189+
} => {
170190
assert_eq!(arm_index, 0);
171191
assert_eq!(bindings.len(), 1);
172192
assert_eq!(bindings[0].0, "x");
@@ -182,15 +202,19 @@ mod tests {
182202
arm_index: 0,
183203
bindings: vec![],
184204
};
185-
205+
186206
let switch = DecisionTree::Switch {
187207
occurrence: Occurrence::new(),
188208
branches: vec![],
189209
default: Some(Box::new(leaf)),
190210
};
191-
211+
192212
match switch {
193-
DecisionTree::Switch { occurrence, branches, default } => {
213+
DecisionTree::Switch {
214+
occurrence,
215+
branches,
216+
default,
217+
} => {
194218
assert_eq!(occurrence, Occurrence::new());
195219
assert_eq!(branches.len(), 0);
196220
assert!(default.is_some());
@@ -201,27 +225,26 @@ mod tests {
201225

202226
#[test]
203227
fn test_is_all_wildcards_helper() {
204-
use crate::ty::pattern_analysis::{PatternMatrix, PatternRowVec, SimplifiedPattern, SimplifiedPatternKind};
205-
use crate::ty::ty_def::TyId;
206-
228+
use crate::ty::pattern_analysis::PatternMatrix;
229+
207230
// Test with empty matrix
208231
let empty_matrix = PatternMatrix::new(vec![]);
209232
assert!(!is_all_wildcards(&empty_matrix, 0));
210233
assert!(!is_all_wildcards(&empty_matrix, 1));
211-
234+
212235
// Test with wildcard pattern - we can't easily create a TyId without a db,
213236
// so this test shows the intended structure
214237
}
215238

216239
// Helper to create a mock database for testing
217240
// For now, we'll create minimal tests that don't require full pattern matrices
218-
241+
219242
#[test]
220243
fn test_decision_tree_api_coverage() {
221244
// Test that our decision tree structures work as expected
222245
let occurrence = Occurrence::new();
223246
let child_occurrence = occurrence.child(0);
224-
247+
225248
// Test leaf creation with bindings
226249
let leaf = DecisionTree::Leaf {
227250
arm_index: 1,
@@ -230,27 +253,36 @@ mod tests {
230253
("y".to_string(), child_occurrence),
231254
],
232255
};
233-
234-
if let DecisionTree::Leaf { arm_index, bindings } = leaf {
256+
257+
if let DecisionTree::Leaf {
258+
arm_index,
259+
bindings,
260+
} = leaf
261+
{
235262
assert_eq!(arm_index, 1);
236263
assert_eq!(bindings.len(), 2);
237264
} else {
238265
panic!("Expected leaf");
239266
}
240-
267+
241268
// Test switch creation with multiple branches
242269
let branch_leaf = DecisionTree::Leaf {
243270
arm_index: 0,
244271
bindings: vec![],
245272
};
246-
273+
247274
let switch = DecisionTree::Switch {
248275
occurrence: Occurrence::new(),
249276
branches: vec![], // We can't easily create ConstructorKind without full setup
250277
default: Some(Box::new(branch_leaf)),
251278
};
252-
253-
if let DecisionTree::Switch { occurrence, branches, default } = switch {
279+
280+
if let DecisionTree::Switch {
281+
occurrence,
282+
branches,
283+
default,
284+
} = switch
285+
{
254286
assert_eq!(occurrence, Occurrence::new());
255287
assert_eq!(branches.len(), 0);
256288
assert!(default.is_some());
@@ -264,16 +296,16 @@ mod tests {
264296
// Test building complex occurrence paths
265297
let root = Occurrence::new();
266298
assert_eq!(root.0, vec![]);
267-
299+
268300
// Simulate accessing tuple.0.field.1
269301
let tuple_field = root.child(0);
270302
let nested_field = tuple_field.child(2);
271303
let final_access = nested_field.child(1);
272-
304+
273305
assert_eq!(tuple_field.0, vec![0]);
274306
assert_eq!(nested_field.0, vec![0, 2]);
275307
assert_eq!(final_access.0, vec![0, 2, 1]);
276-
308+
277309
// Test that different paths are independent
278310
let other_path = root.child(1).child(0);
279311
assert_eq!(other_path.0, vec![1, 0]);
@@ -287,23 +319,35 @@ mod tests {
287319
arm_index: 2,
288320
bindings: vec![("inner".to_string(), Occurrence::new().child(1))],
289321
};
290-
322+
291323
let outer_switch = DecisionTree::Switch {
292324
occurrence: Occurrence::new(),
293-
branches: vec![],
325+
branches: vec![],
294326
default: Some(Box::new(inner_leaf)),
295327
};
296-
328+
297329
let root_switch = DecisionTree::Switch {
298330
occurrence: Occurrence::new(),
299331
branches: vec![],
300332
default: Some(Box::new(outer_switch)),
301333
};
302-
334+
303335
// Verify nested structure
304-
if let DecisionTree::Switch { default: Some(inner), .. } = root_switch {
305-
if let DecisionTree::Switch { default: Some(leaf), .. } = *inner {
306-
if let DecisionTree::Leaf { arm_index, bindings } = *leaf {
336+
if let DecisionTree::Switch {
337+
default: Some(inner),
338+
..
339+
} = root_switch
340+
{
341+
if let DecisionTree::Switch {
342+
default: Some(leaf),
343+
..
344+
} = *inner
345+
{
346+
if let DecisionTree::Leaf {
347+
arm_index,
348+
bindings,
349+
} = *leaf
350+
{
307351
assert_eq!(arm_index, 2);
308352
assert_eq!(bindings.len(), 1);
309353
} else {
@@ -316,4 +360,4 @@ mod tests {
316360
panic!("Expected root switch");
317361
}
318362
}
319-
}
363+
}

crates/hir-analysis/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ pub mod adt_def;
2222
pub mod binder;
2323
pub mod const_ty;
2424

25+
pub mod decision_tree;
2526
pub mod def_analysis;
2627
pub mod diagnostics;
2728
pub mod fold;
2829
pub mod func_def;
2930
pub mod method_table;
3031
pub mod pattern_analysis;
31-
pub mod decision_tree;
3232
pub mod trait_def;
3333
pub mod trait_lower;
3434
pub mod trait_resolution; // This line was previously 'pub mod name_resolution;'

0 commit comments

Comments
 (0)