Skip to content

Commit 2d3a85b

Browse files
committed
Auto merge of #102787 - Dylan-DPC:rollup-fvbb4t9, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - #102300 (Use a macro to not have to copy-paste `ConstFnMutClosure::new(&mut fold, NeverShortCircuit::wrap_mut_2_imp)).0` everywhere) - #102475 (unsafe keyword: trait examples and unsafe_op_in_unsafe_fn update) - #102760 (Avoid repeated re-initialization of the BufReader buffer) - #102764 (Check `WhereClauseReferencesSelf` after all other object safety checks) - #102779 (Fix `type_of` ICE) - #102780 (run Miri CI when std::sys changes) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 43c22af + 3800d40 commit 2d3a85b

File tree

20 files changed

+319
-164
lines changed

20 files changed

+319
-164
lines changed

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,10 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
493493
},
494494
def_id.to_def_id(),
495495
);
496-
if let Some(assoc_item) = assoc_item {
497-
tcx.type_of(tcx.generics_of(assoc_item.def_id).params[idx].def_id)
496+
if let Some(param)
497+
= assoc_item.map(|item| &tcx.generics_of(item.def_id).params[idx]).filter(|param| param.kind.is_ty_or_const())
498+
{
499+
tcx.type_of(param.def_id)
498500
} else {
499501
// FIXME(associated_const_equality): add a useful error message here.
500502
tcx.ty_error_with_message(

compiler/rustc_trait_selection/src/traits/object_safety.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -447,19 +447,6 @@ fn virtual_call_violation_for_method<'tcx>(
447447
return Some(MethodViolationCode::Generic);
448448
}
449449

450-
if tcx
451-
.predicates_of(method.def_id)
452-
.predicates
453-
.iter()
454-
// A trait object can't claim to live more than the concrete type,
455-
// so outlives predicates will always hold.
456-
.cloned()
457-
.filter(|(p, _)| p.to_opt_type_outlives().is_none())
458-
.any(|pred| contains_illegal_self_type_reference(tcx, trait_def_id, pred))
459-
{
460-
return Some(MethodViolationCode::WhereClauseReferencesSelf);
461-
}
462-
463450
let receiver_ty = tcx.liberate_late_bound_regions(method.def_id, sig.input(0));
464451

465452
// Until `unsized_locals` is fully implemented, `self: Self` can't be dispatched on.
@@ -538,6 +525,21 @@ fn virtual_call_violation_for_method<'tcx>(
538525
}
539526
}
540527

528+
// NOTE: This check happens last, because it results in a lint, and not a
529+
// hard error.
530+
if tcx
531+
.predicates_of(method.def_id)
532+
.predicates
533+
.iter()
534+
// A trait object can't claim to live more than the concrete type,
535+
// so outlives predicates will always hold.
536+
.cloned()
537+
.filter(|(p, _)| p.to_opt_type_outlives().is_none())
538+
.any(|pred| contains_illegal_self_type_reference(tcx, trait_def_id, pred))
539+
{
540+
return Some(MethodViolationCode::WhereClauseReferencesSelf);
541+
}
542+
541543
None
542544
}
543545

library/core/src/iter/adapters/array_chunks.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::array;
2-
use crate::const_closure::ConstFnMutClosure;
32
use crate::iter::{ByRefSized, FusedIterator, Iterator};
4-
use crate::ops::{ControlFlow, NeverShortCircuit, Try};
3+
use crate::ops::{ControlFlow, Try};
54

65
/// An iterator over `N` elements of the iterator at a time.
76
///
@@ -83,13 +82,7 @@ where
8382
}
8483
}
8584

86-
fn fold<B, F>(mut self, init: B, mut f: F) -> B
87-
where
88-
Self: Sized,
89-
F: FnMut(B, Self::Item) -> B,
90-
{
91-
self.try_fold(init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp)).0
92-
}
85+
impl_fold_via_try_fold! { fold -> try_fold }
9386
}
9487

9588
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]
@@ -127,13 +120,7 @@ where
127120
try { acc }
128121
}
129122

130-
fn rfold<B, F>(mut self, init: B, mut f: F) -> B
131-
where
132-
Self: Sized,
133-
F: FnMut(B, Self::Item) -> B,
134-
{
135-
self.try_rfold(init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp)).0
136-
}
123+
impl_fold_via_try_fold! { rfold -> try_rfold }
137124
}
138125

139126
impl<I, const N: usize> ArrayChunks<I, N>

library/core/src/iter/adapters/map_while.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,7 @@ where
6464
.into_try()
6565
}
6666

67-
#[inline]
68-
fn fold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
69-
where
70-
Self: Sized,
71-
Fold: FnMut(Acc, Self::Item) -> Acc,
72-
{
73-
#[inline]
74-
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
75-
move |acc, x| Ok(f(acc, x))
76-
}
77-
78-
self.try_fold(init, ok(fold)).unwrap()
79-
}
67+
impl_fold_via_try_fold! { fold -> try_fold }
8068
}
8169

