Skip to content

Commit 69b31b3

Browse files
Rollup merge of rust-lang#76973 - lzutao:unstably-const-assume, r=oli-obk
Unstably allow assume intrinsic in const contexts Not sure much about this usage because there are concerns about [blocking optimization][1] and [slowing down LLVM][2] when using `assme` intrinsic in inline functions. But since Oli suggested in rust-lang#76960 (comment), here we are. [1]: rust-lang#54995 (comment) [2]: rust-lang#49572 (comment)
2 parents d910f87 + 7030bb6 commit 69b31b3

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

core/src/intrinsics.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Compiler intrinsics.
22
//!
3-
//! The corresponding definitions are in `librustc_codegen_llvm/intrinsic.rs`.
4-
//! The corresponding const implementations are in `librustc_mir/interpret/intrinsics.rs`
3+
//! The corresponding definitions are in `compiler/rustc_codegen_llvm/src/intrinsic.rs`.
4+
//! The corresponding const implementations are in `compiler/rustc_mir/src/interpret/intrinsics.rs`
55
//!
66
//! # Const intrinsics
77
//!
@@ -10,7 +10,7 @@
1010
//!
1111
//! In order to make an intrinsic usable at compile-time, one needs to copy the implementation
1212
//! from https://github.com/rust-lang/miri/blob/master/src/shims/intrinsics.rs to
13-
//! `librustc_mir/interpret/intrinsics.rs` and add a
13+
//! `compiler/rustc_mir/src/interpret/intrinsics.rs` and add a
1414
//! `#[rustc_const_unstable(feature = "foo", issue = "01234")]` to the intrinsic.
1515
//!
1616
//! If an intrinsic is supposed to be used from a `const fn` with a `rustc_const_stable` attribute,
@@ -733,6 +733,7 @@ extern "rust-intrinsic" {
733733
/// own, or if it does not enable any significant optimizations.
734734
///
735735
/// This intrinsic does not have a stable counterpart.
736+
#[rustc_const_unstable(feature = "const_assume", issue = "76972")]
736737
pub fn assume(b: bool);
737738

738739
/// Hints to the compiler that branch condition is likely to be true.

core/tests/intrinsics.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::any::TypeId;
2+
use core::intrinsics::assume;
23

34
#[test]
45
fn test_typeid_sized_types() {
@@ -20,3 +21,17 @@ fn test_typeid_unsized_types() {
2021
assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());
2122
assert!(TypeId::of::<X>() != TypeId::of::<Y>());
2223
}
24+
25+
// Check that `const_assume` feature allow `assume` intrinsic
26+
// to be used in const contexts.
27+
#[test]
28+
fn test_assume_can_be_in_const_contexts() {
29+
const unsafe fn foo(x: usize, y: usize) -> usize {
30+
// SAFETY: the entire function is not safe,
31+
// but it is just an example not used elsewhere.
32+
unsafe { assume(y != 0) };
33+
x / y
34+
}
35+
let rs = unsafe { foo(42, 97) };
36+
assert_eq!(rs, 0);
37+
}

core/tests/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#![feature(bound_cloned)]
99
#![feature(box_syntax)]
1010
#![feature(cell_update)]
11+
#![feature(const_assume)]
12+
#![feature(core_intrinsics)]
1113
#![feature(core_private_bignum)]
1214
#![feature(core_private_diy_float)]
1315
#![feature(debug_non_exhaustive)]

panic_unwind/src/seh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub struct _TypeDescriptor {
175175
// to be able to catch Rust panics by simply declaring a `struct rust_panic`.
176176
//
177177
// When modifying, make sure that the type name string exactly matches
178-
// the one used in src/librustc_codegen_llvm/intrinsic.rs.
178+
// the one used in `compiler/rustc_codegen_llvm/src/intrinsic.rs`.
179179
const TYPE_NAME: [u8; 11] = *b"rust_panic\0";
180180

181181
static mut THROW_INFO: _ThrowInfo = _ThrowInfo {

0 commit comments

Comments
 (0)