Skip to content

Commit 972ae5a

Browse files
committed
Point at the Sized obligation in where clauses
1 parent 4b2f1db commit 972ae5a

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

src/librustc/traits/object_safety.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,48 @@ fn get_sized_bounds(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span; 1]>
234234
tcx.hir()
235235
.get_if_local(trait_def_id)
236236
.and_then(|node| match node {
237-
hir::Node::Item(hir::Item { kind: hir::ItemKind::Trait(.., bounds, _), .. }) => Some(
238-
bounds
237+
hir::Node::Item(hir::Item {
238+
kind: hir::ItemKind::Trait(.., generics, bounds, _),
239+
..
240+
}) => Some(
241+
generics
242+
.where_clause
243+
.predicates
239244
.iter()
240-
.filter_map(|b| match b {
245+
.filter_map(|pred| {
246+
match pred {
247+
hir::WherePredicate::BoundPredicate(pred)
248+
if pred.bounded_ty.hir_id.owner_def_id() == trait_def_id =>
249+
{
250+
// Fetch spans for trait bounds that are Sized:
251+
// `trait T where Self: Pred`
252+
Some(pred.bounds.iter().filter_map(|b| match b {
253+
hir::GenericBound::Trait(
254+
trait_ref,
255+
hir::TraitBoundModifier::None,
256+
) if trait_has_sized_self(
257+
tcx,
258+
trait_ref.trait_ref.trait_def_id(),
259+
) =>
260+
{
261+
Some(trait_ref.span)
262+
}
263+
_ => None,
264+
}))
265+
}
266+
_ => None,
267+
}
268+
})
269+
.flatten()
270+
.chain(bounds.iter().filter_map(|b| match b {
241271
hir::GenericBound::Trait(trait_ref, hir::TraitBoundModifier::None)
242272
if trait_has_sized_self(tcx, trait_ref.trait_ref.trait_def_id()) =>
243273
{
274+
// Fetch spans for supertraits that are `Sized`: `trait T: Super`
244275
Some(trait_ref.span)
245276
}
246277
_ => None,
247-
})
278+
}))
248279
.collect::<SmallVec<[Span; 1]>>(),
249280
),
250281
_ => None,

src/test/ui/object-safety/object-safety-sized-2.curr.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
error[E0038]: the trait `Bar` cannot be made into an object
22
--> $DIR/object-safety-sized-2.rs:14:30
33
|
4+
LL | where Self : Sized
5+
| ----- the trait cannot require that `Self : Sized`
6+
...
47
LL | fn make_bar<T:Bar>(t: &T) -> &dyn Bar {
58
| ^^^^^^^^ the trait `Bar` cannot be made into an object
6-
|
7-
= note: the trait cannot require that `Self : Sized`
89

910
error: aborting due to previous error
1011

src/test/ui/object-safety/object-safety-sized-2.object_safe_for_dispatch.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
error[E0038]: the trait `Bar` cannot be made into an object
22
--> $DIR/object-safety-sized-2.rs:16:5
33
|
4+
LL | where Self : Sized
5+
| ----- the trait cannot require that `Self : Sized`
6+
...
47
LL | t
58
| ^ the trait `Bar` cannot be made into an object
69
|
7-
= note: the trait cannot require that `Self : Sized`
810
= note: required because of the requirements on the impl of `std::ops::CoerceUnsized<&dyn Bar>` for `&T`
911
= note: required by cast to type `&dyn Bar`
1012

0 commit comments

Comments
 (0)