8270
#[unstable(issue = "none", feature = "inplace_iteration")]

library/core/src/iter/adapters/mod.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::const_closure::ConstFnMutClosure;
21
use crate::iter::{InPlaceIterable, Iterator};
3-
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, NeverShortCircuit, Residual, Try};
2+
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};
43

54
mod array_chunks;
65
mod by_ref_sized;
@@ -204,13 +203,7 @@ where
204203
.into_try()
205204
}
206205

207-
fn fold<B, F>(mut self, init: B, mut fold: F) -> B
208-
where
209-
Self: Sized,
210-
F: FnMut(B, Self::Item) -> B,
211-
{
212-
self.try_fold(init, ConstFnMutClosure::new(&mut fold, NeverShortCircuit::wrap_mut_2_imp)).0
213-
}
206+
impl_fold_via_try_fold! { fold -> try_fold }
214207
}
215208

216209
#[unstable(issue = "none", feature = "inplace_iteration")]

library/core/src/iter/adapters/scan.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,7 @@ where
7474
self.iter.try_fold(init, scan(state, f, fold)).into_try()
7575
}
7676

77-
#[inline]
78-
fn fold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
79-
where
80-
Self: Sized,
81-
Fold: FnMut(Acc, Self::Item) -> Acc,
82-
{
83-
#[inline]
84-
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
85-
move |acc, x| Ok(f(acc, x))
86-
}
87-
88-
self.try_fold(init, ok(fold)).unwrap()
89-
}
77+
impl_fold_via_try_fold! { fold -> try_fold }
9078
}
9179

9280
#[unstable(issue = "none", feature = "inplace_iteration")]

library/core/src/iter/adapters/skip.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,7 @@ where
206206
if n == 0 { try { init } } else { self.iter.try_rfold(init, check(n, fold)).into_try() }
207207
}
208208

209-
fn rfold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
210-
where
211-
Fold: FnMut(Acc, Self::Item) -> Acc,
212-
{
213-
#[inline]
214-
fn ok<Acc, T>(mut f: impl FnMut(Acc, T) -> Acc) -> impl FnMut(Acc, T) -> Result<Acc, !> {
215-
move |acc, x| Ok(f(acc, x))
216-
}
217-
218-
self.try_rfold(init, ok(fold)).unwrap()
219-
}
209+
impl_fold_via_try_fold! { rfold -> try_rfold }
220210

221211
#[inline]
222212
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {

library/core/src/iter/adapters/take.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,7 @@ where
9898
}
9999
}
100100

101-
#[inline]
102-
fn fold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
103-
where
104-
Self: Sized,
105-
Fold: FnMut(Acc, Self::Item) -> Acc,
106-
{
107-
#[inline]
108-
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
109-
move |acc, x| Ok(f(acc, x))
110-
}
111-
112-
self.try_fold(init, ok(fold)).unwrap()
113-
}
101+
impl_fold_via_try_fold! { fold -> try_fold }
114102

115103
#[inline]
116104
#[rustc_inherit_overflow_checks]

library/core/src/iter/adapters/take_while.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,7 @@ where
9494
}
9595
}
9696

97-
#[inline]
98-
fn fold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
99-
where
100-
Self: Sized,
101-
Fold: FnMut(Acc, Self::Item) -> Acc,
102-
{
103-
#[inline]
104-
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
105-
move |acc, x| Ok(f(acc, x))
106-
}
107-
108-
self.try_fold(init, ok(fold)).unwrap()
109-
}
97+
impl_fold_via_try_fold! { fold -> try_fold }
11098
}
11199

112100
#[stable(feature = "fused", since = "1.26.0")]

library/core/src/iter/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,29 @@
352352
353353
#![stable(feature = "rust1", since = "1.0.0")]
354354

355+
// This needs to be up here in order to be usable in the child modules
356+
macro_rules! impl_fold_via_try_fold {
357+
(fold -> try_fold) => {
358+
impl_fold_via_try_fold! { @internal fold -> try_fold }
359+
};
360+
(rfold -> try_rfold) => {
361+
impl_fold_via_try_fold! { @internal rfold -> try_rfold }
362+
};
363+
(@internal $fold:ident -> $try_fold:ident) => {
364+
#[inline]
365+
fn $fold<AAA, FFF>(mut self, init: AAA, mut fold: FFF) -> AAA
366+
where
367+
FFF: FnMut(AAA, Self::Item) -> AAA,
368+
{
369+
use crate::const_closure::ConstFnMutClosure;
370+
use crate::ops::NeverShortCircuit;
371+
372+
let fold = ConstFnMutClosure::new(&mut fold, NeverShortCircuit::wrap_mut_2_imp);
373+
self.$try_fold(init, fold).0
374+
}
375+
};
376+
}
377+
355378
#[stable(feature = "rust1", since = "1.0.0")]
356379
pub use self::traits::Iterator;
357380

0 commit comments

Comments
 (0)