Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 209f8c8

Browse files
committedJun 12, 2024
Auto merge of rust-lang#126319 - workingjubilee:rollup-lendnud, r=workingjubilee
Rollup of 16 pull requests Successful merges: - rust-lang#123374 (DOC: Add FFI example for slice::from_raw_parts()) - rust-lang#124514 (Recommend to never display zero disambiguators when demangling v0 symbols) - rust-lang#125978 (Cleanup: HIR ty lowering: Consolidate the places that do assoc item probing & access checking) - rust-lang#125980 (Nvptx remove direct passmode) - rust-lang#126187 (For E0277 suggest adding `Result` return type for function when using QuestionMark `?` in the body.) - rust-lang#126210 (docs(core): make more const_ptr doctests assert instead of printing) - rust-lang#126249 (Simplify `[T; N]::try_map` signature) - rust-lang#126256 (Add {{target}} substitution to compiletest) - rust-lang#126263 (Make issue-122805.rs big endian compatible) - rust-lang#126281 (set_env: State the conclusion upfront) - rust-lang#126286 (Make `storage-live.rs` robust against rustc internal changes.) - rust-lang#126287 (Update a cranelift patch file for formatting changes.) - rust-lang#126301 (Use `tidy` to sort crate attributes for all compiler crates.) - rust-lang#126305 (Make PathBuf less Ok with adding UTF-16 then `into_string`) - rust-lang#126310 (Migrate run make prefer rlib) - rust-lang#126314 (fix RELEASES: we do not support upcasting to auto traits) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 63fe960 + 6ec98e7 commit 209f8c8

File tree

8 files changed

+87
-27
lines changed

8 files changed

+87
-27
lines changed
 

