Skip to content

Commit 01a063f

Browse files
committed
Compiler: Rename "object safe" to "dyn compatible"
1 parent f5cd2c5 commit 01a063f

File tree

183 files changed

+523
-510
lines changed

Some content is hidden

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

183 files changed

+523
-510
lines changed

compiler/rustc_const_eval/src/interpret/call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
598598
// codegen'd / interpreted as virtual calls through the vtable.
599599
ty::InstanceKind::Virtual(def_id, idx) => {
600600
let mut args = args.to_vec();
601-
// We have to implement all "object safe receivers". So we have to go search for a
601+
// We have to implement all "dyn-compatible receivers". So we have to go search for a
602602
// pointer or `dyn Trait` type, but it could be wrapped in newtypes. So recursively
603603
// unwrap those newtypes until we are there.
604604
// An `InPlace` does nothing here, we keep the original receiver intact. We can't

compiler/rustc_error_codes/src/error_codes/E0038.md

+18-15
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ trait, written in type positions) but this was a bit too confusing, so we now
55
write `dyn Trait`.
66

77
Some traits are not allowed to be used as trait object types. The traits that
8-
are allowed to be used as trait object types are called "object-safe" traits.
9-
Attempting to use a trait object type for a trait that is not object-safe will
10-
trigger error E0038.
8+
are allowed to be used as trait object types are called "dyn-compatible"[^1]
9+
traits. Attempting to use a trait object type for a trait that is not
10+
dyn-compatible will trigger error E0038.
1111

1212
Two general aspects of trait object types give rise to the restrictions:
1313

@@ -25,13 +25,16 @@ Two general aspects of trait object types give rise to the restrictions:
2525
objects with the same trait object type may point to vtables from different
2626
implementations.
2727

28-
The specific conditions that violate object-safety follow, most of which relate
29-
to missing size information and vtable polymorphism arising from these aspects.
28+
The specific conditions that violate dyn-compatibility follow, most of which
29+
relate to missing size information and vtable polymorphism arising from these
30+
aspects.
31+
32+
[^1]: Formerly known as "object-safe".
3033

3134
### The trait requires `Self: Sized`
3235

3336
Traits that are declared as `Trait: Sized` or which otherwise inherit a
34-
constraint of `Self:Sized` are not object-safe.
37+
constraint of `Self:Sized` are not dyn-compatible.
3538

3639
The reasoning behind this is somewhat subtle. It derives from the fact that Rust
3740
requires (and defines) that every trait object type `dyn Trait` automatically
@@ -58,7 +61,7 @@ implement a sized trait like `Trait:Sized`. So, rather than allow an exception
5861
to the rule that `dyn Trait` always implements `Trait`, Rust chooses to prohibit
5962
such a `dyn Trait` from existing at all.
6063

61-
Only unsized traits are considered object-safe.
64+
Only unsized traits are considered dyn-compatible.
6265

6366
Generally, `Self: Sized` is used to indicate that the trait should not be used
6467
as a trait object. If the trait comes from your own crate, consider removing
@@ -103,8 +106,8 @@ fn call_foo(x: Box<dyn Trait>) {
103106
}
104107
```
105108

106-
If only some methods aren't object-safe, you can add a `where Self: Sized` bound
107-
on them to mark them as explicitly unavailable to trait objects. The
109+
If only some methods aren't dyn-compatible, you can add a `where Self: Sized`
110+
bound on them to mark them as explicitly unavailable to trait objects. The
108111
functionality will still be available to all other implementers, including
109112
`Box<dyn Trait>` which is itself sized (assuming you `impl Trait for Box<dyn
110113
Trait>`).
@@ -117,7 +120,7 @@ trait Trait {
117120
```
118121

119122
Now, `foo()` can no longer be called on a trait object, but you will now be
120-
allowed to make a trait object, and that will be able to call any object-safe
123+
allowed to make a trait object, and that will be able to call any dyn-compatible
121124
methods. With such a bound, one can still call `foo()` on types implementing
122125
that trait that aren't behind trait objects.
123126

