Skip to content

Preserve constness in trait objects up to hir ty lowering #143927

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
Jul 15, 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
} = self.lower_poly_trait_ref(
&trait_bound.trait_ref,
trait_bound.span,
hir::BoundConstness::Never,
trait_bound.modifiers.constness,
hir::BoundPolarity::Positive,
dummy_self,
&mut user_written_bounds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ fn main() {
trait NonConst {}
const fn handle(_: &dyn const NonConst) {}
//~^ ERROR const trait bounds are not allowed in trait object types
//~| ERROR `const` can only be applied to `#[const_trait]` traits
const fn take(_: &dyn [const] NonConst) {}
//~^ ERROR `[const]` is not allowed here
//~| ERROR `[const]` can only be applied to `#[const_trait]` traits
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,34 @@ LL | const fn handle(_: &dyn const NonConst) {}
| ^^^^^^^^^^^^^^

error: `[const]` is not allowed here
--> $DIR/const-trait-bounds-trait-objects.rs:17:23
--> $DIR/const-trait-bounds-trait-objects.rs:18:23
|
LL | const fn take(_: &dyn [const] NonConst) {}
| ^^^^^^^
|
= note: trait objects cannot have `[const]` trait bounds

error: aborting due to 4 previous errors
error: `const` can only be applied to `#[const_trait]` traits
--> $DIR/const-trait-bounds-trait-objects.rs:15:25
|
LL | const fn handle(_: &dyn const NonConst) {}
| ^^^^^ can't be applied to `NonConst`
|
help: mark `NonConst` as `#[const_trait]` to allow it to have `const` implementations
|
LL | #[const_trait] trait NonConst {}
| ++++++++++++++

error: `[const]` can only be applied to `#[const_trait]` traits
--> $DIR/const-trait-bounds-trait-objects.rs:18:23
|
LL | const fn take(_: &dyn [const] NonConst) {}
| ^^^^^^^ can't be applied to `NonConst`
|
help: mark `NonConst` as `#[const_trait]` to allow it to have `const` implementations
|
LL | #[const_trait] trait NonConst {}
| ++++++++++++++

error: aborting due to 6 previous errors

Loading