Skip to content

Commit 748af6b

Browse files
Rollup merge of rust-lang#136522 - compiler-errors:dyn_compatible_for_dispatch, r=oli-obk
Remove `feature(dyn_compatible_for_dispatch)` from the compiler This PR proposes the removal of `feature(dyn_compatible_for_dispatch)` from the compiler. * As far as I can tell from the tracking issue, there's very little demand for this feature. I think that if this feature becomes useful in the future, then a fresh implementation from a fresh set of eyes, with renewed understanding of how this feature fits into the picture of Rust as it exists **today** would be great to have; however, in the absence of this demand, I don't see a particularly good reason to keep this implementation around. * The RFC didn't receive very much discussion outside of the lang team, and while the discussion it received seemed to suggest that this feature was aiming to simplify the language and improve expressibility, I don't think this feature has really demonstrated either of those goals in practice. Furthermore, nobody seems to have owned this feature for quite some time or express desire to push for its stabilization. * Relatedly, I find some of the RFC discussion like "when we make things impossible it's often presumptuous"[^1] and "I tend to want to take a 'we are all adults here' attitude toward unsafe code"[^2] to be particularly uncompelling. Of course this is no criticism to the authors of those comments since they're pretty old comments now, but type soundness is (IMO) the primary goal of the types team. This feature doesn't really do much other than further complicating the story of where we must validate object safety for soundness, along making dyn-incompatible trait object types *almost* seem useful, but very much remain UB to create and misleading to users who don't know better. * Dyn compatibility's story has gotten more complicated since the feature was proposed in 2017, and now it needs to interact with things like associated consts, GATs, RPITITs, trait upcasting, `dyn*`, etc. While some of this is exercised in the codebase today, I'm not confident all of the corners of this feature have been hammered out. Reducing the "surface area" for what can go wrong in the compiler, especially around a side of the language (`dyn Trait`) that has been known to be particularly unsound in the past, seems good enough motivation to get rid of this for now. [^1]: rust-lang/rfcs#2027 (comment) [^2]: rust-lang/rfcs#2027 (comment) cc `@rust-lang/types` `@rust-lang/lang` Tracking: - rust-lang#43561 r? types
2 parents 617aad8 + f3d31f7 commit 748af6b

File tree

58 files changed

+511
-886
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+511
-886
lines changed

compiler/rustc_feature/src/removed.rs

+9
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ declare_features! (
100100
Some("renamed to `doc_notable_trait`")),
101101
/// Allows using `#[unsafe_destructor_blind_to_params]` (RFC 1238).
102102
(removed, dropck_parametricity, "1.38.0", Some(28498), None),
103+
/// Allows making `dyn Trait` well-formed even if `Trait` is not dyn compatible[^1].
104+
/// In that case, `dyn Trait: Trait` does not hold. Moreover, coercions and
105+
/// casts in safe Rust to `dyn Trait` for such a `Trait` is also forbidden.
106+
///
107+
/// Renamed from `object_safe_for_dispatch`.
108+
///
109+
/// [^1]: Formerly known as "object safe".
110+
(removed, dyn_compatible_for_dispatch, "1.83.0", Some(43561),
111+
Some("removed, not used heavily and represented additional complexity in dyn compatibility")),
103112
/// Uses generic effect parameters for ~const bounds
104113
(removed, effects, "1.84.0", Some(102090),
105114
Some("removed, redundant with `#![feature(const_trait_impl)]`")),

compiler/rustc_feature/src/unstable.rs

-8
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,6 @@ declare_features! (
270270
(unstable, doc_notable_trait, "1.52.0", Some(45040)),
271271
/// Allows using the `may_dangle` attribute (RFC 1327).
272272
(unstable, dropck_eyepatch, "1.10.0", Some(34761)),
273-
/// Allows making `dyn Trait` well-formed even if `Trait` is not dyn compatible[^1].
274-
/// In that case, `dyn Trait: Trait` does not hold. Moreover, coercions and
275-
/// casts in safe Rust to `dyn Trait` for such a `Trait` is also forbidden.
276-
///
277-
/// Renamed from `object_safe_for_dispatch`.
278-
///
279-
/// [^1]: Formerly known as "object safe".
280-
(unstable, dyn_compatible_for_dispatch, "1.83.0", Some(43561)),
281273
/// Allows using the `#[fundamental]` attribute.
282274
(unstable, fundamental, "1.0.0", Some(29635)),
283275
/// Allows using `#[link_name="llvm.*"]`.

compiler/rustc_hir_analysis/src/coherence/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,7 @@ fn check_object_overlap<'tcx>(
199199

200200
for component_def_id in component_def_ids {
201201
if !tcx.is_dyn_compatible(component_def_id) {
202-
// Without the 'dyn_compatible_for_dispatch' feature this is an error
203-
// which will be reported by wfcheck. Ignore it here.
204-
// This is tested by `coherence-impl-trait-for-trait-dyn-compatible.rs`.
205-
// With the feature enabled, the trait is not implemented automatically,
206-
// so this is valid.
202+
// This is a WF error tested by `coherence-impl-trait-for-trait-dyn-compatible.rs`.
207203
} else {
208204
let mut supertrait_def_ids = elaborate::supertrait_def_ids(tcx, component_def_id);
209205
if supertrait_def_ids

compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,10 @@ fn receiver_for_self_ty<'tcx>(
538538
/// a pointer.
539539
///
540540
/// In practice, we cannot use `dyn Trait` explicitly in the obligation because it would result in
541-
/// a new check that `Trait` is dyn-compatible, creating a cycle (until dyn_compatible_for_dispatch
542-
/// is stabilized, see tracking issue <https://github.com/rust-lang/rust/issues/43561>).
543-
/// Instead, we fudge a little by introducing a new type parameter `U` such that
541+
/// a new check that `Trait` is dyn-compatible, creating a cycle.
542+
/// Instead, we emulate a placeholder by introducing a new type parameter `U` such that
544543
/// `Self: Unsize<U>` and `U: Trait + ?Sized`, and use `U` in place of `dyn Trait`.
544+
///
545545
/// Written as a chalk-style query:
546546
/// ```ignore (not-rust)
547547
/// forall (U: Trait + ?Sized) {
@@ -572,8 +572,6 @@ fn receiver_is_dispatchable<'tcx>(
572572

573573
// the type `U` in the query
574574
// use a bogus type parameter to mimic a forall(U) query using u32::MAX for now.
575-
// FIXME(mikeyhew) this is a total hack. Once dyn_compatible_for_dispatch is stabilized, we can
576-
// replace this with `dyn Trait`
577575
let unsized_self_ty: Ty<'tcx> =
578576
Ty::new_param(tcx, u32::MAX, rustc_span::sym::RustaceansAreAwesome);
579577

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -859,13 +859,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
859859
}
860860

861861
if let Some(principal) = data.principal() {
862-
if !self.infcx.tcx.features().dyn_compatible_for_dispatch() {
863-
principal.with_self_ty(self.tcx(), self_ty)
864-
} else if self.tcx().is_dyn_compatible(principal.def_id()) {
865-
principal.with_self_ty(self.tcx(), self_ty)
866-
} else {
867-
return;
868-
}
862+
principal.with_self_ty(self.tcx(), self_ty)
869863
} else {
870864
// Only auto trait bounds exist.
871865
return;

compiler/rustc_trait_selection/src/traits/wf.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -904,19 +904,14 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
904904
// FIXME(#27579) RFC also considers adding trait
905905
// obligations that don't refer to Self and
906906
// checking those
907-
908-
let defer_to_coercion = tcx.features().dyn_compatible_for_dispatch();
909-
910-
if !defer_to_coercion {
911-
if let Some(principal) = data.principal_def_id() {
912-
self.out.push(traits::Obligation::with_depth(
913-
tcx,
914-
self.cause(ObligationCauseCode::WellFormed(None)),
915-
self.recursion_depth,
916-
self.param_env,
917-
ty::Binder::dummy(ty::PredicateKind::DynCompatible(principal)),
918-
));
919-
}
907+
if let Some(principal) = data.principal_def_id() {
908+
self.out.push(traits::Obligation::with_depth(
909+
tcx,
910+
self.cause(ObligationCauseCode::WellFormed(None)),
911+
self.recursion_depth,
912+
self.param_env,
913+
ty::Binder::dummy(ty::PredicateKind::DynCompatible(principal)),
914+
));
920915
}
921916
}
922917

src/tools/tidy/src/issues.txt

-1
Original file line numberDiff line numberDiff line change
@@ -3827,7 +3827,6 @@ ui/suggestions/issue-103646.rs
38273827
ui/suggestions/issue-104086-suggest-let.rs
38283828
ui/suggestions/issue-104287.rs
38293829
ui/suggestions/issue-104327.rs
3830-
ui/suggestions/issue-104328.rs
38313830
ui/suggestions/issue-104961.rs
38323831
ui/suggestions/issue-105226.rs
38333832
ui/suggestions/issue-105494.rs

tests/ui/coherence/coherence-unsafe-trait-object-impl.rs

-18
This file was deleted.

tests/ui/coherence/coherence-unsafe-trait-object-impl.stderr

-22
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
//@ check-pass
2-
31
// Regression test for #128176. Previously we would call `type_of` on the `1` anon const
42
// before the anon const had been lowered and had the `type_of` fed with a result.
53

64
#![feature(generic_const_exprs)]
7-
#![feature(dyn_compatible_for_dispatch)]
85
#![allow(incomplete_features)]
96

107
trait X {
@@ -13,6 +10,7 @@ trait X {
1310

1411
const _: () = {
1512
fn f2<'a>(arg: Box<dyn X<Y<1> = &'a ()>>) {}
13+
//~^ ERROR the trait `X` is not dyn compatible
1614
};
1715

1816
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0038]: the trait `X` is not dyn compatible
2+
--> $DIR/cg-in-dyn-issue-128176.rs:12:24
3+
|
4+
LL | fn f2<'a>(arg: Box<dyn X<Y<1> = &'a ()>>) {}
5+
| ^^^^^^^^^^^^^^^^^^^^ `X` is not dyn compatible
6+
|
7+
note: for a trait to be dyn compatible it needs to allow building a vtable
8+
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
9+
--> $DIR/cg-in-dyn-issue-128176.rs:8:10
10+
|
11+
LL | trait X {
12+
| - this trait is not dyn compatible...
13+
LL | type Y<const N: i16>;
14+
| ^ ...because it contains the generic associated type `Y`
15+
= help: consider moving `Y` to another trait
16+
17+
error: aborting due to 1 previous error
18+
19+
For more information about this error, try `rustc --explain E0038`.

tests/ui/dyn-compatibility/associated-consts.dyn_compatible_for_dispatch.stderr

-20
This file was deleted.

tests/ui/dyn-compatibility/associated-consts.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
// Check that we correctly prevent users from making trait objects
22
// from traits with associated consts.
3-
//
4-
//@ revisions: curr dyn_compatible_for_dispatch
5-
6-
#![cfg_attr(dyn_compatible_for_dispatch, feature(dyn_compatible_for_dispatch))]
73

84
trait Bar {
95
const X: usize;
106
}
117

128
fn make_bar<T:Bar>(t: &T) -> &dyn Bar {
13-
//[curr]~^ ERROR E0038
9+
//~^ ERROR E0038
1410
t
1511
//~^ ERROR E0038
1612
}

tests/ui/dyn-compatibility/generics.dyn_compatible_for_dispatch.stderr tests/ui/dyn-compatibility/associated-consts.stderr

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
error[E0038]: the trait `Bar` is not dyn compatible
2-
--> $DIR/generics.rs:20:5
2+
--> $DIR/associated-consts.rs:8:31
33
|
4-
LL | t
5-
| ^ `Bar` is not dyn compatible
4+
LL | fn make_bar<T:Bar>(t: &T) -> &dyn Bar {
5+
| ^^^^^^^ `Bar` is not dyn compatible
66
|
77
note: for a trait to be dyn compatible it needs to allow building a vtable
88
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
9-
--> $DIR/generics.rs:10:8
9+
--> $DIR/associated-consts.rs:5:11
1010
|
1111
LL | trait Bar {
1212
| --- this trait is not dyn compatible...
13-
LL | fn bar<T>(&self, t: T);
14-
| ^^^ ...because method `bar` has generic type parameters
15-
= help: consider moving `bar` to another trait
16-
= note: required for the cast from `&T` to `&dyn Bar`
13+
LL | const X: usize;
14+
| ^ ...because it contains this associated `const`
15+
= help: consider moving `X` to another trait
1716

1817
error[E0038]: the trait `Bar` is not dyn compatible
19-
--> $DIR/generics.rs:27:5
18+
--> $DIR/associated-consts.rs:10:5
2019
|
21-
LL | t as &dyn Bar
20+
LL | t
2221
| ^ `Bar` is not dyn compatible
2322
|
2423
note: for a trait to be dyn compatible it needs to allow building a vtable
2524
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
26-
--> $DIR/generics.rs:10:8
25+
--> $DIR/associated-consts.rs:5:11
2726
|
2827
LL | trait Bar {
2928
| --- this trait is not dyn compatible...
30-
LL | fn bar<T>(&self, t: T);
31-
| ^^^ ...because method `bar` has generic type parameters
32-
= help: consider moving `bar` to another trait
29+
LL | const X: usize;
30+
| ^ ...because it contains this associated `const`
31+
= help: consider moving `X` to another trait
3332
= note: required for the cast from `&T` to `&dyn Bar`
3433

3534
error: aborting due to 2 previous errors

tests/ui/dyn-compatibility/generics.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Check that we correctly prevent users from making trait objects
22
// from traits with generic methods, unless `where Self : Sized` is
33
// present.
4-
//@ revisions: curr dyn_compatible_for_dispatch
5-
6-
#![cfg_attr(dyn_compatible_for_dispatch, feature(dyn_compatible_for_dispatch))]
74

85

96
trait Bar {
@@ -16,18 +13,16 @@ trait Quux {
1613
}
1714

1815
fn make_bar<T:Bar>(t: &T) -> &dyn Bar {
19-
//[curr]~^ ERROR E0038
16+
//~^ ERROR E0038
2017
t
21-
//[dyn_compatible_for_dispatch]~^ ERROR E0038
22-
//[curr]~^^ ERROR E0038
18+
//~^ ERROR E0038
2319
}
2420

2521
fn make_bar_explicit<T:Bar>(t: &T) -> &dyn Bar {
26-
//[curr]~^ ERROR E0038
22+
//~^ ERROR E0038
2723
t as &dyn Bar
28-
//[dyn_compatible_for_dispatch]~^ ERROR E0038
29-
//[curr]~^^ ERROR E0038
30-
//[curr]~| ERROR E0038
24+
//~^ ERROR E0038
25+
//~| ERROR E0038
3126
}
3227

3328
fn make_quux<T:Quux>(t: &T) -> &dyn Quux {

0 commit comments

Comments
 (0)