Skip to content

Commit ee9a149

Browse files
authored
Rollup merge of rust-lang#95895 - CAD97:patch-2, r=Dylan-DPC
Clarify str::from_utf8_unchecked's invariants Specifically, make it clear that it is immediately UB to pass ill-formed UTF-8 into the function. The previous wording left space to interpret that the UB only occurred when calling another function, which "assumes that `&str`s are valid UTF-8." This does not change whether str being UTF-8 is a safety or a validity invariant. (As per previous discussion, it is a safety invariant, not a validity invariant.) It just makes it clear that valid UTF-8 is a precondition of str::from_utf8_unchecked, and that emitting an Abstract Machine fault (e.g. UB or a sanitizer error) on invalid UTF-8 is a valid thing to do. If user code wants to create an unsafe `&str` pointing to ill-formed UTF-8, it must be done via transmutes. Also, just, don't. Zulip discussion: https://rust-lang.zulipchat.com/#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/str.3A.3Afrom_utf8_unchecked.20Safety.20requirement
2 parents e08c70a + b92cd1a commit ee9a149

File tree

1 file changed

+1
-5
lines changed

1 file changed

+1
-5
lines changed

library/core/src/str/converts.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,7 @@ pub const fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
144144
///
145145
/// # Safety
146146
///
147-
/// This function is unsafe because it does not check that the bytes passed to
148-
/// it are valid UTF-8. If this constraint is violated, undefined behavior
149-
/// results, as the rest of Rust assumes that [`&str`]s are valid UTF-8.
150-
///
151-
/// [`&str`]: str
147+
/// The bytes passed in must be valid UTF-8.
152148
///
153149
/// # Examples
154150
///

0 commit comments

Comments
 (0)