1
- //! Compute the object-safety of a trait
1
+ //! Compute the dyn-compatibility of a trait
2
2
3
3
use std:: ops:: ControlFlow ;
4
4
@@ -28,14 +28,14 @@ use crate::{
28
28
} ;
29
29
30
30
#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
31
- pub enum ObjectSafetyViolation {
31
+ pub enum DynCompatibilityViolation {
32
32
SizedSelf ,
33
33
SelfReferential ,
34
34
Method ( FunctionId , MethodViolationCode ) ,
35
35
AssocConst ( ConstId ) ,
36
36
GAT ( TypeAliasId ) ,
37
37
// This doesn't exist in rustc, but added for better visualization
38
- HasNonSafeSuperTrait ( TraitId ) ,
38
+ HasNonCompatibleSuperTrait ( TraitId ) ,
39
39
}
40
40
41
41
#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
@@ -50,70 +50,73 @@ pub enum MethodViolationCode {
50
50
UndispatchableReceiver ,
51
51
}
52
52
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 > {
54
57
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) ) ;
57
60
}
58
61
}
59
62
60
- db. object_safety_of_trait ( trait_)
63
+ db. dyn_compatibility_of_trait ( trait_)
61
64
}
62
65
63
- pub fn object_safety_with_callback < F > (
66
+ pub fn dyn_compatibility_with_callback < F > (
64
67
db : & dyn HirDatabase ,
65
68
trait_ : TraitId ,
66
69
cb : & mut F ,
67
70
) -> ControlFlow < ( ) >
68
71
where
69
- F : FnMut ( ObjectSafetyViolation ) -> ControlFlow < ( ) > ,
72
+ F : FnMut ( DynCompatibilityViolation ) -> ControlFlow < ( ) > ,
70
73
{
71
74
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_) ) ?;
74
77
}
75
78
}
76
79
77
- object_safety_of_trait_with_callback ( db, trait_, cb)
80
+ dyn_compatibility_of_trait_with_callback ( db, trait_, cb)
78
81
}
79
82
80
- pub fn object_safety_of_trait_with_callback < F > (
83
+ pub fn dyn_compatibility_of_trait_with_callback < F > (
81
84
db : & dyn HirDatabase ,
82
85
trait_ : TraitId ,
83
86
cb : & mut F ,
84
87
) -> ControlFlow < ( ) >
85
88
where
86
- F : FnMut ( ObjectSafetyViolation ) -> ControlFlow < ( ) > ,
89
+ F : FnMut ( DynCompatibilityViolation ) -> ControlFlow < ( ) > ,
87
90
{
88
91
// Check whether this has a `Sized` bound
89
92
if generics_require_sized_self ( db, trait_. into ( ) ) {
90
- cb ( ObjectSafetyViolation :: SizedSelf ) ?;
93
+ cb ( DynCompatibilityViolation :: SizedSelf ) ?;
91
94
}
92
95
93
96
// Check if there exist bounds that referencing self
94
97
if predicates_reference_self ( db, trait_) {
95
- cb ( ObjectSafetyViolation :: SelfReferential ) ?;
98
+ cb ( DynCompatibilityViolation :: SelfReferential ) ?;
96
99
}
97
100
if bounds_reference_self ( db, trait_) {
98
- cb ( ObjectSafetyViolation :: SelfReferential ) ?;
101
+ cb ( DynCompatibilityViolation :: SelfReferential ) ?;
99
102
}
100
103
101
104
// rustc checks for non-lifetime binders here, but we don't support HRTB yet
102
105
103
106
let trait_data = db. trait_data ( trait_) ;
104
107
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) ?;
106
109
}
107
110
108
111
ControlFlow :: Continue ( ( ) )
109
112
}
110
113
111
- pub fn object_safety_of_trait_query (
114
+ pub fn dyn_compatibility_of_trait_query (
112
115
db : & dyn HirDatabase ,
113
116
trait_ : TraitId ,
114
- ) -> Option < ObjectSafetyViolation > {
117
+ ) -> Option < DynCompatibilityViolation > {
115
118
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| {
117
120
res = Some ( osv) ;
118
121
ControlFlow :: Break ( ( ) )
119
122
} ) ;
@@ -321,14 +324,14 @@ fn contains_illegal_self_type_reference<T: TypeVisitable<Interner>>(
321
324
t. visit_with ( visitor. as_dyn ( ) , outer_binder) . is_break ( )
322
325
}
323
326
324
- fn object_safety_violation_for_assoc_item < F > (
327
+ fn dyn_compatibility_violation_for_assoc_item < F > (
325
328
db : & dyn HirDatabase ,
326
329
trait_ : TraitId ,
327
330
item : AssocItemId ,
328
331
cb : & mut F ,
329
332
) -> ControlFlow < ( ) >
330
333
where
331
- F : FnMut ( ObjectSafetyViolation ) -> ControlFlow < ( ) > ,
334
+ F : FnMut ( DynCompatibilityViolation ) -> ControlFlow < ( ) > ,
332
335
{
333
336
// Any item that has a `Self : Sized` requisite is otherwise
334
337
// exempt from the regulations.
@@ -337,10 +340,10 @@ where
337
340
}
338
341
339
342
match item {
340
- AssocItemId :: ConstId ( it) => cb ( ObjectSafetyViolation :: AssocConst ( it) ) ,
343
+ AssocItemId :: ConstId ( it) => cb ( DynCompatibilityViolation :: AssocConst ( it) ) ,
341
344
AssocItemId :: FunctionId ( it) => {
342
345
virtual_call_violations_for_method ( db, trait_, it, & mut |mvc| {
343
- cb ( ObjectSafetyViolation :: Method ( it, mvc) )
346
+ cb ( DynCompatibilityViolation :: Method ( it, mvc) )
344
347
} )
345
348
}
346
349
AssocItemId :: TypeAliasId ( it) => {
@@ -350,7 +353,7 @@ where
350
353
} else {
351
354
let generic_params = db. generic_params ( item. into ( ) ) ;
352
355
if !generic_params. is_empty ( ) {
353
- cb ( ObjectSafetyViolation :: GAT ( it) )
356
+ cb ( DynCompatibilityViolation :: GAT ( it) )
354
357
} else {
355
358
ControlFlow :: Continue ( ( ) )
356
359
}
@@ -469,7 +472,7 @@ fn receiver_is_dispatchable(
469
472
return false ;
470
473
} ;
471
474
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
473
476
// See rustc's comment on https://github.com/rust-lang/rust/blob/3f121b9461cce02a703a0e7e450568849dfaa074/compiler/rustc_trait_selection/src/traits/object_safety.rs#L433-L437
474
477
if sig
475
478
. skip_binders ( )
0 commit comments