Skip to content

Suggest the [const] Destruct bound for type parameters in const functions when missing#155013

Open
jakubadamw wants to merge 3 commits into
rust-lang:mainfrom
jakubadamw:issue-103270
Open

Suggest the [const] Destruct bound for type parameters in const functions when missing#155013
jakubadamw wants to merge 3 commits into
rust-lang:mainfrom
jakubadamw:issue-103270

Conversation

@jakubadamw

@jakubadamw jakubadamw commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

When a const function drops a value whose type is a type parameter without a [const] Destruct bound, suggest adding it so that the destructor can be evaluated at compile-time.

Closes #103270.

Example:

const fn f(_x: impl std::fmt::Debug) {}

Before:

error[E0493]: destructor of `impl std::fmt::Debug` cannot be evaluated at compile-time
  --> $DIR/min_const_fn.rs:1:11
   |
LL | const fn f(_x: impl std::fmt::Debug) {}
   |                  ^^                         - value is dropped here
   |                  |
   |                  the destructor for this type cannot be evaluated in constant functions

After:

error[E0493]: destructor of `impl std::fmt::Debug` cannot be evaluated at compile-time
  --> $DIR/min_const_fn.rs:1:11
   |
LL | const fn f(_x: impl std::fmt::Debug) {}
   |                  ^^                         - value is dropped here
   |                  |
   |                  the destructor for this type cannot be evaluated in constant functions
   |
help: consider restricting opaque type `impl std::fmt::Debug` with unstable trait `Destruct`
   |
LL | const fn f(_x: impl std::fmt::Debug + [const] Destruct) {}
   |                                           ++++++++++++++++++

No new tests as the existing ones had their results updated with the new suggestion.

@rustbot

rustbot commented Apr 8, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to constck

cc @fee1-dead

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

@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 Apr 8, 2026
@rustbot

rustbot commented Apr 8, 2026

Copy link
Copy Markdown
Collaborator

r? @wesleywiser

rustbot has assigned @wesleywiser.
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 69 candidates
  • Random selection from 12 candidates

@jakubadamw jakubadamw changed the title Suggest the [const] Destruct bound for type parameters in const functions Suggest the [const] Destruct bound for type parameters in const functions when missing Apr 8, 2026
@Kivooeo

Kivooeo commented Apr 8, 2026

Copy link
Copy Markdown
Member

thanks for the contribution, but this is experimental syntax which means syntax may change in future, so I think it should be held until stabilization

@wesleywiser wesleywiser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we would probably only want to offer this suggestion if the user is on a nightly compiler.

View changes since this review

@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 Apr 24, 2026
@rustbot

rustbot commented Apr 24, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@fee1-dead fee1-dead self-assigned this Apr 25, 2026
@rust-bors

This comment has been minimized.

…ctions

When a const function drops a value whose type is a type parameter,
suggest adding a `[const] Destruct` bound so the destructor can be
evaluated at compile-time.
The suggested bound requires the unstable `const_destruct` feature,
so it is not actionable on a stable compiler.
Add a run-make test that verifies the suggestion is only emitted on nightly.
@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Jul 13, 2026
@rustbot

rustbot commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@jakubadamw jakubadamw requested a review from wesleywiser July 13, 2026 00:05
@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 Jul 13, 2026
@jakubadamw

Copy link
Copy Markdown
Contributor Author

@wesleywiser, thank you, I made the suggestion trigger on nightly only. 🙂

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
17    |     - value is dropped here
+    |
+ help: consider further restricting type parameter `R` with unstable trait `Destruct`
+    |
+ LL |     const fn get<R: Deref<Target = Self> + [const] Destruct>(self: R) -> u32 {
+    |                                          ++++++++++++++++++
18 
19 error[E0801]: invalid generic `self` parameter type: `R`
20   --> $DIR/arbitrary-self-from-method-substs-ice.rs:10:49


The actual stderr differed from the expected stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args self/arbitrary-self-from-method-substs-ice.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/self/arbitrary-self-from-method-substs-ice.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/self/arbitrary-self-from-method-substs-ice" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error[E0015]: cannot perform non-const deref coercion on `R` in constant functions
##[error]  --> /checkout/tests/ui/self/arbitrary-self-from-method-substs-ice.rs:13:9
   |
---

error[E0493]: destructor of `R` cannot be evaluated at compile-time
##[error]  --> /checkout/tests/ui/self/arbitrary-self-from-method-substs-ice.rs:10:43
   |
LL |     const fn get<R: Deref<Target = Self>>(self: R) -> u32 {
   |                                           ^^^^ the destructor for this type cannot be evaluated in constant functions
...
LL |     }
   |     - value is dropped here
   |
help: consider further restricting type parameter `R` with unstable trait `Destruct`
   |
LL |     const fn get<R: Deref<Target = Self> + [const] Destruct>(self: R) -> u32 {
   |                                          ++++++++++++++++++

error[E0801]: invalid generic `self` parameter type: `R`
##[error]  --> /checkout/tests/ui/self/arbitrary-self-from-method-substs-ice.rs:10:49
   |
LL |     const fn get<R: Deref<Target = Self>>(self: R) -> u32 {
   |                                                 ^
   |
   = note: type of `self` must not be a method generic parameter type
   = help: use a concrete type such as `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0015, E0493, E0801.
For more information about an error, try `rustc --explain E0015`.

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

Labels

A-run-make Area: port run-make Makefiles to rmake.rs 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.

Suggest ~const std::marker::Destruct for type parameters when needed

6 participants