Skip to content

Commit f93eb3e

Browse files
committed
Make the inline field on the Const ConstContext more clearly targetted at what it does
1 parent dc16e05 commit f93eb3e

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,10 +2264,12 @@ pub enum ConstContext {
22642264
/// - Array length expressions
22652265
/// - Enum discriminants
22662266
/// - Const generics
2267-
///
2268-
/// For the most part, other contexts are treated just like a regular `const`, so they are
2269-
/// lumped into the same category.
2270-
Const { inline: bool },
2267+
Const {
2268+
/// For backwards compatibility `const` items allow
2269+
/// calls to `const fn` to get promoted.
2270+
/// We forbid that in comptime fns and inline consts.
2271+
allow_const_fn_promotion: bool,
2272+
},
22712273
}
22722274

22732275
impl ConstContext {

compiler/rustc_middle/src/hir/map.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,15 @@ impl<'tcx> TyCtxt<'tcx> {
314314
pub fn hir_body_const_context(self, def_id: LocalDefId) -> Option<ConstContext> {
315315
let def_id = def_id.into();
316316
let ccx = match self.hir_body_owner_kind(def_id) {
317-
BodyOwnerKind::Const { inline } => ConstContext::Const { inline },
317+
BodyOwnerKind::Const { inline } => {
318+
ConstContext::Const { allow_const_fn_promotion: !inline }
319+
}
318320
BodyOwnerKind::Static(mutability) => ConstContext::Static(mutability),
319321

320322
BodyOwnerKind::Fn if self.is_constructor(def_id) => return None,
321323
BodyOwnerKind::Fn | BodyOwnerKind::Closure if self.is_const_fn(def_id) => {
322324
if self.constness(def_id) == rustc_hir::Constness::Comptime {
323-
ConstContext::Const { inline: true }
325+
ConstContext::Const { allow_const_fn_promotion: false }
324326
} else {
325327
ConstContext::ConstFn
326328
}

compiler/rustc_mir_transform/src/promote_consts.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,10 @@ impl<'tcx> Validator<'_, 'tcx> {
667667
// backwards compatibility reason to allow more promotion inside of them.
668668
let promote_all_fn = matches!(
669669
self.const_kind,
670-
Some(hir::ConstContext::Static(_) | hir::ConstContext::Const { inline: false })
670+
Some(
671+
hir::ConstContext::Static(_)
672+
| hir::ConstContext::Const { allow_const_fn_promotion: true }
673+
)
671674
);
672675
if !promote_all_fn {
673676
return Err(Unpromotable);

src/tools/clippy/clippy_lints/src/default_numeric_fallback.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for DefaultNumericFallback {
5656
// Inline const supports type inference.
5757
let is_parent_const = matches!(
5858
cx.tcx.hir_body_const_context(cx.tcx.hir_body_owner_def_id(body.id())),
59-
Some(ConstContext::Const { inline: false } | ConstContext::Static(_))
59+
Some(ConstContext::Const { allow_const_fn_promotion: true } | ConstContext::Static(_))
6060
);
6161
let mut visitor = NumericFallbackVisitor::new(cx, is_parent_const);
6262
visitor.visit_body(body);

src/tools/clippy/clippy_utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ pub fn is_inside_always_const_context(tcx: TyCtxt<'_>, hir_id: HirId) -> bool {
249249
};
250250
match ctx {
251251
ConstFn => false,
252-
Static(_) | Const { inline: _ } => true,
252+
Static(_) | Const { allow_const_fn_promotion: _ } => true,
253253
}
254254
}
255255

0 commit comments

Comments
 (0)