-
-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Stable inline const using unstable intrinsics #149817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Stable inline const using unstable intrinsics #149817
Conversation
Remove the custom special case rejecting const stability on macros as it is now handled by the general check
|
Some changes occurred in compiler/rustc_attr_parsing Some changes occurred to constck cc @fee1-dead Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr Some changes occurred to the CTFE machinery
cc @rust-lang/wg-const-eval |
| #[rustc_intrinsic] | ||
| #[miri::intrinsic_fallback_is_spec] | ||
| #[rustc_intrinsic_const_stable_indirect] | ||
| pub const unsafe fn const_allocate(_size: usize, _align: usize) -> *mut u8 { |
There was a problem hiding this comment.
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
rust/library/alloc/src/boxed/thin.rs
Line 334 in e2893f7
| let alloc: *mut u8 = const_allocate(alloc_size, alloc_align); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
5640606 to
8666a33
Compare
|
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 }) |
There was a problem hiding this comment.
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
There was a problem hiding this 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.
| #[unstable(feature = "core_intrinsics", issue = "none")] | ||
| #[rustc_intrinsic] | ||
| #[rustc_intrinsic_const_stable_indirect] | ||
| pub const fn variant_count<T>() -> usize; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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, &[]), |
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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")] |
There was a problem hiding this comment.
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
|
Reminder, once the PR becomes ready for a review, use |
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