Skip to content

Fix the span of trait bound modifier [const] #143171

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
Jun 29, 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
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ impl<'a> Parser<'a> {
&& self.look_ahead(1, |t| t.is_keyword(kw::Const))
&& self.look_ahead(2, |t| *t == token::CloseBracket)
Comment on lines 1071 to 1072
Copy link
Member Author

@fmease fmease Jun 29, 2025

Choose a reason for hiding this comment

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

(I'm pretty sure we don't need the careful lookahead logic and could just if eat("[") then expect("const") expect("]"), there shouldn't be ambiguity issues (I'll post another, tangentially related PR later that goes into more detail) but that will likely regress diagnostics (because if we fail to parse a bound, we try to reparse it as a type to give "expected trait, found type") and I didn't want to deal with that rn and maybe that's why oli chose this approach)

{
let start = self.prev_token.span;
let start = self.token.span;
self.bump();
self.expect_keyword(exp!(Const)).unwrap();
self.bump();
Expand Down
60 changes: 30 additions & 30 deletions tests/ui/consts/fn_trait_refs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,145 +5,145 @@ LL | #![feature(const_fn_trait_ref_impls)]
| ^^^^^^^^^^^^^^^^^^^^^^^^

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:14:6
--> $DIR/fn_trait_refs.rs:14:8
|
LL | T: [const] Fn<()> + [const] Destruct,
| ^^^^^^^^^ can't be applied to `Fn`
| ^^^^^^^ can't be applied to `Fn`
|
note: `Fn` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:14:6
--> $DIR/fn_trait_refs.rs:14:8
|
LL | T: [const] Fn<()> + [const] Destruct,
| ^^^^^^^^^ can't be applied to `Fn`
| ^^^^^^^ can't be applied to `Fn`
|
note: `Fn` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:14:6
--> $DIR/fn_trait_refs.rs:14:8
|
LL | T: [const] Fn<()> + [const] Destruct,
| ^^^^^^^^^ can't be applied to `Fn`
| ^^^^^^^ can't be applied to `Fn`
|
note: `Fn` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:21:6
--> $DIR/fn_trait_refs.rs:21:8
|
LL | T: [const] FnMut<()> + [const] Destruct,
| ^^^^^^^^^ can't be applied to `FnMut`
| ^^^^^^^ can't be applied to `FnMut`
|
note: `FnMut` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:21:6
--> $DIR/fn_trait_refs.rs:21:8
|
LL | T: [const] FnMut<()> + [const] Destruct,
| ^^^^^^^^^ can't be applied to `FnMut`
| ^^^^^^^ can't be applied to `FnMut`
|
note: `FnMut` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:21:6
--> $DIR/fn_trait_refs.rs:21:8
|
LL | T: [const] FnMut<()> + [const] Destruct,
| ^^^^^^^^^ can't be applied to `FnMut`
| ^^^^^^^ can't be applied to `FnMut`
|
note: `FnMut` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:28:6
--> $DIR/fn_trait_refs.rs:28:8
|
LL | T: [const] FnOnce<()>,
| ^^^^^^^^^ can't be applied to `FnOnce`
| ^^^^^^^ can't be applied to `FnOnce`
|
note: `FnOnce` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:28:6
--> $DIR/fn_trait_refs.rs:28:8
|
LL | T: [const] FnOnce<()>,
| ^^^^^^^^^ can't be applied to `FnOnce`
| ^^^^^^^ can't be applied to `FnOnce`
|
note: `FnOnce` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:28:6
--> $DIR/fn_trait_refs.rs:28:8
|
LL | T: [const] FnOnce<()>,
| ^^^^^^^^^ can't be applied to `FnOnce`
| ^^^^^^^ can't be applied to `FnOnce`
|
note: `FnOnce` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:35:6
--> $DIR/fn_trait_refs.rs:35:8
|
LL | T: [const] Fn<()> + [const] Destruct,
| ^^^^^^^^^ can't be applied to `Fn`
| ^^^^^^^ can't be applied to `Fn`
|
note: `Fn` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:35:6
--> $DIR/fn_trait_refs.rs:35:8
|
LL | T: [const] Fn<()> + [const] Destruct,
| ^^^^^^^^^ can't be applied to `Fn`
| ^^^^^^^ can't be applied to `Fn`
|
note: `Fn` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:35:6
--> $DIR/fn_trait_refs.rs:35:8
|
LL | T: [const] Fn<()> + [const] Destruct,
| ^^^^^^^^^ can't be applied to `Fn`
| ^^^^^^^ can't be applied to `Fn`
|
note: `Fn` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:49:6
--> $DIR/fn_trait_refs.rs:49:8
|
LL | T: [const] FnMut<()> + [const] Destruct,
| ^^^^^^^^^ can't be applied to `FnMut`
| ^^^^^^^ can't be applied to `FnMut`
|
note: `FnMut` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:49:6
--> $DIR/fn_trait_refs.rs:49:8
|
LL | T: [const] FnMut<()> + [const] Destruct,
| ^^^^^^^^^ can't be applied to `FnMut`
| ^^^^^^^ can't be applied to `FnMut`
|
note: `FnMut` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:49:6
--> $DIR/fn_trait_refs.rs:49:8
|
LL | T: [const] FnMut<()> + [const] Destruct,
| ^^^^^^^^^ can't be applied to `FnMut`
| ^^^^^^^ can't be applied to `FnMut`
|
note: `FnMut` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/consts/unstable-const-fn-in-libcore.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/unstable-const-fn-in-libcore.rs:19:30
--> $DIR/unstable-const-fn-in-libcore.rs:19:32
|
LL | const fn unwrap_or_else<F: [const] FnOnce() -> T>(self, f: F) -> T {
| ^^^^^^^^^ can't be applied to `FnOnce`
| ^^^^^^^ can't be applied to `FnOnce`
|
note: `FnOnce` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/unstable-const-fn-in-libcore.rs:19:30
--> $DIR/unstable-const-fn-in-libcore.rs:19:32
|
LL | const fn unwrap_or_else<F: [const] FnOnce() -> T>(self, f: F) -> T {
| ^^^^^^^^^ can't be applied to `FnOnce`
| ^^^^^^^ can't be applied to `FnOnce`
|
note: `FnOnce` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/impl-trait/normalize-tait-in-const.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/normalize-tait-in-const.rs:27:33
--> $DIR/normalize-tait-in-const.rs:27:35
|
LL | const fn with_positive<F: for<'a> [const] Fn(&'a Alias<'a>) + [const] Destruct>(fun: F) {
| ^^^^^^^^^ can't be applied to `Fn`
| ^^^^^^^ can't be applied to `Fn`
|
note: `Fn` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/normalize-tait-in-const.rs:27:33
--> $DIR/normalize-tait-in-const.rs:27:35
|
LL | const fn with_positive<F: for<'a> [const] Fn(&'a Alias<'a>) + [const] Destruct>(fun: F) {
| ^^^^^^^^^ can't be applied to `Fn`
| ^^^^^^^ can't be applied to `Fn`
|
note: `Fn` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/parser/bounds-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ LL | T: [const] ?Tr,
| there is not a well-defined meaning for a `[const] ?` trait

error: `[const]` may only modify trait bounds, not lifetime bounds
--> $DIR/bounds-type.rs:16:6
--> $DIR/bounds-type.rs:16:8
|
LL | T: [const] 'a,
| ^^^^^^^^^
| ^^^^^^^

error: `const` may only modify trait bounds, not lifetime bounds
--> $DIR/bounds-type.rs:17:8
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/specialization/const_trait_impl.stderr
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/const_trait_impl.rs:34:7
--> $DIR/const_trait_impl.rs:34:9
|
LL | impl<T: [const] Default> const A for T {
| ^^^^^^^^^ can't be applied to `Default`
| ^^^^^^^ can't be applied to `Default`
|
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/default.rs:LL:COL

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/const_trait_impl.rs:40:7
--> $DIR/const_trait_impl.rs:40:9
|
LL | impl<T: [const] Default + [const] Sup> const A for T {
| ^^^^^^^^^ can't be applied to `Default`
| ^^^^^^^ can't be applied to `Default`
|
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/default.rs:LL:COL

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/const_trait_impl.rs:46:7
--> $DIR/const_trait_impl.rs:46:9
|
LL | impl<T: [const] Default + [const] Sub> const A for T {
| ^^^^^^^^^ can't be applied to `Default`
| ^^^^^^^ can't be applied to `Default`
|
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/default.rs:LL:COL

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/const_trait_impl.rs:40:7
--> $DIR/const_trait_impl.rs:40:9
|
LL | impl<T: [const] Default + [const] Sup> const A for T {
| ^^^^^^^^^ can't be applied to `Default`
| ^^^^^^^ can't be applied to `Default`
|
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/default.rs:LL:COL
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/const_trait_impl.rs:34:7
--> $DIR/const_trait_impl.rs:34:9
|
LL | impl<T: [const] Default> const A for T {
| ^^^^^^^^^ can't be applied to `Default`
| ^^^^^^^ can't be applied to `Default`
|
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/default.rs:LL:COL
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/const_trait_impl.rs:46:7
--> $DIR/const_trait_impl.rs:46:9
|
LL | impl<T: [const] Default + [const] Sub> const A for T {
| ^^^^^^^^^ can't be applied to `Default`
| ^^^^^^^ can't be applied to `Default`
|
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
--> $SRC_DIR/core/src/default.rs:LL:COL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: `[const]` is not allowed here
--> $DIR/conditionally-const-and-const-params.rs:8:13
--> $DIR/conditionally-const-and-const-params.rs:8:15
|
LL | fn add<A: [const] Add42>(self) -> Foo<{ A::add(N) }> {
| ^^^^^^^^^
| ^^^^^^^
|
note: this function is not `const`, so it cannot have `[const]` trait bounds
--> $DIR/conditionally-const-and-const-params.rs:8:8
Expand All @@ -11,10 +11,10 @@ LL | fn add<A: [const] Add42>(self) -> Foo<{ A::add(N) }> {
| ^^^

error: `[const]` is not allowed here
--> $DIR/conditionally-const-and-const-params.rs:26:9
--> $DIR/conditionally-const-and-const-params.rs:26:11
|
LL | fn bar<A: [const] Add42, const N: usize>(_: Foo<N>) -> Foo<{ A::add(N) }> {
| ^^^^^^^^^
| ^^^^^^^
|
note: this function is not `const`, so it cannot have `[const]` trait bounds
--> $DIR/conditionally-const-and-const-params.rs:26:4
Expand Down
Loading
Loading