Skip to content

Commit 932dbe8

Browse files
committed
Explain unsafety trickery of const functions
1 parent b75d5f1 commit 932dbe8

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/librustc_mir/build/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,20 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t
114114
hir::Unsafety::Normal => Safety::Safe,
115115
hir::Unsafety::Unsafe => Safety::FnUnsafe,
116116
};
117-
let safety = if implicit_argument.is_none() && tcx.is_min_const_fn(fn_def_id) {
118-
// the body of `const unsafe fn`s is treated like the body of safe `const fn`s
119-
Safety::Safe
120-
} else {
121-
safety
117+
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+
}
122131
};
123132

124133
let body = tcx.hir.body(body_id);

0 commit comments

Comments
 (0)