-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Add suggestion for E0401 on inner const items #153566
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
rust-bors
merged 1 commit into
rust-lang:main
from
JohnTitor:sugg-generic-params-from-outer-item-err
May 1, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
tests/ui/resolve/generic-params-from-outer-item-suggestion.fixed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Regression test for <https://github.com/rust-lang/rust/issues/68373>. | ||
| // Ensure we provide a suggestion to change from `const` to `let` | ||
| // on the generic params from outer item errors. | ||
|
|
||
| //@ run-rustfix | ||
|
|
||
| #![allow(unused, non_snake_case)] | ||
|
|
||
| const fn size_plus_one<T:Sized>() -> usize { | ||
| //~^ NOTE type parameter from outer item | ||
| let size: usize = core::mem::size_of::<T>(); | ||
| //~^ ERROR can't use generic parameters from outer item | ||
| //~| NOTE nested items are independent | ||
| //~| NOTE a `const` is a separate item | ||
| //~| NOTE use of generic parameter from outer item | ||
| //~| NOTE generic parameter used in this inner constant item | ||
| //~| HELP try using a local `let` binding instead | ||
| size + 1 | ||
| } | ||
|
|
||
| struct A; | ||
|
|
||
| impl A { | ||
| //~^ NOTE `Self` type implicitly declared here, by this `impl` | ||
| const VALUE: u32 = 1; | ||
|
|
||
| fn f() { | ||
| let K: u32 = A::VALUE; | ||
| //~^ ERROR can't use `Self` from outer item | ||
| //~| NOTE use of `Self` from outer item | ||
| //~| NOTE `Self` used in this inner constant item | ||
| //~| NOTE nested items are independent | ||
| //~| NOTE a `const` is a separate item | ||
| //~| HELP refer to the type directly here instead | ||
| //~| HELP try using a local `let` binding instead | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
39 changes: 39 additions & 0 deletions
39
tests/ui/resolve/generic-params-from-outer-item-suggestion.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Regression test for <https://github.com/rust-lang/rust/issues/68373>. | ||
| // Ensure we provide a suggestion to change from `const` to `let` | ||
| // on the generic params from outer item errors. | ||
|
|
||
| //@ run-rustfix | ||
|
|
||
| #![allow(unused, non_snake_case)] | ||
|
|
||
| const fn size_plus_one<T:Sized>() -> usize { | ||
| //~^ NOTE type parameter from outer item | ||
| const size: usize = core::mem::size_of::<T>(); | ||
| //~^ ERROR can't use generic parameters from outer item | ||
| //~| NOTE nested items are independent | ||
| //~| NOTE a `const` is a separate item | ||
| //~| NOTE use of generic parameter from outer item | ||
| //~| NOTE generic parameter used in this inner constant item | ||
| //~| HELP try using a local `let` binding instead | ||
| size + 1 | ||
| } | ||
|
|
||
| struct A; | ||
|
|
||
| impl A { | ||
| //~^ NOTE `Self` type implicitly declared here, by this `impl` | ||
| const VALUE: u32 = 1; | ||
|
|
||
| fn f() { | ||
| const K: u32 = Self::VALUE; | ||
| //~^ ERROR can't use `Self` from outer item | ||
| //~| NOTE use of `Self` from outer item | ||
| //~| NOTE `Self` used in this inner constant item | ||
| //~| NOTE nested items are independent | ||
| //~| NOTE a `const` is a separate item | ||
| //~| HELP refer to the type directly here instead | ||
| //~| HELP try using a local `let` binding instead | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
46 changes: 46 additions & 0 deletions
46
tests/ui/resolve/generic-params-from-outer-item-suggestion.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| error[E0401]: can't use generic parameters from outer item | ||
| --> $DIR/generic-params-from-outer-item-suggestion.rs:11:46 | ||
| | | ||
| LL | const fn size_plus_one<T:Sized>() -> usize { | ||
| | - type parameter from outer item | ||
| LL | | ||
| LL | const size: usize = core::mem::size_of::<T>(); | ||
| | ---- ^ use of generic parameter from outer item | ||
| | | | ||
| | generic parameter used in this inner constant item | ||
| | | ||
| = note: nested items are independent from their parent item for everything except for privacy and name resolution | ||
| = note: a `const` is a separate item from the item that contains it | ||
| help: try using a local `let` binding instead | ||
| | | ||
| LL - const size: usize = core::mem::size_of::<T>(); | ||
| LL + let size: usize = core::mem::size_of::<T>(); | ||
| | | ||
|
|
||
| error[E0401]: can't use `Self` from outer item | ||
| --> $DIR/generic-params-from-outer-item-suggestion.rs:28:24 | ||
| | | ||
| LL | impl A { | ||
| | ---- `Self` type implicitly declared here, by this `impl` | ||
| ... | ||
| LL | const K: u32 = Self::VALUE; | ||
| | - ^^^^ use of `Self` from outer item | ||
| | | | ||
| | `Self` used in this inner constant item | ||
| | | ||
| = note: nested items are independent from their parent item for everything except for privacy and name resolution | ||
| = note: a `const` is a separate item from the item that contains it | ||
| help: refer to the type directly here instead | ||
| | | ||
| LL - const K: u32 = Self::VALUE; | ||
| LL + const K: u32 = A::VALUE; | ||
| | | ||
| help: try using a local `let` binding instead | ||
| | | ||
| LL - const K: u32 = Self::VALUE; | ||
| LL + let K: u32 = Self::VALUE; | ||
| | | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0401`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to turn this into a proper struct soon :)
View changes since the review