Skip to content

Commit a5c6d96

Browse files
committed
RFC 2027: Non-object safe objects do not impl their traits
- Remove the automatic 'impl Trait for dyn Trait' when Trait is not object safe
1 parent 8b33005 commit a5c6d96

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/librustc/traits/select.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2054,7 +2054,15 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
20542054
}
20552055

20562056
if let Some(principal) = data.principal() {
2057-
principal.with_self_ty(self.tcx(), self_ty)
2057+
if !self.infcx.tcx.features().object_safe_for_dispatch {
2058+
principal.with_self_ty(self.tcx(), self_ty)
2059+
} else {
2060+
if self.tcx().is_object_safe(principal.def_id()) {
2061+
principal.with_self_ty(self.tcx(), self_ty)
2062+
} else {
2063+
return;
2064+
}
2065+
}
20582066
} else {
20592067
// Only auto-trait bounds exist.
20602068
return;

src/librustc_typeck/coherence/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,11 @@ fn check_impl_overlap<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, node_id: ast::NodeI
183183

184184
for component_def_id in component_def_ids {
185185
if !tcx.is_object_safe(component_def_id) {
186-
// This is an error, but it will be reported by wfcheck. Ignore it here.
186+
// Without the 'object_safe_for_dispatch' feature this is an error
187+
// which will be reported by wfcheck. Ignore it here.
187188
// This is tested by `coherence-impl-trait-for-trait-object-safe.rs`.
189+
// With the feature enabled, the trait is not implemented automatically,
190+
// so this is valid.
188191
} else {
189192
let mut supertrait_def_ids =
190193
traits::supertrait_def_ids(tcx, component_def_id);

0 commit comments

Comments
 (0)