Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made std::hint::unreachable_unchecked a const fn. #70324

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/libcore/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ use crate::intrinsics;
/// ```
#[inline]
#[stable(feature = "unreachable", since = "1.27.0")]
pub unsafe fn unreachable_unchecked() -> ! {
#[rustc_const_unstable(feature = "const_unreachable_unchecked", issue = "53188")]
pub const unsafe fn unreachable_unchecked() -> ! {
intrinsics::unreachable()
}

Expand Down
1 change: 1 addition & 0 deletions src/libcore/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ extern "rust-intrinsic" {
///
/// The stabilized version of this intrinsic is
/// [`std::hint::unreachable_unchecked`](../../std/hint/fn.unreachable_unchecked.html).
#[rustc_const_unstable(feature = "const_unreachable_unchecked", issue = "53188")]
pub fn unreachable() -> !;

/// Informs the optimizer that a condition is always true.
Expand Down
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
#![feature(const_ptr_offset_from)]
#![feature(const_result)]
#![feature(const_type_name)]
#![feature(const_unreachable_unchecked)]
#![feature(custom_inner_attributes)]
#![feature(decl_macro)]
#![feature(doc_cfg)]
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let (dest, ret) = match ret {
None => match intrinsic_name {
sym::transmute => throw_ub_format!("transmuting to uninhabited type"),
sym::unreachable_code => throw_ub!(Unreachable),
sym::abort => M::abort(self)?,
// Unsupported diverging intrinsic.
_ => return Ok(false),
Expand Down
28 changes: 28 additions & 0 deletions src/test/ui/consts/const_unsafe_unreachable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// run-pass

#![feature(core_intrinsics)]
#![feature(const_fn)]
#![feature(const_if_match)]
#![feature(const_unreachable_unchecked)]

const unsafe fn f_std_hint_unreachable(x: u8) -> u8 {
match x {
42 => 34,
_ => std::hint::unreachable_unchecked(),
}
}

const unsafe fn f_std_intrinsics_unreachable(x: u8) -> u8 {
match x {
17 => 22,
_ => std::intrinsics::unreachable(),
Copy link
Member

@RalfJung RalfJung Mar 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::hint::unreachable_unchecked is a thing wrapper around the intrinsic, I do not see the point of also testing the intrinsic directly? I don't think we usually do that.

The test @Centril and me asked for was a case where unreachable_unchecked is used incorrectly, where UB is triggered by actually reaching that function. Like, f_std_hint_unreachable(23).

}
}

const FOO:u8 = unsafe { f_std_hint_unreachable(42) };
const BAR:u8 = unsafe { f_std_intrinsics_unreachable(17) };

fn main() {
assert_eq!(FOO, 34);
assert_eq!(BAR, 22);
}
16 changes: 16 additions & 0 deletions src/test/ui/consts/const_unsafe_unreachable_ub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(const_fn)]
#![feature(const_if_match)]
#![feature(const_unreachable_unchecked)]

const unsafe fn foo(x: u8) -> u8 {
match x {
42 => 34,
_ => std::hint::unreachable_unchecked(),
}
}

const BAR:u8 = unsafe { foo(250) };

fn main() {
println!("{}", BAR);
}