Skip to content

Commit

Permalink
iterate List by value
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed May 23, 2020
1 parent 647ae50 commit da57ced
Show file tree
Hide file tree
Showing 40 changed files with 82 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn push_debuginfo_type_name<'tcx>(
}
ty::Tuple(component_types) => {
output.push('(');
for &component_type in component_types {
for component_type in component_types {
push_debuginfo_type_name(tcx, component_type.expect_ty(), true, output, visited);
output.push_str(", ");
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_infer/infer/canonical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
) -> CanonicalVarValues<'tcx> {
let var_values: IndexVec<BoundVar, GenericArg<'tcx>> = variables
.iter()
.map(|info| self.instantiate_canonical_var(span, *info, &universe_map))
.map(|info| self.instantiate_canonical_var(span, info, &universe_map))
.collect();

CanonicalVarValues { var_values }
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_infer/infer/canonical/query_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,12 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
if info.is_existential() {
match opt_values[BoundVar::new(index)] {
Some(k) => k,
None => self.instantiate_canonical_var(cause.span, *info, |u| {
None => self.instantiate_canonical_var(cause.span, info, |u| {
universe_map[u.as_usize()]
}),
}
} else {
self.instantiate_canonical_var(cause.span, *info, |u| {
self.instantiate_canonical_var(cause.span, info, |u| {
universe_map[u.as_usize()]
})
}
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_infer/infer/outlives/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
// for further background and discussion.
let mut bounds = substs
.iter()
.filter_map(|&child| match child.unpack() {
.filter_map(|child| match child.unpack() {
GenericArgKind::Type(ty) => Some(self.type_bound(ty)),
GenericArgKind::Lifetime(_) => None,
GenericArgKind::Const(_) => Some(self.recursive_bound(child)),
Expand Down Expand Up @@ -223,8 +223,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
// like `T` and `T::Item`. It may not work as well for things
// like `<T as Foo<'a>>::Item`.
let c_b = self.param_env.caller_bounds;
let param_bounds =
self.collect_outlives_from_predicate_list(&compare_ty, c_b.into_iter().copied());
let param_bounds = self.collect_outlives_from_predicate_list(&compare_ty, c_b.into_iter());

// Next, collect regions we scraped from the well-formedness
// constraints in the fn signature. To do that, we walk the list
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_middle/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2077,10 +2077,10 @@ impl Debug for Place<'_> {
ProjectionElem::ConstantIndex { offset, min_length, from_end: true } => {
write!(fmt, "[-{:?} of {:?}]", offset, min_length)?;
}
ProjectionElem::Subslice { from, to, from_end: true } if *to == 0 => {
ProjectionElem::Subslice { from, to, from_end: true } if to == 0 => {
write!(fmt, "[{:?}:]", from)?;
}
ProjectionElem::Subslice { from, to, from_end: true } if *from == 0 => {
ProjectionElem::Subslice { from, to, from_end: true } if from == 0 => {
write!(fmt, "[:-{:?}]", to)?;
}
ProjectionElem::Subslice { from, to, from_end: true } => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl FlagComputation {
&ty::Dynamic(ref obj, r) => {
let mut computation = FlagComputation::new();
for predicate in obj.skip_binder().iter() {
match *predicate {
match predicate {
ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),
ty::ExistentialPredicate::Projection(p) => {
let mut proj_computation = FlagComputation::new();
Expand Down
22 changes: 18 additions & 4 deletions src/librustc_middle/ty/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rustc_serialize::{Encodable, Encoder};
use std::cmp::{self, Ordering};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::iter;
use std::mem;
use std::ops::Deref;
use std::ptr;
Expand All @@ -21,6 +22,10 @@ extern "C" {
/// the same contents can exist in the same context.
/// This means we can use pointer for both
/// equality comparisons and hashing.
///
/// Unlike slices, The types contained in `List` are expected to be `Copy`
/// and iterating over a `List` returns `T` instead of a reference.
///
/// Note: `Slice` was already taken by the `Ty`.
#[repr(C)]
pub struct List<T> {
Expand Down Expand Up @@ -61,6 +66,15 @@ impl<T: Copy> List<T> {
result
}
}

// If this method didn't exist, we would use `slice.iter` due to
// deref coercion.
//
// This would be weird, as `self.into_iter` iterates over `T` directly.
#[inline(always)]
pub fn iter(&self) -> <&'_ List<T> as IntoIterator>::IntoIter {
self.into_iter()
}
}

impl<T: fmt::Debug> fmt::Debug for List<T> {
Expand Down Expand Up @@ -128,12 +142,12 @@ impl<T> AsRef<[T]> for List<T> {
}
}

impl<'a, T> IntoIterator for &'a List<T> {
type Item = &'a T;
type IntoIter = <&'a [T] as IntoIterator>::IntoIter;
impl<'a, T: Copy> IntoIterator for &'a List<T> {
type Item = T;
type IntoIter = iter::Copied<<&'a [T] as IntoIterator>::IntoIter>;
#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
self[..].iter()
self[..].iter().copied()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/outlives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn compute_components(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, out: &mut SmallVec<[Compo
// consistent with previous (accidental) behavior.
// See https://github.com/rust-lang/rust/issues/70917
// for further background and discussion.
for &child in substs {
for child in substs {
match child.unpack() {
GenericArgKind::Type(ty) => {
compute_components(tcx, ty, out);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/print/obsolete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl DefPathBasedNames<'tcx> {
}
ty::Tuple(component_types) => {
output.push('(');
for &component_type in component_types {
for component_type in component_types {
self.push_type_name(component_type.expect_ty(), output, debug);
output.push_str(", ");
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_middle/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ pub trait PrettyPrinter<'tcx>:
}
ty::Never => p!(write("!")),
ty::Tuple(ref tys) => {
p!(write("("), comma_sep(tys.iter().copied()));
p!(write("("), comma_sep(tys.iter()));
if tys.len() == 1 {
p!(write(","));
}
Expand Down Expand Up @@ -560,7 +560,7 @@ pub trait PrettyPrinter<'tcx>:
// FIXME(eddyb) print this with `print_def_path`.
if !substs.is_empty() {
p!(write("::"));
p!(generic_delimiters(|cx| cx.comma_sep(substs.iter().copied())));
p!(generic_delimiters(|cx| cx.comma_sep(substs.iter())));
}
return Ok(self);
}
Expand Down Expand Up @@ -1935,7 +1935,7 @@ define_print_and_forward_display! {
(self, cx):

&'tcx ty::List<Ty<'tcx>> {
p!(write("{{"), comma_sep(self.iter().copied()), write("}}"))
p!(write("{{"), comma_sep(self.iter()), write("}}"))
}

ty::TypeAndMut<'tcx> {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_middle/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub fn relate_substs<R: TypeRelation<'tcx>>(

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

Ok(tcx.mk_substs(params)?)
Expand Down Expand Up @@ -319,7 +319,7 @@ impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
) -> RelateResult<'tcx, GeneratorWitness<'tcx>> {
assert_eq!(a.0.len(), b.0.len());
let tcx = relation.tcx();
let types = tcx.mk_type_list(a.0.iter().zip(b.0).map(|(a, b)| relation.relate(a, b)))?;
let types = tcx.mk_type_list(a.0.iter().zip(b.0).map(|(a, b)| relation.relate(&a, &b)))?;
Ok(GeneratorWitness(types))
}
}
Expand Down Expand Up @@ -633,7 +633,7 @@ impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
let tcx = relation.tcx();
let v = a.iter().zip(b.iter()).map(|(ep_a, ep_b)| {
use crate::ty::ExistentialPredicate::*;
match (*ep_a, *ep_b) {
match (ep_a, ep_b) {
(Trait(ref a), Trait(ref b)) => Ok(Trait(relation.relate(a, b)?)),
(Projection(ref a), Projection(ref b)) => Ok(Projection(relation.relate(a, b)?)),
(AutoTrait(ref a), AutoTrait(ref b)) if a == b => Ok(AutoTrait(*a)),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ where
// Look for the first element that changed
if let Some((i, new_t)) = iter.by_ref().enumerate().find_map(|(i, t)| {
let new_t = t.fold_with(folder);
if new_t == *t { None } else { Some((i, new_t)) }
if new_t == t { None } else { Some((i, new_t)) }
}) {
// An element changed, prepare to intern the resulting list
let mut new_list = SmallVec::<[_; 8]>::with_capacity(list.len());
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_middle/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,15 +670,15 @@ impl<'tcx> List<ExistentialPredicate<'tcx>> {
pub fn projection_bounds<'a>(
&'a self,
) -> impl Iterator<Item = ExistentialProjection<'tcx>> + 'a {
self.iter().filter_map(|predicate| match *predicate {
self.iter().filter_map(|predicate| match predicate {
ExistentialPredicate::Projection(projection) => Some(projection),
_ => None,
})
}

#[inline]
pub fn auto_traits<'a>(&'a self) -> impl Iterator<Item = DefId> + 'a {
self.iter().filter_map(|predicate| match *predicate {
self.iter().filter_map(|predicate| match predicate {
ExistentialPredicate::AutoTrait(did) => Some(did),
_ => None,
})
Expand Down Expand Up @@ -709,7 +709,7 @@ impl<'tcx> Binder<&'tcx List<ExistentialPredicate<'tcx>>> {
pub fn iter<'a>(
&'a self,
) -> impl DoubleEndedIterator<Item = Binder<ExistentialPredicate<'tcx>>> + 'tcx {
self.skip_binder().iter().cloned().map(Binder::bind)
self.skip_binder().iter().map(Binder::bind)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_middle/ty/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,11 @@ impl<'a, 'tcx> InternalSubsts<'tcx> {
target_substs: SubstsRef<'tcx>,
) -> SubstsRef<'tcx> {
let defs = tcx.generics_of(source_ancestor);
tcx.mk_substs(target_substs.iter().chain(&self[defs.params.len()..]).cloned())
tcx.mk_substs(target_substs.iter().chain(self.iter().skip(defs.params.len())))
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_middle/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ impl<'tcx> TyCtxt<'tcx> {
let result = item_substs
.iter()
.zip(impl_substs.iter())
.filter(|&(_, &k)| {
.filter(|&(_, k)| {
match k.unpack() {
GenericArgKind::Lifetime(&ty::RegionKind::ReEarlyBound(ref ebr)) => {
!impl_generics.region_param(ebr, self).pure_wrt_drop
Expand All @@ -433,7 +433,7 @@ impl<'tcx> TyCtxt<'tcx> {
}
}
})
.map(|(&item_param, _)| item_param)
.map(|(item_param, _)| item_param)
.collect();
debug!("destructor_constraint({:?}) = {:?}", def.did, result);
result
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_middle/ty/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
stack.push(lt.into());
}
ty::Projection(data) => {
stack.extend(data.substs.iter().copied().rev());
stack.extend(data.substs.iter().rev());
}
ty::Dynamic(obj, lt) => {
stack.push(lt.into());
Expand All @@ -143,7 +143,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
}
};

substs.iter().copied().rev().chain(opt_ty.map(|ty| ty.into()))
substs.iter().rev().chain(opt_ty.map(|ty| ty.into()))
}));
}
ty::Adt(_, substs)
Expand All @@ -152,14 +152,14 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
| ty::Generator(_, substs, _)
| ty::Tuple(substs)
| ty::FnDef(_, substs) => {
stack.extend(substs.iter().copied().rev());
stack.extend(substs.iter().rev());
}
ty::GeneratorWitness(ts) => {
stack.extend(ts.skip_binder().iter().cloned().rev().map(|ty| ty.into()));
stack.extend(ts.skip_binder().iter().rev().map(|ty| ty.into()));
}
ty::FnPtr(sig) => {
stack.push(sig.skip_binder().output().into());
stack.extend(sig.skip_binder().inputs().iter().cloned().rev().map(|ty| ty.into()));
stack.extend(sig.skip_binder().inputs().iter().copied().rev().map(|ty| ty.into()));
}
},
GenericArgKind::Lifetime(_) => {}
Expand All @@ -174,7 +174,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
| ty::ConstKind::Error => {}

ty::ConstKind::Unevaluated(_, substs, _) => {
stack.extend(substs.iter().copied().rev());
stack.extend(substs.iter().rev());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/place_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
for (i, elem) in self.projection.iter().enumerate() {
let proj_base = &self.projection[..i];

if *elem == ProjectionElem::Deref {
if elem == ProjectionElem::Deref {
let ty = Place::ty_from(self.local, proj_base, body, tcx).ty;
match ty.kind {
ty::Ref(_, _, hir::Mutability::Not) if i == 0 => {
Expand Down
14 changes: 7 additions & 7 deletions src/librustc_mir/borrow_check/places_conflict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ fn place_components_conflict<'tcx>(
body,
borrow_local,
borrow_proj_base,
borrow_c,
access_c,
&borrow_c,
&access_c,
bias,
) {
Overlap::Arbitrary => {
Expand Down Expand Up @@ -420,24 +420,24 @@ fn place_projection_conflict<'tcx>(
}
}
(
ProjectionElem::ConstantIndex {
&ProjectionElem::ConstantIndex {
offset: offset_from_begin,
min_length: min_length1,
from_end: false,
},
ProjectionElem::ConstantIndex {
&ProjectionElem::ConstantIndex {
offset: offset_from_end,
min_length: min_length2,
from_end: true,
},
)
| (
ProjectionElem::ConstantIndex {
&ProjectionElem::ConstantIndex {
offset: offset_from_end,
min_length: min_length1,
from_end: true,
},
ProjectionElem::ConstantIndex {
&ProjectionElem::ConstantIndex {
offset: offset_from_begin,
min_length: min_length2,
from_end: false,
Expand All @@ -449,7 +449,7 @@ fn place_projection_conflict<'tcx>(
// element (like -1 in Python) and `min_length` the first.
// Therefore, `min_length - offset_from_end` gives the minimal possible
// offset from the beginning
if *offset_from_begin >= *min_length - *offset_from_end {
if offset_from_begin >= min_length - offset_from_end {
debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY-CONSTANT-INDEX-FE");
Overlap::EqualOrDisjoint
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
return PlaceTy::from_ty(self.tcx().types.err);
}
}
place_ty = self.sanitize_projection(place_ty, elem, place, location)
place_ty = self.sanitize_projection(place_ty, &elem, place, location)
}

if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) = context {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/dataflow/move_paths/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
};

if union_path.is_none() {
base = self.add_move_path(base, elem, |tcx| Place {
base = self.add_move_path(base, &elem, |tcx| Place {
local: place.local,
projection: tcx.intern_place_elems(&place.projection[..i + 1]),
});
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let op = place
.projection
.iter()
.try_fold(base_op, |op, elem| self.operand_projection(op, elem))?;
.try_fold(base_op, |op, elem| self.operand_projection(op, &elem))?;

trace!("eval_place_to_op: got {:?}", *op);
Ok(op)
Expand Down
Loading

0 comments on commit da57ced

Please sign in to comment.