From b77969466c23a51fdd7da25aa3ccc98e52d58d3b Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Fri, 30 Nov 2018 18:18:42 +0100 Subject: [PATCH] Clear up some code --- src/librustc_mir/build/mod.rs | 23 ++++++-------------- src/librustc_mir/transform/check_unsafety.rs | 15 ++++++++----- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/librustc_mir/build/mod.rs b/src/librustc_mir/build/mod.rs index 054fb5f4581..1538c070f37 100644 --- a/src/librustc_mir/build/mod.rs +++ b/src/librustc_mir/build/mod.rs @@ -109,27 +109,18 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t _ => None, }; - // FIXME: safety in closures 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 + }, hir::Unsafety::Unsafe => Safety::FnUnsafe, }; - 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); let explicit_arguments = body.arguments diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs index 25dbd160d19..b47f1957ab4 100644 --- a/src/librustc_mir/transform/check_unsafety.rs +++ b/src/librustc_mir/transform/check_unsafety.rs @@ -351,19 +351,22 @@ fn register_violations(&mut self, } // only some unsafety is allowed in const fn if self.min_const_fn { + let min_const_unsafe_fn = self.tcx.features().min_const_unsafe_fn; for violation in violations { match violation.kind { - // these are allowed - UnsafetyViolationKind::GatedConstFnCall => { + UnsafetyViolationKind::GatedConstFnCall if min_const_unsafe_fn => { + // these function calls to unsafe functions are allowed // if `#![feature(min_const_unsafe_fn)]` is active - if !self.tcx.sess.features_untracked().min_const_unsafe_fn { - if !self.violations.contains(&violation) { - self.violations.push(violation.clone()) - } + }, + UnsafetyViolationKind::GatedConstFnCall => { + // without the feature gate, we report errors + if !self.violations.contains(&violation) { + self.violations.push(violation.clone()) } } // these unsafe things are stable in const fn UnsafetyViolationKind::GeneralAndConstFn => {}, + // these things are forbidden in const fns UnsafetyViolationKind::General | UnsafetyViolationKind::BorrowPacked(_) | UnsafetyViolationKind::ExternStatic(_) => { -- 2.44.0