Skip to content

Commit

Permalink
Give some more intrinsics fallback bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Feb 16, 2024
1 parent 65b4228 commit 7b17521
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 80 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_hir_analysis/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ pub fn check_intrinsic_type(
sym::caller_location => (0, 0, vec![], tcx.caller_location_ty()),
sym::assert_inhabited
| sym::assert_zero_valid
| sym::assert_mem_uninitialized_valid => (1, 0, vec![], Ty::new_unit(tcx)),
| sym::assert_mem_uninitialized_valid => (1, 1, vec![], Ty::new_unit(tcx)),
sym::forget => (1, 0, vec![param(0)], Ty::new_unit(tcx)),
sym::transmute | sym::transmute_unchecked => (2, 0, vec![param(0)], param(1)),
sym::prefetch_read_data
Expand Down Expand Up @@ -408,8 +408,8 @@ pub fn check_intrinsic_type(
sym::float_to_int_unchecked => (2, 0, vec![param(0)], param(1)),

sym::assume => (0, 1, vec![tcx.types.bool], Ty::new_unit(tcx)),
sym::likely => (0, 0, vec![tcx.types.bool], tcx.types.bool),
sym::unlikely => (0, 0, vec![tcx.types.bool], tcx.types.bool),
sym::likely => (0, 1, vec![tcx.types.bool], tcx.types.bool),
sym::unlikely => (0, 1, vec![tcx.types.bool], tcx.types.bool),

sym::read_via_copy => (1, 0, vec![Ty::new_imm_ptr(tcx, param(0))], param(0)),
sym::write_via_move => {
Expand Down
120 changes: 66 additions & 54 deletions library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,43 +955,50 @@ extern "rust-intrinsic" {
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
pub const unsafe fn assume(b: bool) {
if !b {
// SAFETY: the caller must guarantee the argument is never `false`
unsafe { unreachable() }
}
}

extern "rust-intrinsic" {
/// Hints to the compiler that branch condition is likely to be true.
/// Returns the value passed to it.
///
/// Any use other than with `if` statements will probably not have an effect.
///
/// Note that, unlike most intrinsics, this is safe to call;
/// it does not require an `unsafe` block.
/// Therefore, implementations must not require the user to uphold
/// any safety invariants.
///
/// This intrinsic does not have a stable counterpart.
#[rustc_const_unstable(feature = "const_likely", issue = "none")]
#[rustc_safe_intrinsic]
#[rustc_nounwind]
pub fn likely(b: bool) -> bool;
/// Hints to the compiler that branch condition is likely to be true.
/// Returns the value passed to it.
///
/// Any use other than with `if` statements will probably not have an effect.
///
/// Note that, unlike most intrinsics, this is safe to call;
/// it does not require an `unsafe` block.
/// Therefore, implementations must not require the user to uphold
/// any safety invariants.
///
/// This intrinsic does not have a stable counterpart.
#[rustc_const_unstable(feature = "const_likely", issue = "none")]
#[unstable(feature = "core_intrinsics", issue = "none")]
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
#[rustc_nounwind]
pub const fn likely(b: bool) -> bool {
b
}

/// Hints to the compiler that branch condition is likely to be false.
/// Returns the value passed to it.
///
/// Any use other than with `if` statements will probably not have an effect.
///
/// Note that, unlike most intrinsics, this is safe to call;
/// it does not require an `unsafe` block.
/// Therefore, implementations must not require the user to uphold
/// any safety invariants.
///
/// This intrinsic does not have a stable counterpart.
#[rustc_const_unstable(feature = "const_likely", issue = "none")]
#[rustc_safe_intrinsic]
#[rustc_nounwind]
pub fn unlikely(b: bool) -> bool;
/// Hints to the compiler that branch condition is likely to be false.
/// Returns the value passed to it.
///
/// Any use other than with `if` statements will probably not have an effect.
///
/// Note that, unlike most intrinsics, this is safe to call;
/// it does not require an `unsafe` block.
/// Therefore, implementations must not require the user to uphold
/// any safety invariants.
///
/// This intrinsic does not have a stable counterpart.
#[rustc_const_unstable(feature = "const_likely", issue = "none")]
#[unstable(feature = "core_intrinsics", issue = "none")]
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
#[rustc_nounwind]
pub const fn unlikely(b: bool) -> bool {
b
}

extern "rust-intrinsic" {
/// Executes a breakpoint trap, for inspection by a debugger.
///
/// This intrinsic does not have a stable counterpart.
Expand Down Expand Up @@ -1074,33 +1081,38 @@ extern "rust-intrinsic" {
#[rustc_safe_intrinsic]
#[rustc_nounwind]
pub fn type_id<T: ?Sized + 'static>() -> u128;
}

/// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
/// This will statically either panic, or do nothing.
///
/// This intrinsic does not have a stable counterpart.
#[rustc_const_stable(feature = "const_assert_type", since = "1.59.0")]
#[rustc_safe_intrinsic]
#[rustc_nounwind]
pub fn assert_inhabited<T>();
/// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
/// This will statically either panic, or do nothing.
///
/// This intrinsic does not have a stable counterpart.
#[rustc_const_stable(feature = "const_assert_type", since = "1.59.0")]
#[rustc_nounwind]
#[unstable(feature = "core_intrinsics", issue = "none")]
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
pub const fn assert_inhabited<T>() {}

/// A guard for unsafe functions that cannot ever be executed if `T` does not permit
/// zero-initialization: This will statically either panic, or do nothing.
///
/// This intrinsic does not have a stable counterpart.
#[rustc_const_stable(feature = "const_assert_type2", since = "1.75.0")]
#[rustc_safe_intrinsic]
#[rustc_nounwind]
pub fn assert_zero_valid<T>();
/// A guard for unsafe functions that cannot ever be executed if `T` does not permit
/// zero-initialization: This will statically either panic, or do nothing.
///
/// This intrinsic does not have a stable counterpart.
#[rustc_const_stable(feature = "const_assert_type2", since = "1.75.0")]
#[rustc_nounwind]
#[unstable(feature = "core_intrinsics", issue = "none")]
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
pub const fn assert_zero_valid<T>() {}

/// A guard for `std::mem::uninitialized`. This will statically either panic, or do nothing.
///
/// This intrinsic does not have a stable counterpart.
#[rustc_const_stable(feature = "const_assert_type2", since = "1.75.0")]
#[rustc_safe_intrinsic]
#[rustc_nounwind]
pub fn assert_mem_uninitialized_valid<T>();
/// A guard for `std::mem::uninitialized`. This will statically either panic, or do nothing.
///
/// This intrinsic does not have a stable counterpart.
#[rustc_const_stable(feature = "const_assert_type2", since = "1.75.0")]
#[rustc_nounwind]
#[unstable(feature = "core_intrinsics", issue = "none")]
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
pub const fn assert_mem_uninitialized_valid<T>() {}

extern "rust-intrinsic" {
/// Gets a reference to a static `Location` indicating where it was called.
///
/// Note that, unlike most intrinsics, this is safe to call;
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/intrinsics/safe-intrinsic-mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
extern "rust-intrinsic" {
fn size_of<T>() -> usize; //~ ERROR intrinsic safety mismatch
//~^ ERROR intrinsic safety mismatch

#[rustc_safe_intrinsic]
fn assume(b: bool); //~ ERROR intrinsic safety mismatch
//~^ ERROR intrinsic safety mismatch
}

#[rustc_intrinsic]
const fn assume(_b: bool) {} //~ ERROR intrinsic safety mismatch
//~| ERROR intrinsic has wrong type

#[rustc_intrinsic]
const fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize) {}
//~^ ERROR intrinsic safety mismatch
Expand Down
21 changes: 11 additions & 10 deletions tests/ui/intrinsics/safe-intrinsic-mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ error: intrinsic safety mismatch between list of intrinsics within the compiler
LL | fn size_of<T>() -> usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^

error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `assume`
--> $DIR/safe-intrinsic-mismatch.rs:10:5
|
LL | fn assume(b: bool);
| ^^^^^^^^^^^^^^^^^^

error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `size_of`
--> $DIR/safe-intrinsic-mismatch.rs:6:5
|
Expand All @@ -19,12 +13,19 @@ LL | fn size_of<T>() -> usize;
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `assume`
--> $DIR/safe-intrinsic-mismatch.rs:10:5
--> $DIR/safe-intrinsic-mismatch.rs:11:1
|
LL | fn assume(b: bool);
| ^^^^^^^^^^^^^^^^^^
LL | const fn assume(_b: bool) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: intrinsic has wrong type
--> $DIR/safe-intrinsic-mismatch.rs:11:16
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
LL | const fn assume(_b: bool) {}
| ^ expected unsafe fn, found normal fn
|
= note: expected signature `unsafe fn(_)`
found signature `fn(_)`

error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `const_deallocate`
--> $DIR/safe-intrinsic-mismatch.rs:15:1
Expand Down
5 changes: 2 additions & 3 deletions tests/ui/reify-intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ fn b() {
}

fn c() {
let _ = [
std::intrinsics::likely,
let _: [unsafe extern "rust-intrinsic" fn(bool) -> bool; 2] = [
std::intrinsics::likely, //~ ERROR cannot coerce
std::intrinsics::unlikely,
//~^ ERROR cannot coerce
];
}

Expand Down
11 changes: 5 additions & 6 deletions tests/ui/reify-intrinsic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ LL | let _ = std::mem::transmute as unsafe extern "rust-intrinsic" fn(isize)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: cannot coerce intrinsics to function pointers
--> $DIR/reify-intrinsic.rs:18:9
--> $DIR/reify-intrinsic.rs:17:9
|
LL | std::intrinsics::unlikely,
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cannot coerce intrinsics to function pointers
LL | std::intrinsics::likely,
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot coerce intrinsics to function pointers
|
= note: expected fn item `extern "rust-intrinsic" fn(_) -> _ {likely}`
found fn item `extern "rust-intrinsic" fn(_) -> _ {unlikely}`
= note: different fn items have unique types, even if their signatures are the same
= note: expected fn pointer `unsafe extern "rust-intrinsic" fn(_) -> _`
found fn item `fn(_) -> _ {likely}`

error: aborting due to 3 previous errors

Expand Down

0 comments on commit 7b17521

Please sign in to comment.