From 797ba9c7cb177650be4a3bc6ac553e4f414a40c3 Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Fri, 3 Oct 2025 06:54:32 +0300 Subject: [PATCH] Fix erroneous diagnostic incorrect_generics_len when there are generics on enum variant used through type alias --- crates/hir-ty/src/infer/path.rs | 25 +++++++++++++------ .../src/handlers/incorrect_generics_len.rs | 17 +++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/crates/hir-ty/src/infer/path.rs b/crates/hir-ty/src/infer/path.rs index 7517272362..e649e381dd 100644 --- a/crates/hir-ty/src/infer/path.rs +++ b/crates/hir-ty/src/infer/path.rs @@ -95,14 +95,23 @@ impl<'db> InferenceContext<'_, 'db> { return Some(ValuePathResolution::NonGeneric(ty)); }; - let substs = self.with_body_ty_lowering(|ctx| { - let mut path_ctx = ctx.at_path(path, id); - let last_segment = path.segments().len().checked_sub(1); - if let Some(last_segment) = last_segment { - path_ctx.set_current_segment(last_segment) - } - path_ctx.substs_from_path(value_def, true, false) - }); + let substs = if self_subst.is_some_and(|it| !it.is_empty()) + && matches!(value_def, ValueTyDefId::EnumVariantId(_)) + { + // This is something like `TypeAlias::::EnumVariant`. Do not call `substs_from_path()`, + // as it'll try to re-lower the previous segment assuming it refers to the enum, but it refers + // to the type alias and they may have different generics. + self.types.empty_args + } else { + self.with_body_ty_lowering(|ctx| { + let mut path_ctx = ctx.at_path(path, id); + let last_segment = path.segments().len().checked_sub(1); + if let Some(last_segment) = last_segment { + path_ctx.set_current_segment(last_segment) + } + path_ctx.substs_from_path(value_def, true, false) + }) + }; let parent_substs_len = self_subst.map_or(0, |it| it.len()); let substs = GenericArgs::fill_rest( diff --git a/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs b/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs index 7402133f74..894e044642 100644 --- a/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs +++ b/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs @@ -203,6 +203,23 @@ impl WithSignals for Player { fn __signals_from_external(&self) -> Self::SignalCollection<'_, Self> { Self::SignalCollection { _v: self } } +} + "#, + ); + } + + #[test] + fn enum_type_alias_default_param() { + check_diagnostics( + r#" +//- minicore: result + +struct Error; + +type Result = core::result::Result; + +fn main() { + let _ = Result::<()>::Ok(()); } "#, );