‎core/src/array/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,9 @@ impl<T, const N: usize> [T; N] {
533533
/// assert_eq!(c, Some(a));
534534
/// ```
535535
#[unstable(feature = "array_try_map", issue = "79711")]
536-
pub fn try_map<F, R>(self, f: F) -> ChangeOutputType<R, [R::Output; N]>
536+
pub fn try_map<R>(self, f: impl FnMut(T) -> R) -> ChangeOutputType<R, [R::Output; N]>
537537
where
538-
F: FnMut(T) -> R,
539-
R: Try,
540-
R::Residual: Residual<[R::Output; N]>,
538+
R: Try<Residual: Residual<[R::Output; N]>>,
541539
{
542540
drain_array_with(self, |iter| try_from_trusted_iterator(iter.map(f)))
543541
}

‎core/src/ops/try_trait.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,9 @@ pub trait Residual<O> {
363363
}
364364

365365
#[unstable(feature = "pub_crate_should_not_need_unstable_attr", issue = "none")]
366-
pub(crate) type ChangeOutputType<T, V> = <<T as Try>::Residual as Residual<V>>::TryType;
366+
#[allow(type_alias_bounds)]
367+
pub(crate) type ChangeOutputType<T: Try<Residual: Residual<V>>, V> =
368+
<T::Residual as Residual<V>>::TryType;
367369

368370
/// An adapter for implementing non-try methods via the `Try` implementation.
369371
///

‎core/src/ptr/const_ptr.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl<T: ?Sized> *const T {
330330
///
331331
/// unsafe {
332332
/// if let Some(val_back) = ptr.as_ref() {
333-
/// println!("We got back the value: {val_back}!");
333+
/// assert_eq!(val_back, &10);
334334
/// }
335335
/// }
336336
/// ```
@@ -346,7 +346,7 @@ impl<T: ?Sized> *const T {
346346
///
347347
/// unsafe {
348348
/// let val_back = &*ptr;
349-
/// println!("We got back the value: {val_back}!");
349+
/// assert_eq!(val_back, &10);
350350
/// }
351351
/// ```
352352
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
@@ -393,7 +393,7 @@ impl<T: ?Sized> *const T {
393393
/// let ptr: *const u8 = &10u8 as *const u8;
394394
///
395395
/// unsafe {
396-
/// println!("We got back the value: {}!", ptr.as_ref_unchecked());
396+
/// assert_eq!(ptr.as_ref_unchecked(), &10);
397397
/// }
398398
/// ```
399399
// FIXME: mention it in the docs for `as_ref` and `as_uninit_ref` once stabilized.
@@ -439,7 +439,7 @@ impl<T: ?Sized> *const T {
439439
///
440440
/// unsafe {
441441
/// if let Some(val_back) = ptr.as_uninit_ref() {
442-
/// println!("We got back the value: {}!", val_back.assume_init());
442+
/// assert_eq!(val_back.assume_init(), 10);
443443
/// }
444444
/// }
445445
/// ```
@@ -501,8 +501,8 @@ impl<T: ?Sized> *const T {
501501
/// let ptr: *const u8 = s.as_ptr();
502502
///
503503
/// unsafe {
504-
/// println!("{}", *ptr.offset(1) as char);
505-
/// println!("{}", *ptr.offset(2) as char);
504+
/// assert_eq!(*ptr.offset(1) as char, '2');
505+
/// assert_eq!(*ptr.offset(2) as char, '3');
506506
/// }
507507
/// ```
508508
#[stable(feature = "rust1", since = "1.0.0")]
@@ -573,19 +573,21 @@ impl<T: ?Sized> *const T {
573573
/// # Examples
574574
///
575575
/// ```
576+
/// # use std::fmt::Write;
576577
/// // Iterate using a raw pointer in increments of two elements
577578
/// let data = [1u8, 2, 3, 4, 5];
578579
/// let mut ptr: *const u8 = data.as_ptr();
579580
/// let step = 2;
580581
/// let end_rounded_up = ptr.wrapping_offset(6);
581582
///
582-
/// // This loop prints "1, 3, 5, "
583+
/// let mut out = String::new();
583584
/// while ptr != end_rounded_up {
584585
/// unsafe {
585-
/// print!("{}, ", *ptr);
586+
/// write!(&mut out, "{}, ", *ptr).unwrap();
586587
/// }
587588
/// ptr = ptr.wrapping_offset(step);
588589
/// }
590+
/// assert_eq!(out.as_str(), "1, 3, 5, ");
589591
/// ```
590592
#[stable(feature = "ptr_wrapping_offset", since = "1.16.0")]
591593
#[must_use = "returns a new pointer rather than modifying its argument"]
@@ -988,8 +990,8 @@ impl<T: ?Sized> *const T {
988990
/// let ptr: *const u8 = s.as_ptr();
989991
///
990992
/// unsafe {
991-
/// println!("{}", *ptr.add(1) as char);
992-
/// println!("{}", *ptr.add(2) as char);
993+
/// assert_eq!(*ptr.add(1), b'2');
994+
/// assert_eq!(*ptr.add(2), b'3');
993995
/// }
994996
/// ```
995997
#[stable(feature = "pointer_methods", since = "1.26.0")]
@@ -1073,8 +1075,8 @@ impl<T: ?Sized> *const T {
10731075
///
10741076
/// unsafe {
10751077
/// let end: *const u8 = s.as_ptr().add(3);
1076-
/// println!("{}", *end.sub(1) as char);
1077-
/// println!("{}", *end.sub(2) as char);
1078+
/// assert_eq!(*end.sub(1), b'3');
1079+
/// assert_eq!(*end.sub(2), b'2');
10781080
/// }
10791081
/// ```
10801082
#[stable(feature = "pointer_methods", since = "1.26.0")]
@@ -1155,19 +1157,21 @@ impl<T: ?Sized> *const T {
11551157
/// # Examples
11561158
///
11571159
/// ```
1160+
/// # use std::fmt::Write;
11581161
/// // Iterate using a raw pointer in increments of two elements
11591162
/// let data = [1u8, 2, 3, 4, 5];
11601163
/// let mut ptr: *const u8 = data.as_ptr();
11611164
/// let step = 2;
11621165
/// let end_rounded_up = ptr.wrapping_add(6);
11631166
///
1164-
/// // This loop prints "1, 3, 5, "
1167+
/// let mut out = String::new();
11651168
/// while ptr != end_rounded_up {
11661169
/// unsafe {
1167-
/// print!("{}, ", *ptr);
1170+
/// write!(&mut out, "{}, ", *ptr).unwrap();
11681171
/// }
11691172
/// ptr = ptr.wrapping_add(step);
11701173
/// }
1174+
/// assert_eq!(out, "1, 3, 5, ");
11711175
/// ```
11721176
#[stable(feature = "pointer_methods", since = "1.26.0")]
11731177
#[must_use = "returns a new pointer rather than modifying its argument"]
@@ -1234,19 +1238,21 @@ impl<T: ?Sized> *const T {
12341238
/// # Examples
12351239
///
12361240
/// ```
1241+
/// # use std::fmt::Write;
12371242
/// // Iterate using a raw pointer in increments of two elements (backwards)
12381243
/// let data = [1u8, 2, 3, 4, 5];
12391244
/// let mut ptr: *const u8 = data.as_ptr();
12401245
/// let start_rounded_down = ptr.wrapping_sub(2);
12411246
/// ptr = ptr.wrapping_add(4);
12421247
/// let step = 2;
1243-
/// // This loop prints "5, 3, 1, "
1248+
/// let mut out = String::new();
12441249
/// while ptr != start_rounded_down {
12451250
/// unsafe {
1246-
/// print!("{}, ", *ptr);
1251+
/// write!(&mut out, "{}, ", *ptr).unwrap();
12471252
/// }
12481253
/// ptr = ptr.wrapping_sub(step);
12491254
/// }
1255+
/// assert_eq!(out, "5, 3, 1, ");
12501256
/// ```
12511257
#[stable(feature = "pointer_methods", since = "1.26.0")]
12521258
#[must_use = "returns a new pointer rather than modifying its argument"]

‎core/src/slice/raw.rs

+33
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,39 @@ use crate::ub_checks;
8282
/// }
8383
/// ```
8484
///
85+
/// ### FFI: Handling null pointers
86+
///
87+
/// In languages such as C++, pointers to empty collections are not guaranteed to be non-null.
88+
/// When accepting such pointers, they have to be checked for null-ness to avoid undefined
89+
/// behavior.
90+
///
91+
/// ```
92+
/// use std::slice;
93+
///
94+
/// /// Sum the elements of an FFI slice.
95+
/// ///
96+
/// /// # Safety
97+
/// ///
98+
/// /// If ptr is not NULL, it must be correctly aligned and
99+
/// /// point to `len` initialized items of type `f32`.
100+
/// unsafe extern "C" fn sum_slice(ptr: *const f32, len: usize) -> f32 {
101+
/// let data = if ptr.is_null() {
102+
/// // `len` is assumed to be 0.
103+
/// &[]
104+
/// } else {
105+
/// // SAFETY: see function docstring.
106+
/// unsafe { slice::from_raw_parts(ptr, len) }
107+
/// };
108+
/// data.into_iter().sum()
109+
/// }
110+
///
111+
/// // This could be the result of C++'s std::vector::data():
112+
/// let ptr = std::ptr::null();
113+
/// // And this could be std::vector::size():
114+
/// let len = 0;
115+
/// assert_eq!(unsafe { sum_slice(ptr, len) }, 0.0);
116+
/// ```
117+
///
85118
/// [valid]: ptr#safety
86119
/// [`NonNull::dangling()`]: ptr::NonNull::dangling
87120
#[inline]

‎std/src/env.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,10 @@ impl Error for VarError {
323323
/// This function is also always safe to call on Windows, in single-threaded
324324
/// and multi-threaded programs.
325325
///
326-
/// In multi-threaded programs on other operating systems, we strongly suggest
327-
/// not using `set_var` or `remove_var` at all. The exact requirement is: you
326+
/// In multi-threaded programs on other operating systems, the only safe option is
327+
/// to not use `set_var` or `remove_var` at all.
328+
///
329+
/// The exact requirement is: you
328330
/// must ensure that there are no other threads concurrently writing or
329331
/// *reading*(!) the environment through functions or global variables other
330332
/// than the ones in this module. The problem is that these operating systems
@@ -390,8 +392,10 @@ unsafe fn _set_var(key: &OsStr, value: &OsStr) {
390392
/// This function is also always safe to call on Windows, in single-threaded
391393
/// and multi-threaded programs.
392394
///
393-
/// In multi-threaded programs on other operating systems, we strongly suggest
394-
/// not using `set_var` or `remove_var` at all. The exact requirement is: you
395+
/// In multi-threaded programs on other operating systems, the only safe option is
396+
/// to not use `set_var` or `remove_var` at all.
397+
///
398+
/// The exact requirement is: you
395399
/// must ensure that there are no other threads concurrently writing or
396400
/// *reading*(!) the environment through functions or global variables other
397401
/// than the ones in this module. The problem is that these operating systems

‎std/src/sys/pal/windows/rand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use core::{mem, ptr};
2+
13
use crate::sys::c;
2-
use core::mem;
3-
use core::ptr;
44

55
#[cfg(not(target_vendor = "win7"))]
66
#[inline]

‎std/src/sys_common/wtf8.rs

+3
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,9 @@ impl Wtf8Buf {
477477
/// Part of a hack to make PathBuf::push/pop more efficient.
478478
#[inline]
479479
pub(crate) fn as_mut_vec_for_path_buf(&mut self) -> &mut Vec<u8> {
480+
// FIXME: this function should not even exist, as it implies violating Wtf8Buf invariants
481+
// For now, simply assume that is about to happen.
482+
self.is_known_utf8 = false;
480483
&mut self.bytes
481484
}
482485
}

‎std/tests/windows.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![cfg(windows)]
2+
//! An external tests
3+
4+
use std::{ffi::OsString, os::windows::ffi::OsStringExt, path::PathBuf};
5+
6+
#[test]
7+
#[should_panic]
8+
fn os_string_must_know_it_isnt_utf8_issue_126291() {
9+
let mut utf8 = PathBuf::from(OsString::from("utf8".to_owned()));
10+
let non_utf8: OsString =
11+
OsStringExt::from_wide(&[0x6e, 0x6f, 0x6e, 0xd800, 0x75, 0x74, 0x66, 0x38]);
12+
utf8.set_extension(&non_utf8);
13+
utf8.into_os_string().into_string().unwrap();
14+
}

0 commit comments

Comments
 (0)
Failed to load comments.