Skip to content

Commit ebcb693

Browse files
authored
Merge pull request rust-lang#19672 from Veykril/push-tqooypklusty
fix: Fix incorrect diagnostic for lifetime parameter count mismatch
2 parents 8632b52 + 084bd9f commit ebcb693

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/lower.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub(crate) enum GenericArgsPosition {
100100
// to lowering already include them. We probably can't do that, but we will still need to
101101
// account for them when we properly implement lifetime elision.
102102
FnSignature,
103+
OtherSignature,
103104
}
104105

105106
#[derive(Debug)]

src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -921,19 +921,19 @@ fn check_generic_args_len(
921921
}
922922
}
923923

924+
// FIXME: Function signature lifetime elision has to be considered here once we have it
924925
let infer_lifetimes =
925-
(position != GenericArgsPosition::Type || infer_args) && provided_lifetimes_count == 0;
926+
position != GenericArgsPosition::OtherSignature && provided_lifetimes_count == 0;
926927

927-
let min_expected_lifetime_args =
928-
if infer_lifetimes { 0 } else { def_generics.len_lifetimes_self() };
929928
let max_expected_lifetime_args = def_generics.len_lifetimes_self();
930-
if !(min_expected_lifetime_args..=max_expected_lifetime_args)
931-
.contains(&provided_lifetimes_count)
929+
let min_expected_lifetime_args = if infer_lifetimes { 0 } else { max_expected_lifetime_args };
930+
if provided_lifetimes_count < min_expected_lifetime_args
931+
|| max_expected_lifetime_args < provided_lifetimes_count
932932
{
933933
ctx.report_len_mismatch(
934934
def,
935935
provided_lifetimes_count as u32,
936-
def_generics.len_lifetimes_self() as u32,
936+
max_expected_lifetime_args as u32,
937937
IncorrectGenericsLenKind::Lifetimes,
938938
);
939939
had_error = true;
@@ -950,10 +950,12 @@ fn check_generic_args_len(
950950
TypeOrConstParamData::ConstParamData(_) => true,
951951
})
952952
.count();
953+
let expected_max = named_type_and_const_params_count;
953954
let expected_min =
954955
if infer_args { 0 } else { named_type_and_const_params_count - defaults_count };
955-
let expected_max = named_type_and_const_params_count;
956-
if !(expected_min..=expected_max).contains(&provided_types_and_consts_count) {
956+
if provided_types_and_consts_count < expected_min
957+
|| expected_max < provided_types_and_consts_count
958+
{
957959
ctx.report_len_mismatch(
958960
def,
959961
provided_types_and_consts_count as u32,

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,19 @@ fn foo() {
7474
check_diagnostics(
7575
r#"
7676
struct Foo<'a, 'b>(&'a &'b ());
77-
struct Bar<'a>(&'a ());
7877
79-
fn foo() -> Foo {
80-
let _ = Foo;
81-
let _ = Foo::<>;
82-
let _ = Foo::<'static>;
83-
// ^^^^^^^^^^^ error: this struct takes 2 lifetime arguments but 1 lifetime argument was supplied
78+
fn foo(Foo(_): Foo) -> Foo {
79+
let _: Foo = Foo(&&());
80+
let _: Foo::<> = Foo::<>(&&());
81+
let _: Foo::<'static>
82+
// ^^^^^^^^^^^ error: this struct takes 2 lifetime arguments but 1 lifetime argument was supplied
83+
= Foo::<'static>(&&());
84+
// ^^^^^^^^^^^ error: this struct takes 2 lifetime arguments but 1 lifetime argument was supplied
85+
|_: Foo| -> Foo {loop{}};
86+
8487
loop {}
8588
}
8689
87-
fn bar(_v: Bar) -> Foo { loop {} }
8890
"#,
8991
);
9092
}

src/tools/rust-analyzer/crates/query-group-macro/src/queries.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ impl ToTokens for Lookup {
334334
}
335335
}
336336

337+
#[allow(clippy::large_enum_variant)]
337338
pub(crate) enum Queries {
338339
TrackedQuery(TrackedQuery),
339340
InputQuery(InputQuery),

0 commit comments

Comments
 (0)