-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
MGCA: require #[type_const] on free consts too #152129
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
khyperia:require-type_const-on-free-consts
Feb 6, 2026
+125
−50
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
Some comments aren't visible on the classic Files Changed page.
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
5 changes: 5 additions & 0 deletions
5
tests/ui/const-generics/generic_const_exprs/auxiliary/non_local_type_const.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,5 @@ | ||
| #![feature(min_generic_const_args)] | ||
| #![allow(incomplete_features)] | ||
|
|
||
| #[type_const] | ||
| pub const NON_LOCAL_CONST: char = 'a'; |
6 changes: 4 additions & 2 deletions
6
tests/ui/const-generics/generic_const_exprs/non-local-const.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 |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| // regression test for #133808. | ||
| //@ aux-build:non_local_type_const.rs | ||
|
|
||
| #![feature(generic_const_exprs)] | ||
| #![feature(min_generic_const_args)] | ||
| #![allow(incomplete_features)] | ||
| #![crate_type = "lib"] | ||
| extern crate non_local_type_const; | ||
|
|
||
| pub trait Foo {} | ||
| impl Foo for [u8; std::path::MAIN_SEPARATOR] {} | ||
| //~^ ERROR the constant `MAIN_SEPARATOR` is not of type `usize` | ||
| impl Foo for [u8; non_local_type_const::NON_LOCAL_CONST] {} | ||
| //~^ ERROR the constant `'a'` is not of type `usize` |
10 changes: 5 additions & 5 deletions
10
tests/ui/const-generics/generic_const_exprs/non-local-const.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 |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| error: the constant `MAIN_SEPARATOR` is not of type `usize` | ||
| --> $DIR/non-local-const.rs:9:14 | ||
| error: the constant `'a'` is not of type `usize` | ||
| --> $DIR/non-local-const.rs:11:14 | ||
| | | ||
| LL | impl Foo for [u8; std::path::MAIN_SEPARATOR] {} | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `char` | ||
| LL | impl Foo for [u8; non_local_type_const::NON_LOCAL_CONST] {} | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `char` | ||
| | | ||
| = note: the length of array `[u8; MAIN_SEPARATOR]` must be type `usize` | ||
| = note: the length of array `[u8; 'a']` must be type `usize` | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
16 changes: 12 additions & 4 deletions
16
tests/ui/const-generics/mgca/assoc-const-without-type_const.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 |
|---|---|---|
| @@ -1,18 +1,26 @@ | ||
| error: use of trait associated const without `#[type_const]` | ||
| error: use of `const` in the type system without `#[type_const]` | ||
| --> $DIR/assoc-const-without-type_const.rs:8:35 | ||
| | | ||
| LL | fn mk_array<T: Tr>(_x: T) -> [(); T::SIZE] { | ||
| | ^^^^^^^ | ||
| | | ||
| = note: the declaration in the trait must be marked with `#[type_const]` | ||
| help: add `#[type_const]` attribute to `Tr::SIZE` | ||
| | | ||
| LL + #[type_const] | ||
| LL | const SIZE: usize; | ||
| | | ||
|
|
||
| error: use of trait associated const without `#[type_const]` | ||
| error: use of `const` in the type system without `#[type_const]` | ||
| --> $DIR/assoc-const-without-type_const.rs:10:10 | ||
| | | ||
| LL | [(); T::SIZE] | ||
| | ^^^^^^^ | ||
| | | ||
| = note: the declaration in the trait must be marked with `#[type_const]` | ||
| help: add `#[type_const]` attribute to `Tr::SIZE` | ||
| | | ||
| LL + #[type_const] | ||
| LL | const SIZE: usize; | ||
| | | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
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 @@ | ||
| pub const N: usize = 2; |
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
9 changes: 9 additions & 0 deletions
9
tests/ui/const-generics/mgca/non-local-const-without-type_const.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,9 @@ | ||
| // Just a test of the error message (it's different for non-local consts) | ||
| //@ aux-build:non_local_const.rs | ||
| #![feature(min_generic_const_args)] | ||
| #![allow(incomplete_features)] | ||
| extern crate non_local_const; | ||
| fn main() { | ||
| let x = [(); non_local_const::N]; | ||
| //~^ ERROR use of `const` in the type system without `#[type_const]` | ||
| } |
10 changes: 10 additions & 0 deletions
10
tests/ui/const-generics/mgca/non-local-const-without-type_const.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,10 @@ | ||
| error: use of `const` in the type system without `#[type_const]` | ||
| --> $DIR/non-local-const-without-type_const.rs:7:18 | ||
| | | ||
| LL | let x = [(); non_local_const::N]; | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: only consts marked with `#[type_const]` may be used in types | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // regression test, used to ICE | ||
|
|
||
| #![feature(min_generic_const_args)] | ||
| #![allow(incomplete_features)] | ||
|
|
||
| const N: usize = 4; | ||
|
|
||
| fn main() { | ||
| let x = [(); N]; | ||
| //~^ ERROR use of `const` in the type system without `#[type_const]` | ||
| } |
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,14 @@ | ||
| error: use of `const` in the type system without `#[type_const]` | ||
| --> $DIR/unmarked-free-const.rs:9:18 | ||
| | | ||
| LL | let x = [(); N]; | ||
| | ^ | ||
| | | ||
| help: add `#[type_const]` attribute to `N` | ||
| | | ||
| LL + #[type_const] | ||
| LL | const N: usize = 4; | ||
| | | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
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.
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.
pointing to the definition site of the non-type const is rly nice :3 thanks
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.
something I was thinking of: what happens with non-local spans?
I wasn't able to make it not print the source location - referring to a compiletest
auxiliarycrate printed the true source location, as did referring tostd::path::MAIN_SEPARATORRelatedly, printing "hey you need
#[type_const]on this item" when the item is in std isn't super helpful, and also when it's in a crate you don't control - but if it's in a crate you do control, maybe you do want that message, so idk. In any case, I think if we decide to stabilize#[type_const](maybe we don't), this error needs additional work (make it a proper autofix suggestion, etc.) so maybe it's fine to leave it for now.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.
At the end of compiling a crate we'll serialize a bunch of information to disk to allow for accessing information in cross-crate circumstances. Spans of DefIds are one such case.
The source of other crates isnt available only some of the information we computed from it.
I agree that we probably want to customise the diagnostic here for cases when the DefId is non-local 🤔
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.
you can use DefId::is_local/DefId::as_local (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefId.html#method.as_local) to figure out if a DefId is from the current crate or not
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.
ok im fancy and did a
suggestion:3There 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.
i like it!