Skip to content

Evaluation overflow with specialization feature #48515

Open
@joshlf

Description

@joshlf

The current nightly compiler gives the error "overflow evaluating requirement" when a type is used as a trait with a default impl. For example:

#![feature(specialization)]

fn main() {
    println!("{}", <(usize) as TypeString>::type_string());
}

trait TypeString {
    fn type_string() -> &'static str;
}

default impl<T> TypeString for T {
    fn type_string() -> &'static str {
        "unknown type"
    }
}

impl TypeString for () {
    fn type_string() -> &'static str {
        "()"
    }
}

(playground link)

...gives the following error:

error[E0275]: overflow evaluating the requirement `usize: TypeString`
 --> src/main.rs:4:20
  |
4 |     println!("{}", <(usize) as TypeString>::type_string());
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: required by `TypeString::type_string`
 --> src/main.rs:8:5
  |
8 |     fn type_string() -> &'static str;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Note that if we change <(usize) as TypeString>::type_string() to <() as TypeString>::type_string() - () has a specialized impl - it compiles and runs correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-specializationArea: Trait impl specializationC-enhancementCategory: An issue proposing an enhancement or a PR with one.F-specialization`#![feature(specialization)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions