Skip to content

Commit 7f442f8

Browse files
committed
Auto merge of rust-lang#101220 - JohnTitor:rollup-ov7upr7, r=JohnTitor
Rollup of 10 pull requests Successful merges: - rust-lang#100804 (Fix search results color on hover for ayu theme) - rust-lang#100892 (Add `AsFd` implementations for stdio types on WASI.) - rust-lang#100927 (Adding new Fuchsia rustup docs... reworking walkthrough) - rust-lang#101088 (Set DebuginfoKind::Pdb in msvc_base) - rust-lang#101159 (add tracking issue number to const_slice_split_at_not_mut) - rust-lang#101192 (Remove path string) - rust-lang#101193 (Avoid zeroing large stack buffers in stdio on Windows) - rust-lang#101197 (:arrow_up: rust-analyzer) - rust-lang#101200 (Add test for issue rust-lang#85872) - rust-lang#101219 (Update books) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents f07d6e8 + 0cbc589 commit 7f442f8

File tree

88 files changed

+2576
-1001
lines changed

Some content is hidden

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

88 files changed

+2576
-1001
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -2055,22 +2055,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20552055
(exp_found.expected.kind(), exp_found.found.kind())
20562056
{
20572057
if let ty::Adt(found_def, found_substs) = *found_ty.kind() {
2058-
let path_str = format!("{:?}", exp_def);
20592058
if exp_def == &found_def {
2060-
let opt_msg = "you can convert from `&Option<T>` to `Option<&T>` using \
2061-
`.as_ref()`";
2062-
let result_msg = "you can convert from `&Result<T, E>` to \
2063-
`Result<&T, &E>` using `.as_ref()`";
20642059
let have_as_ref = &[
2065-
("std::option::Option", opt_msg),
2066-
("core::option::Option", opt_msg),
2067-
("std::result::Result", result_msg),
2068-
("core::result::Result", result_msg),
2060+
(
2061+
sym::Option,
2062+
"you can convert from `&Option<T>` to `Option<&T>` using \
2063+
`.as_ref()`",
2064+
),
2065+
(
2066+
sym::Result,
2067+
"you can convert from `&Result<T, E>` to \
2068+
`Result<&T, &E>` using `.as_ref()`",
2069+
),
20692070
];
2070-
if let Some(msg) = have_as_ref
2071-
.iter()
2072-
.find_map(|(path, msg)| (&path_str == path).then_some(msg))
2073-
{
2071+
if let Some(msg) = have_as_ref.iter().find_map(|(name, msg)| {
2072+
self.tcx.is_diagnostic_item(*name, exp_def.did()).then_some(msg)
2073+
}) {
20742074
let mut show_suggestion = true;
20752075
for (exp_ty, found_ty) in
20762076
iter::zip(exp_substs.types(), found_substs.types())

compiler/rustc_target/src/spec/msvc_base.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::spec::{LinkerFlavor, LldFlavor, SplitDebuginfo, TargetOptions};
1+
use crate::spec::{DebuginfoKind, LinkerFlavor, LldFlavor, SplitDebuginfo, TargetOptions};
22
use std::borrow::Cow;
33

44
pub fn opts() -> TargetOptions {
@@ -20,6 +20,7 @@ pub fn opts() -> TargetOptions {
2020
// where `*.pdb` files show up next to the final artifact.
2121
split_debuginfo: SplitDebuginfo::Packed,
2222
supported_split_debuginfo: Cow::Borrowed(&[SplitDebuginfo::Packed]),
23+
debuginfo_kind: DebuginfoKind::Pdb,
2324

2425
..Default::default()
2526
}

compiler/rustc_target/src/spec/windows_msvc_base.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::spec::{cvs, DebuginfoKind, TargetOptions};
1+
use crate::spec::{cvs, TargetOptions};
22

33
pub fn opts() -> TargetOptions {
44
let base = super::msvc_base::opts();
@@ -28,7 +28,6 @@ pub fn opts() -> TargetOptions {
2828
// not ever be possible for us to pass this flag.
2929
no_default_libraries: false,
3030
has_thread_local: true,
31-
debuginfo_kind: DebuginfoKind::Pdb,
3231

3332
..base
3433
}

library/core/src/slice/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ impl<T> [T] {
15411541
/// }
15421542
/// ```
15431543
#[stable(feature = "rust1", since = "1.0.0")]
1544-
#[rustc_const_unstable(feature = "const_slice_split_at_not_mut", issue = "none")]
1544+
#[rustc_const_unstable(feature = "const_slice_split_at_not_mut", issue = "101158")]
15451545
#[inline]
15461546
#[track_caller]
15471547
#[must_use]

library/std/src/sys/wasi/stdio.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::fd::WasiFd;
44
use crate::io::{self, IoSlice, IoSliceMut};
55
use crate::mem::ManuallyDrop;
66
use crate::os::raw;
7-
use crate::os::wasi::io::{AsRawFd, FromRawFd};
7+
use crate::os::wasi::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd};
88

99
pub struct Stdin;
1010
pub struct Stdout;
@@ -23,6 +23,13 @@ impl AsRawFd for Stdin {
2323
}
2424
}
2525

26+
impl AsFd for Stdin {
27+
#[inline]
28+
fn as_fd(&self) -> BorrowedFd<'_> {
29+
unsafe { BorrowedFd::borrow_raw(0) }
30+
}
31+
}
32+
2633
impl io::Read for Stdin {
2734
fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
2835
self.read_vectored(&mut [IoSliceMut::new(data)])
@@ -51,6 +58,13 @@ impl AsRawFd for Stdout {
5158
}
5259
}
5360

61+
impl AsFd for Stdout {
62+
#[inline]
63+
fn as_fd(&self) -> BorrowedFd<'_> {
64+
unsafe { BorrowedFd::borrow_raw(1) }
65+
}
66+
}
67+
5468
impl io::Write for Stdout {
5569
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
5670
self.write_vectored(&[IoSlice::new(data)])
@@ -82,6 +96,13 @@ impl AsRawFd for Stderr {
8296
}
8397
}
8498

99+
impl AsFd for Stderr {
100+
#[inline]
101+
fn as_fd(&self) -> BorrowedFd<'_> {
102+
unsafe { BorrowedFd::borrow_raw(2) }
103+
}
104+
}
105+
85106
impl io::Write for Stderr {
86107
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
87108
self.write_vectored(&[IoSlice::new(data)])

library/std/src/sys/windows/stdio.rs

+27-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::char::decode_utf16;
44
use crate::cmp;
55
use crate::io;
6+
use crate::mem::MaybeUninit;
67
use crate::os::windows::io::{FromRawHandle, IntoRawHandle};
78
use crate::ptr;
89
use crate::str;
@@ -169,13 +170,14 @@ fn write(
169170
}
170171

171172
fn write_valid_utf8_to_console(handle: c::HANDLE, utf8: &str) -> io::Result<usize> {
172-
let mut utf16 = [0u16; MAX_BUFFER_SIZE / 2];
173+
let mut utf16 = [MaybeUninit::<u16>::uninit(); MAX_BUFFER_SIZE / 2];
173174
let mut len_utf16 = 0;
174175
for (chr, dest) in utf8.encode_utf16().zip(utf16.iter_mut()) {
175-
*dest = chr;
176+
*dest = MaybeUninit::new(chr);
176177
len_utf16 += 1;
177178
}
178-
let utf16 = &utf16[..len_utf16];
179+
// Safety: We've initialized `len_utf16` values.
180+
let utf16: &[u16] = unsafe { MaybeUninit::slice_assume_init_ref(&utf16[..len_utf16]) };
179181

180182
let mut written = write_u16s(handle, &utf16)?;
181183

@@ -250,27 +252,33 @@ impl io::Read for Stdin {
250252
return Ok(bytes_copied);
251253
} else if buf.len() - bytes_copied < 4 {
252254
// Not enough space to get a UTF-8 byte. We will use the incomplete UTF8.
253-
let mut utf16_buf = [0u16; 1];
255+
let mut utf16_buf = [MaybeUninit::new(0); 1];
254256
// Read one u16 character.
255257
let read = read_u16s_fixup_surrogates(handle, &mut utf16_buf, 1, &mut self.surrogate)?;
256258
// Read bytes, using the (now-empty) self.incomplete_utf8 as extra space.
257-
let read_bytes = utf16_to_utf8(&utf16_buf[..read], &mut self.incomplete_utf8.bytes)?;
259+
let read_bytes = utf16_to_utf8(
260+
unsafe { MaybeUninit::slice_assume_init_ref(&utf16_buf[..read]) },
261+
&mut self.incomplete_utf8.bytes,
262+
)?;
258263

259264
// Read in the bytes from incomplete_utf8 until the buffer is full.
260265
self.incomplete_utf8.len = read_bytes as u8;
261266
// No-op if no bytes.
262267
bytes_copied += self.incomplete_utf8.read(&mut buf[bytes_copied..]);
263268
Ok(bytes_copied)
264269
} else {
265-
let mut utf16_buf = [0u16; MAX_BUFFER_SIZE / 2];
270+
let mut utf16_buf = [MaybeUninit::<u16>::uninit(); MAX_BUFFER_SIZE / 2];
271+
266272
// In the worst case, a UTF-8 string can take 3 bytes for every `u16` of a UTF-16. So
267273
// we can read at most a third of `buf.len()` chars and uphold the guarantee no data gets
268274
// lost.
269275
let amount = cmp::min(buf.len() / 3, utf16_buf.len());
270276
let read =
271277
read_u16s_fixup_surrogates(handle, &mut utf16_buf, amount, &mut self.surrogate)?;
272-
273-
match utf16_to_utf8(&utf16_buf[..read], buf) {
278+
// Safety `read_u16s_fixup_surrogates` returns the number of items
279+
// initialized.
280+
let utf16s = unsafe { MaybeUninit::slice_assume_init_ref(&utf16_buf[..read]) };
281+
match utf16_to_utf8(utf16s, buf) {
274282
Ok(value) => return Ok(bytes_copied + value),
275283
Err(e) => return Err(e),
276284
}
@@ -283,14 +291,14 @@ impl io::Read for Stdin {
283291
// This is a best effort, and might not work if we are not the only reader on Stdin.
284292
fn read_u16s_fixup_surrogates(
285293
handle: c::HANDLE,
286-
buf: &mut [u16],
294+
buf: &mut [MaybeUninit<u16>],
287295
mut amount: usize,
288296
surrogate: &mut u16,
289297
) -> io::Result<usize> {
290298
// Insert possibly remaining unpaired surrogate from last read.
291299
let mut start = 0;
292300
if *surrogate != 0 {
293-
buf[0] = *surrogate;
301+
buf[0] = MaybeUninit::new(*surrogate);
294302
*surrogate = 0;
295303
start = 1;
296304
if amount == 1 {
@@ -303,7 +311,10 @@ fn read_u16s_fixup_surrogates(
303311
let mut amount = read_u16s(handle, &mut buf[start..amount])? + start;
304312

305313
if amount > 0 {
306-
let last_char = buf[amount - 1];
314+
// Safety: The returned `amount` is the number of values initialized,
315+
// and it is not 0, so we know that `buf[amount - 1]` have been
316+
// initialized.
317+
let last_char = unsafe { buf[amount - 1].assume_init() };
307318
if last_char >= 0xD800 && last_char <= 0xDBFF {
308319
// high surrogate
309320
*surrogate = last_char;
@@ -313,7 +324,8 @@ fn read_u16s_fixup_surrogates(
313324
Ok(amount)
314325
}
315326

316-
fn read_u16s(handle: c::HANDLE, buf: &mut [u16]) -> io::Result<usize> {
327+
// Returns `Ok(n)` if it initialized `n` values in `buf`.
328+
fn read_u16s(handle: c::HANDLE, buf: &mut [MaybeUninit<u16>]) -> io::Result<usize> {
317329
// Configure the `pInputControl` parameter to not only return on `\r\n` but also Ctrl-Z, the
318330
// traditional DOS method to indicate end of character stream / user input (SUB).
319331
// See #38274 and https://stackoverflow.com/questions/43836040/win-api-readconsole.
@@ -346,8 +358,9 @@ fn read_u16s(handle: c::HANDLE, buf: &mut [u16]) -> io::Result<usize> {
346358
}
347359
break;
348360
}
349-
350-
if amount > 0 && buf[amount as usize - 1] == CTRL_Z {
361+
// Safety: if `amount > 0`, then that many bytes were written, so
362+
// `buf[amount as usize - 1]` has been initialized.
363+
if amount > 0 && unsafe { buf[amount as usize - 1].assume_init() } == CTRL_Z {
351364
amount -= 1;
352365
}
353366
Ok(amount as usize)

src/doc/nomicon

0 commit comments

Comments
 (0)