Skip to content

Commit 21a6815

Browse files
committed
Make the inline field on the Const ConstContext more clearly targetted at what it does
1 parent 822df2f commit 21a6815

5 files changed

Lines changed: 16 additions & 9 deletions

File tree

compiler/rustc_hir/src/hir.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,10 +2355,12 @@ pub enum ConstContext {
23552355
/// - Array length expressions
23562356
/// - Enum discriminants
23572357
/// - Const generics
2358-
///
2359-
/// For the most part, other contexts are treated just like a regular `const`, so they are
2360-
/// lumped into the same category.
2361-
Const { inline: bool },
2358+
Const {
2359+
/// For backwards compatibility `const` items allow
2360+
/// calls to `const fn` to get promoted.
2361+
/// We forbid that in comptime fns and inline consts.
2362+
allow_const_fn_promotion: bool,
2363+
},
23622364
}
23632365

23642366
impl ConstContext {

compiler/rustc_middle/src/hir/map.rs

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

319321
BodyOwnerKind::Fn if self.is_constructor(def_id) => return None,
@@ -323,7 +325,7 @@ impl<'tcx> TyCtxt<'tcx> {
323325
}
324326
BodyOwnerKind::Fn if self.is_const_fn(def_id) => {
325327
if matches!(self.constness(def_id), rustc_hir::Constness::Const { always: true }) {
326-
ConstContext::Const { inline: true }
328+
ConstContext::Const { allow_const_fn_promotion: false }
327329
} else {
328330
ConstContext::ConstFn
329331
}

compiler/rustc_mir_transform/src/promote_consts.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,10 @@ impl<'tcx> Validator<'_, 'tcx> {
657657
// backwards compatibility reason to allow more promotion inside of them.
658658
let promote_all_fn = matches!(
659659
self.const_kind,
660-
Some(hir::ConstContext::Static(_) | hir::ConstContext::Const { inline: false })
660+
Some(
661+
hir::ConstContext::Static(_)
662+
| hir::ConstContext::Const { allow_const_fn_promotion: true }
663+
)
661664
);
662665
if !promote_all_fn {
663666
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
@@ -243,7 +243,7 @@ pub fn is_inside_always_const_context(tcx: TyCtxt<'_>, hir_id: HirId) -> bool {
243243
};
244244
match ctx {
245245
ConstFn => false,
246-
Static(_) | Const { inline: _ } => true,
246+
Static(_) | Const { allow_const_fn_promotion: _ } => true,
247247
}
248248
}
249249

0 commit comments

Comments
 (0)