Skip to content

Commit aea0c94

Browse files
committed
Auto merge of rust-lang#126300 - workingjubilee:rollup-0v2ypgt, r=workingjubilee
Rollup of 8 pull requests Successful merges: - rust-lang#123374 (DOC: Add FFI example for slice::from_raw_parts()) - rust-lang#126210 (docs(core): make more const_ptr doctests assert instead of printing) - rust-lang#126228 (Provide correct parent for nested anon const) - rust-lang#126242 (Simplify provider api to improve llvm ir) - 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#126286 (Make `storage-live.rs` robust against rustc internal changes.) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 76c7382 + 8e25298 commit aea0c94

File tree

13 files changed

+159
-67
lines changed

13 files changed

+159
-67
lines changed

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_session::lint;
1111
use rustc_span::symbol::{kw, Symbol};
1212
use rustc_span::Span;
1313

14+
#[instrument(level = "debug", skip(tcx))]
1415
pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
1516
use rustc_hir::*;
1617

@@ -66,7 +67,22 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
6667
// FIXME(#43408) always enable this once `lazy_normalization` is
6768
// stable enough and does not need a feature gate anymore.
6869
Node::AnonConst(_) => {
69-
let parent_def_id = tcx.hir().get_parent_item(hir_id);
70+
let parent_did = tcx.parent(def_id.to_def_id());
71+
72+
// We don't do this unconditionally because the `DefId` parent of an anon const
73+
// might be an implicitly created closure during `async fn` desugaring. This would
74+
// have the wrong generics.
75+
//
76+
// i.e. `async fn foo<'a>() { let a = [(); { 1 + 2 }]; bar().await() }`
77+
// would implicitly have a closure in its body that would be the parent of
78+
// the `{ 1 + 2 }` anon const. This closure's generics is simply a witness
79+
// instead of `['a]`.
80+
let parent_did = if let DefKind::AnonConst = tcx.def_kind(parent_did) {
81+
parent_did
82+
} else {
83+
tcx.hir().get_parent_item(hir_id).to_def_id()
84+
};
85+
debug!(?parent_did);
7086

7187
let mut in_param_ty = false;
7288
for (_parent, node) in tcx.hir().parent_iter(hir_id) {
@@ -121,7 +137,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
121137
//
122138
// This has some implications for how we get the predicates available to the anon const
123139
// see `explicit_predicates_of` for more information on this
124-
let generics = tcx.generics_of(parent_def_id.to_def_id());
140+
let generics = tcx.generics_of(parent_did);
125141
let param_def_idx = generics.param_def_id_to_index[&param_id.to_def_id()];
126142
// In the above example this would be .params[..N#0]
127143
let own_params = generics.params_to(param_def_idx as usize, tcx).to_owned();
@@ -147,7 +163,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
147163
//
148164
// Note that we do not supply the parent generics when using
149165
// `min_const_generics`.
150-
Some(parent_def_id.to_def_id())
166+
Some(parent_did)
151167
}
152168
} else {
153169
let parent_node = tcx.parent_hir_node(hir_id);
@@ -159,7 +175,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
159175
Node::Expr(Expr { kind: ExprKind::Repeat(_, constant), .. })
160176
if constant.hir_id() == hir_id =>
161177
{
162-
Some(parent_def_id.to_def_id())
178+
Some(parent_did)
163179
}
164180
// Exclude `GlobalAsm` here which cannot have generics.
165181
Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. })
@@ -171,7 +187,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
171187
_ => false,
172188
}) =>
173189
{
174-
Some(parent_def_id.to_def_id())
190+
Some(parent_did)
175191
}
176192
_ => None,
177193
}

library/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
}

library/core/src/error.rs

