Skip to content

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

@jruderman

Description

@jruderman

Given the following code (playground):

#![feature(const_mut_refs)]
#![feature(const_trait_impl)]

const fn call_twice<F>(mut f: F)
where
    F: ~const FnMut() -> (),
{
    f();
    f();
}

fn main() {
    let mut a = 10;
    call_twice(|| { a -= 1; });
    assert_eq!(a, 8);
}

The current output is:

error[E0493]: destructor of `F` cannot be evaluated at compile-time
  --> src/main.rs:4:24
   |
4  | const fn call_twice<F>(mut f: F)
   |                        ^^^^^ the destructor for this type cannot be evaluated in constant functions
...
10 | }
   | - value is dropped here

Ideally the output should suggest a where-clause fix:

help: consider restricting `F` to be destructible in const contexts:
  | where
 6|   F: ~const FnMut() -> () + ~const std::marker::Destruct,
  |                          +++++++++++++++++++++++++++++++

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions