Skip to content

Commit

Permalink
Rename feature object_safe_for_dispatch to dyn_compatible_for_dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Oct 9, 2024
1 parent 62b24ea commit 2e7a52b
Show file tree
Hide file tree
Showing 42 changed files with 130 additions and 124 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_feature/src/removed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ declare_features! (
/// then removed. But there was no utility storing it separately, so now
/// it's in this list.
(removed, no_stack_check, "1.0.0", None, None),
/// Allows making `dyn Trait` well-formed even if `Trait` is not dyn-compatible (object safe).
/// Renamed to `dyn_compatible_for_dispatch`.
(removed, object_safe_for_dispatch, "CURRENT_RUSTC_VERSION", Some(43561),
Some("renamed to `dyn_compatible_for_dispatch`")),
/// Allows using `#[on_unimplemented(..)]` on traits.
/// (Moved to `rustc_attrs`.)
(removed, on_unimplemented, "1.40.0", None, None),
Expand Down
15 changes: 8 additions & 7 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ declare_features! (
(unstable, doc_notable_trait, "1.52.0", Some(45040)),
/// Allows using the `may_dangle` attribute (RFC 1327).
(unstable, dropck_eyepatch, "1.10.0", Some(34761)),
/// Allows making `dyn Trait` well-formed even if `Trait` is not dyn-compatible[^1].
/// In that case, `dyn Trait: Trait` does not hold. Moreover, coercions and
/// casts in safe Rust to `dyn Trait` for such a `Trait` is also forbidden.
///
/// Renamed from `object_safe_for_dispatch`.
///
/// [^1]: Formerly known as "object safe".
(unstable, dyn_compatible_for_dispatch, "CURRENT_RUSTC_VERSION", Some(43561)),
/// Allows using the `#[fundamental]` attribute.
(unstable, fundamental, "1.0.0", Some(29635)),
/// Allows using `#[link_name="llvm.*"]`.
Expand Down Expand Up @@ -544,13 +552,6 @@ declare_features! (
(unstable, non_exhaustive_omitted_patterns_lint, "1.57.0", Some(89554)),
/// Allows `for<T>` binders in where-clauses
(incomplete, non_lifetime_binders, "1.69.0", Some(108185)),
/// Allows making `dyn Trait` well-formed even if `Trait` is not dyn-compatible[^1].
/// In that case, `dyn Trait: Trait` does not hold. Moreover, coercions and
/// casts in safe Rust to `dyn Trait` for such a `Trait` is also forbidden.
///
/// [^1]: Formerly known as "object safe".
// FIXME(dyn_compat_renaming): Rename feature.
(unstable, object_safe_for_dispatch, "1.40.0", Some(43561)),
/// Allows using enums in offset_of!
(unstable, offset_of_enum, "1.75.0", Some(120141)),
/// Allows using fields with slice type in offset_of!
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/coherence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn check_object_overlap<'tcx>(
for component_def_id in component_def_ids {
if !tcx.is_dyn_compatible(component_def_id) {
// FIXME(dyn_compat_renaming): Rename test and update comment.
// Without the 'object_safe_for_dispatch' feature this is an error
// Without the 'dyn_compatible_for_dispatch' feature this is an error
// which will be reported by wfcheck. Ignore it here.
// This is tested by `coherence-impl-trait-for-trait-object-safe.rs`.
// With the feature enabled, the trait is not implemented automatically,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ symbols! {
dropck_eyepatch,
dropck_parametricity,
dylib,
dyn_compatible_for_dispatch,
dyn_metadata,
dyn_star,
dyn_trait,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,8 @@ fn object_ty_for_trait<'tcx>(
/// contained by the trait object, because the object that needs to be coerced is behind
/// a pointer.
///
/// In practice, we cannot use `dyn Trait` explicitly in the obligation because it would result
/// in a new check that `Trait` is dyn-compatible, creating a cycle (until object_safe_for_dispatch
/// In practice, we cannot use `dyn Trait` explicitly in the obligation because it would result in
/// a new check that `Trait` is dyn-compatible, creating a cycle (until dyn_compatible_for_dispatch
/// is stabilized, see tracking issue <https://github.com/rust-lang/rust/issues/43561>).
/// Instead, we fudge a little by introducing a new type parameter `U` such that
/// `Self: Unsize<U>` and `U: Trait + ?Sized`, and use `U` in place of `dyn Trait`.
Expand Down Expand Up @@ -674,7 +674,7 @@ fn receiver_is_dispatchable<'tcx>(

// the type `U` in the query
// use a bogus type parameter to mimic a forall(U) query using u32::MAX for now.
// FIXME(mikeyhew) this is a total hack. Once object_safe_for_dispatch is stabilized, we can
// FIXME(mikeyhew) this is a total hack. Once dyn_compatible_for_dispatch is stabilized, we can
// replace this with `dyn Trait`
let unsized_self_ty: Ty<'tcx> =
Ty::new_param(tcx, u32::MAX, Symbol::intern("RustaceansAreAwesome"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}

if let Some(principal) = data.principal() {
if !self.infcx.tcx.features().object_safe_for_dispatch {
if !self.infcx.tcx.features().dyn_compatible_for_dispatch {
principal.with_self_ty(self.tcx(), self_ty)
} else if self.tcx().is_dyn_compatible(principal.def_id()) {
principal.with_self_ty(self.tcx(), self_ty)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
// obligations that don't refer to Self and
// checking those

let defer_to_coercion = tcx.features().object_safe_for_dispatch;
let defer_to_coercion = tcx.features().dyn_compatible_for_dispatch;

if !defer_to_coercion {
if let Some(principal) = data.principal_def_id() {
Expand Down
2 changes: 1 addition & 1 deletion tests/crashes/120241-2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ known-bug: #120241
//@ edition:2021
#![feature(object_safe_for_dispatch)]
#![feature(dyn_compatible_for_dispatch)]
#![feature(unsized_fn_params)]

fn guard(_s: Copy) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion tests/crashes/120241.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ known-bug: #120241
//@ edition:2021
#![feature(object_safe_for_dispatch)]
#![feature(dyn_compatible_for_dispatch)]

trait B {
fn f(a: A) -> A;
Expand Down
2 changes: 1 addition & 1 deletion tests/crashes/120482.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ known-bug: #120482
//@ edition:2021
#![feature(object_safe_for_dispatch)]
#![feature(dyn_compatible_for_dispatch)]

trait B {
fn bar(&self, x: &Self);
Expand Down
2 changes: 1 addition & 1 deletion tests/crashes/125512.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ known-bug: rust-lang/rust#125512
//@ edition:2021
#![feature(object_safe_for_dispatch)]
#![feature(dyn_compatible_for_dispatch)]
trait B {
fn f(a: A) -> A;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/crashes/128176.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ known-bug: rust-lang/rust#128176

#![feature(generic_const_exprs)]
#![feature(object_safe_for_dispatch)]
#![feature(dyn_compatible_for_dispatch)]
trait X {
type Y<const N: i16>;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/crashes/130521.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ known-bug: #130521

#![feature(object_safe_for_dispatch)]
#![feature(dyn_compatible_for_dispatch)]
struct Vtable(dyn Cap);

trait Cap<'a> {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/coherence/coherence-unsafe-trait-object-impl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Check that unsafe trait object do not implement themselves
// automatically

#![feature(object_safe_for_dispatch)]
#![feature(dyn_compatible_for_dispatch)]

trait Trait: Sized {
fn call(&self);
Expand Down
41 changes: 41 additions & 0 deletions tests/ui/feature-gates/feature-gate-dyn_compatible_for_dispatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Test that the use of the dyn-incompatible trait objects
// are gated by the `dyn_compatible_for_dispatch` feature gate.

trait DynIncompatible1: Sized {}

trait DynIncompatible2 {
fn static_fn() {}
}

trait DynIncompatible3 {
fn foo<T>(&self);
}

trait DynIncompatible4 {
fn foo(&self, s: &Self);
}

fn takes_non_object_safe_ref<T>(obj: &dyn DynIncompatible1) {
//~^ ERROR E0038
}

fn return_non_object_safe_ref() -> &'static dyn DynIncompatible2 {
//~^ ERROR E0038
loop {}
}

fn takes_non_object_safe_box(obj: Box<dyn DynIncompatible3>) {
//~^ ERROR E0038
}

fn return_non_object_safe_rc() -> std::rc::Rc<dyn DynIncompatible4> {
//~^ ERROR E0038
loop {}
}

trait Trait {}

impl Trait for dyn DynIncompatible1 {}
//~^ ERROR E0038

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
error[E0038]: the trait `NonObjectSafe1` cannot be made into an object
--> $DIR/feature-gate-object_safe_for_dispatch.rs:18:39
error[E0038]: the trait `DynIncompatible1` cannot be made into an object
--> $DIR/feature-gate-dyn_compatible_for_dispatch.rs:18:39
|
LL | fn takes_non_object_safe_ref<T>(obj: &dyn NonObjectSafe1) {
| ^^^^^^^^^^^^^^^^^^ `NonObjectSafe1` cannot be made into an object
LL | fn takes_non_object_safe_ref<T>(obj: &dyn DynIncompatible1) {
| ^^^^^^^^^^^^^^^^^^^^ `DynIncompatible1` cannot be made into an object
|
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/feature-gate-object_safe_for_dispatch.rs:4:23
--> $DIR/feature-gate-dyn_compatible_for_dispatch.rs:4:25
|
LL | trait NonObjectSafe1: Sized {}
| -------------- ^^^^^ ...because it requires `Self: Sized`
LL | trait DynIncompatible1: Sized {}
| ---------------- ^^^^^ ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...

error[E0038]: the trait `NonObjectSafe2` cannot be made into an object
--> $DIR/feature-gate-object_safe_for_dispatch.rs:22:45
error[E0038]: the trait `DynIncompatible2` cannot be made into an object
--> $DIR/feature-gate-dyn_compatible_for_dispatch.rs:22:45
|
LL | fn return_non_object_safe_ref() -> &'static dyn NonObjectSafe2 {
| ^^^^^^^^^^^^^^^^^^ `NonObjectSafe2` cannot be made into an object
LL | fn return_non_object_safe_ref() -> &'static dyn DynIncompatible2 {
| ^^^^^^^^^^^^^^^^^^^^ `DynIncompatible2` cannot be made into an object
|
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/feature-gate-object_safe_for_dispatch.rs:7:8
--> $DIR/feature-gate-dyn_compatible_for_dispatch.rs:7:8
|
LL | trait NonObjectSafe2 {
| -------------- this trait cannot be made into an object...
LL | trait DynIncompatible2 {
| ---------------- this trait cannot be made into an object...
LL | fn static_fn() {}
| ^^^^^^^^^ ...because associated function `static_fn` has no `self` parameter
help: consider turning `static_fn` into a method by giving it a `&self` argument
Expand All @@ -34,47 +34,47 @@ help: alternatively, consider constraining `static_fn` so it does not apply to t
LL | fn static_fn() where Self: Sized {}
| +++++++++++++++++

error[E0038]: the trait `NonObjectSafe3` cannot be made into an object
--> $DIR/feature-gate-object_safe_for_dispatch.rs:27:39
error[E0038]: the trait `DynIncompatible3` cannot be made into an object
--> $DIR/feature-gate-dyn_compatible_for_dispatch.rs:27:39
|
LL | fn takes_non_object_safe_box(obj: Box<dyn NonObjectSafe3>) {
| ^^^^^^^^^^^^^^^^^^ `NonObjectSafe3` cannot be made into an object
LL | fn takes_non_object_safe_box(obj: Box<dyn DynIncompatible3>) {
| ^^^^^^^^^^^^^^^^^^^^ `DynIncompatible3` cannot be made into an object
|
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/feature-gate-object_safe_for_dispatch.rs:11:8
--> $DIR/feature-gate-dyn_compatible_for_dispatch.rs:11:8
|
LL | trait NonObjectSafe3 {
| -------------- this trait cannot be made into an object...
LL | trait DynIncompatible3 {
| ---------------- this trait cannot be made into an object...
LL | fn foo<T>(&self);
| ^^^ ...because method `foo` has generic type parameters
= help: consider moving `foo` to another trait

error[E0038]: the trait `NonObjectSafe4` cannot be made into an object
--> $DIR/feature-gate-object_safe_for_dispatch.rs:31:47
error[E0038]: the trait `DynIncompatible4` cannot be made into an object
--> $DIR/feature-gate-dyn_compatible_for_dispatch.rs:31:47
|
LL | fn return_non_object_safe_rc() -> std::rc::Rc<dyn NonObjectSafe4> {
| ^^^^^^^^^^^^^^^^^^ `NonObjectSafe4` cannot be made into an object
LL | fn return_non_object_safe_rc() -> std::rc::Rc<dyn DynIncompatible4> {
| ^^^^^^^^^^^^^^^^^^^^ `DynIncompatible4` cannot be made into an object
|
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/feature-gate-object_safe_for_dispatch.rs:15:22
--> $DIR/feature-gate-dyn_compatible_for_dispatch.rs:15:22
|
LL | trait NonObjectSafe4 {
| -------------- this trait cannot be made into an object...
LL | trait DynIncompatible4 {
| ---------------- this trait cannot be made into an object...
LL | fn foo(&self, s: &Self);
| ^^^^^ ...because method `foo` references the `Self` type in this parameter
= help: consider moving `foo` to another trait

error[E0038]: the trait `NonObjectSafe1` cannot be made into an object
--> $DIR/feature-gate-object_safe_for_dispatch.rs:38:16
error[E0038]: the trait `DynIncompatible1` cannot be made into an object
--> $DIR/feature-gate-dyn_compatible_for_dispatch.rs:38:16
|
LL | impl Trait for dyn NonObjectSafe1 {}
| ^^^^^^^^^^^^^^^^^^ `NonObjectSafe1` cannot be made into an object
LL | impl Trait for dyn DynIncompatible1 {}
| ^^^^^^^^^^^^^^^^^^^^ `DynIncompatible1` cannot be made into an object
|
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/feature-gate-object_safe_for_dispatch.rs:4:23
--> $DIR/feature-gate-dyn_compatible_for_dispatch.rs:4:25
|
LL | trait NonObjectSafe1: Sized {}
| -------------- ^^^^^ ...because it requires `Self: Sized`
LL | trait DynIncompatible1: Sized {}
| ---------------- ^^^^^ ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...

Expand Down
41 changes: 0 additions & 41 deletions tests/ui/feature-gates/feature-gate-object_safe_for_dispatch.rs

This file was deleted.

8 changes: 4 additions & 4 deletions tests/ui/kindck/kindck-inherited-copy-bound.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Test that Copy bounds inherited by trait are checked.
//
//@ revisions: curr object_safe_for_dispatch
//@ revisions: curr dyn_compatible_for_dispatch

#![cfg_attr(object_safe_for_dispatch, feature(object_safe_for_dispatch))]
#![cfg_attr(dyn_compatible_for_dispatch, feature(dyn_compatible_for_dispatch))]


use std::any::Any;
Expand All @@ -19,7 +19,7 @@ fn take_param<T:Foo>(foo: &T) { }
fn a() {
let x: Box<_> = Box::new(3);
take_param(&x); //[curr]~ ERROR E0277
//[object_safe_for_dispatch]~^ ERROR E0277
//[dyn_compatible_for_dispatch]~^ ERROR E0277
}

fn b() {
Expand All @@ -28,7 +28,7 @@ fn b() {
let z = &x as &dyn Foo;
//[curr]~^ ERROR E0038
//[curr]~| ERROR E0038
//[object_safe_for_dispatch]~^^^ ERROR E0038
//[dyn_compatible_for_dispatch]~^^^ ERROR E0038
}

fn main() { }
4 changes: 2 additions & 2 deletions tests/ui/object-safety/object-safety-associated-consts.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Check that we correctly prevent users from making trait objects
// from traits with associated consts.
//
//@ revisions: curr object_safe_for_dispatch
//@ revisions: curr dyn_compatible_for_dispatch

#![cfg_attr(object_safe_for_dispatch, feature(object_safe_for_dispatch))]
#![cfg_attr(dyn_compatible_for_dispatch, feature(dyn_compatible_for_dispatch))]

trait Bar {
const X: usize;
Expand Down
Loading

0 comments on commit 2e7a52b

Please sign in to comment.