@@ -1356,32 +1356,46 @@ impl<'hir> LoweringContext<'_, 'hir> {
1356
1356
// keep track of the Span info. Now, `add_implicitly_sized` in `AstConv` checks both param bounds and
1357
1357
// where clauses for `?Sized`.
1358
1358
for pred in & generics. where_clause . predicates {
1359
- if let WherePredicate :: BoundPredicate ( ref bound_pred) = * pred {
1360
- ' next_bound: for bound in & bound_pred. bounds {
1361
- if let GenericBound :: Trait ( _, TraitBoundModifier :: Maybe ) = * bound {
1362
- // Check if the where clause type is a plain type parameter.
1363
- match self
1364
- . resolver
1365
- . get_partial_res ( bound_pred. bounded_ty . id )
1366
- . map ( |d| ( d. base_res ( ) , d. unresolved_segments ( ) ) )
1359
+ let bound_pred = match * pred {
1360
+ WherePredicate :: BoundPredicate ( ref bound_pred) => bound_pred,
1361
+ _ => continue ,
1362
+ } ;
1363
+ let mut is_param: Option < bool > = None ;
1364
+ for bound in & bound_pred. bounds {
1365
+ if !matches ! ( * bound, GenericBound :: Trait ( _, TraitBoundModifier :: Maybe ) ) {
1366
+ continue ;
1367
+ }
1368
+ // We only need to compute this once per `WherePredicate`, but don't
1369
+ // need to compute this at all unless there is a Maybe bound.
1370
+ // This closure should be able to be moved out of the loop,
1371
+ // but `get_partial_res` takes a `&mut self`
1372
+ let is_param = * is_param. get_or_insert_with ( || {
1373
+ // Check if the where clause type is a plain type parameter.
1374
+ match self
1375
+ . resolver
1376
+ . get_partial_res ( bound_pred. bounded_ty . id )
1377
+ . map ( |d| ( d. base_res ( ) , d. unresolved_segments ( ) ) )
1378
+ {
1379
+ Some ( ( Res :: Def ( DefKind :: TyParam , def_id) , 0 ) )
1380
+ if bound_pred. bound_generic_params . is_empty ( ) =>
1367
1381
{
1368
- Some ( ( Res :: Def ( DefKind :: TyParam , def_id) , 0 ) )
1369
- if bound_pred. bound_generic_params . is_empty ( ) =>
1370
- {
1371
- for param in & generics. params {
1372
- if def_id == self . resolver . local_def_id ( param. id ) . to_def_id ( ) {
1373
- continue ' next_bound;
1374
- }
1375
- }
1376
- }
1377
- _ => { }
1382
+ generics
1383
+ . params
1384
+ . iter ( )
1385
+ . find ( |p| def_id == self . resolver . local_def_id ( p. id ) . to_def_id ( ) )
1386
+ . is_some ( )
1378
1387
}
1379
- self . diagnostic ( ) . span_err (
1380
- bound_pred. bounded_ty . span ,
1381
- "`?Trait` bounds are only permitted at the \
1382
- point where a type parameter is declared",
1383
- ) ;
1388
+ // Either the `bounded_ty` is not a plain type parameter, or
1389
+ // it's not found in the generic type parameters list.
1390
+ _ => false ,
1384
1391
}
1392
+ } ) ;
1393
+ if !is_param {
1394
+ self . diagnostic ( ) . span_err (
1395
+ bound. span ( ) ,
1396
+ "`?Trait` bounds are only permitted at the \
1397
+ point where a type parameter is declared",
1398
+ ) ;
1385
1399
}
1386
1400
}
1387
1401
}
0 commit comments