Skip to content

Commit 4c87e2b

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 226d93f commit 4c87e2b

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
@@ -2010,7 +2010,15 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
20102010
}
20112011

20122012
if let Some(principal) = data.principal() {
2013-
principal.with_self_ty(self.tcx(), self_ty)
2013+
if !self.infcx.tcx.features().object_safe_for_dispatch {
2014+
principal.with_self_ty(self.tcx(), self_ty)
2015+
} else {
2016+
if self.tcx().is_object_safe(principal.def_id()) {
2017+
principal.with_self_ty(self.tcx(), self_ty)
2018+
} else {
2019+
return;
2020+
}
2021+
}
20142022
} else {
20152023
// Only auto-trait bounds exist.
20162024
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)