diff --git a/src/librustc_trans/mir/constant.rs b/src/librustc_trans/mir/constant.rs index d836d796c41..aa640d3da3c 100644 --- a/src/librustc_trans/mir/constant.rs +++ b/src/librustc_trans/mir/constant.rs @@ -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(_)) => { diff --git a/src/librustc_trans/mir/rvalue.rs b/src/librustc_trans/mir/rvalue.rs index 9d705eda9fb..65f1129890d 100644 --- a/src/librustc_trans/mir/rvalue.rs +++ b/src/librustc_trans/mir/rvalue.rs @@ -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)) => diff --git a/src/test/run-pass/issue-43291.rs b/src/test/run-pass/issue-43291.rs new file mode 100644 index 00000000000..98de8f78859 --- /dev/null +++ b/src/test/run-pass/issue-43291.rs @@ -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 or the MIT license +// , 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 () +}