Skip to content

Commit 7d1457e

Browse files
committed
Auto merge of rust-lang#131723 - matthiaskrgr:rollup-krcslig, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - rust-lang#122670 (Fix bug where `option_env!` would return `None` when env var is present but not valid Unicode) - rust-lang#131095 (Use environment variables instead of command line arguments for merged doctests) - rust-lang#131339 (Expand set_ptr_value / with_metadata_of docs) - rust-lang#131652 (Move polarity into `PolyTraitRef` rather than storing it on the side) - rust-lang#131675 (Update lint message for ABI not supported) - rust-lang#131681 (Fix up-to-date checking for run-make tests) - rust-lang#131702 (Suppress import errors for traits that couldve applied for method lookup error) - rust-lang#131703 (Resolved python deprecation warning in publish_toolstate.py) - rust-lang#131710 (Remove `'apostrophes'` from `rustc_parse_format`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 76342d9 + f1ee2cd commit 7d1457e

File tree

3 files changed

+71
-28
lines changed

3 files changed

+71
-28
lines changed

Diff for: core/src/macros/mod.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1107,17 +1107,19 @@ pub(crate) mod builtin {
11071107
///
11081108
/// If the named environment variable is present at compile time, this will
11091109
/// expand into an expression of type `Option<&'static str>` whose value is
1110-
/// `Some` of the value of the environment variable. If the environment
1111-
/// variable is not present, then this will expand to `None`. See
1112-
/// [`Option<T>`][Option] for more information on this type. Use
1113-
/// [`std::env::var`] instead if you want to read the value at runtime.
1110+
/// `Some` of the value of the environment variable (a compilation error
1111+
/// will be emitted if the environment variable is not a valid Unicode
1112+
/// string). If the environment variable is not present, then this will
1113+
/// expand to `None`. See [`Option<T>`][Option] for more information on this
1114+
/// type. Use [`std::env::var`] instead if you want to read the value at
1115+
/// runtime.
11141116
///
11151117
/// [`std::env::var`]: ../std/env/fn.var.html
11161118
///
1117-
/// A compile time error is never emitted when using this macro regardless
1118-
/// of whether the environment variable is present or not.
1119-
/// To emit a compile error if the environment variable is not present,
1120-
/// use the [`env!`] macro instead.
1119+
/// A compile time error is only emitted when using this macro if the
1120+
/// environment variable exists and is not a valid Unicode string. To also
1121+
/// emit a compile error if the environment variable is not present, use the
1122+
/// [`env!`] macro instead.
11211123
///
11221124
/// # Examples
11231125
///

Diff for: core/src/ptr/const_ptr.rs

+31-10
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,22 @@ impl<T: ?Sized> *const T {
6363
self as _
6464
}
6565

66-
/// Uses the pointer value in a new pointer of another type.
66+
/// Uses the address value in a new pointer of another type.
6767
///
68-
/// In case `meta` is a (fat) pointer to an unsized type, this operation
69-
/// will ignore the pointer part, whereas for (thin) pointers to sized
70-
/// types, this has the same effect as a simple cast.
68+
/// This operation will ignore the address part of its `meta` operand and discard existing
69+
/// metadata of `self`. For pointers to a sized types (thin pointers), this has the same effect
70+
/// as a simple cast. For pointers to an unsized type (fat pointers) this recombines the address
71+
/// with new metadata such as slice lengths or `dyn`-vtable.
7172
///
72-
/// The resulting pointer will have provenance of `self`, i.e., for a fat
73-
/// pointer, this operation is semantically the same as creating a new
74-
/// fat pointer with the data pointer value of `self` but the metadata of
75-
/// `meta`.
73+
/// The resulting pointer will have provenance of `self`. This operation is semantically the
74+
/// same as creating a new pointer with the data pointer value of `self` but the metadata of
75+
/// `meta`, being fat or thin depending on the `meta` operand.
7676
///
7777
/// # Examples
7878
///
79-
/// This function is primarily useful for allowing byte-wise pointer
80-
/// arithmetic on potentially fat pointers:
79+
/// This function is primarily useful for enabling pointer arithmetic on potentially fat
80+
/// pointers. The pointer is cast to a sized pointee to utilize offset operations and then
81+
/// recombined with its own original metadata.
8182
///
8283
/// ```
8384
/// #![feature(set_ptr_value)]
@@ -91,6 +92,26 @@ impl<T: ?Sized> *const T {
9192
/// println!("{:?}", &*ptr); // will print "3"
9293
/// }
9394
/// ```
95+
///
96+
/// # *Incorrect* usage
97+
///
98+
/// The provenance from pointers is *not* combined. The result must only be used to refer to the
99+
/// address allowed by `self`.
100+
///
101+
/// ```rust,no_run
102+
/// #![feature(set_ptr_value)]
103+
/// let x = 0u32;
104+
/// let y = 1u32;
105+
///
106+
/// let x = (&x) as *const u32;
107+
/// let y = (&y) as *const u32;
108+
///
109+
/// let offset = (x as usize - y as usize) / 4;
110+
/// let bad = x.wrapping_add(offset).with_metadata_of(y);
111+
///
112+
/// // This dereference is UB. The pointer only has provenance for `x` but points to `y`.
113+
/// println!("{:?}", unsafe { &*bad });
114+
/// ```
94115
#[unstable(feature = "set_ptr_value", issue = "75091")]
95116
#[rustc_const_stable(feature = "ptr_metadata_const", since = "CURRENT_RUSTC_VERSION")]
96117
#[must_use = "returns a new pointer rather than modifying its argument"]

Diff for: core/src/ptr/mut_ptr.rs

+30-10
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,22 @@ impl<T: ?Sized> *mut T {
4545
self as _
4646
}
4747

48-
/// Uses the pointer value in a new pointer of another type.
48+
/// Uses the address value in a new pointer of another type.
4949
///
50-
/// In case `meta` is a (fat) pointer to an unsized type, this operation
51-
/// will ignore the pointer part, whereas for (thin) pointers to sized
52-
/// types, this has the same effect as a simple cast.
50+
/// This operation will ignore the address part of its `meta` operand and discard existing
51+
/// metadata of `self`. For pointers to a sized types (thin pointers), this has the same effect
52+
/// as a simple cast. For pointers to an unsized type (fat pointers) this recombines the address
53+
/// with new metadata such as slice lengths or `dyn`-vtable.
5354
///
54-
/// The resulting pointer will have provenance of `self`, i.e., for a fat
55-
/// pointer, this operation is semantically the same as creating a new
56-
/// fat pointer with the data pointer value of `self` but the metadata of
57-
/// `meta`.
55+
/// The resulting pointer will have provenance of `self`. This operation is semantically the
56+
/// same as creating a new pointer with the data pointer value of `self` but the metadata of
57+
/// `meta`, being fat or thin depending on the `meta` operand.
5858
///
5959
/// # Examples
6060
///
61-
/// This function is primarily useful for allowing byte-wise pointer
62-
/// arithmetic on potentially fat pointers:
61+
/// This function is primarily useful for enabling pointer arithmetic on potentially fat
62+
/// pointers. The pointer is cast to a sized pointee to utilize offset operations and then
63+
/// recombined with its own original metadata.
6364
///
6465
/// ```
6566
/// #![feature(set_ptr_value)]
@@ -73,6 +74,25 @@ impl<T: ?Sized> *mut T {
7374
/// println!("{:?}", &*ptr); // will print "3"
7475
/// }
7576
/// ```
77+
///
78+
/// # *Incorrect* usage
79+
///
80+
/// The provenance from pointers is *not* combined. The result must only be used to refer to the
81+
/// address allowed by `self`.
82+
///
83+
/// ```rust,no_run
84+
/// #![feature(set_ptr_value)]
85+
/// let mut x = 0u32;
86+
/// let mut y = 1u32;
87+
///
88+
/// let x = (&mut x) as *mut u32;
89+
/// let y = (&mut y) as *mut u32;
90+
///
91+
/// let offset = (x as usize - y as usize) / 4;
92+
/// let bad = x.wrapping_add(offset).with_metadata_of(y);
93+
///
94+
/// // This dereference is UB. The pointer only has provenance for `x` but points to `y`.
95+
/// println!("{:?}", unsafe { &*bad });
7696
#[unstable(feature = "set_ptr_value", issue = "75091")]
7797
#[rustc_const_stable(feature = "ptr_metadata_const", since = "CURRENT_RUSTC_VERSION")]
7898
#[must_use = "returns a new pointer rather than modifying its argument"]

0 commit comments

Comments
 (0)