Skip to content

Add missing cast for retslots in case of "subtyping" due to trait bounds #22667

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

Merged
merged 1 commit into from
Feb 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/librustc_trans/trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,16 @@ pub fn trans_call_inner<'a, 'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
if is_rust_fn {
let mut llargs = Vec::new();

if let (ty::FnConverging(ret_ty), Some(llretslot)) = (ret_ty, opt_llretslot) {
if let (ty::FnConverging(ret_ty), Some(mut llretslot)) = (ret_ty, opt_llretslot) {
if type_of::return_uses_outptr(ccx, ret_ty) {
let llformal_ret_ty = type_of::type_of(ccx, ret_ty).ptr_to();
let llret_ty = common::val_ty(llretslot);
if llformal_ret_ty != llret_ty {
// this could happen due to e.g. subtyping
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we bother to have an assertion about what the relationship is?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of assertion do you have in mind? We just have the LLVM type of the retslot here (can you lookup the original Ty from that?), I don't think we can do much with that, can we?

Ultimately, we should probably ignore the trait bounds when creating the LLVM types, but this seemed like the obvious quick fix because trans_arg_datum already does the same.

And once LLVM drops typed pointers, this is all moot anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping we had access to both of the Rust types, but if not, I guess we can't do anything, as you say.

debug!("casting actual return type ({}) to match formal ({})",
bcx.llty_str(llret_ty), bcx.llty_str(llformal_ret_ty));
llretslot = PointerCast(bcx, llretslot, llformal_ret_ty);
}
llargs.push(llretslot);
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/compile-fail/retslot-cast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2015 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.

#![feature(rustc_attrs)]
#![allow(warnings)]

pub fn fail(x: Option<& (Iterator+Send)>) -> Option<&Iterator> {
// This call used to trigger an LLVM assertion because the return slot had type
// "Option<&Iterator>"* instead of "Option<&(Iterator+Send)>"*
inner(x)
}

pub fn inner(x: Option<& (Iterator+Send)>) -> Option<&(Iterator+Send)> {
x
}

#[rustc_error]
fn main() {} //~ ERROR compilation successful