Skip to content

Commit 9b701e7

Browse files
committed
Auto merge of #95090 - matthiaskrgr:rollup-pho6x6s, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #94115 (Let `try_collect` take advantage of `try_fold` overrides) - #94295 (Always evaluate all cfg predicate in all() and any()) - #94848 (Compare installed browser-ui-test version to the one used in CI) - #94993 (Add test for >65535 hashes in lexing raw string) - #95017 (Derive Eq for std::cmp::Ordering, instead of using manual impl.) - #95058 (Add use of bool::then in sys/unix/process) - #95083 (Document that `Option<extern "abi" fn>` discriminant elision applies for any ABI) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 1bfe40d + 9c40db2 commit 9b701e7

File tree

17 files changed

+215
-31
lines changed

17 files changed

+215
-31
lines changed

compiler/rustc_attr/src/builtin.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,18 @@ pub fn eval_condition(
603603
match cfg.name_or_empty() {
604604
sym::any => mis
605605
.iter()
606-
.any(|mi| eval_condition(mi.meta_item().unwrap(), sess, features, eval)),
606+
// We don't use any() here, because we want to evaluate all cfg condition
607+
// as eval_condition can (and does) extra checks
608+
.fold(false, |res, mi| {
609+
res | eval_condition(mi.meta_item().unwrap(), sess, features, eval)
610+
}),
607611
sym::all => mis
608612
.iter()
609-
.all(|mi| eval_condition(mi.meta_item().unwrap(), sess, features, eval)),
613+
// We don't use all() here, because we want to evaluate all cfg condition
614+
// as eval_condition can (and does) extra checks
615+
.fold(true, |res, mi| {
616+
res & eval_condition(mi.meta_item().unwrap(), sess, features, eval)
617+
}),
610618
sym::not => {
611619
if mis.len() != 1 {
612620
struct_span_err!(

compiler/rustc_lexer/src/tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ fn test_unterminated_no_pound() {
6666
);
6767
}
6868

69+
#[test]
70+
fn test_too_many_hashes() {
71+
let max_count = u16::MAX;
72+
let mut hashes: String = "#".repeat(max_count.into());
73+
74+
// Valid number of hashes (65535 = 2^16 - 1), but invalid string.
75+
check_raw_str(&hashes, max_count, Some(RawStrError::InvalidStarter { bad_char: '\u{0}' }));
76+
77+
// One more hash sign (65536 = 2^16) becomes too many.
78+
hashes.push('#');
79+
check_raw_str(
80+
&hashes,
81+
0,
82+
Some(RawStrError::TooManyDelimiters { found: usize::from(max_count) + 1 }),
83+
);
84+
}
85+
6986
#[test]
7087
fn test_valid_shebang() {
7188
// https://github.com/rust-lang/rust/issues/70528

library/core/src/cmp.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ pub struct AssertParamIsEq<T: Eq + ?Sized> {
333333
/// let result = 2.cmp(&1);
334334
/// assert_eq!(Ordering::Greater, result);
335335
/// ```
336-
#[derive(Clone, Copy, PartialEq, Debug, Hash)]
336+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
337337
#[stable(feature = "rust1", since = "1.0.0")]
338338
#[repr(i8)]
339339
pub enum Ordering {
@@ -861,9 +861,6 @@ pub macro Ord($item:item) {
861861
/* compiler built-in */
862862
}
863863

864-
#[stable(feature = "rust1", since = "1.0.0")]
865-
impl Eq for Ordering {}
866-
867864
#[stable(feature = "rust1", since = "1.0.0")]
868865
impl Ord for Ordering {
869866
#[inline]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use crate::ops::Try;
2+
3+
/// Like `Iterator::by_ref`, but requiring `Sized` so it can forward generics.
4+
///
5+
/// Ideally this will no longer be required, eventually, but as can be seen in
6+
/// the benchmarks (as of Feb 2022 at least) `by_ref` can have performance cost.
7+
pub(crate) struct ByRefSized<'a, I>(pub &'a mut I);
8+
9+
impl<I: Iterator> Iterator for ByRefSized<'_, I> {
10+
type Item = I::Item;
11+
12+
fn next(&mut self) -> Option<Self::Item> {
13+
self.0.next()
14+
}
15+
16+
fn size_hint(&self) -> (usize, Option<usize>) {
17+
self.0.size_hint()
18+
}
19+
20+
fn advance_by(&mut self, n: usize) -> Result<(), usize> {
21+
self.0.advance_by(n)
22+
}
23+
24+
fn nth(&mut self, n: usize) -> Option<Self::Item> {
25+
self.0.nth(n)
26+
}
27+
28+
fn fold<B, F>(self, init: B, f: F) -> B
29+
where
30+
F: FnMut(B, Self::Item) -> B,
31+
{
32+
self.0.fold(init, f)
33+
}
34+
35+
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
36+
where
37+
F: FnMut(B, Self::Item) -> R,
38+
R: Try<Output = B>,
39+
{
40+
self.0.try_fold(init, f)
41+
}
42+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::iter::{InPlaceIterable, Iterator};
22
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, NeverShortCircuit, Residual, Try};
33

4+
mod by_ref_sized;
45
mod chain;
56
mod cloned;
67
mod copied;
@@ -31,6 +32,8 @@ pub use self::{
3132
scan::Scan, skip::Skip, skip_while::SkipWhile, take::Take, take_while::TakeWhile, zip::Zip,
3233
};
3334

35+
pub(crate) use self::by_ref_sized::ByRefSized;
36+
3437
#[stable(feature = "iter_cloned", since = "1.1.0")]
3538
pub use self::cloned::Cloned;
3639

library/core/src/iter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ pub use self::adapters::{
417417
#[unstable(feature = "iter_intersperse", reason = "recently added", issue = "79524")]
418418
pub use self::adapters::{Intersperse, IntersperseWith};
419419

420-
pub(crate) use self::adapters::try_process;
420+
pub(crate) use self::adapters::{try_process, ByRefSized};
421421

422422
mod adapters;
423423
mod range;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::cmp::{self, Ordering};
22
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};
33

44
use super::super::try_process;
5+
use super::super::ByRefSized;
56
use super::super::TrustedRandomAccessNoCoerce;
67
use super::super::{Chain, Cloned, Copied, Cycle, Enumerate, Filter, FilterMap, Fuse};
78
use super::super::{FlatMap, Flatten};
@@ -1861,7 +1862,7 @@ pub trait Iterator {
18611862
<<Self as Iterator>::Item as Try>::Residual: Residual<B>,
18621863
B: FromIterator<<Self::Item as Try>::Output>,
18631864
{
1864-
try_process(self, |i| i.collect())
1865+
try_process(ByRefSized(self), |i| i.collect())
18651866
}
18661867

18671868
/// Collects all the items from an iterator into a collection.

library/core/src/option.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@
8080
//! * [`Box<U>`]
8181
//! * `&U`
8282
//! * `&mut U`
83-
//! * `fn`, `extern "C" fn`
83+
//! * `fn`, `extern "C" fn`[^extern_fn]
8484
//! * [`num::NonZero*`]
8585
//! * [`ptr::NonNull<U>`]
8686
//! * `#[repr(transparent)]` struct around one of the types in this list.
8787
//!
88+
//! [^extern_fn]: this remains true for any other ABI: `extern "abi" fn` (_e.g._, `extern "system" fn`)
89+
//!
8890
//! [`Box<U>`]: ../../std/boxed/struct.Box.html
8991
//! [`num::NonZero*`]: crate::num
9092
//! [`ptr::NonNull<U>`]: crate::ptr::NonNull

library/core/tests/cmp.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ fn ordering_const() {
133133
assert_eq!(THEN, Greater);
134134
}
135135

136+
#[test]
137+
fn ordering_structural_eq() {
138+
// test that consts of type `Ordering` are usable in patterns
139+
140+
const ORDERING: Ordering = Greater;
141+
142+
const REVERSE: Ordering = ORDERING.reverse();
143+
match Ordering::Less {
144+
REVERSE => {}
145+
_ => unreachable!(),
146+
};
147+
}
148+
136149
#[test]
137150
fn cmp_default() {
138151
// Test default methods in PartialOrd and PartialEq

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,30 @@ fn test_collect_into() {
551551
assert!(a == b);
552552
}
553553

554+
#[test]
555+
fn iter_try_collect_uses_try_fold_not_next() {
556+
// This makes sure it picks up optimizations, and doesn't use the `&mut I` impl.
557+
struct PanicOnNext<I>(I);
558+
impl<I: Iterator> Iterator for PanicOnNext<I> {
559+
type Item = I::Item;
560+
fn next(&mut self) -> Option<Self::Item> {
561+
panic!("Iterator::next should not be called!")
562+
}
563+
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
564+
where
565+
Self: Sized,
566+
F: FnMut(B, Self::Item) -> R,
567+
R: std::ops::Try<Output = B>,
568+
{
569+
self.0.try_fold(init, f)
570+
}
571+
}
572+
573+
let it = (0..10).map(Some);
574+
let _ = PanicOnNext(it).try_collect::<Vec<_>>();
575+
// validation is just that it didn't panic.
576+
}
577+
554578
// just tests by whether or not this compiles
555579
fn _empty_impl_all_auto_traits<T>() {
556580
use std::panic::{RefUnwindSafe, UnwindSafe};

0 commit comments

Comments
 (0)