Skip to content

Commit 5640606

Browse files
committed
Require const blocks to only use const stable things
1 parent d79f825 commit 5640606

File tree

7 files changed

+24
-9
lines changed

7 files changed

+24
-9
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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
103103

104104
let kind = match &e.kind {
105105
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
106-
ExprKind::ConstBlock(c) => hir::ExprKind::ConstBlock(self.lower_const_block(c)),
106+
ExprKind::ConstBlock(c) => hir::ExprKind::ConstBlock(self.lower_const_block(c, attrs)),
107107
ExprKind::Repeat(expr, count) => {
108108
let expr = self.lower_expr(expr);
109109
let count = self.lower_array_length_to_const_arg(count);
@@ -386,12 +386,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
386386
})
387387
}
388388

389-
pub(crate) fn lower_const_block(&mut self, c: &AnonConst) -> hir::ConstBlock {
389+
pub(crate) fn lower_const_block(&mut self, c: &AnonConst, attrs: &'hir [hir::Attribute]) -> hir::ConstBlock {
390390
self.with_new_scopes(c.value.span, |this| {
391391
let def_id = this.local_def_id(c.id);
392+
let hir_id = this.lower_node_id(c.id);
393+
if !attrs.is_empty() {
394+
this.attrs.insert(hir_id.local_id, attrs);
395+
}
392396
hir::ConstBlock {
393397
def_id,
394-
hir_id: this.lower_node_id(c.id),
398+
hir_id,
395399
body: this.lower_const_body(c.value.span, Some(&c.value)),
396400
}
397401
})

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/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
]);
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

library/core/src/intrinsics/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,7 @@ pub const fn overflow_checks() -> bool {
26292629
#[rustc_nounwind]
26302630
#[rustc_intrinsic]
26312631
#[miri::intrinsic_fallback_is_spec]
2632+
#[rustc_intrinsic_const_stable_indirect]
26322633
pub const unsafe fn const_allocate(_size: usize, _align: usize) -> *mut u8 {
26332634
// const eval overrides this function, but runtime code for now just returns null pointers.
26342635
// See <https://github.com/rust-lang/rust/issues/93935>.
@@ -2658,6 +2659,7 @@ pub const unsafe fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize)
26582659
#[rustc_nounwind]
26592660
#[rustc_intrinsic]
26602661
#[miri::intrinsic_fallback_is_spec]
2662+
#[rustc_intrinsic_const_stable_indirect]
26612663
pub const unsafe fn const_make_global(ptr: *mut u8) -> *const u8 {
26622664
// const eval overrides this function; at runtime, it is a NOP.
26632665
ptr
@@ -2823,6 +2825,7 @@ pub const fn offset_of<T: PointeeSized>(variant: u32, field: u32) -> usize;
28232825
#[rustc_nounwind]
28242826
#[unstable(feature = "core_intrinsics", issue = "none")]
28252827
#[rustc_intrinsic]
2828+
#[rustc_intrinsic_const_stable_indirect]
28262829
pub const fn variant_count<T>() -> usize;
28272830

28282831
/// The size of the referenced value in bytes.
@@ -2862,6 +2865,7 @@ pub const unsafe fn align_of_val<T: ?Sized>(ptr: *const T) -> usize;
28622865
#[rustc_nounwind]
28632866
#[unstable(feature = "core_intrinsics", issue = "none")]
28642867
#[rustc_intrinsic]
2868+
#[rustc_intrinsic_const_stable_indirect]
28652869
pub const fn type_name<T: ?Sized>() -> &'static str;
28662870

28672871
/// Gets an identifier which is globally unique to the specified type. This
@@ -2877,6 +2881,7 @@ pub const fn type_name<T: ?Sized>() -> &'static str;
28772881
#[rustc_nounwind]
28782882
#[unstable(feature = "core_intrinsics", issue = "none")]
28792883
#[rustc_intrinsic]
2884+
#[rustc_intrinsic_const_stable_indirect]
28802885
pub const fn type_id<T: ?Sized + 'static>() -> crate::any::TypeId;
28812886

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

tests/ui/consts/min_const_fn/const-fn-lang-feature.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ const fn my_fun2() {
1616
}
1717

1818
fn main() {
19-
const { my_fun2() };
19+
#[rustc_const_unstable(feature = "abi_unadjusted", issue = "42")]
20+
const {
21+
my_fun2()
22+
};
2023
}

0 commit comments

Comments
 (0)