Skip to content

Conversation

@oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Dec 9, 2025

cc @RalfJung

Having moved several intrinsics from sth that gets invoked directly in a const fn to sth invoked in a const block kind of broke our "indirectly usable intrinsic" logic. So now we're actually checking it again. This required changing some intrinsics to now be indirectly callable

r? @jdonszelmann

Remove the custom special case rejecting const stability on macros as it is now handled by the general check
@rustbot
Copy link
Collaborator

rustbot commented Dec 9, 2025

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

Some changes occurred to constck

cc @fee1-dead

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

⚠️ #[rustc_intrinsic_const_stable_indirect] controls whether intrinsics can be exposed to stable const
code; adding it needs t-lang approval.

cc @rust-lang/wg-const-eval

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 9, 2025
#[rustc_intrinsic]
#[miri::intrinsic_fallback_is_spec]
#[rustc_intrinsic_const_stable_indirect]
pub const unsafe fn const_allocate(_size: usize, _align: usize) -> *mut u8 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this and make_global are needed in

let alloc: *mut u8 = const_allocate(alloc_size, alloc_align);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one really shouldn't be callable-from-stable, it is the very definition of not stable... same for const_make_global.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would strongly prefer to add allow_const_unstable to new_unsize_zst -- and we should consider if it's really safe to use these highly experimental features there.

That said, Thin is entirely unstable, so we can also add allow_const_unstable with a comment saying that this isn't actually stable -- but then we should also add that as a blocker on the tracking issue as it seems easy to miss.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would strongly prefer to add allow_const_unstable to new_unsize_zst
That does feel nicer, to guard it against any other uses. With a comment there why specifically this call needs it and is safe

@oli-obk oli-obk force-pushed the stable_inline_const_using_unstable_intrinsics branch from 5640606 to 8666a33 Compare December 9, 2025 16:34
@RalfJung
Copy link
Member

RalfJung commented Dec 9, 2025

Is this a fix for #132536? Or only a partial fix since it "just" applies to inline consts, not const items?

|| self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked)
matches!(
self.const_kind,
Some(hir::ConstContext::ConstFn | hir::ConstContext::Const { inline: true })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only a partial fix. I can remove the inline limitation here, but it has a lot of fallout that I haven't analyzed yet. I'll do that in a follow-up

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the remaining intrinsics, they aren't actually used from const fn either, are they? We just expose them via regular functions? The fact that those use a constant internally seems more of an implementation detail -- and we haven't had the t-lang FCP that would be required to actually expose them in a const fn.

View changes since this review

#[unstable(feature = "core_intrinsics", issue = "none")]
#[rustc_intrinsic]
#[rustc_intrinsic_const_stable_indirect]
pub const fn variant_count<T>() -> usize;
Copy link
Member

@RalfJung RalfJung Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't actually stable either, I think? std::mem::variant_count is still unstable.

#[unstable(feature = "core_intrinsics", issue = "none")]
#[rustc_intrinsic]
#[rustc_intrinsic_const_stable_indirect]
pub const fn type_name<T: ?Sized>() -> &'static str;
Copy link
Member

@RalfJung RalfJung Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't const-stable, it can only be invoked via a regular function.

#[unstable(feature = "core_intrinsics", issue = "none")]
#[rustc_intrinsic]
#[rustc_intrinsic_const_stable_indirect]
pub const fn type_id<T: ?Sized + 'static>() -> crate::any::TypeId;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And neither is this const-stable.

}
InlineAsmOperand::Const { anon_const } => hir::InlineAsmOperand::Const {
anon_const: self.lower_const_block(anon_const),
anon_const: self.lower_const_block(anon_const, &[]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can put at least some atributes on const operands. Iirc recently cfg was stabilized here? Though that one is of course expanded out early. Still wonder where those attributes go and whether they should be forwarded here 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even if in this case it only matters for their stability attrs

Allow(Target::AssocConst),
Allow(Target::Trait),
Allow(Target::Static),
Allow(Target::Expression), // FIXME: we really only want to allow inline consts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could make up a new target for that 🤷‍♀️ . This would be its current only use but it's not that hard to do otherwise


let stability = find_attr!(attrs, AttributeKind::Stability { stability, .. } => *stability);

// FIXME(jdonszelmann): make it impossible to miss the or_else in the typesystem
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot what my own fixme here refers to tbh. I think it looks like its already handled? Could you remove it?

let stability = find_attr!(attrs, AttributeKind::Stability { stability, .. } => *stability);

// FIXME(jdonszelmann): make it impossible to miss the or_else in the typesystem
if let Some(sp) = find_attr!(attrs, AttributeKind::ConstStability { span, .. } => *span) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we removing this check from macros? Don't quite follow how its related to the rest of your PR

#[rustc_intrinsic]
#[miri::intrinsic_fallback_is_spec]
#[rustc_intrinsic_const_stable_indirect]
pub const unsafe fn const_allocate(_size: usize, _align: usize) -> *mut u8 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would strongly prefer to add allow_const_unstable to new_unsize_zst
That does feel nicer, to guard it against any other uses. With a comment there why specifically this call needs it and is safe


#[rustc_const_stable(feature = "foo", since = "3.3.3")]
//~^ ERROR macros cannot have const stability attributes
//~^ ERROR attribute cannot be used on macro defs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea again am not entirely sure why removing this error from const stability is part of this PR.


fn main() {
const { my_fun2() };
#[rustc_const_unstable(feature = "abi_unadjusted", issue = "42")]
Copy link
Contributor

@jdonszelmann jdonszelmann Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test seems more what I expect the the goal of the PR to be. Maybe split the macro parts out and separately motivate it? Unless I'm missing how it's actually related

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 10, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 10, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants