Skip to content

Commit ca581f9

Browse files
committed
Don't require associated types with Self: Sized bounds in dyn Trait objects
1 parent 9227ff2 commit ca581f9

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11311131
}
11321132
}
11331133

1134+
for def_ids in associated_types.values_mut() {
1135+
for def_id in def_ids.clone() {
1136+
// If the associated type has a `where Self: Sized` bound, we do not need to constrain the associated
1137+
// type in the `dyn Trait`.
1138+
if tcx.generics_require_sized_self(def_id) {
1139+
def_ids.remove(&def_id);
1140+
}
1141+
}
1142+
}
1143+
11341144
self.complain_about_missing_associated_types(
11351145
associated_types,
11361146
potential_assoc_types,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,10 @@ rustc_queries! {
21972197
desc { "getting cfg-ed out item names" }
21982198
separate_provide_extern
21992199
}
2200+
2201+
query generics_require_sized_self(def_id: DefId) -> bool {
2202+
desc { "check whether the item has a `where Self: Sized` bound" }
2203+
}
22002204
}
22012205

22022206
rustc_query_append! { define_callbacks! }

compiler/rustc_trait_selection/src/traits/object_safety.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,5 +922,10 @@ pub fn contains_illegal_impl_trait_in_trait<'tcx>(
922922
}
923923

924924
pub fn provide(providers: &mut Providers) {
925-
*providers = Providers { object_safety_violations, check_is_object_safe, ..*providers };
925+
*providers = Providers {
926+
object_safety_violations,
927+
check_is_object_safe,
928+
generics_require_sized_self,
929+
..*providers
930+
};
926931
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// check-pass
2+
13
trait Foo {
24
type Bar
35
where
46
Self: Sized;
57
}
68

7-
fn foo(_: &dyn Foo) {} //~ ERROR: the value of the associated type `Bar` (from trait `Foo`) must be specified
9+
fn foo(_: &dyn Foo) {}
810

911
fn main() {}

tests/ui/object-safety/assoc_type_bounds_sized.stderr

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)