Skip to content

Commit

Permalink
One more minor allocation optimization
Browse files Browse the repository at this point in the history
Remove one unnecessary collect at the end of a fn
  • Loading branch information
JulianKnodt committed Jan 1, 2021
1 parent 9c9a046 commit d6695bd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
22 changes: 22 additions & 0 deletions compiler/rustc_typeck/src/astconv/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,28 @@ pub fn create_substs_for_generic_args<'a, 'tcx>(
tcx.intern_substs(&substs)
}

/// Checks that the correct number of generic arguments have been provided.
/// Used specifically for function calls.
pub fn check_generic_arg_count_for_call(
tcx: TyCtxt<'_>,
span: Span,
def: &ty::Generics,
seg: &hir::PathSegment<'_>,
is_method_call: bool,
) -> GenericArgCountResult {
let empty_args = hir::GenericArgs::none();
let suppress_mismatch = check_impl_trait(tcx, seg, &def);
check_generic_arg_count(
tcx,
span,
def,
if let Some(ref args) = seg.args { args } else { &empty_args },
if is_method_call { GenericArgPosition::MethodCall } else { GenericArgPosition::Value },
def.parent.is_none() && def.has_self, // `has_self`
seg.infer_args || suppress_mismatch, // `infer_args`
)
}

/// Checks that the correct number of generic arguments have been provided.
/// This is used both for datatypes and function calls.
pub(crate) fn check_generic_arg_count(
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,27 +583,27 @@ impl ItemCtxt<'tcx> {
param_id: hir::HirId,
ty: Ty<'tcx>,
only_self_bounds: OnlySelfBounds,
) -> Vec<(ty::Predicate<'tcx>, Span)> {
) -> impl Iterator<Item = (ty::Predicate<'tcx>, Span)> + '_ {
let constness = self.default_constness_for_trait_bounds();
let from_ty_params = ast_generics
.params
.iter()
.filter_map(|param| match param.kind {
.filter_map(move |param| match param.kind {
GenericParamKind::Type { .. } if param.hir_id == param_id => Some(&param.bounds),
_ => None,
})
.flat_map(|bounds| bounds.iter())
.flat_map(|b| predicates_from_bound(self, ty, b, constness));
.flat_map(move |bounds| bounds.iter())
.flat_map(move |b| predicates_from_bound(self, ty, b, constness));

let from_where_clauses = ast_generics
.where_clause
.predicates
.iter()
.filter_map(|wp| match *wp {
.filter_map(move |wp| match *wp {
hir::WherePredicate::BoundPredicate(ref bp) => Some(bp),
_ => None,
})
.flat_map(|bp| {
.flat_map(move |bp| {
let bt = if is_param(self.tcx, &bp.bounded_ty, param_id) {
Some(ty)
} else if !only_self_bounds.0 {
Expand All @@ -613,9 +613,9 @@ impl ItemCtxt<'tcx> {
};
bp.bounds.iter().filter_map(move |b| bt.map(|bt| (bt, b)))
})
.flat_map(|(bt, b)| predicates_from_bound(self, bt, b, constness));
.flat_map(move |(bt, b)| predicates_from_bound(self, bt, b, constness));

from_ty_params.chain(from_where_clauses).collect()
from_ty_params.chain(from_where_clauses)
}
}

Expand Down

0 comments on commit d6695bd

Please sign in to comment.