@@ -306,18 +309,18 @@ Here, the supertrait might have methods as follows:
306309

307310
```
308311
trait Super<A: ?Sized> {
309-
fn get_a(&self) -> &A; // note that this is object safe!
312+
fn get_a(&self) -> &A; // note that this is dyn-compatible!
310313
}
311314
```
312315

313316
If the trait `Trait` was deriving from something like `Super<String>` or
314317
`Super<T>` (where `Foo` itself is `Foo<T>`), this is okay, because given a type
315318
`get_a()` will definitely return an object of that type.
316319

317-
However, if it derives from `Super<Self>`, even though `Super` is object safe,
318-
the method `get_a()` would return an object of unknown type when called on the
319-
function. `Self` type parameters let us make object safe traits no longer safe,
320-
so they are forbidden when specifying supertraits.
320+
However, if it derives from `Super<Self>`, even though `Super` is
321+
dyn-compatible, the method `get_a()` would return an object of unknown type when
322+
called on the function. `Self` type parameters let us make dyn-compatible traits
323+
no longer compatible, so they are forbidden when specifying supertraits.
321324

322325
There's no easy fix for this. Generally, code will need to be refactored so that
323326
you no longer need to derive from `Super<Self>`.

compiler/rustc_error_codes/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ E0800: 0800,
623623
// E0314, // closure outlives stack frame
624624
// E0315, // cannot invoke closure outside of its lifetime
625625
// E0319, // trait impls for defaulted traits allowed just for structs/enums
626-
// E0372, // coherence not object safe
626+
// E0372, // coherence not dyn-compatible
627627
// E0385, // {} in an aliasable location
628628
// E0402, // cannot use an outer type parameter in this context
629629
// E0406, // merged into 420

compiler/rustc_feature/src/unstable.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,12 @@ declare_features! (
548548
(unstable, non_exhaustive_omitted_patterns_lint, "1.57.0", Some(89554)),
549549
/// Allows `for<T>` binders in where-clauses
550550
(incomplete, non_lifetime_binders, "1.69.0", Some(108185)),
551-
/// Allows making `dyn Trait` well-formed even if `Trait` is not object safe.
551+
/// Allows making `dyn Trait` well-formed even if `Trait` is not dyn-compatible[^1].
552552
/// In that case, `dyn Trait: Trait` does not hold. Moreover, coercions and
553553
/// casts in safe Rust to `dyn Trait` for such a `Trait` is also forbidden.
554+
///
555+
/// [^1]: Formerly known as "object safe".
556+
// FIXME(dyn_compat_renaming): Rename feature.
554557
(unstable, object_safe_for_dispatch, "1.40.0", Some(43561)),
555558
/// Allows using enums in offset_of!
556559
(unstable, offset_of_enum, "1.75.0", Some(120141)),

compiler/rustc_hir_analysis/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ hir_analysis_unrecognized_intrinsic_function =
558558
.help = if you're adding an intrinsic, be sure to update `check_intrinsic_type`
559559
560560
hir_analysis_unused_associated_type_bounds =
561-
unnecessary associated type bound for not object safe associated type
561+
unnecessary associated type bound for dyn-incompatible associated type
562562
.note = this associated type has a `where Self: Sized` bound, and while the associated type can be specified, it cannot be used because trait objects are never `Sized`
563563
.suggestion = remove this bound
564564

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ fn check_trait_item<'tcx>(
374374
hir::TraitItemKind::Type(_bounds, Some(ty)) => (None, ty.span),
375375
_ => (None, trait_item.span),
376376
};
377-
check_object_unsafe_self_trait_by_name(tcx, trait_item);
377+
check_dyn_incompatible_self_trait_by_name(tcx, trait_item);
378378
let mut res = check_associated_item(tcx, def_id, span, method_sig);
379379

380380
if matches!(trait_item.kind, hir::TraitItemKind::Fn(..)) {
@@ -838,9 +838,10 @@ fn could_be_self(trait_def_id: LocalDefId, ty: &hir::Ty<'_>) -> bool {
838838
}
839839
}
840840

