rust/library/core/src
Jacob Pratt 566c13c88e
Rollup merge of #145726 - aapoalas:reborrow-lang-experiment, r=petrochenkov
Experiment: Reborrow trait

Tracking issue: rust-lang/rust#145612

Starting off really small here: just introduce the unstable feature and the feature gate, and one of the two traits that the Reborrow experiment deals with.

### Cliff-notes explanation

The `Reborrow` trait is conceptually a close cousin of `Copy` with the exception that it disables the source (`self`) for the lifetime of the target / result of the reborrow action. It can be viewed as a method of `fn reborrow(self: Self<'a>) -> Self<'a>` with the compiler adding tracking of the resulting `Self<'a>` (or any value derived from it that retains the `'a` lifetime) to keep the `self` disabled for reads and writes.

No method is planned to be surfaced to the user, however, as reborrowing cannot be seen in code (except for method calls [`a.foo()` reborrows `a`] and explicit reborrows [`&*a`]) and thus triggering user-code in it could be viewed as "spooky action at a distance". Furthermore, the added compiler tracking cannot be seen on the method itself, violating the Golden Rule. Note that the userland "reborrow" method is not True Reborrowing, but rather a form of a "Fancy Deref":
```rust
fn reborrow(&'short self: Self<'long>) -> Self<'short>;
```
The lifetime shortening is the issue here: a reborrowed `Self` or any value derived from it is bound to the method that called `reborrow`, since `&'short` is effectively a local variable. True Reborrowing does not shorten the lifetime of the result.

To avoid having to introduce new kinds of references, new kinds of lifetime annotations, or a blessed trait method, no method will be introduced at all. Instead, the `Reborrow` trait is intended to be a derived trait that effectively reborrows each field individually; `Copy` fields end up just copying, while fields that themselves `Reborrow` get disabled in the source, usually leading to the source itself being disabled (some differences may appear with structs that contain multiple reborrowable fields). The goal of the experiment is to determine how the actual implementation here will shape out, and what the "bottom case" for the recursive / deriving `Reborrow` is.

