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 {
3232 SizedSelf ,
3333 SelfReferential ,
3434 Method ( FunctionId , MethodViolationCode ) ,
3535 AssocConst ( ConstId ) ,
3636 GAT ( 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 {
5050 UndispatchableReceiver ,
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 > {
5457 for 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 > (
6467 db : & dyn HirDatabase ,
6568 trait_ : TraitId ,
6669 cb : & mut F ,
6770) -> ControlFlow < ( ) >
6871where
69- F : FnMut ( ObjectSafetyViolation ) -> ControlFlow < ( ) > ,
72+ F : FnMut ( DynCompatibilityViolation ) -> ControlFlow < ( ) > ,
7073{
7174 for 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 > (
8184 db : & dyn HirDatabase ,
8285 trait_ : TraitId ,
8386 cb : & mut F ,
8487) -> ControlFlow < ( ) >
8588where
86- F : FnMut ( ObjectSafetyViolation ) -> ControlFlow < ( ) > ,
89+ F : FnMut ( DynCompatibilityViolation ) -> ControlFlow < ( ) > ,
8790{
8891 // Check whether this has a `Sized` bound
8992 if 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
9497 if predicates_reference_self ( db, trait_) {
95- cb ( ObjectSafetyViolation :: SelfReferential ) ?;
98+ cb ( DynCompatibilityViolation :: SelfReferential ) ?;
9699 }
97100 if 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
103106 let trait_data = db. trait_data ( trait_) ;
104107 for ( _, 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
108111 ControlFlow :: Continue ( ( ) )
109112}
110113
111- pub fn object_safety_of_trait_query (
114+ pub fn dyn_compatibility_of_trait_query (
112115 db : & dyn HirDatabase ,
113116 trait_ : TraitId ,
114- ) -> Option < ObjectSafetyViolation > {
117+ ) -> Option < DynCompatibilityViolation > {
115118 let 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) ;
118121 ControlFlow :: 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 > (
325328 db : & dyn HirDatabase ,
326329 trait_ : TraitId ,
327330 item : AssocItemId ,
328331 cb : & 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
339342 match item {
340- AssocItemId :: ConstId ( it) => cb ( ObjectSafetyViolation :: AssocConst ( it) ) ,
343+ AssocItemId :: ConstId ( it) => cb ( DynCompatibilityViolation :: AssocConst ( it) ) ,
341344 AssocItemId :: FunctionId ( it) => {
342345 virtual_call_violations_for_method ( db, trait_, it, & mut |mvc| {
343- cb ( ObjectSafetyViolation :: Method ( it, mvc) )
346+ cb ( DynCompatibilityViolation :: Method ( it, mvc) )
344347 } )
345348 }
346349 AssocItemId :: TypeAliasId ( it) => {
@@ -350,7 +353,7 @@ where
350353 } else {
351354 let generic_params = db. generic_params ( item. into ( ) ) ;
352355 if !generic_params. is_empty ( ) {
353- cb ( ObjectSafetyViolation :: GAT ( it) )
356+ cb ( DynCompatibilityViolation :: GAT ( it) )
354357 } else {
355358 ControlFlow :: Continue ( ( ) )
356359 }
@@ -469,7 +472,7 @@ fn receiver_is_dispatchable(
469472 return 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
474477 if sig
475478 . skip_binders ( )
0 commit comments