Skip to content

Commit 943e220

Browse files
Auto merge of #148952 - RalfJung:fn-call-promotion, r=<try>
crater experiment: do not promote function calls in const/static items
2 parents 42f4793 + 5f31375 commit 943e220

File tree

1 file changed

+3
-27
lines changed

1 file changed

+3
-27
lines changed

compiler/rustc_mir_transform/src/promote_consts.rs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//! This pass assumes that every use is dominated by an initialization and can
1010
//! otherwise silence errors, if move analysis runs after promotion on broken
1111
//! MIR.
12+
#![allow(unused)]
1213

1314
use std::assert_matches::assert_matches;
1415
use std::cell::Cell;
@@ -662,33 +663,8 @@ impl<'tcx> Validator<'_, 'tcx> {
662663
}
663664
}
664665

665-
// Ideally, we'd stop here and reject the rest.
666-
// But for backward compatibility, we have to accept some promotion in const/static
667-
// initializers. Inline consts are explicitly excluded, they are more recent so we have no
668-
// backwards compatibility reason to allow more promotion inside of them.
669-
let promote_all_fn = matches!(
670-
self.const_kind,
671-
Some(hir::ConstContext::Static(_) | hir::ConstContext::Const { inline: false })
672-
);
673-
if !promote_all_fn {
674-
return Err(Unpromotable);
675-
}
676-
// Make sure the callee is a `const fn`.
677-
let is_const_fn = match *fn_ty.kind() {
678-
ty::FnDef(def_id, _) => self.tcx.is_const_fn(def_id),
679-
_ => false,
680-
};
681-
if !is_const_fn {
682-
return Err(Unpromotable);
683-
}
684-
// The problem is, this may promote calls to functions that panic.
685-
// We don't want to introduce compilation errors if there's a panic in a call in dead code.
686-
// So we ensure that this is not dead code.
687-
if !self.is_promotion_safe_block(block) {
688-
return Err(Unpromotable);
689-
}
690-
// This passed all checks, so let's accept.
691-
Ok(())
666+
// Ideally, we'd stop here and reject the rest. So let's do that.
667+
return Err(Unpromotable);
692668
}
693669
}
694670

0 commit comments

Comments
 (0)