From 932dbe816e98d874ca1d7a7f0913ca9f67a94a84 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Fri, 30 Nov 2018 18:17:50 +0100 Subject: [PATCH] Explain unsafety trickery of const functions --- src/librustc_mir/build/mod.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/librustc_mir/build/mod.rs b/src/librustc_mir/build/mod.rs index cc927df6350b..054fb5f45811 100644 --- a/src/librustc_mir/build/mod.rs +++ b/src/librustc_mir/build/mod.rs @@ -114,11 +114,20 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t hir::Unsafety::Normal => Safety::Safe, hir::Unsafety::Unsafe => Safety::FnUnsafe, }; - let safety = if implicit_argument.is_none() && tcx.is_min_const_fn(fn_def_id) { - // the body of `const unsafe fn`s is treated like the body of safe `const fn`s - Safety::Safe - } else { - safety + + let safety = match fn_sig.unsafety { + hir::Unsafety::Normal => Safety::Safe, + hir::Unsafety::Unsafe => { + if tcx.is_min_const_fn(fn_def_id) => { + // As specified in #55607, a `const unsafe fn` differs + // from an `unsafe fn` in that its body is still considered + // safe code by default. + assert!(!implicit_argument.is_none()); + Safety::Safe + } else { + Safety::Unsafe + } + } }; let body = tcx.hir.body(body_id);