Skip to content

Commit d79642c

Browse files
committed
Auto merge of #133978 - matthiaskrgr:rollup-6gh1iho, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #130209 (Stabilize `std::io::ErrorKind::CrossesDevices`) - #130254 (Stabilize `std::io::ErrorKind::QuotaExceeded`) - #132187 (Add Extend impls for tuples of arity 1 through 12) - #133875 (handle `--json-output` properly) - #133934 (Do not implement unsafe auto traits for types with unsafe fields) - #133954 (Hide errors whose suggestions would contain error constants or types) - #133960 (rustdoc: remove eq for clean::Attributes) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 75716b4 + d210b91 commit d79642c

File tree

32 files changed

+290
-231
lines changed

32 files changed

+290
-231
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
998998
self.lower_const_arg(ct, FeedConstTy::No).into()
999999
}
10001000
};
1001+
if term.references_error() {
1002+
continue;
1003+
}
10011004
// FIXME(#97583): This isn't syntactically well-formed!
10021005
where_bounds.push(format!(
10031006
" T: {trait}::{assoc_name} = {term}",

compiler/rustc_middle/src/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,10 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
585585
self.trait_def(trait_def_id).implement_via_object
586586
}
587587

588+
fn trait_is_unsafe(self, trait_def_id: Self::DefId) -> bool {
589+
self.trait_def(trait_def_id).safety == hir::Safety::Unsafe
590+
}
591+
588592
fn is_impl_trait_in_trait(self, def_id: DefId) -> bool {
589593
self.is_impl_trait_in_trait(def_id)
590594
}

compiler/rustc_middle/src/ty/sty.rs

+8
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,14 @@ impl<'tcx> rustc_type_ir::inherent::Ty<TyCtxt<'tcx>> for Ty<'tcx> {
978978
fn async_destructor_ty(self, interner: TyCtxt<'tcx>) -> Ty<'tcx> {
979979
self.async_destructor_ty(interner)
980980
}
981+
982+
fn has_unsafe_fields(self) -> bool {
983+
if let ty::Adt(adt_def, ..) = self.kind() {
984+
adt_def.all_fields().any(|x| x.safety == hir::Safety::Unsafe)
985+
} else {
986+
false
987+
}
988+
}
981989
}
982990

983991
/// Type utilities

compiler/rustc_mir_build/src/thir/constant.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_ast as ast;
22
use rustc_hir::LangItem;
33
use rustc_middle::bug;
44
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
5-
use rustc_middle::ty::{self, ScalarInt, TyCtxt};
5+
use rustc_middle::ty::{self, ScalarInt, TyCtxt, TypeVisitableExt as _};
66
use tracing::trace;
77

88
use crate::build::parse_float_into_scalar;
@@ -13,6 +13,10 @@ pub(crate) fn lit_to_const<'tcx>(
1313
) -> Result<ty::Const<'tcx>, LitToConstError> {
1414
let LitToConstInput { lit, ty, neg } = lit_input;
1515

16+
if let Err(guar) = ty.error_reported() {
17+
return Ok(ty::Const::new_error(tcx, guar));
18+
}
19+
1620
let trunc = |n| {
1721
let width = match tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty)) {
1822
Ok(layout) => layout.size,

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

+8
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ where
169169
return result;
170170
}
171171

