Skip to content

Commit fa2a735

Browse files
authored
Merge pull request rust-lang#54 from oli-obk/clippy
Clippy and `assume` intrinsic implementation
2 parents 4051b0e + 23eb8a5 commit fa2a735

File tree

6 files changed

+23
-7
lines changed

6 files changed

+23
-7
lines changed

src/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub enum EvalError<'tcx> {
4343
CalledClosureAsFunction,
4444
VtableForArgumentlessMethod,
4545
ModifiedConstantMemory,
46+
AssumptionNotHeld,
4647
}
4748

4849
pub type EvalResult<'tcx, T> = Result<T, EvalError<'tcx>>;
@@ -97,6 +98,8 @@ impl<'tcx> Error for EvalError<'tcx> {
9798
"tried to call a vtable function without arguments",
9899
EvalError::ModifiedConstantMemory =>
99100
"tried to modify constant memory",
101+
EvalError::AssumptionNotHeld =>
102+
"`assume` argument was false"
100103
}
101104
}
102105

src/interpreter/step.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
3030
let mir = self.mir();
3131
let basic_block = &mir.basic_blocks()[block];
3232

33-
if let Some(ref stmt) = basic_block.statements.get(stmt_id) {
33+
if let Some(stmt) = basic_block.statements.get(stmt_id) {
3434
let mut new = Ok(0);
3535
ConstantExtractor {
3636
span: stmt.source_info.span,

src/interpreter/terminator.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,11 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
284284
"sub_with_overflow" => self.intrinsic_with_overflow(mir::BinOp::Sub, &args[0], &args[1], dest, dest_layout)?,
285285
"mul_with_overflow" => self.intrinsic_with_overflow(mir::BinOp::Mul, &args[0], &args[1], dest, dest_layout)?,
286286

287-
// FIXME: turn into an assertion to catch wrong `assume` that would cause UB in llvm
288-
"assume" => {}
287+
"assume" => {
288+
if !self.memory.read_bool(args_ptrs[0])? {
289+
return Err(EvalError::AssumptionNotHeld);
290+
}
291+
}
289292

290293
"copy_nonoverlapping" => {
291294
let elem_ty = substs.type_at(0);

src/interpreter/vtable.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
2020

2121
debug!("get_vtable(trait_ref={:?})", trait_ref);
2222

23-
let methods: Vec<_> = traits::supertraits(tcx, trait_ref.clone()).flat_map(|trait_ref| {
24-
match self.fulfill_obligation(trait_ref.clone()) {
23+
let methods: Vec<_> = traits::supertraits(tcx, trait_ref).flat_map(|trait_ref| {
24+
match self.fulfill_obligation(trait_ref) {
2525
// Should default trait error here?
2626
traits::VtableDefaultImpl(_) |
2727
traits::VtableBuiltin(_) => {
@@ -164,7 +164,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
164164
// method could then never be called, so we do not want to
165165
// try and trans it, in that case. Issue #23435.
166166
if mth.is_provided {
167-
let predicates = mth.method.predicates.predicates.subst(self.tcx, &mth.substs);
167+
let predicates = mth.method.predicates.predicates.subst(self.tcx, mth.substs);
168168
if !self.normalize_and_test_predicates(predicates) {
169169
debug!("get_vtable_methods: predicates do not hold");
170170
return None;

src/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
314314
Some(&FunctionDefinition {
315315
def_id,
316316
kind: FunctionKind::Closure { ref substs, ref ty }
317-
}) => Ok((def_id, substs.clone(), ty.clone())),
317+
}) => Ok((def_id, *substs, ty.clone())),
318318
Some(&FunctionDefinition {
319319
kind: FunctionKind::Function { .. }, ..
320320
}) => Err(EvalError::CalledClosureAsFunction),

tests/compile-fail/assume.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(core_intrinsics)]
2+
3+
fn main() {
4+
let x = 5;
5+
unsafe {
6+
std::intrinsics::assume(x < 10);
7+
std::intrinsics::assume(x > 1);
8+
std::intrinsics::assume(x > 42); //~ ERROR: `assume` argument was false
9+
}
10+
}

0 commit comments

Comments
 (0)