Skip to content

Commit 29278b4

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 e317bc9 commit 29278b4

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/librustc/traits/select.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -2083,7 +2083,15 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
20832083
}
20842084

20852085
if let Some(principal) = data.principal() {
2086-
principal.with_self_ty(self.tcx(), self_ty)
2086+
if !self.infcx.tcx.features().object_safe_for_dispatch {
2087+
principal.with_self_ty(self.tcx(), self_ty)
2088+
} else {
2089+
if self.tcx().is_object_safe(principal.def_id()) {
2090+
principal.with_self_ty(self.tcx(), self_ty)
2091+
} else {
2092+
return;
2093+
}
2094+
}
20872095
} else {
20882096
// Only auto-trait bounds exist.
20892097
return;

src/librustc_typeck/coherence/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,11 @@ fn check_impl_overlap<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, node_id: ast::NodeI
192192

193193
for component_def_id in component_def_ids {
194194
if !tcx.is_object_safe(component_def_id) {
195-
// This is an error, but it will be reported by wfcheck. Ignore it here.
195+
// Without the 'object_safe_for_dispatch' feature this is an error
196+
// which will be reported by wfcheck. Ignore it here.
196197
// This is tested by `coherence-impl-trait-for-trait-object-safe.rs`.
198+
// With the feature enabled, the trait is not implemented automatically,
199+
// so this is valid.
197200
} else {
198201
let mut supertrait_def_ids =
199202
traits::supertrait_def_ids(tcx, component_def_id);

0 commit comments

Comments
 (0)