172+
// Only consider auto impls of unsafe traits when there are no unsafe
173+
// fields.
174+
if ecx.cx().trait_is_unsafe(goal.predicate.def_id())
175+
&& goal.predicate.self_ty().has_unsafe_fields()
176+
{
177+
return Err(NoSolution);
178+
}
179+
172180
// We only look into opaque types during analysis for opaque types
173181
// outside of their defining scope. Doing so for opaques in the
174182
// defining scope may require calling `typeck` on the same item we're

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_infer::traits::{
1818
use rustc_middle::ty::fast_reject::DeepRejectCtxt;
1919
use rustc_middle::ty::{self, ToPolyTraitRef, Ty, TypeVisitableExt, TypingMode};
2020
use rustc_middle::{bug, span_bug};
21+
use rustc_type_ir::Interner;
2122
use tracing::{debug, instrument, trace};
2223

2324
use super::SelectionCandidate::*;
@@ -794,6 +795,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
794795
| ty::Never
795796
| ty::Tuple(_)
796797
| ty::CoroutineWitness(..) => {
798+
use rustc_type_ir::inherent::*;
799+
800+
// Only consider auto impls of unsafe traits when there are
801+
// no unsafe fields.
802+
if self.tcx().trait_is_unsafe(def_id) && self_ty.has_unsafe_fields() {
803+
return;
804+
}
805+
797806
// Only consider auto impls if there are no manual impls for the root of `self_ty`.
798807
//
799808
// For example, we only consider auto candidates for `&i32: Auto` if no explicit impl

compiler/rustc_type_ir/src/inherent.rs

+3
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ pub trait Ty<I: Interner<Ty = Self>>:
136136
matches!(self.kind(), ty::FnPtr(..))
137137
}
138138

139+
/// Checks whether this type is an ADT that has unsafe fields.
140+
fn has_unsafe_fields(self) -> bool;
141+
139142
fn fn_sig(self, interner: I) -> ty::Binder<I, ty::FnSig<I>> {
140143
match self.kind() {
141144
ty::FnPtr(sig_tys, hdr) => sig_tys.with(hdr),

compiler/rustc_type_ir/src/interner.rs

+3
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ pub trait Interner:
270270

271271
fn trait_may_be_implemented_via_object(self, trait_def_id: Self::DefId) -> bool;
272272

273+
/// Returns `true` if this is an `unsafe trait`.
274+
fn trait_is_unsafe(self, trait_def_id: Self::DefId) -> bool;
275+
273276
fn is_impl_trait_in_trait(self, def_id: Self::DefId) -> bool;
274277

275278
fn delay_bug(self, msg: impl ToString) -> Self::ErrorGuaranteed;

library/core/src/iter/traits/collect.rs

+129-110
Original file line numberDiff line numberDiff line change
@@ -492,131 +492,150 @@ impl Extend<()> for () {
492492
fn extend_one(&mut self, _item: ()) {}
493493
}
494494

495-
#[stable(feature = "extend_for_tuple", since = "1.56.0")]
496-
impl<A, B, ExtendA, ExtendB> Extend<(A, B)> for (ExtendA, ExtendB)
497-
where
498-
ExtendA: Extend<A>,
499-
ExtendB: Extend<B>,
500-
{
501-
/// Allows to `extend` a tuple of collections that also implement `Extend`.
502-
///
503-
/// See also: [`Iterator::unzip`]
504-
///
505-
/// # Examples
506-
/// ```
507-
/// let mut tuple = (vec![0], vec![1]);
508-
/// tuple.extend([(2, 3), (4, 5), (6, 7)]);
509-
/// assert_eq!(tuple.0, [0, 2, 4, 6]);
510-
/// assert_eq!(tuple.1, [1, 3, 5, 7]);
511-
///
512-
/// // also allows for arbitrarily nested tuples as elements
513-
/// let mut nested_tuple = (vec![1], (vec![2], vec![3]));
514-
/// nested_tuple.extend([(4, (5, 6)), (7, (8, 9))]);
515-
///
516-
/// let (a, (b, c)) = nested_tuple;
517-
/// assert_eq!(a, [1, 4, 7]);
518-
/// assert_eq!(b, [2, 5, 8]);
519-
/// assert_eq!(c, [3, 6, 9]);
520-
/// ```
521-
fn extend<T: IntoIterator<Item = (A, B)>>(&mut self, into_iter: T) {
522-
let (a, b) = self;
523-
let iter = into_iter.into_iter();
524-
SpecTupleExtend::extend(iter, a, b);
525-
}
495+
macro_rules! spec_tuple_impl {
496+
( ($ty_name:ident, $var_name:ident, $extend_ty_name: ident, $trait_name:ident, $default_fn_name:ident, $cnt:tt), ) => {
497+
spec_tuple_impl!($trait_name, $default_fn_name, #[doc(fake_variadic)] #[doc = "This trait is implemented for tuples up to twelve items long. The `impl`s for 1- and 3- through 12-ary tuples were stabilized after 2-tuples, in RUSTC_CURRENT_VERSION."] => ($ty_name, $var_name, $extend_ty_name, $cnt),);
498+
};
499+
( ($ty_name:ident, $var_name:ident, $extend_ty_name: ident, $trait_name:ident, $default_fn_name:ident, $cnt:tt), $(($ty_names:ident, $var_names:ident, $extend_ty_names:ident, $trait_names:ident, $default_fn_names:ident, $cnts:tt),)*) => {
526500

527-
fn extend_one(&mut self, item: (A, B)) {
528-
self.0.extend_one(item.0);
529-
self.1.extend_one(item.1);
530-
}
501+
spec_tuple_impl!($(($ty_names, $var_names, $extend_ty_names, $trait_names, $default_fn_names, $cnts),)*);
502+
spec_tuple_impl!($trait_name, $default_fn_name, #[doc(hidden)] => ($ty_name, $var_name, $extend_ty_name, $cnt), $(($ty_names, $var_names, $extend_ty_names, $cnts),)*);
503+
};
504+
($trait_name:ident, $default_fn_name:ident, #[$meta:meta] $(#[$doctext:meta])? => $(($ty_names:ident, $var_names:ident, $extend_ty_names:ident, $cnts:tt),)*) => {
505+
#[$meta]
506+
$(#[$doctext])?
507+
#[stable(feature = "extend_for_tuple", since = "1.56.0")]
508+
impl<$($ty_names,)* $($extend_ty_names,)*> Extend<($($ty_names,)*)> for ($($extend_ty_names,)*)
509+
where
510+
$($extend_ty_names: Extend<$ty_names>,)*
511+
{
512+
/// Allows to `extend` a tuple of collections that also implement `Extend`.
513+
///
514+
/// See also: [`Iterator::unzip`]
515+
///
516+
/// # Examples
517+
/// ```
518+
/// // Example given for a 2-tuple, but 1- through 12-tuples are supported
519+
/// let mut tuple = (vec![0], vec![1]);
520+
/// tuple.extend([(2, 3), (4, 5), (6, 7)]);
521+
/// assert_eq!(tuple.0, [0, 2, 4, 6]);
522+
/// assert_eq!(tuple.1, [1, 3, 5, 7]);
523+
///
524+
/// // also allows for arbitrarily nested tuples as elements
525+
/// let mut nested_tuple = (vec![1], (vec![2], vec![3]));
526+
/// nested_tuple.extend([(4, (5, 6)), (7, (8, 9))]);
527+
///
528+
/// let (a, (b, c)) = nested_tuple;
529+
/// assert_eq!(a, [1, 4, 7]);
530+
/// assert_eq!(b, [2, 5, 8]);
531+
/// assert_eq!(c, [3, 6, 9]);
532+
/// ```
533+
fn extend<T: IntoIterator<Item = ($($ty_names,)*)>>(&mut self, into_iter: T) {
534+
let ($($var_names,)*) = self;
535+
let iter = into_iter.into_iter();
536+
$trait_name::extend(iter, $($var_names,)*);
537+
}
531538

532-
fn extend_reserve(&mut self, additional: usize) {
533-
self.0.extend_reserve(additional);
534-
self.1.extend_reserve(additional);
535-
}
539+
fn extend_one(&mut self, item: ($($ty_names,)*)) {
540+
$(self.$cnts.extend_one(item.$cnts);)*
541+
}
536542

537-
unsafe fn extend_one_unchecked(&mut self, item: (A, B)) {
538-
// SAFETY: Those are our safety preconditions, and we correctly forward `extend_reserve`.
539-
unsafe {
540-
self.0.extend_one_unchecked(item.0);
541-
self.1.extend_one_unchecked(item.1);
542-
}
543-
}
544-
}
543+
fn extend_reserve(&mut self, additional: usize) {
544+
$(self.$cnts.extend_reserve(additional);)*
545+
}
545546

546-
fn default_extend_tuple<A, B, ExtendA, ExtendB>(
547-
iter: impl Iterator<Item = (A, B)>,
548-
a: &mut ExtendA,
549-
b: &mut ExtendB,
550-
) where
551-
ExtendA: Extend<A>,
552-
ExtendB: Extend<B>,
553-
{
554-
fn extend<'a, A, B>(
555-
a: &'a mut impl Extend<A>,
556-
b: &'a mut impl Extend<B>,
557-
) -> impl FnMut((), (A, B)) + 'a {
558-
move |(), (t, u)| {
559-
a.extend_one(t);
560-
b.extend_one(u);
547+
unsafe fn extend_one_unchecked(&mut self, item: ($($ty_names,)*)) {
548+
// SAFETY: Those are our safety preconditions, and we correctly forward `extend_reserve`.
549+
unsafe {
550+
$(self.$cnts.extend_one_unchecked(item.$cnts);)*
551+
}
552+
}
561553
}
562-
}
563554

564-
let (lower_bound, _) = iter.size_hint();
565-
if lower_bound > 0 {
566-
a.extend_reserve(lower_bound);
567-
b.extend_reserve(lower_bound);
568-
}
555+
trait $trait_name<$($ty_names),*> {
556+
fn extend(self, $($var_names: &mut $ty_names,)*);
557+
}
569558

570-
iter.fold((), extend(a, b));
571-
}
559+
fn $default_fn_name<$($ty_names,)* $($extend_ty_names,)*>(
560+
iter: impl Iterator<Item = ($($ty_names,)*)>,
561+
$($var_names: &mut $extend_ty_names,)*
562+
) where
563+
$($extend_ty_names: Extend<$ty_names>,)*
564+
{
565+
fn extend<'a, $($ty_names,)*>(
566+
$($var_names: &'a mut impl Extend<$ty_names>,)*
567+
) -> impl FnMut((), ($($ty_names,)*)) + 'a {
568+
#[allow(non_snake_case)]
569+
move |(), ($($extend_ty_names,)*)| {
570+
$($var_names.extend_one($extend_ty_names);)*
571+
}
572+
}
572573

573-
trait SpecTupleExtend<A, B> {
574-
fn extend(self, a: &mut A, b: &mut B);
575-
}
574+
let (lower_bound, _) = iter.size_hint();
575+
if lower_bound > 0 {
576+
$($var_names.extend_reserve(lower_bound);)*
577+
}
576578

577-
impl<A, B, ExtendA, ExtendB, Iter> SpecTupleExtend<ExtendA, ExtendB> for Iter
578-
where
579-
ExtendA: Extend<A>,
580-
ExtendB: Extend<B>,
581-
Iter: Iterator<Item = (A, B)>,
582-
{
583-
default fn extend(self, a: &mut ExtendA, b: &mut ExtendB) {
584-
default_extend_tuple(self, a, b);
585-
}
586-
}
579+
iter.fold((), extend($($var_names,)*));
580+
}
587581

588-
impl<A, B, ExtendA, ExtendB, Iter> SpecTupleExtend<ExtendA, ExtendB> for Iter
589-
where
590-
ExtendA: Extend<A>,
591-
ExtendB: Extend<B>,
592-
Iter: TrustedLen<Item = (A, B)>,
593-
{
594-
fn extend(self, a: &mut ExtendA, b: &mut ExtendB) {
595-
fn extend<'a, A, B>(
596-
a: &'a mut impl Extend<A>,
597-
b: &'a mut impl Extend<B>,
598-
) -> impl FnMut((), (A, B)) + 'a {
599-
// SAFETY: We reserve enough space for the `size_hint`, and the iterator is `TrustedLen`
600-
// so its `size_hint` is exact.
601-
move |(), (t, u)| unsafe {
602-
a.extend_one_unchecked(t);
603-
b.extend_one_unchecked(u);
582+
impl<$($ty_names,)* $($extend_ty_names,)* Iter> $trait_name<$($extend_ty_names),*> for Iter
583+
where
584+
$($extend_ty_names: Extend<$ty_names>,)*
585+
Iter: Iterator<Item = ($($ty_names,)*)>,
586+
{
587+
default fn extend(self, $($var_names: &mut $extend_ty_names),*) {
588+
$default_fn_name(self, $($var_names),*);
604589
}
605590
}
606591

607-
let (lower_bound, upper_bound) = self.size_hint();
592+
impl<$($ty_names,)* $($extend_ty_names,)* Iter> $trait_name<$($extend_ty_names),*> for Iter
593+
where
594+
$($extend_ty_names: Extend<$ty_names>,)*
595+
Iter: TrustedLen<Item = ($($ty_names,)*)>,
596+
{
597+
fn extend(self, $($var_names: &mut $extend_ty_names,)*) {
598+
fn extend<'a, $($ty_names,)*>(
599+
$($var_names: &'a mut impl Extend<$ty_names>,)*
600+
) -> impl FnMut((), ($($ty_names,)*)) + 'a {
601+
#[allow(non_snake_case)]
602+
// SAFETY: We reserve enough space for the `size_hint`, and the iterator is `TrustedLen`
603+
// so its `size_hint` is exact.
604+
move |(), ($($extend_ty_names,)*)| unsafe {
605+
$($var_names.extend_one_unchecked($extend_ty_names);)*
606+
}
607+
}
608608

609-
if upper_bound.is_none() {
610-
// We cannot reserve more than `usize::MAX` items, and this is likely to go out of memory anyway.
611-
default_extend_tuple(self, a, b);
612-
return;
613-
}
609+
let (lower_bound, upper_bound) = self.size_hint();
614610

615-
if lower_bound > 0 {
616-
a.extend_reserve(lower_bound);
617-
b.extend_reserve(lower_bound);
618-
}
611+
if upper_bound.is_none() {
612+
// We cannot reserve more than `usize::MAX` items, and this is likely to go out of memory anyway.
613+
$default_fn_name(self, $($var_names,)*);
614+
return;
615+
}
619616

620-
self.fold((), extend(a, b));
617+
if lower_bound > 0 {
618+
$($var_names.extend_reserve(lower_bound);)*
619+
}
620+
621+
self.fold((), extend($($var_names,)*));
622+
}
621623
}
624+
625+
};
622626
}
627+
628+
spec_tuple_impl!(
629+
(L, l, EL, TraitL, default_extend_tuple_l, 11),
630+
(K, k, EK, TraitK, default_extend_tuple_k, 10),
631+
(J, j, EJ, TraitJ, default_extend_tuple_j, 9),
632+
(I, i, EI, TraitI, default_extend_tuple_i, 8),
633+
(H, h, EH, TraitH, default_extend_tuple_h, 7),
634+
(G, g, EG, TraitG, default_extend_tuple_g, 6),
635+
(F, f, EF, TraitF, default_extend_tuple_f, 5),
636+
(E, e, EE, TraitE, default_extend_tuple_e, 4),
637+
(D, d, ED, TraitD, default_extend_tuple_d, 3),
638+
(C, c, EC, TraitC, default_extend_tuple_c, 2),
639+
(B, b, EB, TraitB, default_extend_tuple_b, 1),
640+
(A, a, EA, TraitA, default_extend_tuple_a, 0),
641+
);

library/core/tests/iter/traits/iterator.rs

+13
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,19 @@ fn test_next_chunk() {
617617
assert_eq!(it.next_chunk::<0>().unwrap(), []);
618618
}
619619

620+
#[test]
621+
fn test_collect_into_tuples() {
622+
let a = vec![(1, 2, 3), (4, 5, 6), (7, 8, 9)];
623+
let b = vec![1, 4, 7];
624+
let c = vec![2, 5, 8];
625+
let d = vec![3, 6, 9];
626+
let mut e = (Vec::new(), Vec::new(), Vec::new());
627+
a.iter().cloned().collect_into(&mut e);
628+
assert!(e.0 == b);
629+
assert!(e.1 == c);
630+
assert!(e.2 == d);
631+
}
632+
620633
// just tests by whether or not this compiles
621634
fn _empty_impl_all_auto_traits<T>() {
622635
use std::panic::{RefUnwindSafe, UnwindSafe};

0 commit comments

Comments
 (0)