Skip to content

Commit dd40a80

Browse files
committed
Give the (un)likely intrinsics fallback bodies
1 parent 6a671bd commit dd40a80

File tree

6 files changed

+61
-56
lines changed

6 files changed

+61
-56
lines changed

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -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

+37-31
Original file line numberDiff line numberDiff line change
@@ -960,39 +960,45 @@ pub const unsafe fn assume(b: bool) {
960960
}
961961
}
962962

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

980-
/// Hints to the compiler that branch condition is likely to be false.
981-
/// Returns the value passed to it.
982-
///
983-
/// Any use other than with `if` statements will probably not have an effect.
984-
///
985-
/// Note that, unlike most intrinsics, this is safe to call;
986-
/// it does not require an `unsafe` block.
987-
/// Therefore, implementations must not require the user to uphold
988-
/// any safety invariants.
989-
///
990-
/// This intrinsic does not have a stable counterpart.
991-
#[rustc_const_unstable(feature = "const_likely", issue = "none")]
992-
#[rustc_safe_intrinsic]
993-
#[rustc_nounwind]
994-
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+
}
9951000

1001+
extern "rust-intrinsic" {
9961002
/// Executes a breakpoint trap, for inspection by a debugger.
9971003
///
9981004
/// This intrinsic does not have a stable counterpart.

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

+4-4
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

+11-10
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

+2-3
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

+5-6
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)