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 passing a trait object by reference #17322

Closed
kmcallister opened this issue Sep 16, 2014 · 14 comments · Fixed by #18746
Closed

ICE passing a trait object by reference #17322

kmcallister opened this issue Sep 16, 2014 · 14 comments · Fixed by #18746
Labels
A-DSTs Area: Dynamically-sized types (DSTs) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@kmcallister
Copy link
Contributor

use std::io;

fn f(wr: &mut Writer) {
    wr.write_str("hello").ok().expect("failed");
}

fn main() {
    let mut wr = box io::stdout() as Box<Writer + 'static>;
    f(&mut wr);
}
$ rustc foo.rs  
error: internal compiler error: trying to take the sizing type of std::io::Writer, an unsized type  
note: the compiler hit an unexpected failure path. this is a bug.  
note: we would appreciate a bug report: http://doc.rust-lang.org/complement-bugreport.html  
note: run with `RUST_BACKTRACE=1` for a backtrace  
task 'rustc' failed at 'Box<Any>', /home/keegan/rust/src/libsyntax/diagnostic.rs:169  

upstream Rust version: 828e075

@nrc nrc added the A-DSTs Area: Dynamically-sized types (DSTs) label Sep 16, 2014
@nrc
Copy link
Member

nrc commented Sep 16, 2014

cc me

@kmcallister
Copy link
Contributor Author

It works with &mut *wr.

@nrc
Copy link
Member

nrc commented Sep 16, 2014

The code example is incorrect code, but it should give an error rather than ICE

kmcallister added a commit to kmcallister/rust-http that referenced this issue Sep 16, 2014
@nrc
Copy link
Member

nrc commented Sep 17, 2014

This seems to be a regression from trait reform - we used to do vtable::check_object_cast for coercions as well as casts, but now we seem to only do it for casts.

cc @nikomatsakis

@nikomatsakis
Copy link
Contributor

I'll investigate.

@nrc
Copy link
Member

nrc commented Sep 17, 2014

@nikomatsakis we used to do this check in vtable.rs.

@nikomatsakis
Copy link
Contributor

Are you sure this code is incorrect? There is an impl of Writer for Box<Writer+'a> for all 'a, after all, so it seems legit to invoke the function f as given. Am I missing something obvious?

@nikomatsakis
Copy link
Contributor

(Also, or perhaps the same question, what check exactly did we used to do in vtable?)

@nrc
Copy link
Member

nrc commented Sep 17, 2014

@nikomatsakis yes. Here is simplified code:

use std::io;

fn main() {
    let mut wr: Box<Writer + 'static> = box io::stdout();
    let x: &mut Box<Writer> = &mut wr;
    let f: &mut Writer = x;
}

The error is on the third line in main where we try to coerce a &mut Box to &mut Writer.

@nrc
Copy link
Member

nrc commented Sep 17, 2014

@nikomatsakis we used to call check_object_cast for adjustments as well as explicit casts

@nikomatsakis
Copy link
Contributor

@nick29581 so I agree that there is likely a bug introduced by trait-reform, but not that the code is illegal. Here is a version of the example which also ICEs, but which does not depend on io. This code works with the stage0 compiler.

trait Test {
    fn test(&mut self) -> int;
}

impl Test for Box<Test+'static> {
    fn test(&mut self) -> int {
        self.test()
    }
}

impl Test for int {
    fn test(&mut self) -> int {
        *self
    }
}

fn main() {
    let mut b: Box<Test+'static> = box 3i;
    let mut c: &mut Box<Test+'static> = &mut b;
    let mut d: &mut Test = c;
}

@nrc
Copy link
Member

nrc commented Sep 18, 2014

Hmm, do we have some blanket impl T for Box<T>? In which case, I guess it should work.

@nikomatsakis
Copy link
Contributor

@nick29581 we do for Writer, yes. I will investigate anyway, since it seems I broke it.

@alexcrichton
Copy link
Member

Another test case for a very similar error:

trait Foo {}               

fn foo(_: &[&Foo]) {}      

impl<'a> Foo for &'a str {}

fn main() {                
    foo(&[&"foo"]);        
}                          
$ rustc bar.rs  
error: internal compiler error: trying to take the sizing type of str, an unsized type
note: the compiler hit an unexpected failure path. this is a bug.
note: we would appreciate a bug report: http://doc.rust-lang.org/complement-bugreport.html
note: run with `RUST_BACKTRACE=1` for a backtrace
task 'rustc' failed at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/libsyntax/ast_util.rs:694

@huonw huonw added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Sep 18, 2014
nikomatsakis added a commit to nikomatsakis/rust that referenced this issue Nov 19, 2014
bors added a commit that referenced this issue Nov 19, 2014
Pass the unadjusted type into the unsize_info function, which seems to be what it expects. Fixes #17322.

r? @nick29581 

Full disclosure: still running make check locally ;)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-DSTs Area: Dynamically-sized types (DSTs) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants