Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #20787 from ChayimFriedman2/fix-type-alias-enum
fix: Fix erroneous diagnostic incorrect_generics_len when there are generics on enum variant used through type alias
Shoyu Vanilla (Flint) 7 months ago
parent 378ea41 · parent 797ba9c · commit dcabe2b
-rw-r--r--crates/hir-ty/src/infer/path.rs25
-rw-r--r--crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs17
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::<Args>::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
@@ -207,4 +207,21 @@ impl WithSignals for Player {
"#,
);
}
+
+ #[test]
+ fn enum_type_alias_default_param() {
+ check_diagnostics(
+ r#"
+//- minicore: result
+
+struct Error;
+
+type Result<T, E = Error> = core::result::Result<T, E>;
+
+fn main() {
+ let _ = Result::<()>::Ok(());
+}
+ "#,
+ );
+ }
}