Skip to content

rustc_resolve: improve const generic errors#152913

Open
Unique-Usman wants to merge 1 commit intorust-lang:mainfrom
Unique-Usman:ua/constnottype
Open

rustc_resolve: improve const generic errors#152913
Unique-Usman wants to merge 1 commit intorust-lang:mainfrom
Unique-Usman:ua/constnottype

Conversation

@Unique-Usman
Copy link
Contributor

No description provided.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 20, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 20, 2026

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 68 candidates
  • Random selection from 14 candidates

@Unique-Usman
Copy link
Contributor Author

r? @estebank

@rustbot rustbot assigned estebank and unassigned JonathanBrouwer Feb 20, 2026
@Unique-Usman Unique-Usman marked this pull request as draft February 20, 2026 14:49
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 20, 2026
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 28, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

.struct_constructors
.insert(local_def_id, (ctor_res, ctor_vis.to_def_id(), ret_fields));
}
self.r.struct_generics.insert(local_def_id, generics.clone());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see if we can struct_generics store references. That should keep mem usage to a minimum.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted.

Comment on lines +4472 to +4475
if let Some(const_err) = const_err {
err.cancel();
err = const_err;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently inactive, right? const_err isn't None anywhere right now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const_err is actually always None except in the case of where we add the error we just introduce see it here -> https://github.com/rust-lang/rust/pull/152913/changes#diff-2c81e6d8048a31a3ce43f30181aaa037e0ad0076e7aee67792ca993f28c75ac3R3440-R3441

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is to prevent the type error from showing up.

@Unique-Usman Unique-Usman requested a review from estebank March 6, 2026 17:35
@Unique-Usman Unique-Usman marked this pull request as ready for review March 6, 2026 17:35
@rustbot
Copy link
Collaborator

rustbot commented Mar 6, 2026

HIR ty lowering was modified

cc @fmease

@rust-log-analyzer

This comment has been minimized.

@Unique-Usman
Copy link
Contributor Author

@estebank

@rust-log-analyzer

This comment has been minimized.

@@ -4,18 +4,19 @@ error[E0575]: expected associated type, found associated constant `Trait::ASSOC`
LL | bar::<<T as Trait>::ASSOC>();
| ^^^^^^^^^^^^^^^^^^^ not a associated type

error[E0747]: unresolved item provided when a constant was expected
--> $DIR/assoc_const_as_type_argument.rs:8:11
error[E0284]: type annotations needed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unfortunate that after silencing E0747 we end up with these inference errors instead...

I think we can split the work into two PRs: one silencing the unnecessary/redundant resolve error ("unresolved item when X was expected") and another for the appropriate const generic suggestions. That would make things easier to review and evolve on isolation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, thanks. we can resolve the E0747 error after getting this pr merged. I already update the patch to not silence the E0747 for now.

@estebank
Copy link
Contributor

Lets see what impact keeping track of all struct Generics in Resolver has. I don't think we can make the lifetimes work with borrowing &Generics without some more significant changes, but I will try tomorrow.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Mar 12, 2026
rustc_resolve: improve const generic errors
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 12, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 12, 2026

☀️ Try build successful (CI)
Build commit: 94de249 (94de2490ee19b3ff6f37c252af545a638a79ec5d, parent: d1ee5e59a964a419b84b760812a35075034f4861)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (94de249): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (secondary 0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.4% [0.9%, 1.9%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.0% [-1.0%, -1.0%] 1
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 480.488s -> 481.825s (0.28%)
Artifact size: 394.87 MiB -> 394.91 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 12, 2026
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will continue reading the logic in diagnostics.rs.

I tried today to silence the errors. I'd found one way that works for almost every case, except one where an outer const parameter is used in a nested item, which ICEs, so I'll look for another alternative.

View changes since this review

Comment on lines 31 to +38
//~| ERROR: cannot find type `asd` in this scope
//~| ERROR: unresolved item provided when a constant was expected
//~| ERROR unresolved item provided when a constant was expected

reuse foo::<A, B, C> as xd;
//~^ ERROR can't use generic parameters from outer item
//~| ERROR can't use generic parameters from outer item
//~| ERROR can't use generic parameters from outer item
//~| ERROR: unresolved item provided when a constant was expected
//~| ERROR unresolved item provided when a constant was expected
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert these trivial changes to the test.


#[derive(Subdiagnostic)]
#[suggestion(
"you might have meant to introduce a const parameter on the `{$item_name}`",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"you might have meant to introduce a const parameter on the `{$item_name}`",
"you might have meant to introduce a const parameter on `{$item_name}`",

Comment on lines 16 to +21
--> $DIR/invalid-const-arguments.rs:14:32
|
LL | struct A<const N: u8>;
| ---------------------- similarly named struct `A` defined here
...
LL | struct C<const C: u8, const N: u8>;
| ----------- const parameter `T` is defined on the type
LL | impl<const N: u8> Foo for C<N, T> {}
| ^
| ^ not found in this scope
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads a bit weirdly because the name of the const in the struct is different than on the impl. Let's think about how we can make it clearer, specially in the first span label.

Comment on lines +3281 to +3307
// Detects missing const parameters in `impl` blocks and suggests adding them.
//
// When a const parameter is used in the self type of an `impl` but not declared
// in the `impl`'s own generic parameter list, this function emits a targeted
// diagnostic with a suggestion to add it at the correct position.
//
// Example:
//
// struct C<const A: u8, const X: u8, const P: u32>;
//
// impl Foo for C<A, X, P> {}
// // ^ the struct `C` in `C<A, X, P>` is used as the self type
// // ^ ^ ^ but A, X and P are not declared on the impl
//
// Suggested fix:
//
// impl<const A: u8, const X: u8, const P: u32> Foo for C<A, X, P> {}
//
// Current behavior (suggestions are emitted one-by-one):
//
// impl<const A: u8> Foo for C<A, X, P> {}
// impl<const X: u8> Foo for C<A, X, P> {}
// impl<const P: u32> Foo for C<A, X, P> {}
//
// Ideally the suggestion should aggregate them into a single line:
//
// impl<const A: u8, const X: u8, const P: u32> Foo for C<A, X, P> {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turn this into a docstring with ///.

Comment on lines +3313 to +3315
if let Some(item) = self.diag_metadata.current_item
&& let ItemKind::Impl(impl_) = &item.kind
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use early return to avoid one level of indentation:

let Some(item) = self.diag_metadata.current_item else { return None };
let ItemKind::Impl(impl_) = &item.kind else { return None };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants