1- //! Compute the object-safety of a trait
1+ //! Compute the dyn-compatibility of a trait
22
33use std:: ops:: ControlFlow ;
44
@@ -28,14 +28,14 @@ use crate::{
2828} ;
2929
3030#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
31- pub enum ObjectSafetyViolation {
31+ pub enum DynCompatibilityViolation {
3232SizedSelf ,
3333SelfReferential ,
3434Method ( FunctionId , MethodViolationCode ) ,
3535AssocConst ( ConstId ) ,
3636GAT ( TypeAliasId ) ,
3737// This doesn't exist in rustc, but added for better visualization
38- HasNonSafeSuperTrait ( TraitId ) ,
38+ HasNonCompatibleSuperTrait ( TraitId ) ,
3939}
4040
4141#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
@@ -50,70 +50,73 @@ pub enum MethodViolationCode {
5050UndispatchableReceiver ,
5151}
5252
53- pub fn object_safety ( db : & dyn HirDatabase , trait_ : TraitId ) -> Option < ObjectSafetyViolation > {
53+ pub fn dyn_compatibility (
54+ db : & dyn HirDatabase ,
55+ trait_ : TraitId ,
56+ ) -> Option < DynCompatibilityViolation > {
5457for super_trait in all_super_traits ( db. upcast ( ) , trait_) . into_iter ( ) . skip ( 1 ) . rev ( ) {
55- if db. object_safety_of_trait ( super_trait) . is_some ( ) {
56- return Some ( ObjectSafetyViolation :: HasNonSafeSuperTrait ( super_trait) ) ;
58+ if db. dyn_compatibility_of_trait ( super_trait) . is_some ( ) {
59+ return Some ( DynCompatibilityViolation :: HasNonCompatibleSuperTrait ( super_trait) ) ;
5760}
5861}
5962
60- db. object_safety_of_trait ( trait_)
63+ db. dyn_compatibility_of_trait ( trait_)
6164}
6265
63- pub fn object_safety_with_callback < F > (
66+ pub fn dyn_compatibility_with_callback < F > (
6467db : & dyn HirDatabase ,
6568trait_ : TraitId ,
6669cb : & mut F ,
6770) -> ControlFlow < ( ) >
6871where
69- F : FnMut ( ObjectSafetyViolation ) -> ControlFlow < ( ) > ,
72+ F : FnMut ( DynCompatibilityViolation ) -> ControlFlow < ( ) > ,
7073{
7174for super_trait in all_super_traits ( db. upcast ( ) , trait_) . into_iter ( ) . skip ( 1 ) . rev ( ) {
72- if db. object_safety_of_trait ( super_trait) . is_some ( ) {
73- cb ( ObjectSafetyViolation :: HasNonSafeSuperTrait ( trait_) ) ?;
75+ if db. dyn_compatibility_of_trait ( super_trait) . is_some ( ) {
76+ cb ( DynCompatibilityViolation :: HasNonCompatibleSuperTrait ( trait_) ) ?;
7477}
7578}
7679
77- object_safety_of_trait_with_callback ( db, trait_, cb)
80+ dyn_compatibility_of_trait_with_callback ( db, trait_, cb)
7881}
7982
80- pub fn object_safety_of_trait_with_callback < F > (
83+ pub fn dyn_compatibility_of_trait_with_callback < F > (
8184db : & dyn HirDatabase ,
8285trait_ : TraitId ,
8386cb : & mut F ,
8487) -> ControlFlow < ( ) >
8588where
86- F : FnMut ( ObjectSafetyViolation ) -> ControlFlow < ( ) > ,
89+ F : FnMut ( DynCompatibilityViolation ) -> ControlFlow < ( ) > ,
8790{
8891// Check whether this has a `Sized` bound
8992if generics_require_sized_self ( db, trait_. into ( ) ) {
90- cb ( ObjectSafetyViolation :: SizedSelf ) ?;
93+ cb ( DynCompatibilityViolation :: SizedSelf ) ?;
9194}
9295
9396// Check if there exist bounds that referencing self
9497if predicates_reference_self ( db, trait_) {
95- cb ( ObjectSafetyViolation :: SelfReferential ) ?;
98+ cb ( DynCompatibilityViolation :: SelfReferential ) ?;
9699}
97100if bounds_reference_self ( db, trait_) {
98- cb ( ObjectSafetyViolation :: SelfReferential ) ?;
101+ cb ( DynCompatibilityViolation :: SelfReferential ) ?;
99102}
100103
101104// rustc checks for non-lifetime binders here, but we don't support HRTB yet
102105
103106let trait_data = db. trait_data ( trait_) ;
104107for ( _, assoc_item) in & trait_data. items {
105- object_safety_violation_for_assoc_item ( db, trait_, * assoc_item, cb) ?;
108+ dyn_compatibility_violation_for_assoc_item ( db, trait_, * assoc_item, cb) ?;
106109}
107110
108111ControlFlow :: Continue ( ( ) )
109112}
110113
111- pub fn object_safety_of_trait_query (
114+ pub fn dyn_compatibility_of_trait_query (
112115db : & dyn HirDatabase ,
113116trait_ : TraitId ,
114- ) -> Option < ObjectSafetyViolation > {
117+ ) -> Option < DynCompatibilityViolation > {
115118let mut res = None ;
116- object_safety_of_trait_with_callback ( db, trait_, & mut |osv| {
119+ dyn_compatibility_of_trait_with_callback ( db, trait_, & mut |osv| {
117120 res = Some ( osv) ;
118121ControlFlow :: Break ( ( ) )
119122} ) ;
@@ -321,14 +324,14 @@ fn contains_illegal_self_type_reference<T: TypeVisitable<Interner>>(
321324 t. visit_with ( visitor. as_dyn ( ) , outer_binder) . is_break ( )
322325}
323326
324- fn object_safety_violation_for_assoc_item < F > (
327+ fn dyn_compatibility_violation_for_assoc_item < F > (
325328db : & dyn HirDatabase ,
326329trait_ : TraitId ,
327330item : AssocItemId ,
328331cb : & mut F ,
329332) -> ControlFlow < ( ) >
330333where
331- F : FnMut ( ObjectSafetyViolation ) -> ControlFlow < ( ) > ,
334+ F : FnMut ( DynCompatibilityViolation ) -> ControlFlow < ( ) > ,
332335{
333336// Any item that has a `Self : Sized` requisite is otherwise
334337// exempt from the regulations.
@@ -337,10 +340,10 @@ where
337340}
338341
339342match item {
340- AssocItemId :: ConstId ( it) => cb ( ObjectSafetyViolation :: AssocConst ( it) ) ,
343+ AssocItemId :: ConstId ( it) => cb ( DynCompatibilityViolation :: AssocConst ( it) ) ,
341344AssocItemId :: FunctionId ( it) => {
342345virtual_call_violations_for_method ( db, trait_, it, & mut |mvc| {
343- cb ( ObjectSafetyViolation :: Method ( it, mvc) )
346+ cb ( DynCompatibilityViolation :: Method ( it, mvc) )
344347} )
345348}
346349AssocItemId :: TypeAliasId ( it) => {
@@ -350,7 +353,7 @@ where
350353} else {
351354let generic_params = db. generic_params ( item. into ( ) ) ;
352355if !generic_params. is_empty ( ) {
353- cb ( ObjectSafetyViolation :: GAT ( it) )
356+ cb ( DynCompatibilityViolation :: GAT ( it) )
354357} else {
355358ControlFlow :: Continue ( ( ) )
356359}
@@ -469,7 +472,7 @@ fn receiver_is_dispatchable(
469472return false ;
470473} ;
471474
472- // `self: Self` can't be dispatched on, but this is already considered object safe.
475+ // `self: Self` can't be dispatched on, but this is already considered dyn compatible
473476// See rustc's comment on https://github.com/rust-lang/rust/blob/3f121b9461cce02a703a0e7e450568849dfaa074/compiler/rustc_trait_selection/src/traits/object_safety.rs#L433-L437
474477if sig
475478. skip_binders ( )
0 commit comments