841-
/// Detect when an object unsafe trait is referring to itself in one of its associated items.
842-
/// When this is done, suggest using `Self` instead.
843-
fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem<'_>) {
841+
/// Detect when a dyn-incompatible trait is referring to itself in one of its associated items.
842+
///
843+
/// In such cases, suggest using `Self` instead.
844+
fn check_dyn_incompatible_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem<'_>) {
844845
let (trait_name, trait_def_id) =
845846
match tcx.hir_node_by_def_id(tcx.hir().get_parent_item(item.hir_id()).def_id) {
846847
hir::Node::Item(item) => match item.kind {
@@ -872,7 +873,7 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem
872873
_ => {}
873874
}
874875
if !trait_should_be_self.is_empty() {
875-
if tcx.is_object_safe(trait_def_id) {
876+
if tcx.is_dyn_compatible(trait_def_id) {
876877
return;
877878
}
878879
let sugg = trait_should_be_self.iter().map(|span| (*span, "Self".to_string())).collect();

compiler/rustc_hir_analysis/src/coherence/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ fn check_object_overlap<'tcx>(
179179

180180
// check for overlap with the automatic `impl Trait for dyn Trait`
181181
if let ty::Dynamic(data, ..) = trait_ref.self_ty().kind() {
182-
// This is something like impl Trait1 for Trait2. Illegal
183-
// if Trait1 is a supertrait of Trait2 or Trait2 is not object safe.
182+
// This is something like `impl Trait1 for Trait2`. Illegal if
183+
// Trait1 is a supertrait of Trait2 or Trait2 is not dyn-compatible.
184184

185185
let component_def_ids = data.iter().flat_map(|predicate| {
186186
match predicate.skip_binder() {
@@ -193,7 +193,8 @@ fn check_object_overlap<'tcx>(
193193
});
194194

195195
for component_def_id in component_def_ids {
196-
if !tcx.is_object_safe(component_def_id) {
196+
if !tcx.is_dyn_compatible(component_def_id) {
197+
// FIXME(dyn_compat_renaming): Rename test and update comment.
197198
// Without the 'object_safe_for_dispatch' feature this is an error
198199
// which will be reported by wfcheck. Ignore it here.
199200
// This is tested by `coherence-impl-trait-for-trait-object-safe.rs`.

compiler/rustc_hir_analysis/src/coherence/orphan.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,16 @@ pub(crate) fn orphan_check_impl(
109109
//
110110
// auto trait AutoTrait {}
111111
//
112-
// trait ObjectSafeTrait {
112+
// trait DynCompatibleTrait {
113113
// fn f(&self) where Self: AutoTrait;
114114
// }
115115
//
116-
// We can allow f to be called on `dyn ObjectSafeTrait + AutoTrait`.
116+
// We can allow f to be called on `dyn DynCompatibleTrait + AutoTrait`.
117117
//
118118
// If we didn't deny `impl AutoTrait for dyn Trait`, it would be unsound
119-
// for the ObjectSafeTrait shown above to be object safe because someone
120-
// could take some type implementing ObjectSafeTrait but not AutoTrait,
121-
// unsize it to `dyn ObjectSafeTrait`, and call .f() which has no
119+
// for the `DynCompatibleTrait` shown above to be dyn-compatible because someone
120+
// could take some type implementing `DynCompatibleTrait` but not `AutoTrait`,
121+
// unsize it to `dyn DynCompatibleTrait`, and call `.f()` which has no
122122
// concrete implementation (issue #50781).
123123
enum LocalImpl {
124124
Allow,

compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use rustc_middle::ty::{
1111
self, DynKind, ExistentialPredicateStableCmpExt as _, Ty, TyCtxt, TypeFoldable, Upcast,
1212
};
1313
use rustc_span::{ErrorGuaranteed, Span};
14-
use rustc_trait_selection::error_reporting::traits::report_object_safety_error;
15-
use rustc_trait_selection::traits::{self, hir_ty_lowering_object_safety_violations};
14+
use rustc_trait_selection::error_reporting::traits::report_dyn_incompatibility;
15+
use rustc_trait_selection::traits::{self, hir_ty_lowering_dyn_compatibility_violations};
1616
use smallvec::{SmallVec, smallvec};
1717
use tracing::{debug, instrument};
1818

@@ -99,19 +99,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
9999
return Ty::new_error(tcx, reported);
100100
}
101101

102-
// Check that there are no gross object safety violations;
102+
// Check that there are no gross dyn-compatibility violations;
103103
// most importantly, that the supertraits don't contain `Self`,
104104
// to avoid ICEs.
105105
for item in &regular_traits {
106-
let object_safety_violations =
107-
hir_ty_lowering_object_safety_violations(tcx, item.trait_ref().def_id());
108-
if !object_safety_violations.is_empty() {
109-
let reported = report_object_safety_error(
106+
let violations =
107+
hir_ty_lowering_dyn_compatibility_violations(tcx, item.trait_ref().def_id());
108+
if !violations.is_empty() {
109+
let reported = report_dyn_incompatibility(
110110
tcx,
111111
span,
112112
Some(hir_id),
113113
item.trait_ref().def_id(),
114-
&object_safety_violations,
114+
&violations,
115115
)
116116
.emit();
117117
return Ty::new_error(tcx, reported);
@@ -275,8 +275,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
275275
tcx.item_name(def_id),
276276
)
277277
.with_note(
278-
rustc_middle::traits::ObjectSafetyViolation::SupertraitSelf(smallvec![])
279-
.error_msg(),
278+
rustc_middle::traits::DynCompatibilityViolation::SupertraitSelf(
279+
smallvec![],
280+
)
281+
.error_msg(),
280282
)
281283
.emit();
282284
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ use rustc_session::parse::feature_err;
1919
use rustc_span::edit_distance::find_best_match_for_name;
2020
use rustc_span::symbol::{Ident, kw, sym};
2121
use rustc_span::{BytePos, DUMMY_SP, Span, Symbol};
22-
use rustc_trait_selection::error_reporting::traits::report_object_safety_error;
22+
use rustc_trait_selection::error_reporting::traits::report_dyn_incompatibility;
2323
use rustc_trait_selection::traits::{
24-
FulfillmentError, TraitAliasExpansionInfo, object_safety_violations_for_assoc_item,
24+
FulfillmentError, TraitAliasExpansionInfo, dyn_compatibility_violations_for_assoc_item,
2525
};
2626

2727
use crate::errors::{
@@ -739,7 +739,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
739739
// Account for things like `dyn Foo + 'a`, like in tests `issue-22434.rs` and
740740
// `issue-22560.rs`.
741741
let mut trait_bound_spans: Vec<Span> = vec![];
742-
let mut object_safety_violations = false;
742+
let mut dyn_compatibility_violations = false;
743743
for (span, items) in &associated_types {
744744
if !items.is_empty() {
745745
trait_bound_spans.push(*span);
@@ -750,14 +750,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
750750
names_len += 1;
751751

752752
let violations =
753-
object_safety_violations_for_assoc_item(tcx, trait_def_id, *assoc_item);
753+
dyn_compatibility_violations_for_assoc_item(tcx, trait_def_id, *assoc_item);
754754
if !violations.is_empty() {
755-
report_object_safety_error(tcx, *span, None, trait_def_id, &violations).emit();
756-
object_safety_violations = true;
755+
report_dyn_incompatibility(tcx, *span, None, trait_def_id, &violations).emit();
756+
dyn_compatibility_violations = true;
757757
}
758758
}
759759
}
760-
if object_safety_violations {
760+
if dyn_compatibility_violations {
761761
return;
762762
}
763763

compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
7777
if self_ty.span.can_be_used_for_suggestions()
7878
&& !self.maybe_suggest_impl_trait(self_ty, &mut diag)
7979
{
80-
// FIXME: Only emit this suggestion if the trait is object safe.
80+
// FIXME: Only emit this suggestion if the trait is dyn-compatible.
8181
diag.multipart_suggestion_verbose(label, sugg, Applicability::MachineApplicable);
8282
}
8383
// Check if the impl trait that we are considering is an impl of a local trait.
@@ -89,7 +89,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
8989
lint.primary_message("trait objects without an explicit `dyn` are deprecated");
9090
if self_ty.span.can_be_used_for_suggestions() {
9191
lint.multipart_suggestion_verbose(
92-
"if this is an object-safe trait, use `dyn`",
92+
"if this is a dyn-compatible trait, use `dyn`",
9393
sugg,
9494
Applicability::MachineApplicable,
9595
);
@@ -196,15 +196,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
196196
let mut is_downgradable = true;
197197

198198
// Check if trait object is safe for suggesting dynamic dispatch.
199-
let is_object_safe = match self_ty.kind {
199+
let is_dyn_compatible = match self_ty.kind {
200200
hir::TyKind::TraitObject(objects, ..) => {
201201
objects.iter().all(|(o, _)| match o.trait_ref.path.res {
202202
Res::Def(DefKind::Trait, id) => {
203203
if Some(id) == owner {
204204
// For recursive traits, don't downgrade the error. (#119652)
205205
is_downgradable = false;
206206
}
207-
tcx.is_object_safe(id)
207+
tcx.is_dyn_compatible(id)
208208
}
209209
_ => false,
210210
})
@@ -221,8 +221,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
221221
if let hir::FnRetTy::Return(ty) = sig.decl.output
222222
&& ty.peel_refs().hir_id == self_ty.hir_id
223223
{
224-
let pre = if !is_object_safe {
225-
format!("`{trait_name}` is not object safe, ")
224+
let pre = if !is_dyn_compatible {
225+
format!("`{trait_name}` is dyn-incompatible, ")
226226
} else {
227227
String::new()
228228
};
@@ -234,7 +234,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
234234
diag.multipart_suggestion_verbose(msg, impl_sugg, Applicability::MachineApplicable);
235235

236236
// Suggest `Box<dyn Trait>` for return type
237-
if is_object_safe {
237+
if is_dyn_compatible {
238238
// If the return type is `&Trait`, we don't want
239239
// the ampersand to be displayed in the `Box<dyn Trait>`
240240
// suggestion.
@@ -253,7 +253,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
253253
Applicability::MachineApplicable,
254254
);
255255
} else if is_downgradable {
256-
// We'll emit the object safety error already, with a structured suggestion.
256+
// We'll emit the dyn-compatibility error already, with a structured suggestion.
257257
diag.downgrade_to_delayed_bug();
258258
}
259259
return true;
@@ -276,10 +276,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
276276
impl_sugg,
277277
Applicability::MachineApplicable,
278278
);
279-
if !is_object_safe {
280-
diag.note(format!("`{trait_name}` it is not object safe, so it can't be `dyn`"));
279+
if !is_dyn_compatible {
280+
diag.note(format!("`{trait_name}` it is dyn-incompatible, so it can't be `dyn`"));
281281
if is_downgradable {
282-
// We'll emit the object safety error already, with a structured suggestion.
282+
// We'll emit the dyn-compatibility error already, with a structured suggestion.
283283
diag.downgrade_to_delayed_bug();
284284
}
285285
} else {

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
1616
mod bounds;
1717
mod cmse;
18+
mod dyn_compatibility;
1819
pub mod errors;
1920
pub mod generics;
2021
mod lint;
21-
mod object_safety;
2222

2323
use std::slice;
2424

compiler/rustc_hir_typeck/src/coercion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
655655
return Err(TypeError::Mismatch);
656656
}
657657

658-
// Object safety violations or miscellaneous.
658+
// Dyn-compatibility violations or miscellaneous.
659659
Err(err) => {
660660
self.err_ctxt().report_selection_error(obligation.clone(), &obligation, &err);
661661
// Treat this like an obligation and follow through

0 commit comments

Comments
 (0)