Skip to content

Commit

Permalink
Auto merge of rust-lang#47147 - projektir:to_ptr_cast, r=eddyb
Browse files Browse the repository at this point in the history
Force appropriate extension when converting from int to ptr rust-lang#43291

Fixes rust-lang#43291.

Looking for feedback if I've missed something and/or need to add more tests.

@eddyb @retep998 @nagisa @oli-obk
  • Loading branch information
bors committed Jan 4, 2018
2 parents 78f24d8 + 6536d36 commit 4cd918c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/librustc_trans/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,10 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
consts::ptrcast(llval, ll_t_out)
}
(CastTy::Int(_), CastTy::Ptr(_)) => {
llvm::LLVMConstIntToPtr(llval, ll_t_out.to_ref())
let s = signed as llvm::Bool;
let usize_llval = llvm::LLVMConstIntCast(llval,
self.ccx.isize_ty().to_ref(), s);
llvm::LLVMConstIntToPtr(usize_llval, ll_t_out.to_ref())
}
(CastTy::Ptr(_), CastTy::Int(_)) |
(CastTy::FnPtr, CastTy::Int(_)) => {
Expand Down
6 changes: 4 additions & 2 deletions src/librustc_trans/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,10 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
(CastTy::Ptr(_), CastTy::Int(_)) |
(CastTy::FnPtr, CastTy::Int(_)) =>
bcx.ptrtoint(llval, ll_t_out),
(CastTy::Int(_), CastTy::Ptr(_)) =>
bcx.inttoptr(llval, ll_t_out),
(CastTy::Int(_), CastTy::Ptr(_)) => {
let usize_llval = bcx.intcast(llval, bcx.ccx.isize_ty(), signed);
bcx.inttoptr(usize_llval, ll_t_out)
}
(CastTy::Int(_), CastTy::Float) =>
cast_int_to_float(&bcx, signed, llval, ll_t_in, ll_t_out),
(CastTy::Float, CastTy::Int(IntTy::I)) =>
Expand Down
18 changes: 18 additions & 0 deletions src/test/run-pass/issue-43291.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2018 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.

pub fn main() {
assert_eq!(!0usize as *const (), foo(0, 1));
assert_eq!(!0usize as *const (), (0i8 - 1) as *const ());
}

pub fn foo(a: i8, b: i8) -> *const () {
(a - b) as *const ()
}

0 comments on commit 4cd918c

Please sign in to comment.