-
-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -243,7 +243,21 @@ impl<S: Stage> AttributeParser<S> for ConstStabilityParser { | |
| this.promotable = true; | ||
| }), | ||
| ]; | ||
| const ALLOWED_TARGETS: AllowedTargets = ALLOWED_TARGETS; | ||
| const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[ | ||
| Allow(Target::Fn), | ||
| Allow(Target::Method(MethodKind::Inherent)), | ||
| Allow(Target::Method(MethodKind::TraitImpl)), | ||
| Allow(Target::Method(MethodKind::Trait { body: true })), | ||
| Allow(Target::Impl { of_trait: false }), | ||
| Allow(Target::Impl { of_trait: true }), | ||
| Allow(Target::Use), // FIXME I don't think this does anything? | ||
| Allow(Target::Const), | ||
| Allow(Target::AssocConst), | ||
| Allow(Target::Trait), | ||
| Allow(Target::Static), | ||
| Allow(Target::Expression), // FIXME: we really only want to allow inline consts | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| Allow(Target::Crate), | ||
| ]); | ||
|
|
||
| fn finalize(mut self, cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> { | ||
| if self.promotable { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,9 +54,11 @@ impl<'mir, 'tcx> ConstCx<'mir, 'tcx> { | |
| pub fn enforce_recursive_const_stability(&self) -> bool { | ||
| // We can skip this if neither `staged_api` nor `-Zforce-unstable-if-unmarked` are enabled, | ||
| // since in such crates `lookup_const_stability` will always be `None`. | ||
| self.const_kind == Some(hir::ConstContext::ConstFn) | ||
| && (self.tcx.features().staged_api() | ||
| || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked) | ||
| matches!( | ||
| self.const_kind, | ||
| Some(hir::ConstContext::ConstFn | hir::ConstContext::Const { inline: true }) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| ) && (self.tcx.features().staged_api() | ||
| || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked) | ||
| && is_fn_or_trait_safe_to_expose_on_stable(self.tcx, self.def_id().to_def_id()) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -979,12 +979,6 @@ impl SyntaxExtension { | |
| let stability = find_attr!(attrs, AttributeKind::Stability { stability, .. } => *stability); | ||
|
|
||
| // FIXME(jdonszelmann): make it impossible to miss the or_else in the typesystem | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| if let Some(sp) = find_attr!(attrs, AttributeKind::ConstStability { span, .. } => *span) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| sess.dcx().emit_err(errors::MacroConstStability { | ||
| span: sp, | ||
| head_span: sess.source_map().guess_head_span(span), | ||
| }); | ||
| } | ||
| if let Some(sp) = find_attr!(attrs, AttributeKind::BodyStability{ span, .. } => *span) { | ||
| sess.dcx().emit_err(errors::MacroBodyStability { | ||
| span: sp, | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -2629,6 +2629,7 @@ pub const fn overflow_checks() -> bool { | |||
| #[rustc_nounwind] | ||||
| #[rustc_intrinsic] | ||||
| #[miri::intrinsic_fallback_is_spec] | ||||
| #[rustc_intrinsic_const_stable_indirect] | ||||
| pub const unsafe fn const_allocate(_size: usize, _align: usize) -> *mut u8 { | ||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would strongly prefer to add allow_const_unstable to That said,
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
| // const eval overrides this function, but runtime code for now just returns null pointers. | ||||
| // 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) | |||
| #[rustc_nounwind] | ||||
| #[rustc_intrinsic] | ||||
| #[miri::intrinsic_fallback_is_spec] | ||||
| #[rustc_intrinsic_const_stable_indirect] | ||||
| pub const unsafe fn const_make_global(ptr: *mut u8) -> *const u8 { | ||||
| // const eval overrides this function; at runtime, it is a NOP. | ||||
| ptr | ||||
|
|
@@ -2823,6 +2825,7 @@ pub const fn offset_of<T: PointeeSized>(variant: u32, field: u32) -> usize; | |||
| #[rustc_nounwind] | ||||
| #[unstable(feature = "core_intrinsics", issue = "none")] | ||||
| #[rustc_intrinsic] | ||||
| #[rustc_intrinsic_const_stable_indirect] | ||||
| pub const fn variant_count<T>() -> usize; | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't actually stable either, I think? |
||||
|
|
||||
| /// 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; | |||
| #[rustc_nounwind] | ||||
| #[unstable(feature = "core_intrinsics", issue = "none")] | ||||
| #[rustc_intrinsic] | ||||
| #[rustc_intrinsic_const_stable_indirect] | ||||
| pub const fn type_name<T: ?Sized>() -> &'static str; | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||
|
|
||||
| /// 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; | |||
| #[rustc_nounwind] | ||||
| #[unstable(feature = "core_intrinsics", issue = "none")] | ||||
| #[rustc_intrinsic] | ||||
| #[rustc_intrinsic_const_stable_indirect] | ||||
| pub const fn type_id<T: ?Sized + 'static>() -> crate::any::TypeId; | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And neither is this const-stable. |
||||
|
|
||||
| /// Tests (at compile-time) if two [`crate::any::TypeId`] instances identify the | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,13 @@ | |
| #![stable(feature = "rust1", since = "1.0.0")] | ||
|
|
||
| #[rustc_const_stable(feature = "foo", since = "3.3.3")] | ||
| //~^ ERROR macros cannot have const stability attributes | ||
| //~^ ERROR attribute cannot be used on macro defs | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| macro_rules! foo { | ||
| () => {}; | ||
| } | ||
|
|
||
| #[rustc_const_unstable(feature = "bar", issue="none")] | ||
| //~^ ERROR macros cannot have const stability attributes | ||
| #[rustc_const_unstable(feature = "bar", issue = "none")] | ||
| //~^ ERROR attribute cannot be used on macro defs | ||
| macro_rules! bar { | ||
| () => {}; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,18 @@ | ||
| error: macros cannot have const stability attributes | ||
| error: `#[rustc_const_stable]` attribute cannot be used on macro defs | ||
| --> $DIR/const-stability-on-macro.rs:4:1 | ||
| | | ||
| LL | #[rustc_const_stable(feature = "foo", since = "3.3.3")] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid const stability attribute | ||
| LL | | ||
| LL | macro_rules! foo { | ||
| | ---------------- const stability attribute affects this macro | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: `#[rustc_const_stable]` can be applied to associated consts, constants, crates, functions, impl blocks, statics, traits, and use statements | ||
|
|
||
| error: macros cannot have const stability attributes | ||
| error: `#[rustc_const_unstable]` attribute cannot be used on macro defs | ||
| --> $DIR/const-stability-on-macro.rs:10:1 | ||
| | | ||
| LL | #[rustc_const_unstable(feature = "bar", issue="none")] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid const stability attribute | ||
| LL | | ||
| LL | macro_rules! bar { | ||
| | ---------------- const stability attribute affects this macro | ||
| LL | #[rustc_const_unstable(feature = "bar", issue = "none")] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: `#[rustc_const_unstable]` can be applied to associated consts, constants, crates, functions, impl blocks, statics, traits, and use statements | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,5 +16,8 @@ const fn my_fun2() { | |
| } | ||
|
|
||
| fn main() { | ||
| const { my_fun2() }; | ||
| #[rustc_const_unstable(feature = "abi_unadjusted", issue = "42")] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| const { | ||
| my_fun2() | ||
| }; | ||
| } | ||
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