diff options
| author | Lzu Tao <taolzu@gmail.com> | 2020-09-20 13:30:32 +0000 |
|---|---|---|
| committer | Lzu Tao <taolzu@gmail.com> | 2020-09-20 14:00:40 +0000 |
| commit | 4387480dea6e2611483d431857f0f85b65e3c00c (patch) | |
| tree | 15bce14c5c22ac62e7f07142bbd47ed0d56885f9 | |
| parent | 3e08354fb0dc7a5f7733da9b308d483b9c1d2514 (diff) | |
| download | rust-4387480dea6e2611483d431857f0f85b65e3c00c.tar.gz rust-4387480dea6e2611483d431857f0f85b65e3c00c.zip | |
Add unstably const support for assume intrinsic
| -rw-r--r-- | compiler/rustc_mir/src/interpret/intrinsics.rs | 6 | ||||
| -rw-r--r-- | library/core/src/intrinsics.rs | 1 | ||||
| -rw-r--r-- | src/test/ui/consts/const-eval/const_assume.rs | 17 |
3 files changed, 24 insertions, 0 deletions
diff --git a/compiler/rustc_mir/src/interpret/intrinsics.rs b/compiler/rustc_mir/src/interpret/intrinsics.rs index 0664f25e409..d3b6d706337 100644 --- a/compiler/rustc_mir/src/interpret/intrinsics.rs +++ b/compiler/rustc_mir/src/interpret/intrinsics.rs @@ -435,6 +435,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // These just return their argument self.copy_op(args[0], dest)?; } + sym::assume => { + let cond = self.read_scalar(args[0])?.check_init()?.to_bool()?; + if !cond { + throw_ub_format!("`assume` intrinsic called with `false`"); + } + } _ => return Ok(false), } diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs index de4d2efe294..c68200c90d8 100644 --- a/library/core/src/intrinsics.rs +++ b/library/core/src/intrinsics.rs @@ -733,6 +733,7 @@ extern "rust-intrinsic" { /// own, or if it does not enable any significant optimizations. /// /// This intrinsic does not have a stable counterpart. + #[rustc_const_unstable(feature = "const_assume", issue = "76972")] pub fn assume(b: bool); /// Hints to the compiler that branch condition is likely to be true. diff --git a/src/test/ui/consts/const-eval/const_assume.rs b/src/test/ui/consts/const-eval/const_assume.rs new file mode 100644 index 00000000000..f72f151824b --- /dev/null +++ b/src/test/ui/consts/const-eval/const_assume.rs @@ -0,0 +1,17 @@ +// check-pass + +// Check that `const_assume` feature allow `assume` intrinsic +// to be used in const contexts. + +#![feature(core_intrinsics, const_assume)] + +extern crate core; + +use core::intrinsics::assume; + +pub const unsafe fn foo(x: usize, y: usize) -> usize { + assume(y != 0); + x / y +} + +fn main() {} |
