Skip to content

Commit 4795a5a

Browse files
committed
Auto merge of rust-lang#121200 - estebank:inferred_outlives_of-impl, r=<try>
Deduplicate object safety errors on `impl dyn Trait { .. }` `impl dyn Trait {}` has an implicit `dyn Trait: 'static` bound. We add it in `inferred_outlives_of` for more context in errors. With this we point at `impl dyn Trait` and not to it's associated functions, deduplicating errors to a single one per `impl dyn Trait` instead of one for it plus one for each associated function. CC rust-lang#83246, as this is necessary but not sufficient to help with the lack of tracking of `'static` obligations.
2 parents d2e8ecd + 1e03e2b commit 4795a5a

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

compiler/rustc_hir_analysis/src/outlives/mod.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_hir as hir;
12
use rustc_hir::def::DefKind;
23
use rustc_hir::def_id::LocalDefId;
34
use rustc_middle::query::Providers;
@@ -46,6 +47,24 @@ fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[(ty::Clau
4647
&[]
4748
}
4849
}
50+
DefKind::Impl { of_trait: false }
51+
if let hir::ItemKind::Impl(item) = tcx.hir().expect_item(item_def_id).kind
52+
&& let hir::Impl { self_ty, .. } = &item
53+
&& let hir::TyKind::TraitObject(
54+
_,
55+
hir::Lifetime { res: hir::LifetimeName::ImplicitObjectLifetimeDefault, .. },
56+
_,
57+
) = self_ty.kind =>
58+
{
59+
// `impl dyn Trait {}` has an implicit `dyn Trait: 'static` bound. We add it here for
60+
// more context in errors. With this we point at `impl dyn Trait` and not to it's
61+
// associated functions, deduplicating errors to a single one per `impl dyn Trait` instead
62+
// of one for it plus one for each associated function.
63+
let ty = tcx.type_of(item_def_id).instantiate_identity();
64+
let clause_kind =
65+
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty, tcx.lifetimes.re_static));
66+
&*tcx.arena.alloc_from_iter([(clause_kind.to_predicate(tcx), self_ty.span)])
67+
}
4968
_ => &[],
5069
}
5170
}

tests/ui/associated-consts/associated-const-in-trait.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ trait Trait {
66

77
impl dyn Trait {
88
//~^ ERROR the trait `Trait` cannot be made into an object [E0038]
9+
//~| ERROR the trait `Trait` cannot be made into an object [E0038]
910
const fn n() -> usize { Self::N }
1011
//~^ ERROR the trait `Trait` cannot be made into an object [E0038]
1112
}

tests/ui/associated-consts/associated-const-in-trait.stderr

+18-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,23 @@ LL | const N: usize;
1414
= help: consider moving `N` to another trait
1515

1616
error[E0038]: the trait `Trait` cannot be made into an object
17-
--> $DIR/associated-const-in-trait.rs:9:29
17+
--> $DIR/associated-const-in-trait.rs:7:6
18+
|
19+
LL | impl dyn Trait {
20+
| ^^^^^^^^^ `Trait` cannot be made into an object
21+
|
22+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
23+
--> $DIR/associated-const-in-trait.rs:4:11
24+
|
25+
LL | trait Trait {
26+
| ----- this trait cannot be made into an object...
27+
LL | const N: usize;
28+
| ^ ...because it contains this associated `const`
29+
= help: consider moving `N` to another trait
30+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
31+
32+
error[E0038]: the trait `Trait` cannot be made into an object
33+
--> $DIR/associated-const-in-trait.rs:10:29
1834
|
1935
LL | const fn n() -> usize { Self::N }
2036
| ^^^^ `Trait` cannot be made into an object
@@ -28,6 +44,6 @@ LL | const N: usize;
2844
| ^ ...because it contains this associated `const`
2945
= help: consider moving `N` to another trait
3046

31-
error: aborting due to 2 previous errors
47+
error: aborting due to 3 previous errors
3248

3349
For more information about this error, try `rustc --explain E0038`.

tests/ui/impl-trait/in-trait/cycle-effective-visibilities-during-object-safety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ impl MyTrait for Outer {
1515

1616
impl dyn MyTrait {
1717
//~^ ERROR the trait `MyTrait` cannot be made into an object
18+
//~| ERROR the trait `MyTrait` cannot be made into an object
1819
fn other(&self) -> impl Marker {
19-
//~^ ERROR the trait `MyTrait` cannot be made into an object
2020
MyTrait::foo(&self)
2121
//~^ ERROR the trait bound `&dyn MyTrait: MyTrait` is not satisfied
2222
//~| ERROR the trait bound `&dyn MyTrait: MyTrait` is not satisfied

tests/ui/impl-trait/in-trait/cycle-effective-visibilities-during-object-safety.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ LL | fn foo(&self) -> impl Marker;
4949
= help: only type `Outer` implements the trait, consider using it directly instead
5050

5151
error[E0038]: the trait `MyTrait` cannot be made into an object
52-
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:18:15
52+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:16:6
5353
|
54-
LL | fn other(&self) -> impl Marker {
55-
| ^^^^ `MyTrait` cannot be made into an object
54+
LL | impl dyn MyTrait {
55+
| ^^^^^^^^^^^ `MyTrait` cannot be made into an object
5656
|
5757
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
5858
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:5:22
@@ -63,6 +63,7 @@ LL | fn foo(&self) -> impl Marker;
6363
| ^^^^^^^^^^^ ...because method `foo` references an `impl Trait` type in its return type
6464
= help: consider moving `foo` to another trait
6565
= help: only type `Outer` implements the trait, consider using it directly instead
66+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
6667

6768
error: aborting due to 5 previous errors
6869

0 commit comments

Comments
 (0)