Skip to content

Commit 2d3a85b

Browse files
committed
Auto merge of rust-lang#102787 - Dylan-DPC:rollup-fvbb4t9, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - rust-lang#102300 (Use a macro to not have to copy-paste `ConstFnMutClosure::new(&mut fold, NeverShortCircuit::wrap_mut_2_imp)).0` everywhere) - rust-lang#102475 (unsafe keyword: trait examples and unsafe_op_in_unsafe_fn update) - rust-lang#102760 (Avoid repeated re-initialization of the BufReader buffer) - rust-lang#102764 (Check `WhereClauseReferencesSelf` after all other object safety checks) - rust-lang#102779 (Fix `type_of` ICE) - rust-lang#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

+4-2
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

+15-13
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

+3-16
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

+1-13
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

+2-9
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

+1-13
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

+1-11
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

+1-13
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

+1-13
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

+23
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

library/core/src/iter/range.rs

+2-26
Original file line numberDiff line numberDiff line change
@@ -1150,19 +1150,7 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
11501150
self.spec_try_fold(init, f)
11511151
}
11521152

1153-
#[inline]
1154-
fn fold<B, F>(mut self, init: B, f: F) -> B
1155-
where
1156-
Self: Sized,
1157-
F: FnMut(B, Self::Item) -> B,
1158-
{
1159-
#[inline]
1160-
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
1161-
move |acc, x| Ok(f(acc, x))
1162-
}
1163-
1164-
self.try_fold(init, ok(f)).unwrap()
1165-
}
1153+
impl_fold_via_try_fold! { fold -> try_fold }
11661154

11671155
#[inline]
11681156
fn last(mut self) -> Option<A> {
@@ -1230,19 +1218,7 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
12301218
self.spec_try_rfold(init, f)
12311219
}
12321220

1233-
#[inline]
1234-
fn rfold<B, F>(mut self, init: B, f: F) -> B
1235-
where
1236-
Self: Sized,
1237-
F: FnMut(B, Self::Item) -> B,
1238-
{
1239-
#[inline]
1240-
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
1241-
move |acc, x| Ok(f(acc, x))
1242-
}
1243-
1244-
self.try_rfold(init, ok(f)).unwrap()
1245-
}
1221+
impl_fold_via_try_fold! { rfold -> try_rfold }
12461222
}
12471223

12481224
// Safety: See above implementation for `ops::Range<A>`

library/std/src/io/buffered/bufreader.rs

+8
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ impl<R> BufReader<R> {
224224
}
225225
}
226226

227+
// This is only used by a test which asserts that the initialization-tracking is correct.
228+
#[cfg(test)]
229+
impl<R> BufReader<R> {
230+
pub fn initialized(&self) -> usize {
231+
self.buf.initialized()
232+
}
233+
}
234+
227235
impl<R: Seek> BufReader<R> {
228236
/// Seeks relative to the current position. If the new position lies within the buffer,
229237
/// the buffer will not be flushed, allowing for more efficient seeks.

library/std/src/io/buffered/bufreader/buffer.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ pub struct Buffer {
2020
// Each call to `fill_buf` sets `filled` to indicate how many bytes at the start of `buf` are
2121
// initialized with bytes from a read.
2222
filled: usize,
23+
// This is the max number of bytes returned across all `fill_buf` calls. We track this so that we
24+
// can accurately tell `read_buf` how many bytes of buf are initialized, to bypass as much of its
25+
// defensive initialization as possible. Note that while this often the same as `filled`, it
26+
// doesn't need to be. Calls to `fill_buf` are not required to actually fill the buffer, and
27+
// omitting this is a huge perf regression for `Read` impls that do not.
28+
initialized: usize,
2329
}
2430

2531
impl Buffer {
2632
#[inline]
2733
pub fn with_capacity(capacity: usize) -> Self {
2834
let buf = Box::new_uninit_slice(capacity);
29-
Self { buf, pos: 0, filled: 0 }
35+
Self { buf, pos: 0, filled: 0, initialized: 0 }
3036
}
3137

3238
#[inline]
@@ -51,6 +57,12 @@ impl Buffer {
5157
self.pos
5258
}
5359

60+
// This is only used by a test which asserts that the initialization-tracking is correct.
61+
#[cfg(test)]
62+
pub fn initialized(&self) -> usize {
63+
self.initialized
64+
}
65+
5466
#[inline]
5567
pub fn discard_buffer(&mut self) {
5668
self.pos = 0;
@@ -96,13 +108,14 @@ impl Buffer {
96108
let mut buf = BorrowedBuf::from(&mut *self.buf);
97109
// SAFETY: `self.filled` bytes will always have been initialized.
98110
unsafe {
99-
buf.set_init(self.filled);
111+
buf.set_init(self.initialized);
100112
}
101113

102114
reader.read_buf(buf.unfilled())?;
103115

104-
self.filled = buf.len();
105116
self.pos = 0;
117+
self.filled = buf.len();
118+
self.initialized = buf.init_len();
106119
}
107120
Ok(self.buffer())
108121
}

library/std/src/io/buffered/tests.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1039,3 +1039,27 @@ fn single_formatted_write() {
10391039
writeln!(&mut writer, "{}, {}!", "hello", "world").unwrap();
10401040
assert_eq!(writer.get_ref().events, [RecordedEvent::Write("hello, world!\n".to_string())]);
10411041
}
1042+
1043+
#[test]
1044+
fn bufreader_full_initialize() {
1045+
struct OneByteReader;
1046+
impl Read for OneByteReader {
1047+
fn read(&mut self, buf: &mut [u8]) -> crate::io::Result<usize> {
1048+
if buf.len() > 0 {
1049+
buf[0] = 0;
1050+
Ok(1)
1051+
} else {
1052+
Ok(0)
1053+
}
1054+
}
1055+
}
1056+
let mut reader = BufReader::new(OneByteReader);
1057+
// Nothing is initialized yet.
1058+
assert_eq!(reader.initialized(), 0);
1059+
1060+
let buf = reader.fill_buf().unwrap();
1061+
// We read one byte...
1062+
assert_eq!(buf.len(), 1);
1063+
// But we initialized the whole buffer!
1064+
assert_eq!(reader.initialized(), reader.capacity());
1065+
}

0 commit comments

Comments
 (0)