Auto merge of #47147 - projektir:to_ptr_cast, r=eddyb

Force appropriate extension when converting from int to ptr #43291

Fixes #43291.

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

@eddyb @retep998 @nagisa @oli-obk
This commit is contained in:
bors 2018-01-04 09:54:15 +00:00
commit 4cd918c4ca
3 changed files with 26 additions and 3 deletions

View File

@ -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(_)) => {

View File

@ -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)) =>

View File

@ -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 ()
}