Skip to content

Commit 6536d36

Browse files
committed
Force appropriate extension when converting from int to ptr #43291
1 parent 687d3d1 commit 6536d36

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

Diff for: src/librustc_trans/mir/constant.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,10 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
811811
consts::ptrcast(llval, ll_t_out)
812812
}
813813
(CastTy::Int(_), CastTy::Ptr(_)) => {
814-
llvm::LLVMConstIntToPtr(llval, ll_t_out.to_ref())
814+
let s = signed as llvm::Bool;
815+
let usize_llval = llvm::LLVMConstIntCast(llval,
816+
self.ccx.isize_ty().to_ref(), s);
817+
llvm::LLVMConstIntToPtr(usize_llval, ll_t_out.to_ref())
815818
}
816819
(CastTy::Ptr(_), CastTy::Int(_)) |
817820
(CastTy::FnPtr, CastTy::Int(_)) => {

Diff for: src/librustc_trans/mir/rvalue.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,10 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
313313
(CastTy::Ptr(_), CastTy::Int(_)) |
314314
(CastTy::FnPtr, CastTy::Int(_)) =>
315315
bcx.ptrtoint(llval, ll_t_out),
316-
(CastTy::Int(_), CastTy::Ptr(_)) =>
317-
bcx.inttoptr(llval, ll_t_out),
316+
(CastTy::Int(_), CastTy::Ptr(_)) => {
317+
let usize_llval = bcx.intcast(llval, bcx.ccx.isize_ty(), signed);
318+
bcx.inttoptr(usize_llval, ll_t_out)
319+
}
318320
(CastTy::Int(_), CastTy::Float) =>
319321
cast_int_to_float(&bcx, signed, llval, ll_t_in, ll_t_out),
320322
(CastTy::Float, CastTy::Int(IntTy::I)) =>

Diff for: src/test/run-pass/issue-43291.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn main() {
12+
assert_eq!(!0usize as *const (), foo(0, 1));
13+
assert_eq!(!0usize as *const (), (0i8 - 1) as *const ());
14+
}
15+
16+
pub fn foo(a: i8, b: i8) -> *const () {
17+
(a - b) as *const ()
18+
}

0 commit comments

Comments
 (0)