Skip to content

Commit d6695bd

Browse files
committed
One more minor allocation optimization
Remove one unnecessary collect at the end of a fn
1 parent 9c9a046 commit d6695bd

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

compiler/rustc_typeck/src/astconv/generics.rs

+22
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,28 @@ pub fn create_substs_for_generic_args<'a, 'tcx>(
333333
tcx.intern_substs(&substs)
334334
}
335335

336+
/// Checks that the correct number of generic arguments have been provided.
337+
/// Used specifically for function calls.
338+
pub fn check_generic_arg_count_for_call(
339+
tcx: TyCtxt<'_>,
340+
span: Span,
341+
def: &ty::Generics,
342+
seg: &hir::PathSegment<'_>,
343+
is_method_call: bool,
344+
) -> GenericArgCountResult {
345+
let empty_args = hir::GenericArgs::none();
346+
let suppress_mismatch = check_impl_trait(tcx, seg, &def);
347+
check_generic_arg_count(
348+
tcx,
349+
span,
350+
def,
351+
if let Some(ref args) = seg.args { args } else { &empty_args },
352+
if is_method_call { GenericArgPosition::MethodCall } else { GenericArgPosition::Value },
353+
def.parent.is_none() && def.has_self, // `has_self`
354+
seg.infer_args || suppress_mismatch, // `infer_args`
355+
)
356+
}
357+
336358
/// Checks that the correct number of generic arguments have been provided.
337359
/// This is used both for datatypes and function calls.
338360
pub(crate) fn check_generic_arg_count(

compiler/rustc_typeck/src/collect.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -583,27 +583,27 @@ impl ItemCtxt<'tcx> {
583583
param_id: hir::HirId,
584584
ty: Ty<'tcx>,
585585
only_self_bounds: OnlySelfBounds,
586-
) -> Vec<(ty::Predicate<'tcx>, Span)> {
586+
) -> impl Iterator<Item = (ty::Predicate<'tcx>, Span)> + '_ {
587587
let constness = self.default_constness_for_trait_bounds();
588588
let from_ty_params = ast_generics
589589
.params
590590
.iter()
591-
.filter_map(|param| match param.kind {
591+
.filter_map(move |param| match param.kind {
592592
GenericParamKind::Type { .. } if param.hir_id == param_id => Some(&param.bounds),
593593
_ => None,
594594
})
595-
.flat_map(|bounds| bounds.iter())
596-
.flat_map(|b| predicates_from_bound(self, ty, b, constness));
595+
.flat_map(move |bounds| bounds.iter())
596+
.flat_map(move |b| predicates_from_bound(self, ty, b, constness));
597597

598598
let from_where_clauses = ast_generics
599599
.where_clause
600600
.predicates
601601
.iter()
602-
.filter_map(|wp| match *wp {
602+
.filter_map(move |wp| match *wp {
603603
hir::WherePredicate::BoundPredicate(ref bp) => Some(bp),
604604
_ => None,
605605
})
606-
.flat_map(|bp| {
606+
.flat_map(move |bp| {
607607
let bt = if is_param(self.tcx, &bp.bounded_ty, param_id) {
608608
Some(ty)
609609
} else if !only_self_bounds.0 {
@@ -613,9 +613,9 @@ impl ItemCtxt<'tcx> {
613613
};
614614
bp.bounds.iter().filter_map(move |b| bt.map(|bt| (bt, b)))
615615
})
616-
.flat_map(|(bt, b)| predicates_from_bound(self, bt, b, constness));
616+
.flat_map(move |(bt, b)| predicates_from_bound(self, bt, b, constness));
617617

618-
from_ty_params.chain(from_where_clauses).collect()
618+
from_ty_params.chain(from_where_clauses)
619619
}
620620
}
621621

0 commit comments

Comments
 (0)