Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Feb 21, 2024
1 parent a304a97 commit 13b18c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
28 changes: 18 additions & 10 deletions crates/common2/src/recursive_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ use std::{fmt::Debug, hash::Hash};
use ena::unify::{InPlaceUnificationTable, UnifyKey};
use rustc_hash::FxHashMap;

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

for def in defs.iter() {
Expand All @@ -68,6 +77,8 @@ where
Self { defs, table, keys }
}

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

#[test]
fn one_recursion() {
let defs = vec![
RecursiveDef::new(0, 1, ((), ())),
RecursiveDef::new(1, 0, ((), ())),
];
let defs = vec![RecursiveDef::new(0, 1, ()), RecursiveDef::new(1, 0, ())];

let mut helper = RecursiveDefHelper::new(defs);
let disjoint_constituents = helper.remove_disjoint_set();
Expand All @@ -114,11 +122,11 @@ fn one_recursion() {
#[test]
fn two_recursions() {
let defs = vec![
RecursiveDef::new(0, 1, ((), ())),
RecursiveDef::new(1, 0, ((), ())),
RecursiveDef::new(2, 3, ((), ())),
RecursiveDef::new(3, 4, ((), ())),
RecursiveDef::new(4, 2, ((), ())),
RecursiveDef::new(0, 1, ()),
RecursiveDef::new(1, 0, ()),
RecursiveDef::new(2, 3, ()),
RecursiveDef::new(3, 4, ()),
RecursiveDef::new(4, 2, ()),
];

let mut helper = RecursiveDefHelper::new(defs);
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-analysis/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,22 @@ impl<'db> ModuleAnalysisPass for TypeDefAnalysisPass<'db> {
.iter()
.map(|c| AdtRefId::from_contract(self.db, *c)),
);
let (diags, adt_recursive_defs): (Vec<_>, Vec<_>) = adts
let (diags, recursive_adt_defs): (Vec<_>, Vec<_>) = adts
.map(|adt| {
(
analyze_adt::accumulated::<AdtDefDiagAccumulator>(self.db, adt),
analyze_adt::accumulated::<RecursiveAdtDefAccumulator>(self.db, adt),
)
})
.unzip();
let adt_recursive_defs = adt_recursive_defs.into_iter().flatten().collect_vec();
let recursive_adt_defs = recursive_adt_defs.into_iter().flatten().collect_vec();

diags
.into_iter()
.flatten()
.map(|diag| diag.to_voucher())
.chain(
adt_recursion_diags(adt_recursive_defs)
adt_recursion_diags(recursive_adt_defs)
.iter()
.map(|diag| diag.to_voucher()),
)
Expand Down

0 comments on commit 13b18c4

Please sign in to comment.