Skip to content

Commit d03456d

Browse files
committed
Auto merge of rust-lang#87338 - SparrowLii:MaybeTrait, r=wesleywiser
Simplify the collecting of `? Trait` bounds in where clause This PR fixes the FIXME about using less rightward drift and only one error reporting when collecting of `?Trait` bounds in where clause. Checking whether the path length of `bound_ty` is 1 can be replaced by whether `unresolved_segments` in the partial_res is 0. Checking whether the `param.kind` is `Type{...}` can also be omitted. One Fx hash calculation will be done for Const or Lifetime param, but the impact on efficiency should be small IMO
2 parents 3b4a0df + 0f5bfc2 commit d03456d

File tree

1 file changed

+17
-33
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+17
-33
lines changed

compiler/rustc_ast_lowering/src/item.rs

+17-33
Original file line numberDiff line numberDiff line change
@@ -1373,50 +1373,34 @@ impl<'hir> LoweringContext<'_, 'hir> {
13731373
itctx: ImplTraitContext<'_, 'hir>,
13741374
) -> GenericsCtor<'hir> {
13751375
// Collect `?Trait` bounds in where clause and move them to parameter definitions.
1376-
// FIXME: this could probably be done with less rightward drift. It also looks like two
1377-
// control paths where `report_error` is called are the only paths that advance to after the
1378-
// match statement, so the error reporting could probably just be moved there.
13791376
let mut add_bounds: NodeMap<Vec<_>> = Default::default();
13801377
for pred in &generics.where_clause.predicates {
13811378
if let WherePredicate::BoundPredicate(ref bound_pred) = *pred {
13821379
'next_bound: for bound in &bound_pred.bounds {
13831380
if let GenericBound::Trait(_, TraitBoundModifier::Maybe) = *bound {
1384-
let report_error = |this: &mut Self| {
1385-
this.diagnostic().span_err(
1386-
bound_pred.bounded_ty.span,
1387-
"`?Trait` bounds are only permitted at the \
1388-
point where a type parameter is declared",
1389-
);
1390-
};
13911381
// Check if the where clause type is a plain type parameter.
1392-
match bound_pred.bounded_ty.kind {
1393-
TyKind::Path(None, ref path)
1394-
if path.segments.len() == 1
1395-
&& bound_pred.bound_generic_params.is_empty() =>
1382+
match self
1383+
.resolver
1384+
.get_partial_res(bound_pred.bounded_ty.id)
1385+
.map(|d| (d.base_res(), d.unresolved_segments()))
1386+
{
1387+
Some((Res::Def(DefKind::TyParam, def_id), 0))
1388+
if bound_pred.bound_generic_params.is_empty() =>
13961389
{
1397-
if let Some(Res::Def(DefKind::TyParam, def_id)) = self
1398-
.resolver
1399-
.get_partial_res(bound_pred.bounded_ty.id)
1400-
.map(|d| d.base_res())
1401-
{
1402-
if let Some(def_id) = def_id.as_local() {
1403-
for param in &generics.params {
1404-
if let GenericParamKind::Type { .. } = param.kind {
1405-
if def_id == self.resolver.local_def_id(param.id) {
1406-
add_bounds
1407-
.entry(param.id)
1408-
.or_default()
1409-
.push(bound.clone());
1410-
continue 'next_bound;
1411-
}
1412-
}
1413-
}
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;
14141394
}
14151395
}
1416-
report_error(self)
14171396
}
1418-
_ => report_error(self),
1397+
_ => {}
14191398
}
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+
);
14201404
}
14211405
}
14221406
}

0 commit comments

Comments
 (0)