Skip to content

fix: Fix incorrect diagnostic for lifetime parameter count mismatch #19672

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
Apr 23, 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
1 change: 1 addition & 0 deletions crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
18 changes: 10 additions & 8 deletions crates/hir-ty/src/lower/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
16 changes: 9 additions & 7 deletions crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {} }
"#,
);
}
Expand Down
1 change: 1 addition & 0 deletions crates/query-group-macro/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ impl ToTokens for Lookup {
}
}

#[allow(clippy::large_enum_variant)]
pub(crate) enum Queries {
TrackedQuery(TrackedQuery),
InputQuery(InputQuery),
Expand Down