Skip to content

Commit 2bf4c76

Browse files
committed
Require const blocks to only use const stable things
1 parent 3b550fe commit 2bf4c76

File tree

13 files changed

+68
-22
lines changed

13 files changed

+68
-22
lines changed

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
197197
}
198198
}
199199
InlineAsmOperand::Const { anon_const } => hir::InlineAsmOperand::Const {
200-
anon_const: self.lower_const_block(anon_const),
200+
anon_const: self.lower_const_block(anon_const, &[]),
201201
},
202202
InlineAsmOperand::Sym { sym } => {
203203
let static_def_id = self

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
106106

107107
let kind = match &e.kind {
108108
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
109-
ExprKind::ConstBlock(c) => hir::ExprKind::ConstBlock(self.lower_const_block(c)),
109+
ExprKind::ConstBlock(c) => {
110+
hir::ExprKind::ConstBlock(self.lower_const_block(c, attrs))
111+
}
110112
ExprKind::Repeat(expr, count) => {
111113
let expr = self.lower_expr(expr);
112114
let count = self.lower_array_length_to_const_arg(count);
@@ -391,12 +393,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
391393
})
392394
}
393395

394-
pub(crate) fn lower_const_block(&mut self, c: &AnonConst) -> hir::ConstBlock {
396+
pub(crate) fn lower_const_block(
397+
&mut self,
398+
c: &AnonConst,
399+
attrs: &'hir [hir::Attribute],
400+
) -> hir::ConstBlock {
395401
self.with_new_scopes(c.value.span, |this| {
396402
let def_id = this.local_def_id(c.id);
403+
let hir_id = this.lower_node_id(c.id);
404+
if !attrs.is_empty() {
405+
this.attrs.insert(hir_id.local_id, attrs);
406+
}
397407
hir::ConstBlock {
398408
def_id,
399-
hir_id: this.lower_node_id(c.id),
409+
hir_id,
400410
body: this.lower_const_body(c.value.span, Some(&c.value)),
401411
}
402412
})

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
399399
ExprKind::Lit(lit) => {
400400
hir::PatExprKind::Lit { lit: self.lower_lit(lit, span), negated: false }
401401
}
402-
ExprKind::ConstBlock(c) => hir::PatExprKind::ConstBlock(self.lower_const_block(c)),
402+
ExprKind::ConstBlock(c) => hir::PatExprKind::ConstBlock(self.lower_const_block(c, &[])),
403403
ExprKind::IncludedBytes(byte_sym) => hir::PatExprKind::Lit {
404404
lit: respan(span, LitKind::ByteStr(*byte_sym, StrStyle::Cooked)),
405405
negated: false,

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ impl<S: Stage> CombineAttributeParser<S> for AllowConstFnUnstableParser {
6363
Allow(Target::Method(MethodKind::Inherent)),
6464
Allow(Target::Method(MethodKind::Trait { body: true })),
6565
Allow(Target::Method(MethodKind::TraitImpl)),
66+
Allow(Target::Expression), // FIXME: should only allow inline consts
6667
]);
6768
const TEMPLATE: AttributeTemplate = template!(Word, List: &["feat1, feat2, ..."]);
6869

compiler/rustc_attr_parsing/src/attributes/stability.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ impl<S: Stage> AttributeParser<S> for ConstStabilityParser {
255255
Allow(Target::AssocConst),
256256
Allow(Target::Trait),
257257
Allow(Target::Static),
258+
Allow(Target::Expression), // FIXME: we really only want to allow inline consts
258259
Allow(Target::Crate),
259260
Allow(Target::MacroDef), // FIXME(oli-obk): remove this and eliminate the manual check for it
260261
]);

compiler/rustc_const_eval/src/check_consts/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ impl<'mir, 'tcx> ConstCx<'mir, 'tcx> {
5454
pub fn enforce_recursive_const_stability(&self) -> bool {
5555
// We can skip this if neither `staged_api` nor `-Zforce-unstable-if-unmarked` are enabled,
5656
// since in such crates `lookup_const_stability` will always be `None`.
57-
self.const_kind == Some(hir::ConstContext::ConstFn)
58-
&& (self.tcx.features().staged_api()
59-
|| self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked)
57+
matches!(
58+
self.const_kind,
59+
Some(hir::ConstContext::ConstFn | hir::ConstContext::Const { inline: true })
60+
) && (self.tcx.features().staged_api()
61+
|| self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked)
6062
&& is_fn_or_trait_safe_to_expose_on_stable(self.tcx, self.def_id().to_def_id())
6163
}
6264

compiler/rustc_passes/src/stability.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
601601
let attrs = self.tcx.hir_attrs(item.hir_id());
602602
let stab = find_attr!(attrs, AttributeKind::Stability{stability, span} => (*stability, *span));
603603

604-
// FIXME(jdonszelmann): make it impossible to miss the or_else in the typesystem
605604
let const_stab = find_attr!(attrs, AttributeKind::ConstStability{stability, ..} => *stability);
606605

607606
let unstable_feature_stab =

library/alloc/src/boxed/thin.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,12 @@ impl<H> WithHeader<H> {
321321
// of the header, past the padding, so the assigned type makes sense.
322322
// It also ensures that the address at the end of the header is sufficiently
323323
// aligned for T.
324-
let alloc: &<Dyn as Pointee>::Metadata = const {
324+
// We generate the vtable in a const block instead of having the compiler
325+
// generate it for us. The basics to allocate new memory during ctfe are
326+
// unstable, but we can always change the intrinsic logic to support the
327+
// needs of new_unsize_zst in the future.
328+
let alloc: &<Dyn as Pointee>::Metadata = #[rustc_allow_const_fn_unstable(const_heap)]
329+
const {
325330
// FIXME: just call `WithHeader::alloc_layout` with size reset to 0.
326331
// Currently that's blocked on `Layout::extend` not being `const fn`.
327332

library/core/src/any.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,10 @@ impl fmt::Debug for TypeId {
864864
#[stable(feature = "type_name", since = "1.38.0")]
865865
#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
866866
pub const fn type_name<T: ?Sized>() -> &'static str {
867-
const { intrinsics::type_name::<T>() }
867+
#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
868+
const {
869+
intrinsics::type_name::<T>()
870+
}
868871
}
869872

870873
/// Returns the type name of the pointed-to value as a string slice.

library/core/src/intrinsics/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2881,6 +2881,7 @@ pub const fn type_name<T: ?Sized>() -> &'static str;
28812881
#[rustc_nounwind]
28822882
#[unstable(feature = "core_intrinsics", issue = "none")]
28832883
#[rustc_intrinsic]
2884+
#[rustc_intrinsic_const_stable_indirect]
28842885
pub const fn type_id<T: ?Sized + 'static>() -> crate::any::TypeId;
28852886

28862887
/// Tests (at compile-time) if two [`crate::any::TypeId`] instances identify the

0 commit comments

Comments
 (0)