+23-26
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,9 @@ fn request_by_type_tag<'a, I>(err: &'a (impl Error + ?Sized)) -> Option<I::Reifi
404404
where
405405
I: tags::Type<'a>,
406406
{
407-
let mut tagged = TaggedOption::<'a, I>(None);
407+
let mut tagged = Tagged { tag_id: TypeId::of::<I>(), value: TaggedOption::<'a, I>(None) };
408408
err.provide(tagged.as_request());
409-
tagged.0
409+
tagged.value.0
410410
}
411411

412412
///////////////////////////////////////////////////////////////////////////////
@@ -507,16 +507,9 @@ where
507507
///
508508
#[unstable(feature = "error_generic_member_access", issue = "99301")]
509509
#[cfg_attr(not(doc), repr(transparent))] // work around https://github.com/rust-lang/rust/issues/90435
510-
pub struct Request<'a>(dyn Erased<'a> + 'a);
510+
pub struct Request<'a>(Tagged<dyn Erased<'a> + 'a>);
511511

512512
impl<'a> Request<'a> {
513-
/// Create a new `&mut Request` from a `&mut dyn Erased` trait object.
514-
fn new<'b>(erased: &'b mut (dyn Erased<'a> + 'a)) -> &'b mut Request<'a> {
515-
// SAFETY: transmuting `&mut (dyn Erased<'a> + 'a)` to `&mut Request<'a>` is safe since
516-
// `Request` is repr(transparent).
517-
unsafe { &mut *(erased as *mut dyn Erased<'a> as *mut Request<'a>) }
518-
}
519-
520513
/// Provide a value or other type with only static lifetimes.
521514
///
522515
/// # Examples
@@ -940,37 +933,38 @@ pub(crate) mod tags {
940933
#[repr(transparent)]
941934
pub(crate) struct TaggedOption<'a, I: tags::Type<'a>>(pub Option<I::Reified>);
942935

943-
impl<'a, I: tags::Type<'a>> TaggedOption<'a, I> {
936+
impl<'a, I: tags::Type<'a>> Tagged<TaggedOption<'a, I>> {
944937
pub(crate) fn as_request(&mut self) -> &mut Request<'a> {
945-
Request::new(self as &mut (dyn Erased<'a> + 'a))
938+
let erased = self as &mut Tagged<dyn Erased<'a> + 'a>;
939+
// SAFETY: transmuting `&mut Tagged<dyn Erased<'a> + 'a>` to `&mut Request<'a>` is safe since
940+
// `Request` is repr(transparent).
941+
unsafe { &mut *(erased as *mut Tagged<dyn Erased<'a>> as *mut Request<'a>) }
946942
}
947943
}
948944

949945
/// Represents a type-erased but identifiable object.
950946
///
951947
/// This trait is exclusively implemented by the `TaggedOption` type.
952-
unsafe trait Erased<'a>: 'a {
953-
/// The `TypeId` of the erased type.
954-
fn tag_id(&self) -> TypeId;
955-
}
948+
unsafe trait Erased<'a>: 'a {}
956949

957-
unsafe impl<'a, I: tags::Type<'a>> Erased<'a> for TaggedOption<'a, I> {
958-
fn tag_id(&self) -> TypeId {
959-
TypeId::of::<I>()
960-
}
950+
unsafe impl<'a, I: tags::Type<'a>> Erased<'a> for TaggedOption<'a, I> {}
951+
952+
struct Tagged<E: ?Sized> {
953+
tag_id: TypeId,
954+
value: E,
961955
}
962956

963-
impl<'a> dyn Erased<'a> + 'a {
957+
impl<'a> Tagged<dyn Erased<'a> + 'a> {
964958
/// Returns some reference to the dynamic value if it is tagged with `I`,
965959
/// or `None` otherwise.
966960
#[inline]
967961
fn downcast<I>(&self) -> Option<&TaggedOption<'a, I>>
968962
where
969963
I: tags::Type<'a>,
970964
{
971-
if self.tag_id() == TypeId::of::<I>() {
965+
if self.tag_id == TypeId::of::<I>() {
972966
// SAFETY: Just checked whether we're pointing to an I.
973-
Some(unsafe { &*(self as *const Self).cast::<TaggedOption<'a, I>>() })
967+
Some(&unsafe { &*(self as *const Self).cast::<Tagged<TaggedOption<'a, I>>>() }.value)
974968
} else {
975969
None
976970
}
@@ -983,9 +977,12 @@ impl<'a> dyn Erased<'a> + 'a {
983977
where
984978
I: tags::Type<'a>,
985979
{
986-
if self.tag_id() == TypeId::of::<I>() {
987-
// SAFETY: Just checked whether we're pointing to an I.
988-
Some(unsafe { &mut *(self as *mut Self).cast::<TaggedOption<'a, I>>() })
980+
if self.tag_id == TypeId::of::<I>() {
981+
Some(
982+
// SAFETY: Just checked whether we're pointing to an I.
983+
&mut unsafe { &mut *(self as *mut Self).cast::<Tagged<TaggedOption<'a, I>>>() }
984+
.value,
985+
)
989986
} else {
990987
None
991988
}

library/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
///

library/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"]

library/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]

src/tools/compiletest/src/header.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,7 @@ fn expand_variables(mut value: String, config: &Config) -> String {
12811281
const BUILD_BASE: &str = "{{build-base}}";
12821282
const SYSROOT_BASE: &str = "{{sysroot-base}}";
12831283
const TARGET_LINKER: &str = "{{target-linker}}";
1284+
const TARGET: &str = "{{target}}";
12841285

12851286
if value.contains(CWD) {
12861287
let cwd = env::current_dir().unwrap();
@@ -1303,6 +1304,10 @@ fn expand_variables(mut value: String, config: &Config) -> String {
13031304
value = value.replace(TARGET_LINKER, config.target_linker.as_deref().unwrap_or(""));
13041305
}
13051306

1307+
if value.contains(TARGET) {
1308+
value = value.replace(TARGET, &config.target);
1309+
}
1310+
13061311
value
13071312
}
13081313

0 commit comments

Comments
 (0)