`Reborrow` has a friend trait, `CoerceShared`, which is equivalent to a `&'a mut T -> &'a T` conversion. This is needed as a different trait and different operation due to the different semantics it enforces on the source: a `CoerceShared` operation only disables the source for writes / exclusive access for the lifetime of the result. That trait is not yet introduced in this PR, though there is no particular reason why it could not be introduced.
2025-08-22 22:00:55 -04:00
..
alloc Address dangling doc 2025-08-12 13:18:48 +01:00
array Rollup merge of #143383 - fee1-dead-contrib:push-mstmlwuskxyy, r=dtolnay 2025-08-21 01:12:13 -04:00
ascii Add std::ascii::Char MIN and MAX constants 2025-08-05 15:03:11 -04:00
async_iter clippy fix: markdown indentation for indented items after line break 2025-07-08 11:48:15 +00:00
bstr Replace colon with parentheses, add missing period 2025-04-21 10:48:46 -04:00
cell add poisoning documentation to LazyCell 2025-08-03 22:57:07 +02:00
char Optimize char::is_alphanumeric 2025-08-07 00:08:11 +01:00
clone CloneToUninit: use a private specialization trait 2024-07-29 20:44:43 +03:00
cmp tidy 2025-08-06 16:55:50 +01:00
convert tidy 2025-08-06 16:55:50 +01:00
ffi replace version placeholder 2025-08-06 13:22:38 +01:00
fmt Rollup merge of #143730 - pascaldekloe:fmt-radix-trim, r=tgross35 2025-08-19 19:42:03 +08:00
future Address documentation issues identified with Future 2025-06-07 20:13:54 +01:00
hash Remove some unsized tuple impls now that we don't support unsizing tuples anymore 2025-07-02 14:17:54 +00:00
intrinsics make prefetch intrinsics safe 2025-08-20 00:35:42 +02:00
io Rollup merge of #143829 - a1phyr:trim_borrowed_buf, r=ChrisDenton 2025-07-17 03:58:32 +02:00
iter Rollup merge of #144963 - rossmacarthur-forks:stabilize-core-iter-chain, r=jhpratt 2025-08-15 18:13:29 -04:00
macros Rollup merge of #144922 - Kobzol:derive-from, r=nnethercote 2025-08-15 18:13:28 -04:00
marker phantom_variance_markers: fix identifier usage in macro 2025-06-22 16:06:25 -07:00
mem Rollup merge of #144478 - joshtriplett:doc-code-formatting-prep, r=Amanieu 2025-08-02 11:24:24 +02:00
net Rollup merge of #141744 - GrigorenkoPV:ip_from, r=Amanieu 2025-08-19 14:18:15 +10:00
num Rollup merge of #145381 - Gnurou:int_lowest_highest_one, r=jhpratt 2025-08-20 00:45:56 -04:00
ops fix typo 2025-08-12 09:43:17 +02:00
panic corrected lifetime in core::panic::Location::file return type 2025-08-02 17:04:07 -04:00
pin UnsafePinned::raw_get: sync signature with get 2025-08-19 08:25:09 +02:00
prelude Remove the From derive macro from prelude 2025-08-18 13:12:19 +02:00
ptr Auto merge of #144081 - RalfJung:const-ptr-fragments, r=oli-obk 2025-08-17 04:33:31 +00:00
range Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
slice Consolidate panicking functions in slice/index.rs 2025-08-21 11:07:25 +01:00
str tidy 2025-08-06 16:55:50 +01:00
sync Rollup merge of #144192 - RalfJung:atomicrmw-ptr, r=nikic 2025-08-08 14:22:44 -05:00
task fix: typo 2025-08-20 22:03:26 +03:00
unicode Hide docs for core::unicode 2025-08-13 12:27:44 -04:00
any.rs Print regions in type_name. 2025-08-14 21:13:06 +10:00
arch.rs update version placeholders 2025-05-12 15:33:30 +02:00
ascii.rs add doc(alias("AsciiChar")) to core::ascii::Char 2025-06-20 11:11:51 -05:00
asserting.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
bool.rs Add methods for converting bool to Result<(), E> 2025-06-19 23:20:06 +02:00
borrow.rs clippy fix: rely on autoderef 2025-07-04 11:35:00 +00:00
cell.rs Rollup merge of #144054 - jsimmons:stabilize-as-array-of-cells, r=tgross35 2025-08-15 18:13:27 -04:00
clone.rs Rollup merge of #144330 - gewitternacht:document-clone-eq, r=Amanieu 2025-08-11 12:21:07 +10:00
cmp.rs Merge remote-tracking branch 'upstream/master' 2025-08-19 11:15:52 +02:00
contracts.rs Apply suggestions from code review 2025-04-10 16:32:56 -07:00
default.rs update issue number for const_trait_impl 2025-07-13 23:55:06 +08:00
error.md Mention core's PanicInfo in error.md. 2024-06-11 15:47:00 +02:00
error.rs clippy fix: indentation 2025-07-04 11:52:17 +00:00
escape.rs Get rid of EscapeDebugInner. 2025-06-15 22:08:41 +02:00
hint.rs Improve and regularize comment placement in doc code 2025-07-25 22:02:09 -07:00
internal_macros.rs Constify remaining operators 2025-08-10 01:11:45 -04: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 Introduce Reborrow lang item and trait 2025-08-21 23:53:25 +03:00
marker.rs Introduce Reborrow lang item and trait 2025-08-21 23:53:25 +03:00
option.rs Rollup merge of #142871 - chenyukang:yukang-fix-doc-for-transpose, r=ibraheemdev 2025-08-19 14:18:17 +10:00
panic.rs Revert "Partially outline code inside the panic! macro". 2025-08-12 12:52:39 +02:00
panicking.rs Revert "Partially outline code inside the panic! macro". 2025-08-12 12:52:39 +02:00
pat.rs update cfgs 2025-04-09 12:29:59 +01:00
pin.rs clippy fix: indentation 2025-07-04 11:52:17 +00:00
primitive.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
primitive_docs.rs Avoid placing // FIXME comments inside doc code blocks 2025-07-25 22:02:09 -07:00
random.rs random: Provide a Distribution<T> trait 2025-07-11 10:21:34 -07:00
range.rs Document (internally) that Range*<&T> as RangeBounds<T> impls are intentionally not T: ?Sized, and document (publically) an alternative. 2025-07-23 15:39:03 -05:00
result.rs tidy 2025-08-06 16:55:50 +01:00
time.rs Rollup merge of #143949 - clarfonthey:const-arith-ops, r=Amanieu 2025-08-11 12:21:06 +10:00
tuple.rs random: Provide a Distribution<T> trait 2025-07-11 10:21:34 -07:00
ub_checks.rs fix: don't panic on volatile access to null 2025-07-18 13:41:34 -03:00
unit.rs core: Make Debug impl of raw pointers print metadata if present 2025-02-15 17:27:55 +01:00
unsafe_binder.rs Add unwrap_unsafe_binder and wrap_unsafe_binder macro operators 2024-12-12 16:29:40 +00:00