Skip to content

Commit da57ced

Browse files
committed
iterate List by value
1 parent 647ae50 commit da57ced

File tree

40 files changed

+82
-72
lines changed

40 files changed

+82
-72
lines changed

src/librustc_codegen_ssa/debuginfo/type_names.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn push_debuginfo_type_name<'tcx>(
4848
}
4949
ty::Tuple(component_types) => {
5050
output.push('(');
51-
for &component_type in component_types {
51+
for component_type in component_types {
5252
push_debuginfo_type_name(tcx, component_type.expect_ty(), true, output, visited);
5353
output.push_str(", ");
5454
}

src/librustc_infer/infer/canonical/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
8787
) -> CanonicalVarValues<'tcx> {
8888
let var_values: IndexVec<BoundVar, GenericArg<'tcx>> = variables
8989
.iter()
90-
.map(|info| self.instantiate_canonical_var(span, *info, &universe_map))
90+
.map(|info| self.instantiate_canonical_var(span, info, &universe_map))
9191
.collect();
9292

9393
CanonicalVarValues { var_values }

src/librustc_infer/infer/canonical/query_response.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,12 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
464464
if info.is_existential() {
465465
match opt_values[BoundVar::new(index)] {
466466
Some(k) => k,
467-
None => self.instantiate_canonical_var(cause.span, *info, |u| {
467+
None => self.instantiate_canonical_var(cause.span, info, |u| {
468468
universe_map[u.as_usize()]
469469
}),
470470
}
471471
} else {
472-
self.instantiate_canonical_var(cause.span, *info, |u| {
472+
self.instantiate_canonical_var(cause.span, info, |u| {
473473
universe_map[u.as_usize()]
474474
})
475475
}

src/librustc_infer/infer/outlives/verify.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
5050
// for further background and discussion.
5151
let mut bounds = substs
5252
.iter()
53-
.filter_map(|&child| match child.unpack() {
53+
.filter_map(|child| match child.unpack() {
5454
GenericArgKind::Type(ty) => Some(self.type_bound(ty)),
5555
GenericArgKind::Lifetime(_) => None,
5656
GenericArgKind::Const(_) => Some(self.recursive_bound(child)),
@@ -223,8 +223,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
223223
// like `T` and `T::Item`. It may not work as well for things
224224
// like `<T as Foo<'a>>::Item`.
225225
let c_b = self.param_env.caller_bounds;
226-
let param_bounds =
227-
self.collect_outlives_from_predicate_list(&compare_ty, c_b.into_iter().copied());
226+
let param_bounds = self.collect_outlives_from_predicate_list(&compare_ty, c_b.into_iter());
228227

229228
// Next, collect regions we scraped from the well-formedness
230229
// constraints in the fn signature. To do that, we walk the list

src/librustc_middle/mir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2077,10 +2077,10 @@ impl Debug for Place<'_> {
20772077
ProjectionElem::ConstantIndex { offset, min_length, from_end: true } => {
20782078
write!(fmt, "[-{:?} of {:?}]", offset, min_length)?;
20792079
}
2080-
ProjectionElem::Subslice { from, to, from_end: true } if *to == 0 => {
2080+
ProjectionElem::Subslice { from, to, from_end: true } if to == 0 => {
20812081
write!(fmt, "[{:?}:]", from)?;
20822082
}
2083-
ProjectionElem::Subslice { from, to, from_end: true } if *from == 0 => {
2083+
ProjectionElem::Subslice { from, to, from_end: true } if from == 0 => {
20842084
write!(fmt, "[:-{:?}]", to)?;
20852085
}
20862086
ProjectionElem::Subslice { from, to, from_end: true } => {

src/librustc_middle/ty/flags.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl FlagComputation {
129129
&ty::Dynamic(ref obj, r) => {
130130
let mut computation = FlagComputation::new();
131131
for predicate in obj.skip_binder().iter() {
132-
match *predicate {
132+
match predicate {
133133
ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),
134134
ty::ExistentialPredicate::Projection(p) => {
135135
let mut proj_computation = FlagComputation::new();

src/librustc_middle/ty/list.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_serialize::{Encodable, Encoder};
55
use std::cmp::{self, Ordering};
66
use std::fmt;
77
use std::hash::{Hash, Hasher};
8+
use std::iter;
89
use std::mem;
910
use std::ops::Deref;
1011
use std::ptr;
@@ -21,6 +22,10 @@ extern "C" {
2122
/// the same contents can exist in the same context.
2223
/// This means we can use pointer for both
2324
/// equality comparisons and hashing.
25+
///
26+
/// Unlike slices, The types contained in `List` are expected to be `Copy`
27+
/// and iterating over a `List` returns `T` instead of a reference.
28+
///
2429
/// Note: `Slice` was already taken by the `Ty`.
2530
#[repr(C)]
2631
pub struct List<T> {
@@ -61,6 +66,15 @@ impl<T: Copy> List<T> {
6166
result
6267
}
6368
}
69+
70+
// If this method didn't exist, we would use `slice.iter` due to
71+
// deref coercion.
72+
//
73+
// This would be weird, as `self.into_iter` iterates over `T` directly.
74+
#[inline(always)]
75+
pub fn iter(&self) -> <&'_ List<T> as IntoIterator>::IntoIter {
76+
self.into_iter()
77+
}
6478
}
6579

6680
impl<T: fmt::Debug> fmt::Debug for List<T> {
@@ -128,12 +142,12 @@ impl<T> AsRef<[T]> for List<T> {
128142
}
129143
}
130144

131-
impl<'a, T> IntoIterator for &'a List<T> {
132-
type Item = &'a T;
133-
type IntoIter = <&'a [T] as IntoIterator>::IntoIter;
145+
impl<'a, T: Copy> IntoIterator for &'a List<T> {
146+
type Item = T;
147+
type IntoIter = iter::Copied<<&'a [T] as IntoIterator>::IntoIter>;
134148
#[inline(always)]
135149
fn into_iter(self) -> Self::IntoIter {
136-
self[..].iter()
150+
self[..].iter().copied()
137151
}
138152
}
139153

src/librustc_middle/ty/outlives.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn compute_components(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, out: &mut SmallVec<[Compo
7070
// consistent with previous (accidental) behavior.
7171
// See https://github.com/rust-lang/rust/issues/70917
7272
// for further background and discussion.
73-
for &child in substs {
73+
for child in substs {
7474
match child.unpack() {
7575
GenericArgKind::Type(ty) => {
7676
compute_components(tcx, ty, out);

src/librustc_middle/ty/print/obsolete.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl DefPathBasedNames<'tcx> {
4747
}
4848
ty::Tuple(component_types) => {
4949
output.push('(');
50-
for &component_type in component_types {
50+
for component_type in component_types {
5151
self.push_type_name(component_type.expect_ty(), output, debug);
5252
output.push_str(", ");
5353
}

src/librustc_middle/ty/print/pretty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ pub trait PrettyPrinter<'tcx>:
495495
}
496496
ty::Never => p!(write("!")),
497497
ty::Tuple(ref tys) => {
498-
p!(write("("), comma_sep(tys.iter().copied()));
498+
p!(write("("), comma_sep(tys.iter()));
499499
if tys.len() == 1 {
500500
p!(write(","));
501501
}
@@ -560,7 +560,7 @@ pub trait PrettyPrinter<'tcx>:
560560
// FIXME(eddyb) print this with `print_def_path`.
561561
if !substs.is_empty() {
562562
p!(write("::"));
563-
p!(generic_delimiters(|cx| cx.comma_sep(substs.iter().copied())));
563+
p!(generic_delimiters(|cx| cx.comma_sep(substs.iter())));
564564
}
565565
return Ok(self);
566566
}
@@ -1935,7 +1935,7 @@ define_print_and_forward_display! {
19351935
(self, cx):
19361936

19371937
&'tcx ty::List<Ty<'tcx>> {
1938-
p!(write("{{"), comma_sep(self.iter().copied()), write("}}"))
1938+
p!(write("{{"), comma_sep(self.iter()), write("}}"))
19391939
}
19401940

19411941
ty::TypeAndMut<'tcx> {

src/librustc_middle/ty/relate.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub fn relate_substs<R: TypeRelation<'tcx>>(
143143

144144
let params = a_subst.iter().zip(b_subst).enumerate().map(|(i, (a, b))| {
145145
let variance = variances.map_or(ty::Invariant, |v| v[i]);
146-
relation.relate_with_variance(variance, a, b)
146+
relation.relate_with_variance(variance, &a, &b)
147147
});
148148

149149
Ok(tcx.mk_substs(params)?)
@@ -319,7 +319,7 @@ impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
319319
) -> RelateResult<'tcx, GeneratorWitness<'tcx>> {
320320
assert_eq!(a.0.len(), b.0.len());
321321
let tcx = relation.tcx();
322-
let types = tcx.mk_type_list(a.0.iter().zip(b.0).map(|(a, b)| relation.relate(a, b)))?;
322+
let types = tcx.mk_type_list(a.0.iter().zip(b.0).map(|(a, b)| relation.relate(&a, &b)))?;
323323
Ok(GeneratorWitness(types))
324324
}
325325
}
@@ -633,7 +633,7 @@ impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
633633
let tcx = relation.tcx();
634634
let v = a.iter().zip(b.iter()).map(|(ep_a, ep_b)| {
635635
use crate::ty::ExistentialPredicate::*;
636-
match (*ep_a, *ep_b) {
636+
match (ep_a, ep_b) {
637637
(Trait(ref a), Trait(ref b)) => Ok(Trait(relation.relate(a, b)?)),
638638
(Projection(ref a), Projection(ref b)) => Ok(Projection(relation.relate(a, b)?)),
639639
(AutoTrait(ref a), AutoTrait(ref b)) if a == b => Ok(AutoTrait(*a)),

src/librustc_middle/ty/structural_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ where
10931093
// Look for the first element that changed
10941094
if let Some((i, new_t)) = iter.by_ref().enumerate().find_map(|(i, t)| {
10951095
let new_t = t.fold_with(folder);
1096-
if new_t == *t { None } else { Some((i, new_t)) }
1096+
if new_t == t { None } else { Some((i, new_t)) }
10971097
}) {
10981098
// An element changed, prepare to intern the resulting list
10991099
let mut new_list = SmallVec::<[_; 8]>::with_capacity(list.len());

src/librustc_middle/ty/sty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -670,15 +670,15 @@ impl<'tcx> List<ExistentialPredicate<'tcx>> {
670670
pub fn projection_bounds<'a>(
671671
&'a self,
672672
) -> impl Iterator<Item = ExistentialProjection<'tcx>> + 'a {
673-
self.iter().filter_map(|predicate| match *predicate {
673+
self.iter().filter_map(|predicate| match predicate {
674674
ExistentialPredicate::Projection(projection) => Some(projection),
675675
_ => None,
676676
})
677677
}
678678

679679
#[inline]
680680
pub fn auto_traits<'a>(&'a self) -> impl Iterator<Item = DefId> + 'a {
681-
self.iter().filter_map(|predicate| match *predicate {
681+
self.iter().filter_map(|predicate| match predicate {
682682
ExistentialPredicate::AutoTrait(did) => Some(did),
683683
_ => None,
684684
})
@@ -709,7 +709,7 @@ impl<'tcx> Binder<&'tcx List<ExistentialPredicate<'tcx>>> {
709709
pub fn iter<'a>(
710710
&'a self,
711711
) -> impl DoubleEndedIterator<Item = Binder<ExistentialPredicate<'tcx>>> + 'tcx {
712-
self.skip_binder().iter().cloned().map(Binder::bind)
712+
self.skip_binder().iter().map(Binder::bind)
713713
}
714714
}
715715

src/librustc_middle/ty/subst.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,11 @@ impl<'a, 'tcx> InternalSubsts<'tcx> {
340340
target_substs: SubstsRef<'tcx>,
341341
) -> SubstsRef<'tcx> {
342342
let defs = tcx.generics_of(source_ancestor);
343-
tcx.mk_substs(target_substs.iter().chain(&self[defs.params.len()..]).cloned())
343+
tcx.mk_substs(target_substs.iter().chain(self.iter().skip(defs.params.len())))
344344
}
345345

346346
pub fn truncate_to(&self, tcx: TyCtxt<'tcx>, generics: &ty::Generics) -> SubstsRef<'tcx> {
347-
tcx.mk_substs(self.iter().take(generics.count()).cloned())
347+
tcx.mk_substs(self.iter().take(generics.count()))
348348
}
349349
}
350350

src/librustc_middle/ty/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl<'tcx> TyCtxt<'tcx> {
413413
let result = item_substs
414414
.iter()
415415
.zip(impl_substs.iter())
416-
.filter(|&(_, &k)| {
416+
.filter(|&(_, k)| {
417417
match k.unpack() {
418418
GenericArgKind::Lifetime(&ty::RegionKind::ReEarlyBound(ref ebr)) => {
419419
!impl_generics.region_param(ebr, self).pure_wrt_drop
@@ -433,7 +433,7 @@ impl<'tcx> TyCtxt<'tcx> {
433433
}
434434
}
435435
})
436-
.map(|(&item_param, _)| item_param)
436+
.map(|(item_param, _)| item_param)
437437
.collect();
438438
debug!("destructor_constraint({:?}) = {:?}", def.did, result);
439439
result

src/librustc_middle/ty/walk.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
128128
stack.push(lt.into());
129129
}
130130
ty::Projection(data) => {
131-
stack.extend(data.substs.iter().copied().rev());
131+
stack.extend(data.substs.iter().rev());
132132
}
133133
ty::Dynamic(obj, lt) => {
134134
stack.push(lt.into());
@@ -143,7 +143,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
143143
}
144144
};
145145

146-
substs.iter().copied().rev().chain(opt_ty.map(|ty| ty.into()))
146+
substs.iter().rev().chain(opt_ty.map(|ty| ty.into()))
147147
}));
148148
}
149149
ty::Adt(_, substs)
@@ -152,14 +152,14 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
152152
| ty::Generator(_, substs, _)
153153
| ty::Tuple(substs)
154154
| ty::FnDef(_, substs) => {
155-
stack.extend(substs.iter().copied().rev());
155+
stack.extend(substs.iter().rev());
156156
}
157157
ty::GeneratorWitness(ts) => {
158-
stack.extend(ts.skip_binder().iter().cloned().rev().map(|ty| ty.into()));
158+
stack.extend(ts.skip_binder().iter().rev().map(|ty| ty.into()));
159159
}
160160
ty::FnPtr(sig) => {
161161
stack.push(sig.skip_binder().output().into());
162-
stack.extend(sig.skip_binder().inputs().iter().cloned().rev().map(|ty| ty.into()));
162+
stack.extend(sig.skip_binder().inputs().iter().copied().rev().map(|ty| ty.into()));
163163
}
164164
},
165165
GenericArgKind::Lifetime(_) => {}
@@ -174,7 +174,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
174174
| ty::ConstKind::Error => {}
175175

176176
ty::ConstKind::Unevaluated(_, substs, _) => {
177-
stack.extend(substs.iter().copied().rev());
177+
stack.extend(substs.iter().rev());
178178
}
179179
}
180180
}

src/librustc_mir/borrow_check/place_ext.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
4747
for (i, elem) in self.projection.iter().enumerate() {
4848
let proj_base = &self.projection[..i];
4949

50-
if *elem == ProjectionElem::Deref {
50+
if elem == ProjectionElem::Deref {
5151
let ty = Place::ty_from(self.local, proj_base, body, tcx).ty;
5252
match ty.kind {
5353
ty::Ref(_, _, hir::Mutability::Not) if i == 0 => {

src/librustc_mir/borrow_check/places_conflict.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ fn place_components_conflict<'tcx>(
163163
body,
164164
borrow_local,
165165
borrow_proj_base,
166-
borrow_c,
167-
access_c,
166+
&borrow_c,
167+
&access_c,
168168
bias,
169169
) {
170170
Overlap::Arbitrary => {
@@ -420,24 +420,24 @@ fn place_projection_conflict<'tcx>(
420420
}
421421
}
422422
(
423-
ProjectionElem::ConstantIndex {
423+
&ProjectionElem::ConstantIndex {
424424
offset: offset_from_begin,
425425
min_length: min_length1,
426426
from_end: false,
427427
},
428-
ProjectionElem::ConstantIndex {
428+
&ProjectionElem::ConstantIndex {
429429
offset: offset_from_end,
430430
min_length: min_length2,
431431
from_end: true,
432432
},
433433
)
434434
| (
435-
ProjectionElem::ConstantIndex {
435+
&ProjectionElem::ConstantIndex {
436436
offset: offset_from_end,
437437
min_length: min_length1,
438438
from_end: true,
439439
},
440-
ProjectionElem::ConstantIndex {
440+
&ProjectionElem::ConstantIndex {
441441
offset: offset_from_begin,
442442
min_length: min_length2,
443443
from_end: false,
@@ -449,7 +449,7 @@ fn place_projection_conflict<'tcx>(
449449
// element (like -1 in Python) and `min_length` the first.
450450
// Therefore, `min_length - offset_from_end` gives the minimal possible
451451
// offset from the beginning
452-
if *offset_from_begin >= *min_length - *offset_from_end {
452+
if offset_from_begin >= min_length - offset_from_end {
453453
debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY-CONSTANT-INDEX-FE");
454454
Overlap::EqualOrDisjoint
455455
} else {

src/librustc_mir/borrow_check/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
497497
return PlaceTy::from_ty(self.tcx().types.err);
498498
}
499499
}
500-
place_ty = self.sanitize_projection(place_ty, elem, place, location)
500+
place_ty = self.sanitize_projection(place_ty, &elem, place, location)
501501
}
502502

503503
if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) = context {

src/librustc_mir/dataflow/move_paths/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
158158
};
159159

160160
if union_path.is_none() {
161-
base = self.add_move_path(base, elem, |tcx| Place {
161+
base = self.add_move_path(base, &elem, |tcx| Place {
162162
local: place.local,
163163
projection: tcx.intern_place_elems(&place.projection[..i + 1]),
164164
});

src/librustc_mir/interpret/operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
466466
let op = place
467467
.projection
468468
.iter()
469-
.try_fold(base_op, |op, elem| self.operand_projection(op, elem))?;
469+
.try_fold(base_op, |op, elem| self.operand_projection(op, &elem))?;
470470

471471
trace!("eval_place_to_op: got {:?}", *op);
472472
Ok(op)

0 commit comments

Comments
 (0)