Skip to content

ICE: never type as default associated type #51506

Closed

Description

@malbarbo

This code generates an ICE

#![feature(never_type, specialization)]
#![allow(incomplete_features)]

use std::iter::{self, Empty};

trait Trait {
    type Out: Iterator<Item = u32>;

    fn f(&self) -> Option<Self::Out>;
}

impl<T> Trait for T {
    default type Out = !;

    default fn f(&self) -> Option<Self::Out> {
        None
    }
}

struct X;

impl Trait for X {
    type Out = Empty<u32>;

    fn f(&self) -> Option<Self::Out> {
        Some(iter::empty())
    }
}

fn f<T: Trait>(a: T) {
    if let Some(iter) = a.f() {
        println!("Some");
        for x in iter {
            println!("x = {}", x);
        }
    }
}

pub fn main() {
    f(10);
}

with this message.

error: internal compiler error: librustc/traits/codegen/mod.rs:169: Encountered errors `[FulfillmentError(Obligation(predicate=Binder(TraitPredicate(<! as std::iter::Iterator>)),depth=1),Unimplemented)]` resolving bounds after type-checking

thread 'main' panicked at 'Box<Any>', librustc_errors/lib.rs:499:9
note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: aborting due to previous error

Replacing 1.f() by X.f() makes the code to compile and run fine, which is strange because ! does not implements Iterator .

By removing the specialization the compiler refuses to compile the code with the error:

error[E0277]: the trait bound `!: std::iter::Iterator` is not satisfied
  --> src/main.rs:10:9
   |
10 | impl<T> Trait for T {
   |         ^^^^^ `!` is not an iterator; maybe try calling `.iter()` or a similar method
   |
   = help: the trait `std::iter::Iterator` is not implemented for `!`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

Activity

  1. added
    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
    on Sep 11, 2018
  2. added
    C-bugCategory: This is a bug.
    F-never_type`#![feature(never_type)]`
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.
    on Aug 6, 2019
  3. JohnTitor commented on Nov 13, 2019

    @JohnTitor
    Member

    The ICE has been fixed in the latest nightly, marked as E-needstest

  4. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Nov 13, 2019
  5. malbarbo commented on Nov 13, 2019

    @malbarbo
    ContributorAuthor

    Thanks @JohnTitor. Indeed, the example compiles fine in nightly (4f03f4a 2019-11-12), however, replacing the main function by these two

    fn f<T: Trait>(a: T) {
        if let Some(iter) = a.f() {
            println!("Some");
            for x in iter {
                println!("x = {}", x);
            }
        }
    }
    
    pub fn main() {
        f(10);
    }

    generates the same ICE.

  6. JohnTitor commented on Nov 13, 2019

    @JohnTitor
    Member

    @malbarbo ah, thanks for the update! I confirmed it (playground)

  7. removed
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Nov 13, 2019
  8. JohnTitor commented on Jun 22, 2020

    @JohnTitor
    Member

    Triage: the ICE doesn't occur anymore with the latest nightly, marking as E-needs-test.

  9. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Jun 22, 2020
  10. added a commit that references this issue on Jun 23, 2020
    ec8af88
  11. added a commit that references this issue on Jun 24, 2020
    b71464c
  12. added a commit that references this issue on Jun 24, 2020
    45de677
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-specializationArea: Trait impl specializationA-trait-systemArea: Trait systemC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.F-never_type`#![feature(never_type)]`F-specialization`#![feature(specialization)]`I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.glacierICE tracked in rust-lang/glacier.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions