rust/library/core/src
bors 54761cb3e8 Auto merge of #131349 - RalfJung:const-stability-checks, r=compiler-errors
Const stability checks v2

The const stability system has served us well ever since `const fn` were first stabilized. It's main feature is that it enforces *recursive* validity -- a stable const fn cannot internally make use of unstable const features without an explicit marker in the form of `#[rustc_allow_const_fn_unstable]`. This is done to make sure that we don't accidentally expose unstable const features on stable in a way that would be hard to take back. As part of this, it is enforced that a `#[rustc_const_stable]` can only call `#[rustc_const_stable]` functions. However, some problems have been coming up with increased usage:
- It is baffling that we have to mark private or even unstable functions as `#[rustc_const_stable]` when they are used as helpers in regular stable `const fn`, and often people will rather add `#[rustc_allow_const_fn_unstable]` instead which was not our intention.
- The system has several gaping holes: a private `const fn` without stability attributes whose inherited stability (walking up parent modules) is `#[stable]` is allowed to call *arbitrary* unstable const operations, but can itself be called from stable `const fn`. Similarly, `#[allow_internal_unstable]` on a macro completely bypasses the recursive nature of the check.

Fundamentally, the problem is that we have *three* disjoint categories of functions, and not enough attributes to distinguish them:
1. const-stable functions
2. private/unstable functions that are meant to be callable from const-stable functions
3. functions that can make use of unstable const features

Functions in the first two categories cannot use unstable const features and they can only call functions from the first two categories.

This PR implements the following system:
- `#[rustc_const_stable]` puts functions in the first category. It may only be applied to `#[stable]` functions.
- `#[rustc_const_unstable]` by default puts functions in the third category. The new attribute `#[rustc_const_stable_indirect]` can be added to such a function to move it into the second category.
- `const fn` without a const stability marker are in the second category if they are still unstable. They automatically inherit the feature gate for regular calls, it can now also be used for const-calls.

Also, all the holes mentioned above have been closed. There's still one potential hole that is hard to avoid, which is when MIR building automatically inserts calls to a particular function in stable functions -- which happens in the panic machinery. Those need to be manually marked `#[rustc_const_stable_indirect]` to be sure they follow recursive const stability. But that's a fairly rare and special case so IMO it's fine.

The net effect of this is that a `#[unstable]` or unmarked function can be constified simply by marking it as `const fn`, and it will then be const-callable from stable `const fn` and subject to recursive const stability requirements. If it is publicly reachable (which implies it cannot be unmarked), it will be const-unstable under the same feature gate. Only if the function ever becomes `#[stable]` does it need a `#[rustc_const_unstable]` or `#[rustc_const_stable]` marker to decide if this should also imply const-stability.

Adding `#[rustc_const_unstable]` is only needed for (a) functions that need to use unstable const lang features (including intrinsics), or (b) `#[stable]` functions that are not yet intended to be const-stable. Adding `#[rustc_const_stable]` is only needed for functions that are actually meant to be directly callable from stable const code. `#[rustc_const_stable_indirect]` is used to mark intrinsics as const-callable and for `#[rustc_const_unstable]` functions that are actually called from other, exposed-on-stable `const fn`. No other attributes are required.

Also see the updated dev-guide at https://github.com/rust-lang/rustc-dev-guide/pull/2098.

I think in the future we may want to tweak this further, so that in the hopefully common case where a public function's const-stability just exactly mirrors its regular stability, we never have to add any attribute. But right now, once the function is stable this requires `#[rustc_const_stable]`.

### Open question

There is one point I could see we might want to do differently, and that is putting `#[rustc_const_unstable]`  functions (but not intrinsics) in category 2 by default, and requiring an extra attribute for `#[rustc_const_not_exposed_on_stable]` or so. This would require a bunch of extra annotations, but would have the advantage that turning a `#[rustc_const_unstable]` into `#[rustc_const_stable]`  will never change the way the function is const-checked. Currently, we often discover in the const stabilization PR that a function needs some other unstable const things, and then we rush to quickly deal with that. In this alternative universe, we'd work towards getting rid of the `rustc_const_not_exposed_on_stable` before stabilization, and once that is done stabilization becomes a trivial matter. `#[rustc_const_stable_indirect]` would then only be used for intrinsics.

I think I like this idea, but might want to do it in a follow-up PR, as it will need a whole bunch of annotations in the standard library. Also, we probably want to convert all const intrinsics to the "new" form (`#[rustc_intrinsic]` instead of an `extern` block) before doing this to avoid having to deal with two different ways of declaring intrinsics.

Cc `@rust-lang/wg-const-eval` `@rust-lang/libs-api`
Part of https://github.com/rust-lang/rust/issues/129815 (but not finished since this is not yet sufficient to safely let us expose `const fn` from hashbrown)
Fixes https://github.com/rust-lang/rust/issues/131073 by making it so that const-stable functions are always stable

try-job: test-various
2024-10-25 23:29:40 +00:00
..
alloc Auto merge of #131349 - RalfJung:const-stability-checks, r=compiler-errors 2024-10-25 23:29:40 +00:00
array update bootstrap configs 2024-10-15 20:30:23 -07:00
ascii Add more precondition check tests 2024-10-09 19:34:27 -04:00
async_iter Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
cell Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
char Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
clone CloneToUninit: use a private specialization trait 2024-07-29 20:44:43 +03:00
cmp Use generic NonZero everywhere in core. 2024-02-22 15:17:33 +01:00
convert Fix doc nits 2024-07-26 13:26:33 +01:00
ffi Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
fmt Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
future Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
hash Reformat use declarations. 2024-07-29 08:26:52 +10:00
intrinsics library: consistently use American spelling for 'behavior' 2024-10-25 12:02:47 +02:00
io Fix doc nits 2024-07-26 13:26:33 +01:00
iter Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
macros Missing parenthesis 2024-10-18 01:04:01 +05:00
mem library: consistently use American spelling for 'behavior' 2024-10-25 12:02:47 +02:00
net Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
num Auto merge of #131349 - RalfJung:const-stability-checks, r=compiler-errors 2024-10-25 23:29:40 +00:00
ops library: consistently use American spelling for 'behavior' 2024-10-25 12:02:47 +02:00
panic Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
prelude Avoid comments that describe multiple use items. 2024-07-17 08:02:46 +10:00
ptr Auto merge of #131349 - RalfJung:const-stability-checks, r=compiler-errors 2024-10-25 23:29:40 +00:00
range Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
slice Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
str library: consistently use American spelling for 'behavior' 2024-10-25 12:02:47 +02:00
sync Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
task Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
unicode switch unicode-data back to 'static' 2024-10-13 11:53:06 +02:00
any.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
arch.rs update bootstrap configs 2024-10-15 20:30:23 -07:00
ascii.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
asserting.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
bool.rs [Clippy] Swap filter_map_bool_then to use diagnostic item instead of path 2024-09-19 13:13:42 +01:00
borrow.rs Suggest borrowing on fn argument that is impl AsRef 2024-05-09 23:25:31 +00:00
cell.rs Auto merge of #131349 - RalfJung:const-stability-checks, r=compiler-errors 2024-10-25 23:29:40 +00:00
clone.rs update cfgs 2024-09-05 17:24:01 +01:00
cmp.rs library: consistently use American spelling for 'behavior' 2024-10-25 12:02:47 +02:00
default.rs update cfgs 2024-09-05 17:24:01 +01:00
error.md Mention core's PanicInfo in error.md. 2024-06-11 15:47:00 +02:00
error.rs Library: Rename "object safe" to "dyn compatible" 2024-10-09 18:48:29 +02:00
escape.rs Optimize escape_ascii 2024-10-09 17:17:50 -04:00
hint.rs Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
internal_macros.rs Fix doc nits 2024-07-26 13:26:33 +01:00
intrinsics.rs Auto merge of #131349 - RalfJung:const-stability-checks, r=compiler-errors 2024-10-25 23:29:40 +00:00
lib.miri.rs add 'x.py miri', and make it work for 'library/{core,alloc,std}' 2024-04-03 20:27:20 +02:00
lib.rs Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
marker.rs Remove associated type based effects logic 2024-10-24 09:46:36 +00:00
option.rs library: consistently use American spelling for 'behavior' 2024-10-25 12:02:47 +02:00
panic.rs simplify abort_unwind 2024-09-15 14:27:24 -04:00
panicking.rs Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
pat.rs Add tracking issue to core-pattern-type 2024-08-07 20:43:05 -04:00
pin.rs Rename Receiver -> LegacyReceiver 2024-10-22 12:55:16 +00:00
primitive.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
primitive_docs.rs library: consistently use American spelling for 'behavior' 2024-10-25 12:02:47 +02:00
random.rs random: add tracking issue, address other comments 2024-09-23 10:36:16 +02:00
range.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
result.rs update bootstrap configs 2024-10-15 20:30:23 -07:00
time.rs replace placeholder version 2024-10-15 20:13:55 -07:00
tuple.rs update cfgs 2024-09-05 17:24:01 +01:00
ub_checks.rs Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
unit.rs Import the 2021 prelude in the core crate 2024-03-25 13:12:06 -07:00