Skip to content

Commit 20b1dad

Browse files
authored
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](#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 #95228. [FCP comment](#130350 (comment))
2 parents 3ec4308 + 56ee492 commit 20b1dad

File tree

90 files changed

+352
-514
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+352
-514
lines changed

compiler/rustc_arena/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#![feature(maybe_uninit_slice)]
2424
#![feature(rustc_attrs)]
2525
#![feature(rustdoc_internals)]
26-
#![feature(strict_provenance)]
2726
#![warn(unreachable_pub)]
2827
// tidy-alphabetical-end
2928

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#![feature(let_chains)]
1212
#![feature(negative_impls)]
1313
#![feature(rustdoc_internals)]
14-
#![feature(strict_provenance)]
1514
#![feature(trait_alias)]
1615
#![feature(try_blocks)]
1716
#![warn(unreachable_pub)]

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
361361
(Int(..) | Float(_), Int(..) | Float(_)) => bx.bitcast(imm, to_backend_ty),
362362
(Pointer(..), Pointer(..)) => bx.pointercast(imm, to_backend_ty),
363363
(Int(..), Pointer(..)) => bx.ptradd(bx.const_null(bx.type_ptr()), imm),
364-
(Pointer(..), Int(..)) => bx.ptrtoint(imm, to_backend_ty),
364+
(Pointer(..), Int(..)) => {
365+
// FIXME: this exposes the provenance, which shouldn't be necessary.
366+
bx.ptrtoint(imm, to_backend_ty)
367+
}
365368
(Float(_), Pointer(..)) => {
366369
let int_imm = bx.bitcast(imm, bx.cx().type_isize());
367370
bx.ptradd(bx.const_null(bx.type_ptr()), int_imm)
368371
}
369372
(Pointer(..), Float(_)) => {
373+
// FIXME: this exposes the provenance, which shouldn't be necessary.
370374
let int_imm = bx.ptrtoint(imm, bx.cx().type_isize());
371375
bx.bitcast(int_imm, to_backend_ty)
372376
}

compiler/rustc_const_eval/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#![feature(never_type)]
1111
#![feature(rustdoc_internals)]
1212
#![feature(slice_ptr_get)]
13-
#![feature(strict_provenance)]
1413
#![feature(trait_alias)]
1514
#![feature(try_blocks)]
1615
#![feature(unqualified_local_imports)]

compiler/rustc_data_structures/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#![feature(ptr_alignment_type)]
3434
#![feature(rustc_attrs)]
3535
#![feature(rustdoc_internals)]
36-
#![feature(strict_provenance)]
3736
#![feature(test)]
3837
#![feature(thread_id_value)]
3938
#![feature(type_alias_impl_trait)]

compiler/rustc_feature/src/unstable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ declare_features! (
595595
/// Allows attributes on expressions and non-item statements.
596596
(unstable, stmt_expr_attributes, "1.6.0", Some(15701)),
597597
/// Allows lints part of the strict provenance effort.
598-
(unstable, strict_provenance, "1.61.0", Some(95228)),
598+
(unstable, strict_provenance_lints, "1.61.0", Some(130351)),
599599
/// Allows string patterns to dereference values to match them.
600600
(unstable, string_deref_patterns, "1.67.0", Some(87121)),
601601
/// Allows the use of `#[target_feature]` on safe functions.

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,7 +2667,6 @@ declare_lint! {
26672667
/// ### Example
26682668
///
26692669
/// ```rust
2670-
/// #![feature(strict_provenance)]
26712670
/// #![warn(fuzzy_provenance_casts)]
26722671
///
26732672
/// fn main() {
@@ -2701,7 +2700,7 @@ declare_lint! {
27012700
pub FUZZY_PROVENANCE_CASTS,
27022701
Allow,
27032702
"a fuzzy integer to pointer cast is used",
2704-
@feature_gate = strict_provenance;
2703+
@feature_gate = strict_provenance_lints;
27052704
}
27062705

27072706
declare_lint! {
@@ -2711,7 +2710,6 @@ declare_lint! {
27112710
/// ### Example
27122711
///
27132712
/// ```rust
2714-
/// #![feature(strict_provenance)]
27152713
/// #![warn(lossy_provenance_casts)]
27162714
///
27172715
/// fn main() {
@@ -2747,7 +2745,7 @@ declare_lint! {
27472745
pub LOSSY_PROVENANCE_CASTS,
27482746
Allow,
27492747
"a lossy pointer to integer cast is used",
2750-
@feature_gate = strict_provenance;
2748+
@feature_gate = strict_provenance_lints;
27512749
}
27522750

27532751
declare_lint! {

compiler/rustc_middle/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
#![feature(ptr_alignment_type)]
5757
#![feature(rustc_attrs)]
5858
#![feature(rustdoc_internals)]
59-
#![feature(strict_provenance)]
6059
#![feature(trait_upcasting)]
6160
#![feature(trusted_len)]
6261
#![feature(try_blocks)]

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1913,7 +1913,7 @@ symbols! {
19131913
str_trim,
19141914
str_trim_end,
19151915
str_trim_start,
1916-
strict_provenance,
1916+
strict_provenance_lints,
19171917
string_as_mut_str,
19181918
string_as_str,
19191919
string_deref_patterns,

library/alloc/benches/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#![feature(iter_next_chunk)]
55
#![feature(repr_simd)]
66
#![feature(slice_partition_dedup)]
7-
#![feature(strict_provenance)]
7+
#![cfg_attr(bootstrap, feature(strict_provenance))]
8+
#![cfg_attr(not(bootstrap), feature(strict_provenance_lints))]
89
#![feature(test)]
910
#![deny(fuzzy_provenance_casts)]
1011

0 commit comments

Comments
 (0)