Skip to content

Commit 6be420b

Browse files
committed
Use more Iterators instead of Vecs
1 parent 2cb4b58 commit 6be420b

File tree

9 files changed

+60
-81
lines changed

9 files changed

+60
-81
lines changed

src/librustc_middle/ty/trait_def.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,13 @@ impl<'tcx> TyCtxt<'tcx> {
168168
}
169169

170170
/// Returns a vector containing all impls
171-
pub fn all_impls(self, def_id: DefId) -> Vec<DefId> {
172-
let impls = self.trait_impls_of(def_id);
171+
pub fn all_impls(self, def_id: DefId) -> impl Iterator<Item = DefId> + 'tcx {
172+
let TraitImpls { blanket_impls, non_blanket_impls } = self.trait_impls_of(def_id);
173173

174-
impls
175-
.blanket_impls
176-
.iter()
177-
.chain(impls.non_blanket_impls.values().flatten())
174+
blanket_impls
175+
.into_iter()
176+
.chain(non_blanket_impls.into_iter().map(|(_, v)| v).flatten())
178177
.cloned()
179-
.collect()
180178
}
181179
}
182180

src/librustc_trait_selection/traits/error_reporting/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1208,22 +1208,18 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
12081208

12091209
match simp {
12101210
Some(simp) => all_impls
1211-
.iter()
1212-
.filter_map(|&def_id| {
1211+
.filter_map(|def_id| {
12131212
let imp = self.tcx.impl_trait_ref(def_id).unwrap();
12141213
let imp_simp = fast_reject::simplify_type(self.tcx, imp.self_ty(), true);
12151214
if let Some(imp_simp) = imp_simp {
12161215
if simp != imp_simp {
12171216
return None;
12181217
}
12191218
}
1220-
12211219
Some(imp)
12221220
})
12231221
.collect(),
1224-
None => {
1225-
all_impls.iter().map(|&def_id| self.tcx.impl_trait_ref(def_id).unwrap()).collect()
1226-
}
1222+
None => all_impls.map(|def_id| self.tcx.impl_trait_ref(def_id).unwrap()).collect(),
12271223
}
12281224
}
12291225

src/librustc_trait_selection/traits/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ pub enum TraitQueryMode {
110110
pub fn predicates_for_generics<'tcx>(
111111
cause: ObligationCause<'tcx>,
112112
param_env: ty::ParamEnv<'tcx>,
113-
generic_bounds: &ty::InstantiatedPredicates<'tcx>,
114-
) -> PredicateObligations<'tcx> {
113+
generic_bounds: ty::InstantiatedPredicates<'tcx>,
114+
) -> impl Iterator<Item = PredicateObligation<'tcx>> {
115115
util::predicates_for_generics(cause, 0, param_env, generic_bounds)
116116
}
117117

src/librustc_trait_selection/traits/specialize/mod.rs

