Skip to content

Commit

Permalink
hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Dec 20, 2023
1 parent d664a7a commit 313a6b5
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 51 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/hir-analysis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ either = "1.8"
derive_more = "0.99"
itertools = "0.10"
ena = "0.14"

indexmap = "1.6.2"
hir = { path = "../hir", package = "fe-hir" }
common = { path = "../common2", package = "fe-common2" }
macros = { path = "../macros", package = "fe-macros" }
Expand Down
1 change: 1 addition & 0 deletions crates/hir-analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub struct Jar(
ty::diagnostics::ImplTraitDefDiagAccumulator,
ty::diagnostics::ImplDefDiagAccumulator,
ty::diagnostics::FuncDefDiagAccumulator,
ty::RecursiveAdtConstituentAccumulator,
);

pub trait HirAnalysisDb: salsa::DbWithJar<Jar> + HirDb {
Expand Down
17 changes: 10 additions & 7 deletions crates/hir-analysis/src/ty/def_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ use super::{
GenericParamOwnerId,
},
visitor::{walk_ty, TyVisitor},
AdtRecursionConstituent,
};
use crate::ty::RecursiveAdtConstituentAccumulator;

/// This function implements analysis for the ADT definition.
/// The analysis includes the following:
Expand All @@ -64,8 +66,8 @@ pub fn analyze_adt(db: &dyn HirAnalysisDb, adt_ref: AdtRefId) {
AdtDefDiagAccumulator::push(db, diag);
}

if let Some(diag) = check_recursive_adt(db, adt_ref) {
AdtDefDiagAccumulator::push(db, diag);
if let Some(cycle) = check_recursive_adt(db, adt_ref) {
RecursiveAdtConstituentAccumulator::push(db, cycle);
}
}

Expand Down Expand Up @@ -701,7 +703,7 @@ impl<'db> Visitor for DefAnalyzer<'db> {
pub(crate) fn check_recursive_adt(
db: &dyn HirAnalysisDb,
adt: AdtRefId,
) -> Option<TyDiagCollection> {
) -> Option<AdtRecursionConstituent> {
let adt_def = lower_adt(db, adt);
for field in adt_def.fields(db) {
for ty in field.iter_types(db) {
Expand All @@ -718,7 +720,7 @@ fn check_recursive_adt_impl(
db: &dyn HirAnalysisDb,
cycle: &salsa::Cycle,
adt: AdtRefId,
) -> Option<TyDiagCollection> {
) -> Option<AdtRecursionConstituent> {
let participants: FxHashSet<_> = cycle
.participant_keys()
.map(|key| check_recursive_adt::key_from_id(key.key_index()))
Expand All @@ -729,11 +731,12 @@ fn check_recursive_adt_impl(
for (ty_idx, ty) in field.iter_types(db).enumerate() {
for field_adt_ref in ty.collect_direct_adts(db) {
if participants.contains(&field_adt_ref) && participants.contains(&adt) {
let diag = TyLowerDiag::recursive_type(
adt.name_span(db),
let constituent = AdtRecursionConstituent::new(
(adt, adt.name_span(db)),
field_adt_ref,
adt_def.variant_ty_span(db, field_idx, ty_idx),
);
return Some(diag.into());
return Some(constituent);
}
}
}
Expand Down
43 changes: 22 additions & 21 deletions crates/hir-analysis/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use crate::HirAnalysisDb;

use super::{
constraint::PredicateId,
ty_def::{Kind, TyId},
ty_def::{AdtRefId, Kind, TyId},
AdtRecursionConstituent,
};

use itertools::Itertools;
Expand Down Expand Up @@ -55,10 +56,7 @@ impl TyDiagCollection {
pub enum TyLowerDiag {
NotFullyAppliedType(DynLazySpan),
InvalidTypeArgKind(DynLazySpan, String),
RecursiveType {
primary_span: DynLazySpan,
field_span: DynLazySpan,
},
RecursiveType(Vec<AdtRecursionConstituent>),

UnboundTypeAliasParam {
span: DynLazySpan,
Expand Down Expand Up @@ -117,11 +115,8 @@ impl TyLowerDiag {
Self::InvalidTypeArgKind(span, msg)
}

pub(super) fn recursive_type(primary_span: DynLazySpan, field_span: DynLazySpan) -> Self {
Self::RecursiveType {
primary_span,
field_span,
}
pub(super) fn recursive_type(constituents: Vec<AdtRecursionConstituent>) -> Self {
Self::RecursiveType(constituents)
}

pub(super) fn unbound_type_alias_param(
Expand Down Expand Up @@ -235,22 +230,28 @@ impl TyLowerDiag {
span.resolve(db),
)],

Self::RecursiveType {
primary_span,
field_span,
} => {
vec![
SubDiagnostic::new(
Self::RecursiveType(constituents) => {
let mut diags = vec![];

for AdtRecursionConstituent {
from,
to,
field_span,
} in constituents
{
diags.push(SubDiagnostic::new(
LabelStyle::Primary,
"recursive type definition".to_string(),
primary_span.resolve(db),
),
SubDiagnostic::new(
from.1.resolve(db),
));
diags.push(SubDiagnostic::new(
LabelStyle::Secondary,
"recursion occurs here".to_string(),
field_span.resolve(db),
),
]
));
}

diags
}

Self::UnboundTypeAliasParam {
Expand Down
86 changes: 79 additions & 7 deletions crates/hir-analysis/src/ty/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::HirAnalysisDb;
use hir::{analysis_pass::ModuleAnalysisPass, hir_def::TopLevelMod};
use hir::{analysis_pass::ModuleAnalysisPass, hir_def::TopLevelMod, span::DynLazySpan};
use indexmap::indexset;

use self::{
def_analysis::{
Expand All @@ -8,7 +9,8 @@ use self::{
},
diagnostics::{
AdtDefDiagAccumulator, FuncDefDiagAccumulator, ImplDefDiagAccumulator,
ImplTraitDefDiagAccumulator, TraitDefDiagAccumulator, TypeAliasDefDiagAccumulator,
ImplTraitDefDiagAccumulator, TraitDefDiagAccumulator, TyDiagCollection, TyLowerDiag,
TypeAliasDefDiagAccumulator,
},
ty_def::AdtRefId,
};
Expand Down Expand Up @@ -60,12 +62,82 @@ impl<'db> ModuleAnalysisPass for TypeDefAnalysisPass<'db> {
.iter()
.map(|c| AdtRefId::from_contract(self.db, *c)),
);
let mut cycles = vec![];
let mut diags = adts
.flat_map(|adt| {
cycles.append(&mut analyze_adt::accumulated::<
RecursiveAdtConstituentAccumulator,
>(self.db, adt));
analyze_adt::accumulated::<AdtDefDiagAccumulator>(self.db, adt).into_iter()
})
.map(|diag| diag.to_voucher())
.collect();

if cycles.is_empty() {
diags
} else {
diags.append(
&mut recursive_adt_diags(&cycles)
.into_iter()
.map(|constituent| TyDiagCollection::Ty(constituent).to_voucher())
.collect(),
);
diags
}
}
}

#[salsa::accumulator]
pub struct RecursiveAdtConstituentAccumulator(pub(super) AdtRecursionConstituent);

pub fn recursive_adt_diags(constituents: &[AdtRecursionConstituent]) -> Vec<TyLowerDiag> {
let mut diags = vec![];
let mut unified_constituents = indexset! {};

while let Some(mut cur) =
(0..constituents.len()).find(|index| !unified_constituents.contains(index))
{
unified_constituents.insert(cur);
let mut recursion = vec![cur];

while constituents[recursion[0]].from.0 != constituents[cur].to {
if let Some(index) = (0..constituents.len()).find(|index| {
!unified_constituents.contains(index)
&& constituents[cur].to == constituents[*index].from.0
}) {
cur = index;
unified_constituents.insert(index);
recursion.push(index);
} else {
break;
};
}

diags.push(TyLowerDiag::recursive_type(
recursion
.iter()
.map(|index| constituents[*index].to_owned())
.collect(),
));
}

diags
}

#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct AdtRecursionConstituent {
pub from: (AdtRefId, DynLazySpan),
pub to: AdtRefId,
pub field_span: DynLazySpan,
}

adts.flat_map(|adt| {
analyze_adt::accumulated::<AdtDefDiagAccumulator>(self.db, adt).into_iter()
})
.map(|diag| diag.to_voucher())
.collect()
impl AdtRecursionConstituent {
pub fn new(from: (AdtRefId, DynLazySpan), to: AdtRefId, field_span: DynLazySpan) -> Self {
Self {
from,
to,
field_span,
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/uitest/fixtures/ty/def/recursive_type.fe
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ pub struct S5<T> {

pub struct S6 {
s: S5<S6>
}
}
22 changes: 8 additions & 14 deletions crates/uitest/fixtures/ty/def/recursive_type.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/uitest/tests/ty.rs
expression: diags
input_file: crates/uitest/fixtures/ty/recursive_type.fe
input_file: crates/uitest/fixtures/ty/def/recursive_type.fe
---
error[3-0002]: recursive type is not allowed
┌─ recursive_type.fe:1:12
Expand All @@ -12,24 +12,18 @@ error[3-0002]: recursive type is not allowed
│ -- recursion occurs here

error[3-0002]: recursive type is not allowed
┌─ recursive_type.fe:5:12
5 │ pub struct S2 {
│ ^^ recursive type definition
6 │ s: S3
│ -- recursion occurs here

error[3-0002]: recursive type is not allowed
┌─ recursive_type.fe:9:12
┌─ recursive_type.fe:5:12
5 │ pub struct S2 {
│ ^^ recursive type definition
6 │ s: S3
│ -- recursion occurs here
·
9 │ pub struct S3 {
│ ^^ recursive type definition
10 │ s: S4
│ -- recursion occurs here

error[3-0002]: recursive type is not allowed
┌─ recursive_type.fe:13:12
·
13 │ pub struct S4 {
│ ^^ recursive type definition
14 │ s: S2
Expand Down

0 comments on commit 313a6b5

Please sign in to comment.