Skip to content

Commit 90c50bd

Browse files
committed
Cleanup lower_generics_mut and make span be the bound itself, not the type
1 parent 2c7bc5e commit 90c50bd

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

compiler/rustc_ast_lowering/src/item.rs

+37-23
Original file line numberDiff line numberDiff line change
@@ -1356,32 +1356,46 @@ impl<'hir> LoweringContext<'_, 'hir> {
13561356
// keep track of the Span info. Now, `add_implicitly_sized` in `AstConv` checks both param bounds and
13571357
// where clauses for `?Sized`.
13581358
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() =>
13671381
{
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()
13781387
}
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,
13841391
}
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+
);
13851399
}
13861400
}
13871401
}

src/test/ui/maybe-bounds-where.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
error: `?Trait` bounds are only permitted at the point where a type parameter is declared
2-
--> $DIR/maybe-bounds-where.rs:1:23
2+
--> $DIR/maybe-bounds-where.rs:1:28
33
|
44
LL | struct S1<T>(T) where (T): ?Sized;
5-
| ^^^
5+
| ^^^^^^
66

77
error: `?Trait` bounds are only permitted at the point where a type parameter is declared
8-
--> $DIR/maybe-bounds-where.rs:4:23
8+
--> $DIR/maybe-bounds-where.rs:4:27
99
|
1010
LL | struct S2<T>(T) where u8: ?Sized;
11-
| ^^
11+
| ^^^^^^
1212

1313
error: `?Trait` bounds are only permitted at the point where a type parameter is declared
14-
--> $DIR/maybe-bounds-where.rs:7:23
14+
--> $DIR/maybe-bounds-where.rs:7:35
1515
|
1616
LL | struct S3<T>(T) where &'static T: ?Sized;
17-
| ^^^^^^^^^^
17+
| ^^^^^^
1818

1919
error: `?Trait` bounds are only permitted at the point where a type parameter is declared
20-
--> $DIR/maybe-bounds-where.rs:12:31
20+
--> $DIR/maybe-bounds-where.rs:12:34
2121
|
2222
LL | struct S4<T>(T) where for<'a> T: ?Trait<'a>;
23-
| ^
23+
| ^^^^^^^^^^
2424

2525
error: `?Trait` bounds are only permitted at the point where a type parameter is declared
26-
--> $DIR/maybe-bounds-where.rs:21:18
26+
--> $DIR/maybe-bounds-where.rs:21:21
2727
|
2828
LL | fn f() where T: ?Sized {}
29-
| ^
29+
| ^^^^^^
3030

3131
warning: default bound relaxed for a type parameter, but this does nothing because the given bound is not a default; only `?Sized` is supported
3232
--> $DIR/maybe-bounds-where.rs:12:11

0 commit comments

Comments
 (0)