Skip to content

Commit

Permalink
Fix unreachable code handling (rust-lang#540).
Browse files Browse the repository at this point in the history
Our codegen for unreachable statements were incorrect. CBMC doesn't
treat assert(0) as a terminal instruction. This was causing CBMC to get
stuck in a false loop.

Instead, we should generate an assertion followed by an assumption to
guarantee that CBMC would end there.
  • Loading branch information
celinval authored and tedinski committed Oct 6, 2021
1 parent adc2bc2 commit 74d8048
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 7 additions & 1 deletion compiler/rustc_codegen_rmc/src/codegen/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ impl<'tcx> GotocCtx<'tcx> {
}
}
}
TerminatorKind::Unreachable => Stmt::assert_false("unreachable code", loc),
TerminatorKind::Unreachable => Stmt::block(
vec![
Stmt::assert_false("unreachable code", loc.clone()),
Stmt::assume(Expr::bool_false(), loc.clone()),
],
loc,
),
TerminatorKind::Drop { place, target, unwind: _ } => self.codegen_drop(place, target),
TerminatorKind::DropAndReplace { .. } => {
unreachable!("this instruction is unreachable")
Expand Down
3 changes: 0 additions & 3 deletions src/test/rmc/Enum/result3.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

// There is an implicit loop inside PartialEq, so we need an explicit unwind
// cbmc-flags: --unwind 3 --unwinding-assertions

include!("../../rmc-prelude.rs");

#[derive(Debug, PartialEq)]
Expand Down

0 comments on commit 74d8048

Please sign in to comment.