- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Closed
Labels
A-associated-itemsArea: Associated items (types, constants & functions)Area: Associated items (types, constants & functions)I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Description
I have two similar pieces of code that I would expect to behave in a similar fashion. The first piece of code compiles and functions properly:
trait HasId {
    fn id() -> i32;
}
trait Foo {}
impl HasId for Foo {
    fn id() -> i32 { 1 }
}
trait Bar {}
impl HasId for Bar {
    fn id() -> i32 { 2 }
}
fn print_id<T: HasId + ?Sized>() {
    println!("{}", <T as HasId>::id());
}
fn main() {
    print_id::<Foo>();
    print_id::<Bar>();
}The second piece of code replaces the static methods with an associated constant:
#![feature(associated_consts)]
trait HasId {
    const ID: i32;
}
trait Foo {}
impl HasId for Foo {
    const ID: i32 = 1;
}
trait Bar {}
impl HasId for Bar {
    const ID: i32 = 2;
}
fn print_id<T: HasId + ?Sized>() {
    println!("{}", <T as HasId>::ID);
}
fn main() {
    print_id::<Foo>();
    print_id::<Bar>();
}This code fails to compile with the following ICE:
<anon>:4:5: 4:19 error: internal compiler error: Encountered error `Unimplemented` when trying to select an implementation for constant trait item reference.
<anon>:4     const ID: i32;
             ^~~~~~~~~~~~~~
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
note: run with `RUST_BACKTRACE=1` for a backtrace
thread 'rustc' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:170
What's interesting is that the following code does compile:
#![feature(associated_consts)]
trait HasId {
    const ID: i32;
}
trait Foo {}
impl HasId for Foo {
    const ID: i32 = 1;
}
trait Bar {}
impl HasId for Bar {
    const ID: i32 = 2;
}
fn main() {
    println!("{}", <Foo as HasId>::ID);
    println!("{}", <Bar as HasId>::ID);
}So it would appear as if this behavior is related to the use of a generic in the UFCS resolution for the associated constant.
Metadata
Metadata
Assignees
Labels
A-associated-itemsArea: Associated items (types, constants & functions)Area: Associated items (types, constants & functions)I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️