+15-19
Original file line numberDiff line numberDiff line change
@@ -189,26 +189,22 @@ fn fulfill_implication<'a, 'tcx>(
189189

190190
let selcx = &mut SelectionContext::new(&infcx);
191191
let target_substs = infcx.fresh_substs_for_item(DUMMY_SP, target_impl);
192-
let (target_trait_ref, mut obligations) =
192+
let (target_trait_ref, obligations) =
193193
impl_trait_ref_and_oblig(selcx, param_env, target_impl, target_substs);
194-
debug!(
195-
"fulfill_implication: target_trait_ref={:?}, obligations={:?}",
196-
target_trait_ref, obligations
197-
);
198194

199195
// do the impls unify? If not, no specialization.
200-
match infcx.at(&ObligationCause::dummy(), param_env).eq(source_trait_ref, target_trait_ref) {
201-
Ok(InferOk { obligations: o, .. }) => {
202-
obligations.extend(o);
203-
}
204-
Err(_) => {
205-
debug!(
206-
"fulfill_implication: {:?} does not unify with {:?}",
207-
source_trait_ref, target_trait_ref
208-
);
209-
return Err(());
210-
}
211-
}
196+
let more_obligations =
197+
match infcx.at(&ObligationCause::dummy(), param_env).eq(source_trait_ref, target_trait_ref)
198+
{
199+
Ok(InferOk { obligations, .. }) => obligations,
200+
Err(_) => {
201+
debug!(
202+
"fulfill_implication: {:?} does not unify with {:?}",
203+
source_trait_ref, target_trait_ref
204+
);
205+
return Err(());
206+
}
207+
};
212208

213209
// attempt to prove all of the predicates for impl2 given those for impl1
214210
// (which are packed up in penv)
@@ -226,7 +222,7 @@ fn fulfill_implication<'a, 'tcx>(
226222
// we already make a mockery out of the region system, so
227223
// why not ignore them a bit earlier?
228224
let mut fulfill_cx = FulfillmentContext::new_ignoring_regions();
229-
for oblig in obligations.into_iter() {
225+
for oblig in obligations.chain(more_obligations) {
230226
fulfill_cx.register_predicate_obligation(&infcx, oblig);
231227
}
232228
match fulfill_cx.select_all_or_error(infcx) {
@@ -261,7 +257,7 @@ pub(super) fn specialization_graph_provider(
261257
) -> &specialization_graph::Graph {
262258
let mut sg = specialization_graph::Graph::new();
263259

264-
let mut trait_impls = tcx.all_impls(trait_id);
260+
let mut trait_impls: Vec<_> = tcx.all_impls(trait_id).collect();
265261

266262
// The coherence checking implementation seems to rely on impls being
267263
// iterated over (roughly) in definition order, so we are sorting by

src/librustc_trait_selection/traits/util.rs

+16-24
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,10 @@ impl<'tcx> TraitAliasExpansionInfo<'tcx> {
8181

8282
pub fn expand_trait_aliases<'tcx>(
8383
tcx: TyCtxt<'tcx>,
84-
trait_refs: impl IntoIterator<Item = (ty::PolyTraitRef<'tcx>, Span)>,
84+
trait_refs: impl Iterator<Item = (ty::PolyTraitRef<'tcx>, Span)>,
8585
) -> TraitAliasExpander<'tcx> {
86-
let items: Vec<_> = trait_refs
87-
.into_iter()
88-
.map(|(trait_ref, span)| TraitAliasExpansionInfo::new(trait_ref, span))
89-
.collect();
86+
let items: Vec<_> =
87+
trait_refs.map(|(trait_ref, span)| TraitAliasExpansionInfo::new(trait_ref, span)).collect();
9088
TraitAliasExpander { tcx, stack: items }
9189
}
9290

@@ -199,7 +197,7 @@ pub fn impl_trait_ref_and_oblig<'a, 'tcx>(
199197
param_env: ty::ParamEnv<'tcx>,
200198
impl_def_id: DefId,
201199
impl_substs: SubstsRef<'tcx>,
202-
) -> (ty::TraitRef<'tcx>, Vec<PredicateObligation<'tcx>>) {
200+
) -> (ty::TraitRef<'tcx>, impl Iterator<Item = PredicateObligation<'tcx>>) {
203201
let impl_trait_ref = selcx.tcx().impl_trait_ref(impl_def_id).unwrap();
204202
let impl_trait_ref = impl_trait_ref.subst(selcx.tcx(), impl_substs);
205203
let Normalized { value: impl_trait_ref, obligations: normalization_obligations1 } =
@@ -210,13 +208,11 @@ pub fn impl_trait_ref_and_oblig<'a, 'tcx>(
210208
let Normalized { value: predicates, obligations: normalization_obligations2 } =
211209
super::normalize(selcx, param_env, ObligationCause::dummy(), &predicates);
212210
let impl_obligations =
213-
predicates_for_generics(ObligationCause::dummy(), 0, param_env, &predicates);
211+
predicates_for_generics(ObligationCause::dummy(), 0, param_env, predicates);
214212

215-
let impl_obligations: Vec<_> = impl_obligations
216-
.into_iter()
217-
.chain(normalization_obligations1)
218-
.chain(normalization_obligations2)
219-
.collect();
213+
let impl_obligations = impl_obligations
214+
.chain(normalization_obligations1.into_iter())
215+
.chain(normalization_obligations2.into_iter());
220216

221217
(impl_trait_ref, impl_obligations)
222218
}
@@ -226,20 +222,16 @@ pub fn predicates_for_generics<'tcx>(
226222
cause: ObligationCause<'tcx>,
227223
recursion_depth: usize,
228224
param_env: ty::ParamEnv<'tcx>,
229-
generic_bounds: &ty::InstantiatedPredicates<'tcx>,
230-
) -> Vec<PredicateObligation<'tcx>> {
225+
generic_bounds: ty::InstantiatedPredicates<'tcx>,
226+
) -> impl Iterator<Item = PredicateObligation<'tcx>> {
231227
debug!("predicates_for_generics(generic_bounds={:?})", generic_bounds);
232228

233-
generic_bounds
234-
.predicates
235-
.iter()
236-
.map(|&predicate| Obligation {
237-
cause: cause.clone(),
238-
recursion_depth,
239-
param_env,
240-
predicate,
241-
})
242-
.collect()
229+
generic_bounds.predicates.into_iter().map(move |predicate| Obligation {
230+
cause: cause.clone(),
231+
recursion_depth,
232+
param_env,
233+
predicate,
234+
})
243235
}
244236

245237
pub fn predicate_for_trait_ref<'tcx>(

src/librustc_typeck/check/method/confirm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
114114
// a custom error in that case.
115115
if illegal_sized_bound.is_none() {
116116
let method_ty = self.tcx.mk_fn_ptr(ty::Binder::bind(method_sig));
117-
self.add_obligations(method_ty, all_substs, &method_predicates);
117+
self.add_obligations(method_ty, all_substs, method_predicates);
118118
}
119119

120120
// Create the final `MethodCallee`.
@@ -395,7 +395,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
395395
&mut self,
396396
fty: Ty<'tcx>,
397397
all_substs: SubstsRef<'tcx>,
398-
method_predicates: &ty::InstantiatedPredicates<'tcx>,
398+
method_predicates: ty::InstantiatedPredicates<'tcx>,
399399
) {
400400
debug!(
401401
"add_obligations: fty={:?} all_substs={:?} method_predicates={:?}",

src/librustc_typeck/check/method/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
390390
assert!(!bounds.has_escaping_bound_vars());
391391

392392
let cause = traits::ObligationCause::misc(span, self.body_id);
393-
obligations.extend(traits::predicates_for_generics(cause.clone(), self.param_env, &bounds));
393+
obligations.extend(traits::predicates_for_generics(cause.clone(), self.param_env, bounds));
394394

395395
// Also add an obligation for the method type being well-formed.
396396
let method_ty = tcx.mk_fn_ptr(ty::Binder::bind(fn_sig));

src/librustc_typeck/check/method/probe.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
13421342
// clauses) that must be considered. Make sure that those
13431343
// match as well (or at least may match, sometimes we
13441344
// don't have enough information to fully evaluate).
1345-
let candidate_obligations: Vec<_> = match probe.kind {
1345+
match probe.kind {
13461346
InherentImplCandidate(ref substs, ref ref_obligations) => {
13471347
// Check whether the impl imposes obligations we have to worry about.
13481348
let impl_def_id = probe.item.container.id();
@@ -1353,19 +1353,23 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
13531353

13541354
// Convert the bounds into obligations.
13551355
let impl_obligations =
1356-
traits::predicates_for_generics(cause, self.param_env, &impl_bounds);
1356+
traits::predicates_for_generics(cause, self.param_env, impl_bounds);
13571357

1358-
debug!("impl_obligations={:?}", impl_obligations);
1359-
impl_obligations
1360-
.into_iter()
1358+
let candidate_obligations = impl_obligations
13611359
.chain(norm_obligations.into_iter())
1362-
.chain(ref_obligations.iter().cloned())
1363-
.collect()
1360+
.chain(ref_obligations.iter().cloned());
1361+
// Evaluate those obligations to see if they might possibly hold.
1362+
for o in candidate_obligations {
1363+
let o = self.resolve_vars_if_possible(&o);
1364+
if !self.predicate_may_hold(&o) {
1365+
result = ProbeResult::NoMatch;
1366+
possibly_unsatisfied_predicates.push((o.predicate, None));
1367+
}
1368+
}
13641369
}
13651370

13661371
ObjectCandidate | WhereClauseCandidate(..) => {
13671372
// These have no additional conditions to check.
1368-
vec![]
13691373
}
13701374

13711375
TraitCandidate(trait_ref) => {
@@ -1412,17 +1416,11 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14121416
return ProbeResult::NoMatch;
14131417
}
14141418
}
1415-
vec![]
14161419
}
1417-
};
1418-
1419-
debug!(
1420-
"consider_probe - candidate_obligations={:?} sub_obligations={:?}",
1421-
candidate_obligations, sub_obligations
1422-
);
1420+
}
14231421

14241422
// Evaluate those obligations to see if they might possibly hold.
1425-
for o in candidate_obligations.into_iter().chain(sub_obligations) {
1423+
for o in sub_obligations {
14261424
let o = self.resolve_vars_if_possible(&o);
14271425
if !self.predicate_may_hold(&o) {
14281426
result = ProbeResult::NoMatch;

src/librustc_typeck/check/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -3434,7 +3434,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34343434
pub fn add_obligations_for_parameters(
34353435
&self,
34363436
cause: traits::ObligationCause<'tcx>,
3437-
predicates: &ty::InstantiatedPredicates<'tcx>,
3437+
predicates: ty::InstantiatedPredicates<'tcx>,
34383438
) {
34393439
assert!(!predicates.has_escaping_bound_vars());
34403440

@@ -4385,7 +4385,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
43854385
let (bounds, _) = self.instantiate_bounds(path_span, did, substs);
43864386
let cause =
43874387
traits::ObligationCause::new(path_span, self.body_id, traits::ItemObligation(did));
4388-
self.add_obligations_for_parameters(cause, &bounds);
4388+
self.add_obligations_for_parameters(cause, bounds);
43894389

43904390
Some((variant, ty))
43914391
} else {
@@ -5654,9 +5654,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
56545654
for (i, mut obligation) in traits::predicates_for_generics(
56555655
traits::ObligationCause::new(span, self.body_id, traits::ItemObligation(def_id)),
56565656
self.param_env,
5657-
&bounds,
5657+
bounds,
56585658
)
5659-
.into_iter()
56605659
.enumerate()
56615660
{
56625661
// This makes the error point at the bound, but we want to point at the argument

0 commit comments

Comments
 (0)