Skip to content

Commit 84365ff

Browse files
committedOct 19, 2022
Auto merge of #103225 - matthiaskrgr:rollup-1zkv87y, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - #103166 (Optimize `slice_iter.copied().next_chunk()`) - #103176 (Fix `TyKind::is_simple_path`) - #103178 (Partially fix `src/test/run-make/coverage-reports` when cross-compiling) - #103198 (Update cargo) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 415d8fc + 7f6b581 commit 84365ff

File tree

10 files changed

+157
-8
lines changed

10 files changed

+157
-8
lines changed
 

‎compiler/rustc_ast/src/ast.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -2060,8 +2060,11 @@ impl TyKind {
20602060
}
20612061

20622062
pub fn is_simple_path(&self) -> Option<Symbol> {
2063-
if let TyKind::Path(None, Path { segments, .. }) = &self && segments.len() == 1 {
2064-
Some(segments[0].ident.name)
2063+
if let TyKind::Path(None, Path { segments, .. }) = &self
2064+
&& let [segment] = &segments[..]
2065+
&& segment.args.is_none()
2066+
{
2067+
Some(segment.ident.name)
20652068
} else {
20662069
None
20672070
}

‎library/core/benches/iter.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use core::iter::*;
2+
use core::mem;
3+
use core::num::Wrapping;
24
use test::{black_box, Bencher};
35

46
#[bench]
@@ -398,3 +400,21 @@ fn bench_trusted_random_access_adapters(b: &mut Bencher) {
398400
acc
399401
})
400402
}
403+
404+
/// Exercises the iter::Copied specialization for slice::Iter
405+
#[bench]
406+
fn bench_copied_array_chunks(b: &mut Bencher) {
407+
let v = vec![1u8; 1024];
408+
409+
b.iter(|| {
410+
black_box(&v)
411+
.iter()
412+
.copied()
413+
.array_chunks::<{ mem::size_of::<u64>() }>()
414+
.map(|ary| {
415+
let d = u64::from_ne_bytes(ary);
416+
Wrapping(d.rotate_left(7).wrapping_add(1))
417+
})
418+
.sum::<Wrapping<u64>>()
419+
})
420+
}

‎library/core/benches/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![feature(int_log)]
55
#![feature(test)]
66
#![feature(trusted_random_access)]
7+
#![feature(iter_array_chunks)]
78

89
extern crate test;
910

‎library/core/src/iter/adapters/copied.rs

+74
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use crate::iter::adapters::{
22
zip::try_get_unchecked, TrustedRandomAccess, TrustedRandomAccessNoCoerce,
33
};
44
use crate::iter::{FusedIterator, TrustedLen};
5+
use crate::mem::MaybeUninit;
6+
use crate::mem::SizedTypeProperties;
57
use crate::ops::Try;
8+
use crate::{array, ptr};
69

710
/// An iterator that copies the elements of an underlying iterator.
811
///
@@ -44,6 +47,15 @@ where
4447
self.it.next().copied()
4548
}
4649

50+
fn next_chunk<const N: usize>(
51+
&mut self,
52+
) -> Result<[Self::Item; N], array::IntoIter<Self::Item, N>>
53+
where
54+
Self: Sized,
55+
{
56+
<I as SpecNextChunk<'_, N, T>>::spec_next_chunk(&mut self.it)
57+
}
58+
4759
fn size_hint(&self) -> (usize, Option<usize>) {
4860
self.it.size_hint()
4961
}
@@ -166,3 +178,65 @@ where
166178
T: Copy,
167179
{
168180
}
181+
182+
trait SpecNextChunk<'a, const N: usize, T: 'a>: Iterator<Item = &'a T>
183+
where
184+
T: Copy,
185+
{
186+
fn spec_next_chunk(&mut self) -> Result<[T; N], array::IntoIter<T, N>>;
187+
}
188+
189+
impl<'a, const N: usize, I, T: 'a> SpecNextChunk<'a, N, T> for I
190+
where
191+
I: Iterator<Item = &'a T>,
192+
T: Copy,
193+
{
194+
default fn spec_next_chunk(&mut self) -> Result<[T; N], array::IntoIter<T, N>> {
195+
array::iter_next_chunk(&mut self.map(|e| *e))
196+
}
197+
}
198+
199+
impl<'a, const N: usize, T: 'a> SpecNextChunk<'a, N, T> for crate::slice::Iter<'a, T>
200+
where
201+
T: Copy,
202+
{
203+
fn spec_next_chunk(&mut self) -> Result<[T; N], array::IntoIter<T, N>> {
204+
let mut raw_array = MaybeUninit::uninit_array();
205+
206+
let len = self.len();
207+
208+
if T::IS_ZST {
209+
if len < N {
210+
let _ = self.advance_by(len);
211+
// SAFETY: ZSTs can be conjured ex nihilo; only the amount has to be correct
212+
return Err(unsafe { array::IntoIter::new_unchecked(raw_array, 0..len) });
213+
}
214+
215+
let _ = self.advance_by(N);
216+
// SAFETY: ditto
217+
return Ok(unsafe { MaybeUninit::array_assume_init(raw_array) });
218+
}
219+
220+
if len < N {
221+
// SAFETY: `len` indicates that this many elements are available and we just checked that
222+
// it fits into the array.
223+
unsafe {
224+
ptr::copy_nonoverlapping(
225+
self.as_ref().as_ptr(),
226+
raw_array.as_mut_ptr() as *mut T,
227+
len,
228+
);
229+
let _ = self.advance_by(len);
230+
return Err(array::IntoIter::new_unchecked(raw_array, 0..len));
231+
}
232+
}
233+
234+
// SAFETY: `len` is larger than the array size. Copy a fixed amount here to fully initialize
235+
// the array.
236+
unsafe {
237+
ptr::copy_nonoverlapping(self.as_ref().as_ptr(), raw_array.as_mut_ptr() as *mut T, N);
238+
let _ = self.advance_by(N);
239+
Ok(MaybeUninit::array_assume_init(raw_array))
240+
}
241+
}
242+
}

‎src/test/run-make/coverage-reports/Makefile

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# needs-profiler-support
22
# ignore-windows-gnu
33

4+
# FIXME(pietroalbini): this test currently does not work on cross-compiled
5+
# targets because remote-test is not capable of sending back the *.profraw
6+
# files generated by the LLVM instrumentation.
7+
# ignore-cross-compile
8+
49
# Rust coverage maps support LLVM Coverage Mapping Format versions 5 and 6,
510
# corresponding with LLVM versions 12 and 13, respectively.
611
# When upgrading LLVM versions, consider whether to enforce a minimum LLVM
@@ -81,13 +86,13 @@ include clear_expected_if_blessed
8186
# Compile the test library with coverage instrumentation
8287
$(RUSTC) $(SOURCEDIR)/lib/$@.rs \
8388
$$( sed -n 's/^\/\/ compile-flags: \([^#]*\).*/\1/p' $(SOURCEDIR)/lib/$@.rs ) \
84-
--crate-type rlib -Cinstrument-coverage
89+
--crate-type rlib -Cinstrument-coverage --target $(TARGET)
8590

8691
%: $(SOURCEDIR)/%.rs
8792
# Compile the test program with coverage instrumentation
8893
$(RUSTC) $(SOURCEDIR)/$@.rs \
8994
$$( sed -n 's/^\/\/ compile-flags: \([^#]*\).*/\1/p' $(SOURCEDIR)/$@.rs ) \
90-
-L "$(TMPDIR)" -Cinstrument-coverage
95+
-L "$(TMPDIR)" -Cinstrument-coverage --target $(TARGET)
9196

9297
# Run it in order to generate some profiling data,
9398
# with `LLVM_PROFILE_FILE=<profdata_file>` environment variable set to

‎src/test/ui/deriving/deriving-all-codegen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ enum Mixed {
8585
P,
8686
Q,
8787
R(u32),
88-
S { d1: u32, d2: u32 },
88+
S { d1: Option<u32>, d2: Option<i32> },
8989
}
9090

9191
// An enum with no fieldless variants. Note that `Default` cannot be derived

‎src/test/ui/deriving/deriving-all-codegen.stdout

+6-2
Original file line numberDiff line numberDiff line change
@@ -809,15 +809,17 @@ enum Mixed {
809809
Q,
810810
R(u32),
811811
S {
812-
d1: u32,
813-
d2: u32,
812+
d1: Option<u32>,
813+
d2: Option<i32>,
814814
},
815815
}
816816
#[automatically_derived]
817817
impl ::core::clone::Clone for Mixed {
818818
#[inline]
819819
fn clone(&self) -> Mixed {
820820
let _: ::core::clone::AssertParamIsClone<u32>;
821+
let _: ::core::clone::AssertParamIsClone<Option<u32>>;
822+
let _: ::core::clone::AssertParamIsClone<Option<i32>>;
821823
*self
822824
}
823825
}
@@ -886,6 +888,8 @@ impl ::core::cmp::Eq for Mixed {
886888
#[no_coverage]
887889
fn assert_receiver_is_total_eq(&self) -> () {
888890
let _: ::core::cmp::AssertParamIsEq<u32>;
891+
let _: ::core::cmp::AssertParamIsEq<Option<u32>>;
892+
let _: ::core::cmp::AssertParamIsEq<Option<i32>>;
889893
}
890894
}
891895
#[automatically_derived]

‎src/test/ui/deriving/issue-103157.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// check-fail
2+
3+
#[derive(PartialEq, Eq)]
4+
pub enum Value {
5+
Boolean(Option<bool>),
6+
Float(Option<f64>), //~ ERROR the trait bound `f64: Eq` is not satisfied
7+
}
8+
9+
fn main() {
10+
let a = Value::Float(Some(f64::NAN));
11+
assert!(a == a);
12+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0277]: the trait bound `f64: Eq` is not satisfied
2+
--> $DIR/issue-103157.rs:6:11
3+
|
4+
LL | #[derive(PartialEq, Eq)]
5+
| -- in this derive macro expansion
6+
...
7+
LL | Float(Option<f64>),
8+
| ^^^^^^^^^^^ the trait `Eq` is not implemented for `f64`
9+
|
10+
= help: the following other types implement trait `Eq`:
11+
i128
12+
i16
13+
i32
14+
i64
15+
i8
16+
isize
17+
u128
18+
u16
19+
and 4 others
20+
= note: required for `Option<f64>` to implement `Eq`
21+
note: required by a bound in `AssertParamIsEq`
22+
--> $SRC_DIR/core/src/cmp.rs:LL:COL
23+
|
24+
LL | pub struct AssertParamIsEq<T: Eq + ?Sized> {
25+
| ^^ required by this bound in `AssertParamIsEq`
26+
= note: this error originates in the derive macro `Eq` (in Nightly builds, run with -Z macro-backtrace for more info)
27+
28+
error: aborting due to previous error
29+
30+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)
Please sign in to comment.