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 fictitious type in sizing_type_of() #23958

Closed
nikomatsakis opened this issue Apr 1, 2015 · 4 comments
Closed

ICE fictitious type in sizing_type_of() #23958

nikomatsakis opened this issue Apr 1, 2015 · 4 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@nikomatsakis
Copy link
Contributor

This example causes an ICE:

Play: http://is.gd/pqOpJQ

trait MyIntoIterator {
  type IntoIter: Iterator;
  fn into_iter(self) -> Self::IntoIter;
}

// This should be saying `if for any lifetime, a ref to Self is MyIntoIterator`.
trait Iterable where for<'a> &'a Self: MyIntoIterator {
  fn iter<'a>(&'a self) -> <&'a Self as MyIntoIterator>::IntoIter {
    self.into_iter()
  }
}
impl<T> Iterable for T where for<'a> &'a T: MyIntoIterator {}

// Impl MyIntoIterator for &Vec<T> for all lifetimes.
impl<'a, T> MyIntoIterator for &'a Vec<T> {
  type IntoIter = ::std::slice::Iter<'a, T>;
  fn into_iter(self) -> <Self as MyIntoIterator>::IntoIter { self.iter() }
}

// Impl MyIntoIterator for &String for all lifetimes.
impl<'a> MyIntoIterator for &'a String {
  type IntoIter = ::std::str::Chars<'a>;
  fn into_iter(self) -> <Self as MyIntoIterator>::IntoIter { self.chars() }
}

fn iterate<T>(thing: T) where T: Iterable, for<'a> &'a T: MyIntoIterator {
  for x in thing.iter() {
    println!("a thing");
  }
}

fn main() {
  iterate(vec![1,2,3]);
  iterate("abc".to_string());
}

output:

error: internal compiler error: fictitious type <&'static collections::vec::Vec<i32> as MyIntoIterator>::IntoIter in sizing_type_of()
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
@nikomatsakis nikomatsakis added the A-associated-items Area: Associated items (types, constants & functions) label Apr 1, 2015
@jdm jdm added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Apr 1, 2015
@Stebalien
Copy link
Contributor

Shorter example:

trait Collection where for<'a> &'a Self: IntoIterator {
    fn my_iter(&self) -> <&Self as IntoIterator>::IntoIter {
        self.into_iter()
    }
}

impl<T> Collection for [T] { }

fn main() {
    let v = [0usize];
    let _ = v.my_iter();
}

@malbarbo
Copy link
Contributor

Interesting, if the where clause is moved to my_iter method its works

trait Collection {
    fn my_iter<'a>(&'a self) -> <&'a Self as IntoIterator>::IntoIter where &'a Self: IntoIterator {
        self.into_iter()
    }
}

impl<T> Collection for [T] { }

fn main() {
    let v = [0usize];
    let _ = v.my_iter();
}

@malbarbo
Copy link
Contributor

It seems to be related with #23406.

@withoutboats
Copy link
Contributor

Ran into this with code like the below. The IntoIterator impl is fine, only the method on Foo crashes.

pub struct Foo(Vec<i32>);

impl Foo {
    pub fn iter(&self) -> <&Self as IntoIterator>::IntoIter {
        self.into_iter()
    }
}

impl<'a> IntoIterator for &'a Foo {
    type IntoIter = <&'a Vec<i32> as IntoIterator>::IntoIter;
    type Item = <&'a Vec<i32> as IntoIterator>::Item;
    fn into_iter(self) -> Self::IntoIter {
        (&self.0).into_iter()
    }
}

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

5 participants