about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2025-04-23 16:36:13 +0200
committerLukas Wirth <lukastw97@gmail.com>2025-04-23 16:36:13 +0200
commit084bd9fd1fd0de4d7550960205d154727dccf463 (patch)
treee1f2fd6908765f2bf95228cc050bb9351c52f55f
parent677505f1fa179d5f63a0375e1d4aac5ea67f8e6e (diff)
downloadrust-084bd9fd1fd0de4d7550960205d154727dccf463.tar.gz
rust-084bd9fd1fd0de4d7550960205d154727dccf463.zip
fix: Fix incorrect diagnostic for lifetime parameter count mismatch
-rw-r--r--src/tools/rust-analyzer/crates/hir-ty/src/lower.rs1
-rw-r--r--src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs18
-rw-r--r--src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs16
-rw-r--r--src/tools/rust-analyzer/crates/query-group-macro/src/queries.rs1
4 files changed, 21 insertions, 15 deletions
diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/lower.rs b/src/tools/rust-analyzer/crates/hir-ty/src/lower.rs
index 2fb6cdc02a7..073a584d8dd 100644
--- a/src/tools/rust-analyzer/crates/hir-ty/src/lower.rs
+++ b/src/tools/rust-analyzer/crates/hir-ty/src/lower.rs
@@ -100,6 +100,7 @@ pub(crate) enum GenericArgsPosition {
     // to lowering already include them. We probably can't do that, but we will still need to
     // account for them when we properly implement lifetime elision.
     FnSignature,
+    OtherSignature,
 }
 
 #[derive(Debug)]
diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs b/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs
index be358163991..c89aad87f79 100644
--- a/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs
+++ b/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs
@@ -921,19 +921,19 @@ fn check_generic_args_len(
         }
     }
 
+    // FIXME: Function signature lifetime elision has to be considered here once we have it
     let infer_lifetimes =
-        (position != GenericArgsPosition::Type || infer_args) && provided_lifetimes_count == 0;
+        position != GenericArgsPosition::OtherSignature && provided_lifetimes_count == 0;
 
-    let min_expected_lifetime_args =
-        if infer_lifetimes { 0 } else { def_generics.len_lifetimes_self() };
     let max_expected_lifetime_args = def_generics.len_lifetimes_self();
-    if !(min_expected_lifetime_args..=max_expected_lifetime_args)
-        .contains(&provided_lifetimes_count)
+    let min_expected_lifetime_args = if infer_lifetimes { 0 } else { max_expected_lifetime_args };
+    if provided_lifetimes_count < min_expected_lifetime_args
+        || max_expected_lifetime_args < provided_lifetimes_count
     {
         ctx.report_len_mismatch(
             def,
             provided_lifetimes_count as u32,
-            def_generics.len_lifetimes_self() as u32,
+            max_expected_lifetime_args as u32,
             IncorrectGenericsLenKind::Lifetimes,
         );
         had_error = true;
@@ -950,10 +950,12 @@ fn check_generic_args_len(
             TypeOrConstParamData::ConstParamData(_) => true,
         })
         .count();
+    let expected_max = named_type_and_const_params_count;
     let expected_min =
         if infer_args { 0 } else { named_type_and_const_params_count - defaults_count };
-    let expected_max = named_type_and_const_params_count;
-    if !(expected_min..=expected_max).contains(&provided_types_and_consts_count) {
+    if provided_types_and_consts_count < expected_min
+        || expected_max < provided_types_and_consts_count
+    {
         ctx.report_len_mismatch(
             def,
             provided_types_and_consts_count as u32,
diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs
index cc4bcd02b82..8244f303d96 100644
--- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs
+++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs
@@ -74,17 +74,19 @@ fn foo() {
         check_diagnostics(
             r#"
 struct Foo<'a, 'b>(&'a &'b ());
-struct Bar<'a>(&'a ());
 
-fn foo() -> Foo {
-    let _ = Foo;
-    let _ = Foo::<>;
-    let _ = Foo::<'static>;
-            // ^^^^^^^^^^^ error: this struct takes 2 lifetime arguments but 1 lifetime argument was supplied
+fn foo(Foo(_): Foo) -> Foo {
+    let _: Foo = Foo(&&());
+    let _: Foo::<> = Foo::<>(&&());
+    let _: Foo::<'static>
+           // ^^^^^^^^^^^ error: this struct takes 2 lifetime arguments but 1 lifetime argument was supplied
+                          = Foo::<'static>(&&());
+                            // ^^^^^^^^^^^ error: this struct takes 2 lifetime arguments but 1 lifetime argument was supplied
+    |_: Foo| -> Foo {loop{}};
+
     loop {}
 }
 
-fn bar(_v: Bar) -> Foo { loop {} }
         "#,
         );
     }
diff --git a/src/tools/rust-analyzer/crates/query-group-macro/src/queries.rs b/src/tools/rust-analyzer/crates/query-group-macro/src/queries.rs
index edbb645bf42..d4d40588bfc 100644
--- a/src/tools/rust-analyzer/crates/query-group-macro/src/queries.rs
+++ b/src/tools/rust-analyzer/crates/query-group-macro/src/queries.rs
@@ -334,6 +334,7 @@ impl ToTokens for Lookup {
     }
 }
 
+#[allow(clippy::large_enum_variant)]
 pub(crate) enum Queries {
     TrackedQuery(TrackedQuery),
     InputQuery(InputQuery),