Skip to content

Commit 0771f9c

Browse files
committed
refactor LoweringContext::lower_generics_mut
Further reduce nesting by using early exits and replacing the innermost loop with `.find()`. Also eliminates the labeled branch. Add comments explaining why the copying of `?Sized` bounds is necessary.
1 parent d9aa287 commit 0771f9c

File tree

1 file changed

+36
-25
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+36
-25
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,37 +1372,48 @@ impl<'hir> LoweringContext<'_, 'hir> {
13721372
generics: &Generics,
13731373
itctx: ImplTraitContext<'_, 'hir>,
13741374
) -> GenericsCtor<'hir> {
1375-
// Collect `?Trait` bounds in where clause and move them to parameter definitions.
1375+
// Collect `?Trait` bounds in where clause and move them to parameter
1376+
// definitions. Currently, the decision to add the predicate for the
1377+
// implicit `Sized` bound only examines the generic parameters, not the
1378+
// where clauses, to discover any `?Sized` bounds. (e.g.,
1379+
// `AstConv::is_unsized`)
13761380
let mut add_bounds: NodeMap<Vec<_>> = Default::default();
13771381
for pred in &generics.where_clause.predicates {
1378-
if let WherePredicate::BoundPredicate(ref bound_pred) = *pred {
1379-
'next_bound: for bound in &bound_pred.bounds {
1380-
if let GenericBound::Trait(_, TraitBoundModifier::Maybe) = *bound {
1381-
// Check if the where clause type is a plain type parameter.
1382-
match self
1383-
.resolver
1384-
.get_partial_res(bound_pred.bounded_ty.id)
1385-
.map(|d| (d.base_res(), d.unresolved_segments()))
1382+
let bound_pred = match *pred {
1383+
WherePredicate::BoundPredicate(ref bound_pred) => bound_pred,
1384+
_ => continue,
1385+
};
1386+
for bound in &bound_pred.bounds {
1387+
if !matches!(*bound, GenericBound::Trait(_, TraitBoundModifier::Maybe)) {
1388+
continue;
1389+
}
1390+
// Check if the where clause type is a plain type parameter.
1391+
match self
1392+
.resolver
1393+
.get_partial_res(bound_pred.bounded_ty.id)
1394+
.map(|d| (d.base_res(), d.unresolved_segments()))
1395+
{
1396+
Some((Res::Def(DefKind::TyParam, def_id), 0))
1397+
if bound_pred.bound_generic_params.is_empty() =>
1398+
{
1399+
if let Some(param) = generics
1400+
.params
1401+
.iter()
1402+
.find(|p| def_id == self.resolver.local_def_id(p.id).to_def_id())
13861403
{
1387-
Some((Res::Def(DefKind::TyParam, def_id), 0))
1388-
if bound_pred.bound_generic_params.is_empty() =>
1389-
{
1390-
for param in &generics.params {
1391-
if def_id == self.resolver.local_def_id(param.id).to_def_id() {
1392-
add_bounds.entry(param.id).or_default().push(bound.clone());
1393-
continue 'next_bound;
1394-
}
1395-
}
1396-
}
1397-
_ => {}
1404+
add_bounds.entry(param.id).or_default().push(bound.clone());
1405+
continue;
13981406
}
1399-
self.diagnostic().span_err(
1400-
bound_pred.bounded_ty.span,
1401-
"`?Trait` bounds are only permitted at the \
1402-
point where a type parameter is declared",
1403-
);
14041407
}
1408+
_ => {}
14051409
}
1410+
// Either the `bounded_ty` is not a plain type parameter, or
1411+
// it's not found in the generic type parameters list.
1412+
self.diagnostic().span_err(
1413+
bound_pred.bounded_ty.span,
1414+
"`?Trait` bounds are only permitted at the \
1415+
point where a type parameter is declared",
1416+
);
14061417
}
14071418
}
14081419

0 commit comments

Comments
 (0)