Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICE: Encountered error Unimplemented when trying to select an implementation for constant trait item reference. <anon>:4 const ID: i32; #25606

Closed
Mr-Byte opened this issue May 19, 2015 · 6 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@Mr-Byte
Copy link

Mr-Byte commented May 19, 2015

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.

@arielb1
Copy link
Contributor

arielb1 commented May 19, 2015

cc @quantheory

@arielb1
Copy link
Contributor

arielb1 commented May 19, 2015

I think you wrote your static-method example twice (I couldn't find the ICE-ing example).

@arielb1
Copy link
Contributor

arielb1 commented May 19, 2015

For the record, this ICEs:

#![feature(associated_consts)]

trait HasId {
    const ID: i32;
}

fn get_id<T: HasId + ?Sized>() -> i32 {
    <T as HasId>::ID
}

fn main() {
}

@Mr-Byte
Copy link
Author

Mr-Byte commented May 19, 2015

Sorry about that, grabbed the same code twice. I've updated my example that ICEs. You were able to further reduce it down though, thanks!

@quantheory
Copy link
Contributor

This is a duplicate of #25046 (see my comment there), and will be fixed by #25091. I would very much like to have this code actually work in the future, but for now the correct behavior is to reject such uses of associated constants.

@Aatch
Copy link
Contributor

Aatch commented May 20, 2015

Duplicate of #25046

@Aatch Aatch closed this as completed May 20, 2015
@Aatch Aatch added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ A-associated-items Area: Associated items (types, constants & functions) labels May 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

4 participants