Skip to content

Commit d3c7934

Browse files
committed
Auto merge of #69590 - Dylan-DPC:rollup-i3z0sic, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - #69504 (Use assert_ne in hash tests) - #69571 (remove unneeded .as_ref() calls.) - #69572 (use .iter() instead of .into_iter() on references) - #69581 (fix aliasing violation in align_to_mut) - #69582 (improve transmute and Vec::from_raw_parts docs) - #69584 (Correct comment to match behavior) - #69587 (rustc_parse: Tweak the function parameter name check) Failed merges: r? @ghost
2 parents 4f0edbd + ad200af commit d3c7934

File tree

29 files changed

+65
-46
lines changed

29 files changed

+65
-46
lines changed

src/liballoc/vec.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,10 @@ impl<T> Vec<T> {
404404
///
405405
/// * `ptr` needs to have been previously allocated via [`String`]/`Vec<T>`
406406
/// (at least, it's highly likely to be incorrect if it wasn't).
407-
/// * `ptr`'s `T` needs to have the same size and alignment as it was allocated with.
407+
/// * `T` needs to have the same size and alignment as what `ptr` was allocated with.
408+
/// (`T` having a less strict alignment is not sufficient, the alignment really
409+
/// needs to be equal to satsify the [`dealloc`] requirement that memory must be
410+
/// allocated and deallocated with the same layout.)
408411
/// * `length` needs to be less than or equal to `capacity`.
409412
/// * `capacity` needs to be the capacity that the pointer was allocated with.
410413
///
@@ -423,6 +426,7 @@ impl<T> Vec<T> {
423426
/// function.
424427
///
425428
/// [`String`]: ../../std/string/struct.String.html
429+
/// [`dealloc`]: ../../alloc/alloc/trait.GlobalAlloc.html#tymethod.dealloc
426430
///
427431
/// # Examples
428432
///

src/libcore/intrinsics.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,8 @@ extern "rust-intrinsic" {
881881
/// // clone the vector as we will reuse them later
882882
/// let v_clone = v_orig.clone();
883883
///
884-
/// // Using transmute: this is Undefined Behavior, and a bad idea.
884+
/// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
885+
/// // bad idea and could cause Undefined Behavior.
885886
/// // However, it is no-copy.
886887
/// let v_transmuted = unsafe {
887888
/// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
@@ -897,13 +898,14 @@ extern "rust-intrinsic" {
897898
///
898899
/// let v_clone = v_orig.clone();
899900
///
900-
/// // The no-copy, unsafe way, still using transmute, but not UB.
901-
/// // This is equivalent to the original, but safer, and reuses the
902-
/// // same `Vec` internals. Therefore, the new inner type must have the
903-
/// // exact same size, and the same alignment, as the old type.
901+
/// // The no-copy, unsafe way, still using transmute, but not relying on the data layout.
902+
/// // Like the first approach, this reuses the `Vec` internals.
903+
/// // Therefore, the new inner type must have the
904+
/// // exact same size, *and the same alignment*, as the old type.
904905
/// // The same caveats exist for this method as transmute, for
905906
/// // the original inner type (`&i32`) to the converted inner type
906-
/// // (`Option<&i32>`), so read the nomicon pages linked above.
907+
/// // (`Option<&i32>`), so read the nomicon pages linked above and also
908+
/// // consult the [`from_raw_parts`] documentation.
907909
/// let v_from_raw = unsafe {
908910
// FIXME Update this when vec_into_raw_parts is stabilized
909911
/// // Ensure the original vector is not dropped.
@@ -914,6 +916,8 @@ extern "rust-intrinsic" {
914916
/// };
915917
/// ```
916918
///
919+
/// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
920+
///
917921
/// Implementing `split_at_mut`:
918922
///
919923
/// ```

src/libcore/slice/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2571,11 +2571,13 @@ impl<T> [T] {
25712571
let (left, rest) = self.split_at_mut(offset);
25722572
// now `rest` is definitely aligned, so `from_raw_parts_mut` below is okay
25732573
let (us_len, ts_len) = rest.align_to_offsets::<U>();
2574+
let rest_len = rest.len();
25742575
let mut_ptr = rest.as_mut_ptr();
2576+
// We can't use `rest` again after this, that would invalidate its alias `mut_ptr`!
25752577
(
25762578
left,
25772579
from_raw_parts_mut(mut_ptr as *mut U, us_len),
2578-
from_raw_parts_mut(mut_ptr.add(rest.len() - ts_len), ts_len),
2580+
from_raw_parts_mut(mut_ptr.add(rest_len - ts_len), ts_len),
25792581
)
25802582
}
25812583
}

src/libcore/tests/slice.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,18 @@ fn test_align_to_empty_mid() {
15701570
}
15711571
}
15721572

1573+
#[test]
1574+
fn test_align_to_mut_aliasing() {
1575+
let mut val = [1u8, 2, 3, 4, 5];
1576+
// `align_to_mut` used to create `mid` in a way that there was some intermediate
1577+
// incorrect aliasing, invalidating the resulting `mid` slice.
1578+
let (begin, mid, end) = unsafe { val.align_to_mut::<[u8; 2]>() };
1579+
assert!(begin.len() == 0);
1580+
assert!(end.len() == 1);
1581+
mid[0] = mid[1];
1582+
assert_eq!(val, [3, 4, 3, 4, 5])
1583+
}
1584+
15731585
#[test]
15741586
fn test_slice_partition_dedup_by() {
15751587
let mut slice: [i32; 9] = [1, -1, 2, 3, 1, -5, 5, -2, 2];

src/librustc/ty/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,13 +2291,13 @@ impl<'tcx> TyCtxt<'tcx> {
22912291

22922292
#[inline]
22932293
pub fn intern_tup(self, ts: &[Ty<'tcx>]) -> Ty<'tcx> {
2294-
let kinds: Vec<_> = ts.into_iter().map(|&t| GenericArg::from(t)).collect();
2294+
let kinds: Vec<_> = ts.iter().map(|&t| GenericArg::from(t)).collect();
22952295
self.mk_ty(Tuple(self.intern_substs(&kinds)))
22962296
}
22972297

22982298
pub fn mk_tup<I: InternAs<[Ty<'tcx>], Ty<'tcx>>>(self, iter: I) -> I::Output {
22992299
iter.intern_with(|ts| {
2300-
let kinds: Vec<_> = ts.into_iter().map(|&t| GenericArg::from(t)).collect();
2300+
let kinds: Vec<_> = ts.iter().map(|&t| GenericArg::from(t)).collect();
23012301
self.mk_ty(Tuple(self.intern_substs(&kinds)))
23022302
})
23032303
}

src/librustc_builtin_macros/proc_macro_harness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'a> CollectProcMacros<'a> {
149149
.span_err(attr.span(), "attribute must be of form: `attributes(foo, bar)`");
150150
&[]
151151
})
152-
.into_iter()
152+
.iter()
153153
.filter_map(|attr| {
154154
let attr = match attr.meta_item() {
155155
Some(meta_item) => meta_item,

src/librustc_infer/infer/canonical/query_response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ pub fn make_query_region_constraints<'tcx>(
630630
assert!(givens.is_empty());
631631

632632
let outlives: Vec<_> = constraints
633-
.into_iter()
633+
.iter()
634634
.map(|(k, _)| match *k {
635635
// Swap regions because we are going from sub (<=) to outlives
636636
// (>=).

src/librustc_infer/traits/on_unimplemented.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,8 @@ impl<'tcx> OnUnimplementedDirective {
237237
}
238238
}
239239

240-
let options: FxHashMap<Symbol, String> = options
241-
.into_iter()
242-
.filter_map(|(k, v)| v.as_ref().map(|v| (*k, v.to_owned())))
243-
.collect();
240+
let options: FxHashMap<Symbol, String> =
241+
options.iter().filter_map(|(k, v)| v.as_ref().map(|v| (*k, v.to_owned()))).collect();
244242
OnUnimplementedNote {
245243
label: label.map(|l| l.format(tcx, trait_ref, &options)),
246244
message: message.map(|m| m.format(tcx, trait_ref, &options)),

src/librustc_infer/traits/select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2411,7 +2411,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
24112411

24122412
types
24132413
.skip_binder()
2414-
.into_iter()
2414+
.iter()
24152415
.flat_map(|ty| {
24162416
// binder moved -\
24172417
let ty: ty::Binder<Ty<'tcx>> = ty::Binder::bind(ty); // <----/

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ impl EarlyLintPass for DeprecatedAttr {
739739
}
740740

741741
fn warn_if_doc(cx: &EarlyContext<'_>, node_span: Span, node_kind: &str, attrs: &[ast::Attribute]) {
742-
let mut attrs = attrs.into_iter().peekable();
742+
let mut attrs = attrs.iter().peekable();
743743

744744
// Accumulate a single span for sugared doc comments.
745745
let mut sugared_span: Option<Span> = None;

0 commit comments

Comments
 (0)