Skip to content

Can't impl const From<T> for U anymore #111275

Description

@nazar-pc

I tried this code:

#![feature(const_trait_impl)]
struct S;

impl const From<()> for S {
    fn from(value: ()) -> Self {
        Self
    }
}

I expected to see this happen: Code must compile

Instead, this happened:

Code doesn't compile in latest Nightly rustc 1.71.0-nightly (f9a6b7158 2023-05-05), but worked fine (with corresponding features enabled) in rustc 1.70.0-nightly (0599b6b93 2023-04-01)

error: const `impl` for trait `From` which is not marked with `#[const_trait]`
 --> src/lib.rs:4:12
  |
4 | impl const From<()> for S {
  |            ^^^^^^^^
  |
  = note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
  = note: adding a non-const method body in the future would be a breaking change

Error message is also confusing to me because From does have #[const_trait] attribute and various types in standard library have const implementation, like impl const From<u8> for usize.

Does this mean it is no longer possible to have const implementations in user code?

Meta

rustc --version --verbose:

rustc 1.71.0-nightly (f9a6b7158 2023-05-05)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions