Skip to content

Commit 1ee5e12

Browse files
authored
Rollup merge of rust-lang#117534 - RalfJung:str, r=Mark-Simulacrum
clarify that the str invariant is a safety, not validity, invariant Updates these docs to match rust-lang/reference#792
2 parents 805a56f + 0550ba5 commit 1ee5e12

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

library/core/src/primitive_docs.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ mod prim_never {}
291291
/// Surrogate code points, used by UTF-16, are in the range 0xD800 to 0xDFFF.
292292
///
293293
/// No `char` may be constructed, whether as a literal or at runtime, that is not a
294-
/// Unicode scalar value:
294+
/// Unicode scalar value. Violating this rule causes undefined behavior.
295295
///
296296
/// ```compile_fail
297297
/// // Each of these is a compiler error
@@ -308,9 +308,10 @@ mod prim_never {}
308308
/// let _ = unsafe { char::from_u32_unchecked(0x110000) };
309309
/// ```
310310
///
311-
/// USVs are also the exact set of values that may be encoded in UTF-8. Because
312-
/// `char` values are USVs and `str` values are valid UTF-8, it is safe to store
313-
/// any `char` in a `str` or read any character from a `str` as a `char`.
311+
/// Unicode scalar values are also the exact set of values that may be encoded in UTF-8. Because
312+
/// `char` values are Unicode scalar values and functions may assume [incoming `str` values are
313+
/// valid UTF-8](primitive.str.html#invariant), it is safe to store any `char` in a `str` or read
314+
/// any character from a `str` as a `char`.
314315
///
315316
/// The gap in valid `char` values is understood by the compiler, so in the
316317
/// below example the two ranges are understood to cover the whole range of
@@ -324,11 +325,10 @@ mod prim_never {}
324325
/// };
325326
/// ```
326327
///
327-
/// All USVs are valid `char` values, but not all of them represent a real
328-
/// character. Many USVs are not currently assigned to a character, but may be
329-
/// in the future ("reserved"); some will never be a character
330-
/// ("noncharacters"); and some may be given different meanings by different
331-
/// users ("private use").
328+
/// All Unicode scalar values are valid `char` values, but not all of them represent a real
329+
/// character. Many Unicode scalar values are not currently assigned to a character, but may be in
330+
/// the future ("reserved"); some will never be a character ("noncharacters"); and some may be given
331+
/// different meanings by different users ("private use").
332332
///
333333
/// `char` is guaranteed to have the same size and alignment as `u32` on all
334334
/// platforms.
@@ -894,8 +894,6 @@ mod prim_slice {}
894894
/// type. It is usually seen in its borrowed form, `&str`. It is also the type
895895
/// of string literals, `&'static str`.
896896
///
897-
/// String slices are always valid UTF-8.
898-
///
899897
/// # Basic Usage
900898
///
901899
/// String literals are string slices:
@@ -949,6 +947,14 @@ mod prim_slice {}
949947
/// Note: This example shows the internals of `&str`. `unsafe` should not be
950948
/// used to get a string slice under normal circumstances. Use `as_str`
951949
/// instead.
950+
///
951+
/// # Invariant
952+
///
953+
/// Rust libraries may assume that string slices are always valid UTF-8.
954+
///
955+
/// Constructing a non-UTF-8 string slice is not immediate undefined behavior, but any function
956+
/// called on a string slice may assume that it is valid UTF-8, which means that a non-UTF-8 string
957+
/// slice can lead to undefined behavior down the road.
952958
#[stable(feature = "rust1", since = "1.0.0")]
953959
mod prim_str {}
954960

0 commit comments

Comments
 (0)