Skip to content

ICE with Default Methods on Traits #5139

Closed
@mitsuhiko

Description

@mitsuhiko

The following code is currently causing an internal compiler error:

trait Iterable<T, I:Iterator<T>> {
    fn get_iterator(&self) -> I;
}

trait Iterator<T> {
    fn next(&mut self) -> Option<&self/T>;
    fn each(&mut self, f: fn(&T) -> bool) {
        loop {
            match self.next() {
                None => { break; }
                Some(ref x) => { if !f(x) { break; } }
            }
        }
    }
}

struct VectorIterator<T> {
    priv vec: &[T],
    priv pos: uint,
    priv end: uint,
}

impl <T> Iterable<T, VectorIterator<T>> for &[T] {
    fn get_iterator(&self) -> VectorIterator/&self<T> {
        VectorIterator { vec: *self, pos: 0, end: vec::len(*self) }
    }
}

impl <T> Iterator<T> for VectorIterator<T> {
    fn next(&mut self) -> Option<&self/T> {
        if self.pos >= self.end {
            None
        } else {
            let x = self.pos;
            self.pos += 1;
            Some(&self.vec[x])
        }
    }
}

fn main() {
    let v = ["foo", "bar", "baz"];
    for v.get_iterator().each |&x| {
        io::println(fmt!("> %s", x));
    }
}

The exception:

rust: task failed at 'index out of bounds: the len is 0 but the index is 0', /Users/mitsuhiko/Development/rust/src/librustc/middle/ty.rs:1424

The line in question is the ty_param(p) => subst.tps[p.idx] one: https://github.com/mozilla/rust/blob/ba7a8706609a96080f75de57ab0af7ac86dbb583/src/librustc/middle/ty.rs#L1425

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions