Skip to content

codegen bug with a slightly modified run-pass/trait-generic.rs #7355

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

Closed
thestinger opened this issue Jun 24, 2013 · 2 comments
Closed

codegen bug with a slightly modified run-pass/trait-generic.rs #7355

thestinger opened this issue Jun 24, 2013 · 2 comments
Labels
A-codegen Area: Code generation I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@thestinger
Copy link
Contributor

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// xfail-fast

use std::int;

trait to_str {
    fn to_str(&self) -> ~str;
}
impl to_str for int {
    fn to_str(&self) -> ~str { int::to_str(*self) }
}
impl to_str for ~str {
    fn to_str(&self) -> ~str { self.clone() }
}
impl to_str for () {
    fn to_str(&self) -> ~str { ~"()" }
}

trait map<T> {
    fn map<U:Copy>(&self, f: &fn(&T) -> U) -> ~[U];
}
impl<T> map<T> for ~[T] {
    fn map<U:Copy>(&self, f: &fn(&T) -> U) -> ~[U] {
        let mut r = ~[];
        /*for std::vec::each(*self) |x| { r += ~[f(x)]; }*/
        for self.iter().advance |x| { r += ~[f(x)]; }
        r
    }
}

fn foo<U, T: map<U>>(x: T) -> ~[~str] {
    x.map(|_e| ~"hi" )
}
fn bar<U:to_str,T:map<U>>(x: T) -> ~[~str] {
    x.map(|_e| _e.to_str() )
}

pub fn main() {
    assert_eq!(foo(~[1]), ~[~"hi"]);
    assert_eq!(bar::<int, ~[int]>(~[4, 5]), ~[~"4", ~"5"]);
    assert_eq!(bar::<~str, ~[~str]>(~[~"x", ~"y"]), ~[~"x", ~"y"]);
    assert_eq!(bar::<(), ~[()]>(~[()]), ~[~"()"]);
}

With the each line changed to iter().advance, this generates bad code. It's definitely a bug in codegen and not the libraries/test.

@bblum
Copy link
Contributor

bblum commented Aug 16, 2013

Trying to modernize this for the new for syntax; cannot reproduce. I do, however, find an ICE.

The line marked "works fine" solves the compiler error (+ICE) but appears to generate correct code.

trait map<T> {
    fn map<U>(&self, f: &fn(&T) -> U) -> ~[U];
}
impl<T> map<T> for ~[T] {
    fn map<U>(&self, f: &fn(&T) -> U) -> ~[U] {
        let mut r: ~[U] = ~[];
        // for x in self.iter() { r.push(f(x)); } // works fine
        for x in self.iter() { r += ~[f(x)]; }
        r
    }
}

pub fn main() { }

emits:

foo.rs:8:31: 8:43 error: binary operation + cannot be applied to type `~[U]`
foo.rs:8         for x in self.iter() { r += ~[f(x)]; }
                                        ^~~~~~~~~~~~
error: internal compiler error: no type for node 61: expr [f(x)] (id=61) in fcx 7fa3c8c584d0

@alexcrichton
Copy link
Member

I cannot reproduce this, and I also don't hit an ICE any more. Due to being unable to reproduce, closing.

flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 17, 2021
Fix link in docs

The [docs](https://github.com/rust-lang/rust-clippy/blob/master/doc/common_tools_writing_lints.md) had an incorrect link for "Checking if a type defines a specific method".

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

3 participants