Skip to content

fix: Cycle handlers for HirDatabase::infer, const_param_ty_with_diagnostics #19894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/hir-ty/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::{
#[query_group::query_group]
pub trait HirDatabase: DefDatabase + std::fmt::Debug {
#[salsa::invoke(crate::infer::infer_query)]
#[salsa::cycle(cycle_result = crate::infer::infer_cycle_result)]
fn infer(&self, def: DefWithBodyId) -> Arc<InferenceResult>;

// region:mir
Expand Down Expand Up @@ -132,6 +133,7 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {

// FIXME: Make this a non-interned query.
#[salsa::invoke_interned(crate::lower::const_param_ty_with_diagnostics_query)]
#[salsa::cycle(cycle_result = crate::lower::const_param_ty_with_diagnostics_cycle_result)]
fn const_param_ty_with_diagnostics(&self, def: ConstParamId) -> (Ty, Diagnostics);

#[salsa::invoke(crate::lower::const_param_ty_query)]
Expand Down
7 changes: 7 additions & 0 deletions crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<Infer
Arc::new(ctx.resolve_all())
}

pub(crate) fn infer_cycle_result(_: &dyn HirDatabase, _: DefWithBodyId) -> Arc<InferenceResult> {
Arc::new(InferenceResult { has_errors: true, ..Default::default() })
}

/// Fully normalize all the types found within `ty` in context of `owner` body definition.
///
/// This is appropriate to use only after type-check: it assumes
Expand Down Expand Up @@ -558,6 +562,9 @@ impl InferenceResult {
ExprOrPatId::PatId(id) => self.type_of_pat.get(id),
}
}
pub fn is_erroneous(&self) -> bool {
self.has_errors && self.type_of_expr.iter().count() == 0
}
}

impl Index<ExprId> for InferenceResult {
Expand Down
20 changes: 14 additions & 6 deletions crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,14 @@ pub(crate) fn impl_self_ty_with_diagnostics_query(
)
}

pub(crate) fn impl_self_ty_with_diagnostics_cycle_result(
db: &dyn HirDatabase,
impl_id: ImplId,
) -> (Binders<Ty>, Diagnostics) {
let generics = generics(db, impl_id.into());
(make_binders(db, &generics, TyKind::Error.intern(Interner)), None)
}

pub(crate) fn const_param_ty_query(db: &dyn HirDatabase, def: ConstParamId) -> Ty {
db.const_param_ty_with_diagnostics(def).0
}
Expand Down Expand Up @@ -1633,12 +1641,12 @@ pub(crate) fn const_param_ty_with_diagnostics_query(
(ty, create_diagnostics(ctx.diagnostics))
}

pub(crate) fn impl_self_ty_with_diagnostics_cycle_result(
db: &dyn HirDatabase,
impl_id: ImplId,
) -> (Binders<Ty>, Diagnostics) {
let generics = generics(db, impl_id.into());
(make_binders(db, &generics, TyKind::Error.intern(Interner)), None)
pub(crate) fn const_param_ty_with_diagnostics_cycle_result(
_: &dyn HirDatabase,
_: crate::db::HirDatabaseData,
_: ConstParamId,
) -> (Ty, Diagnostics) {
(TyKind::Error.intern(Interner), None)
}

pub(crate) fn impl_trait_query(db: &dyn HirDatabase, impl_id: ImplId) -> Option<Binders<TraitRef>> {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/mir/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2182,7 +2182,7 @@ pub fn lower_to_mir(
// need to take this input explicitly.
root_expr: ExprId,
) -> Result<MirBody> {
if infer.type_mismatches().next().is_some() {
if infer.type_mismatches().next().is_some() || infer.is_erroneous() {
return Err(MirLowerError::HasErrors);
}
let mut ctx = MirLowerCtx::new(db, owner, body, infer);
Expand Down
48 changes: 48 additions & 0 deletions crates/hir-ty/src/tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2301,3 +2301,51 @@ trait Foo {
"#]],
);
}

#[test]
fn no_panic_on_recursive_const() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Managed to reproduce panics here

check_infer(
r#"
struct Foo<const N: usize> {}
impl<const N: Foo<N>> Foo<N> {
fn foo(self) {}
}

fn test() {
let _ = N;
}
"#,
expect![[r#"
72..76 'self': Foo<N>
78..80 '{}': ()
94..112 '{ ...= N; }': ()
104..105 '_': {unknown}
108..109 'N': {unknown}
"#]],
);

check_infer(
r#"
struct Foo<const N: usize>;
const N: Foo<N> = Foo;

impl<const N: usize> Foo<N> {
fn foo(self) -> usize {
N
}
}

fn test() {
let _ = N;
}
"#,
expect![[r#"
93..97 'self': Foo<N>
108..125 '{ ... }': usize
118..119 'N': usize
139..157 '{ ...= N; }': ()
149..150 '_': Foo<_>
153..154 'N': Foo<_>
"#]],
);
}