Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7b17521

Browse files
committedFeb 16, 2024
Give some more intrinsics fallback bodies
1 parent 65b4228 commit 7b17521

File tree

6 files changed

+91
-80
lines changed

6 files changed

+91
-80
lines changed
 

‎compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ pub fn check_intrinsic_type(
222222
sym::caller_location => (0, 0, vec![], tcx.caller_location_ty()),
223223
sym::assert_inhabited
224224
| sym::assert_zero_valid
225-
| sym::assert_mem_uninitialized_valid => (1, 0, vec![], Ty::new_unit(tcx)),
225+
| sym::assert_mem_uninitialized_valid => (1, 1, vec![], Ty::new_unit(tcx)),
226226
sym::forget => (1, 0, vec![param(0)], Ty::new_unit(tcx)),
227227
sym::transmute | sym::transmute_unchecked => (2, 0, vec![param(0)], param(1)),
228228
sym::prefetch_read_data
@@ -408,8 +408,8 @@ pub fn check_intrinsic_type(
408408
sym::float_to_int_unchecked => (2, 0, vec![param(0)], param(1)),
409409

410410
sym::assume => (0, 1, vec![tcx.types.bool], Ty::new_unit(tcx)),
411-
sym::likely => (0, 0, vec![tcx.types.bool], tcx.types.bool),
412-
sym::unlikely => (0, 0, vec![tcx.types.bool], tcx.types.bool),
411+
sym::likely => (0, 1, vec![tcx.types.bool], tcx.types.bool),
412+
sym::unlikely => (0, 1, vec![tcx.types.bool], tcx.types.bool),
413413

414414
sym::read_via_copy => (1, 0, vec![Ty::new_imm_ptr(tcx, param(0))], param(0)),
415415
sym::write_via_move => {

‎library/core/src/intrinsics.rs

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -955,43 +955,50 @@ extern "rust-intrinsic" {
955955
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
956956
pub const unsafe fn assume(b: bool) {
957957
if !b {
958+
// SAFETY: the caller must guarantee the argument is never `false`
958959
unsafe { unreachable() }
959960
}
960961
}
961962

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

979-
/// Hints to the compiler that branch condition is likely to be false.
980-
/// Returns the value passed to it.
981-
///
982-
/// Any use other than with `if` statements will probably not have an effect.
983-
///
984-
/// Note that, unlike most intrinsics, this is safe to call;
985-
/// it does not require an `unsafe` block.
986-
/// Therefore, implementations must not require the user to uphold
987-
/// any safety invariants.
988-
///
989-
/// This intrinsic does not have a stable counterpart.
990-
#[rustc_const_unstable(feature = "const_likely", issue = "none")]
991-
#[rustc_safe_intrinsic]
992-
#[rustc_nounwind]
993-
pub fn unlikely(b: bool) -> bool;
982+
/// Hints to the compiler that branch condition is likely to be false.
983+
/// Returns the value passed to it.
984+
///
985+
/// Any use other than with `if` statements will probably not have an effect.
986+
///
987+
/// Note that, unlike most intrinsics, this is safe to call;
988+
/// it does not require an `unsafe` block.
989+
/// Therefore, implementations must not require the user to uphold
990+
/// any safety invariants.
991+
///
992+
/// This intrinsic does not have a stable counterpart.
993+
#[rustc_const_unstable(feature = "const_likely", issue = "none")]
994+
#[unstable(feature = "core_intrinsics", issue = "none")]
995+
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
996+
#[rustc_nounwind]
997+
pub const fn unlikely(b: bool) -> bool {
998+
b
999+
}
9941000

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

1078-
/// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
1079-
/// This will statically either panic, or do nothing.
1080-
///
1081-
/// This intrinsic does not have a stable counterpart.
1082-
#[rustc_const_stable(feature = "const_assert_type", since = "1.59.0")]
1083-
#[rustc_safe_intrinsic]
1084-
#[rustc_nounwind]
1085-
pub fn assert_inhabited<T>();
1086+
/// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
1087+
/// This will statically either panic, or do nothing.
1088+
///
1089+
/// This intrinsic does not have a stable counterpart.
1090+
#[rustc_const_stable(feature = "const_assert_type", since = "1.59.0")]
1091+
#[rustc_nounwind]
1092+
#[unstable(feature = "core_intrinsics", issue = "none")]
1093+
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
1094+
pub const fn assert_inhabited<T>() {}
10861095

1087-
/// A guard for unsafe functions that cannot ever be executed if `T` does not permit
1088-
/// zero-initialization: This will statically either panic, or do nothing.
1089-
///
1090-
/// This intrinsic does not have a stable counterpart.
1091-
#[rustc_const_stable(feature = "const_assert_type2", since = "1.75.0")]
1092-
#[rustc_safe_intrinsic]
1093-
#[rustc_nounwind]
1094-
pub fn assert_zero_valid<T>();
1096+
/// A guard for unsafe functions that cannot ever be executed if `T` does not permit
1097+
/// zero-initialization: This will statically either panic, or do nothing.
1098+
///
1099+
/// This intrinsic does not have a stable counterpart.
1100+
#[rustc_const_stable(feature = "const_assert_type2", since = "1.75.0")]
1101+
#[rustc_nounwind]
1102+
#[unstable(feature = "core_intrinsics", issue = "none")]
1103+
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
1104+
pub const fn assert_zero_valid<T>() {}
10951105

1096-
/// A guard for `std::mem::uninitialized`. This will statically either panic, or do nothing.
1097-
///
1098-
/// This intrinsic does not have a stable counterpart.
1099-
#[rustc_const_stable(feature = "const_assert_type2", since = "1.75.0")]
1100-
#[rustc_safe_intrinsic]
1101-
#[rustc_nounwind]
1102-
pub fn assert_mem_uninitialized_valid<T>();
1106+
/// A guard for `std::mem::uninitialized`. This will statically either panic, or do nothing.
1107+
///
1108+
/// This intrinsic does not have a stable counterpart.
1109+
#[rustc_const_stable(feature = "const_assert_type2", since = "1.75.0")]
1110+
#[rustc_nounwind]
1111+
#[unstable(feature = "core_intrinsics", issue = "none")]
1112+
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
1113+
pub const fn assert_mem_uninitialized_valid<T>() {}
11031114

1115+
extern "rust-intrinsic" {
11041116
/// Gets a reference to a static `Location` indicating where it was called.
11051117
///
11061118
/// Note that, unlike most intrinsics, this is safe to call;

‎tests/ui/intrinsics/safe-intrinsic-mismatch.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
extern "rust-intrinsic" {
66
fn size_of<T>() -> usize; //~ ERROR intrinsic safety mismatch
77
//~^ ERROR intrinsic safety mismatch
8-
9-
#[rustc_safe_intrinsic]
10-
fn assume(b: bool); //~ ERROR intrinsic safety mismatch
11-
//~^ ERROR intrinsic safety mismatch
128
}
139

10+
#[rustc_intrinsic]
11+
const fn assume(_b: bool) {} //~ ERROR intrinsic safety mismatch
12+
//~| ERROR intrinsic has wrong type
13+
1414
#[rustc_intrinsic]
1515
const fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize) {}
1616
//~^ ERROR intrinsic safety mismatch

‎tests/ui/intrinsics/safe-intrinsic-mismatch.stderr

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ error: intrinsic safety mismatch between list of intrinsics within the compiler
44
LL | fn size_of<T>() -> usize;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `assume`
8-
--> $DIR/safe-intrinsic-mismatch.rs:10:5
9-
|
10-
LL | fn assume(b: bool);
11-
| ^^^^^^^^^^^^^^^^^^
12-
137
error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `size_of`
148
--> $DIR/safe-intrinsic-mismatch.rs:6:5
159
|
@@ -19,12 +13,19 @@ LL | fn size_of<T>() -> usize;
1913
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
2014

2115
error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `assume`
22-
--> $DIR/safe-intrinsic-mismatch.rs:10:5
16+
--> $DIR/safe-intrinsic-mismatch.rs:11:1
2317
|
24-
LL | fn assume(b: bool);
25-
| ^^^^^^^^^^^^^^^^^^
18+
LL | const fn assume(_b: bool) {}
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
error[E0308]: intrinsic has wrong type
22+
--> $DIR/safe-intrinsic-mismatch.rs:11:16
2623
|
27-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
24+
LL | const fn assume(_b: bool) {}
25+
| ^ expected unsafe fn, found normal fn
26+
|
27+
= note: expected signature `unsafe fn(_)`
28+
found signature `fn(_)`
2829

2930
error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `const_deallocate`
3031
--> $DIR/safe-intrinsic-mismatch.rs:15:1

‎tests/ui/reify-intrinsic.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ fn b() {
1313
}
1414

1515
fn c() {
16-
let _ = [
17-
std::intrinsics::likely,
16+
let _: [unsafe extern "rust-intrinsic" fn(bool) -> bool; 2] = [
17+
std::intrinsics::likely, //~ ERROR cannot coerce
1818
std::intrinsics::unlikely,
19-
//~^ ERROR cannot coerce
2019
];
2120
}
2221

‎tests/ui/reify-intrinsic.stderr

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ LL | let _ = std::mem::transmute as unsafe extern "rust-intrinsic" fn(isize)
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1717

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

2827
error: aborting due to 3 previous errors
2928

0 commit comments

Comments
 (0)
Please sign in to comment.