Skip to content

Commit f0e2fc7

Browse files
committed
Improve type safety
1 parent ea60335 commit f0e2fc7

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

src/librustc_codegen_llvm/common.rs

+13-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//! Code that is useful in various codegen modules.
44
5-
use crate::llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef};
5+
use crate::llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef, ConstantInt};
66
use crate::abi;
77
use crate::consts;
88
use crate::type_::Type;
@@ -246,30 +246,22 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
246246
}
247247

248248
fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> {
249-
if is_const_integral(v) {
250-
unsafe {
251-
Some(llvm::LLVMConstIntGetZExtValue(v))
252-
}
253-
} else {
254-
None
255-
}
249+
try_as_const_integral(v).map(|v| unsafe {
250+
llvm::LLVMConstIntGetZExtValue(v)
251+
})
256252
}
257253

258254
fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
259-
unsafe {
260-
if is_const_integral(v) {
261-
let (mut lo, mut hi) = (0u64, 0u64);
262-
let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
263-
&mut hi, &mut lo);
264-
if success {
265-
Some(hi_lo_to_u128(lo, hi))
266-
} else {
267-
None
268-
}
255+
try_as_const_integral(v).and_then(|v| unsafe {
256+
let (mut lo, mut hi) = (0u64, 0u64);
257+
let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
258+
&mut hi, &mut lo);
259+
if success {
260+
Some(hi_lo_to_u128(lo, hi))
269261
} else {
270262
None
271263
}
272-
}
264+
})
273265
}
274266

275267
fn scalar_to_backend(
@@ -387,8 +379,8 @@ fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
387379
((hi as u128) << 64) | (lo as u128)
388380
}
389381

390-
fn is_const_integral(v: &'ll Value) -> bool {
382+
fn try_as_const_integral(v: &'ll Value) -> Option<&'ll ConstantInt> {
391383
unsafe {
392-
llvm::LLVMIsAConstantInt(v).is_some()
384+
llvm::LLVMIsAConstantInt(v)
393385
}
394386
}

src/librustc_codegen_llvm/llvm/ffi.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ extern { pub type Module; }
510510
extern { pub type Context; }
511511
extern { pub type Type; }
512512
extern { pub type Value; }
513+
extern { pub type ConstantInt; }
513514
extern { pub type Metadata; }
514515
extern { pub type BasicBlock; }
515516
#[repr(C)]
@@ -719,8 +720,8 @@ extern "C" {
719720
pub fn LLVMConstInt(IntTy: &Type, N: c_ulonglong, SignExtend: Bool) -> &Value;
720721
pub fn LLVMConstIntOfArbitraryPrecision(IntTy: &Type, Wn: c_uint, Ws: *const u64) -> &Value;
721722
pub fn LLVMConstReal(RealTy: &Type, N: f64) -> &Value;
722-
pub fn LLVMConstIntGetZExtValue(ConstantVal: &Value) -> c_ulonglong;
723-
pub fn LLVMRustConstInt128Get(ConstantVal: &Value, SExt: bool,
723+
pub fn LLVMConstIntGetZExtValue(ConstantVal: &ConstantInt) -> c_ulonglong;
724+
pub fn LLVMRustConstInt128Get(ConstantVal: &ConstantInt, SExt: bool,
724725
high: &mut u64, low: &mut u64) -> bool;
725726

726727

@@ -1666,7 +1667,7 @@ extern "C" {
16661667
#[allow(improper_ctypes)]
16671668
pub fn LLVMRustWriteValueToString(value_ref: &Value, s: &RustString);
16681669

1669-
pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&Value>;
1670+
pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&ConstantInt>;
16701671

16711672
pub fn LLVMRustPassKind(Pass: &Pass) -> PassKind;
16721673
pub fn LLVMRustFindAndCreatePass(Pass: *const c_char) -> Option<&'static mut Pass>;

0 commit comments

Comments
 (0)