Skip to content

Commit b779694

Browse files
committed
Clear up some code
1 parent 932dbe8 commit b779694

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

src/librustc_mir/build/mod.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,18 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t
109109
_ => None,
110110
};
111111

112-
// FIXME: safety in closures
113112
let safety = match fn_sig.unsafety {
114113
hir::Unsafety::Normal => Safety::Safe,
114+
hir::Unsafety::Unsafe if tcx.is_min_const_fn(fn_def_id) => {
115+
// As specified in #55607, a `const unsafe fn` differs
116+
// from an `unsafe fn` in that its body is still considered
117+
// safe code by default.
118+
assert!(!implicit_argument.is_none());
119+
Safety::Safe
120+
},
115121
hir::Unsafety::Unsafe => Safety::FnUnsafe,
116122
};
117123

118-
let safety = match fn_sig.unsafety {
119-
hir::Unsafety::Normal => Safety::Safe,
120-
hir::Unsafety::Unsafe => {
121-
if tcx.is_min_const_fn(fn_def_id) => {
122-
// As specified in #55607, a `const unsafe fn` differs
123-
// from an `unsafe fn` in that its body is still considered
124-
// safe code by default.
125-
assert!(!implicit_argument.is_none());
126-
Safety::Safe
127-
} else {
128-
Safety::Unsafe
129-
}
130-
}
131-
};
132-
133124
let body = tcx.hir.body(body_id);
134125
let explicit_arguments =
135126
body.arguments

src/librustc_mir/transform/check_unsafety.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,19 +351,22 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
351351
}
352352
// only some unsafety is allowed in const fn
353353
if self.min_const_fn {
354+
let min_const_unsafe_fn = self.tcx.features().min_const_unsafe_fn;
354355
for violation in violations {
355356
match violation.kind {
356-
// these are allowed
357-
UnsafetyViolationKind::GatedConstFnCall => {
357+
UnsafetyViolationKind::GatedConstFnCall if min_const_unsafe_fn => {
358+
// these function calls to unsafe functions are allowed
358359
// if `#![feature(min_const_unsafe_fn)]` is active
359-
if !self.tcx.sess.features_untracked().min_const_unsafe_fn {
360-
if !self.violations.contains(&violation) {
361-
self.violations.push(violation.clone())
362-
}
360+
},
361+
UnsafetyViolationKind::GatedConstFnCall => {
362+
// without the feature gate, we report errors
363+
if !self.violations.contains(&violation) {
364+
self.violations.push(violation.clone())
363365
}
364366
}
365367
// these unsafe things are stable in const fn
366368
UnsafetyViolationKind::GeneralAndConstFn => {},
369+
// these things are forbidden in const fns
367370
UnsafetyViolationKind::General |
368371
UnsafetyViolationKind::BorrowPacked(_) |
369372
UnsafetyViolationKind::ExternStatic(_) => {

0 commit comments

Comments
 (0)