Skip to content

Commit 38ab0c9

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 84ad41f commit 38ab0c9

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
@@ -173,8 +173,11 @@ fn check_impl_overlap<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, node_id: ast::NodeI
173173

174174
if let Some(principal_def_id) = data.principal_def_id() {
175175
if !tcx.is_object_safe(principal_def_id) {
176-
// This is an error, but it will be reported by wfcheck. Ignore it here.
176+
// Without the 'object_safe_for_dispatch' feature this is an error
177+
// which will be reported by wfcheck. Ignore it here.
177178
// This is tested by `coherence-impl-trait-for-trait-object-safe.rs`.
179+
// With the feature enabled, the trait is not implemented automatically,
180+
// so this is valid.
178181
} else {
179182
let mut supertrait_def_ids =
180183
traits::supertrait_def_ids(tcx, principal_def_id);

0 commit comments

Comments
 (0)