rust/compiler/rustc_middle/src
Matthias Krüger 20b1dadf92
Rollup merge of #130350 - RalfJung:strict-provenance, r=dtolnay
stabilize Strict Provenance and Exposed Provenance APIs

Given that [RFC 3559](https://rust-lang.github.io/rfcs/3559-rust-has-provenance.html) has been accepted, t-lang has approved the concept of provenance to exist in the language. So I think it's time that we stabilize the strict provenance and exposed provenance APIs, and discuss provenance explicitly in the docs:
```rust
// core::ptr
pub const fn without_provenance<T>(addr: usize) -> *const T;
pub const fn dangling<T>() -> *const T;
pub const fn without_provenance_mut<T>(addr: usize) -> *mut T;
pub const fn dangling_mut<T>() -> *mut T;
pub fn with_exposed_provenance<T>(addr: usize) -> *const T;
pub fn with_exposed_provenance_mut<T>(addr: usize) -> *mut T;

impl<T: ?Sized> *const T {
    pub fn addr(self) -> usize;
    pub fn expose_provenance(self) -> usize;
    pub fn with_addr(self, addr: usize) -> Self;
    pub fn map_addr(self, f: impl FnOnce(usize) -> usize) -> Self;
}

impl<T: ?Sized> *mut T {
    pub fn addr(self) -> usize;
    pub fn expose_provenance(self) -> usize;
    pub fn with_addr(self, addr: usize) -> Self;
    pub fn map_addr(self, f: impl FnOnce(usize) -> usize) -> Self;
}

impl<T: ?Sized> NonNull<T> {
    pub fn addr(self) -> NonZero<usize>;
    pub fn with_addr(self, addr: NonZero<usize>) -> Self;
    pub fn map_addr(self, f: impl FnOnce(NonZero<usize>) -> NonZero<usize>) -> Self;
}
```

I also did a pass over the docs to adjust them, because this is no longer an "experiment". The `ptr` docs now discuss the concept of provenance in general, and then they go into the two families of APIs for dealing with provenance: Strict Provenance and Exposed Provenance. I removed the discussion of how pointers also have an associated "address space" -- that is not actually tracked in the pointer value, it is tracked in the type, so IMO it just distracts from the core point of provenance. I also adjusted the docs for `with_exposed_provenance` to make it clear that we cannot guarantee much about this function, it's all best-effort.

There are two unstable lints associated with the strict_provenance feature gate; I moved them to a new [strict_provenance_lints](https://github.com/rust-lang/rust/issues/130351) feature since I didn't want this PR to have an even bigger FCP. ;)

`@rust-lang/opsem` Would be great to get some feedback on the docs here. :)
Nominating for `@rust-lang/libs-api.`

Part of https://github.com/rust-lang/rust/issues/95228.

[FCP comment](https://github.com/rust-lang/rust/pull/130350#issuecomment-2395114536)
2024-10-21 18:11:19 +02:00
..
dep_graph Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
hir rm ItemKind::OpaqueTy 2024-10-04 23:28:22 +00:00
hooks Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
infer move defining_opaque_types out of Canonical 2024-10-17 10:22:52 +02:00
middle Simplify bound var resolution. 2024-10-04 23:44:27 +00:00
mir Auto merge of #130950 - compiler-errors:yeet-eval, r=BoxyUwU 2024-10-21 03:46:28 +00:00
query Rollup merge of #131802 - compiler-errors:fnonce-coverage, r=Zalathar 2024-10-18 12:00:51 +01:00
thir various fixes for naked_asm! implementation 2024-10-06 19:00:09 +02:00
traits Rip out old effects var handling code from traits 2024-10-20 13:40:22 +00:00
ty Auto merge of #131988 - matthiaskrgr:rollup-tx173wn, r=matthiaskrgr 2024-10-21 06:13:34 +00:00
util Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
arena.rs Compiler: Rename "object safe" to "dyn compatible" 2024-09-25 13:26:48 +02:00
error.rs Use bool in favor of Option<()> for diagnostics 2024-08-21 01:31:11 -04:00
lib.rs move strict provenance lints to new feature gate, remove old feature gates 2024-10-21 15:22:17 +01:00
lint.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
macros.rs Improve const traits diagnostics for new desugaring 2024-10-02 19:45:17 +08:00
metadata.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
tests.rs Give an item related to issue 27438 a more meaningful name 2024-04-30 22:27:19 +02:00
thir.rs various fixes for naked_asm! implementation 2024-10-06 19:00:09 +02:00
values.rs only query params_in_repr if def kind is adt 2024-10-02 17:36:31 +08:00