Skip to content

Commit 13b18c4

Browse files
author
Grant Wuerker
committed
cleanup
1 parent a304a97 commit 13b18c4

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

crates/common2/src/recursive_def.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ use std::{fmt::Debug, hash::Hash};
33
use ena::unify::{InPlaceUnificationTable, UnifyKey};
44
use rustc_hash::FxHashMap;
55

6+
/// Represents a definition that contains a direct reference to itself.
7+
///
8+
/// Recursive definitions are not valid and must be reported to the user.
9+
/// It is preferable to group definitions together such that recursions
10+
/// are reported in-whole rather than separately. `RecursiveDef` can be
11+
/// used with `RecursiveDefHelper` to perform this grouping operation.
12+
///
13+
/// The fields `from` and `to` are the relevant identifiers and `site` can
14+
/// be used to carry diagnostic information.
615
#[derive(Eq, PartialEq, Clone, Debug, Hash)]
716
pub struct RecursiveDef<T, U>
817
where
@@ -58,7 +67,7 @@ where
5867
let mut table = InPlaceUnificationTable::new();
5968
let keys: FxHashMap<_, _> = defs
6069
.iter()
61-
.map(|def: &RecursiveDef<T, U>| (def.from, table.new_key(())))
70+
.map(|def| (def.from, table.new_key(())))
6271
.collect();
6372

6473
for def in defs.iter() {
@@ -68,6 +77,8 @@ where
6877
Self { defs, table, keys }
6978
}
7079

80+
/// Removes a disjoint set of recursive definitions from the helper
81+
/// and returns it, if one exists.
7182
pub fn remove_disjoint_set(&mut self) -> Option<Vec<RecursiveDef<T, U>>> {
7283
let mut disjoint_set = vec![];
7384
let mut remaining_set = vec![];
@@ -99,10 +110,7 @@ where
99110

100111
#[test]
101112
fn one_recursion() {
102-
let defs = vec![
103-
RecursiveDef::new(0, 1, ((), ())),
104-
RecursiveDef::new(1, 0, ((), ())),
105-
];
113+
let defs = vec![RecursiveDef::new(0, 1, ()), RecursiveDef::new(1, 0, ())];
106114

107115
let mut helper = RecursiveDefHelper::new(defs);
108116
let disjoint_constituents = helper.remove_disjoint_set();
@@ -114,11 +122,11 @@ fn one_recursion() {
114122
#[test]
115123
fn two_recursions() {
116124
let defs = vec![
117-
RecursiveDef::new(0, 1, ((), ())),
118-
RecursiveDef::new(1, 0, ((), ())),
119-
RecursiveDef::new(2, 3, ((), ())),
120-
RecursiveDef::new(3, 4, ((), ())),
121-
RecursiveDef::new(4, 2, ((), ())),
125+
RecursiveDef::new(0, 1, ()),
126+
RecursiveDef::new(1, 0, ()),
127+
RecursiveDef::new(2, 3, ()),
128+
RecursiveDef::new(3, 4, ()),
129+
RecursiveDef::new(4, 2, ()),
122130
];
123131

124132
let mut helper = RecursiveDefHelper::new(defs);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,22 @@ impl<'db> ModuleAnalysisPass for TypeDefAnalysisPass<'db> {
6464
.iter()
6565
.map(|c| AdtRefId::from_contract(self.db, *c)),
6666
);
67-
let (diags, adt_recursive_defs): (Vec<_>, Vec<_>) = adts
67+
let (diags, recursive_adt_defs): (Vec<_>, Vec<_>) = adts
6868
.map(|adt| {
6969
(
7070
analyze_adt::accumulated::<AdtDefDiagAccumulator>(self.db, adt),
7171
analyze_adt::accumulated::<RecursiveAdtDefAccumulator>(self.db, adt),
7272
)
7373
})
7474
.unzip();
75-
let adt_recursive_defs = adt_recursive_defs.into_iter().flatten().collect_vec();
75+
let recursive_adt_defs = recursive_adt_defs.into_iter().flatten().collect_vec();
7676

7777
diags
7878
.into_iter()
7979
.flatten()
8080
.map(|diag| diag.to_voucher())
8181
.chain(
82-
adt_recursion_diags(adt_recursive_defs)
82+
adt_recursion_diags(recursive_adt_defs)
8383
.iter()
8484
.map(|diag| diag.to_voucher()),
8585
)

0 commit comments

Comments
 (0)