@@ -62,6 +62,37 @@ fn object_safety_violations(tcx: TyCtxt<'_>, trait_def_id: DefId) -> &'_ [Object
62
62
)
63
63
}
64
64
65
+ fn is_object_safe ( tcx : TyCtxt < ' _ > , trait_def_id : DefId ) -> bool {
66
+ let violations = tcx. object_safety_violations ( trait_def_id) ;
67
+
68
+ if violations. is_empty ( ) {
69
+ return true ;
70
+ }
71
+
72
+ // If the trait contains any other violations, then let the error reporting path
73
+ // report it instead of emitting a warning here.
74
+ if violations. iter ( ) . all ( |violation| {
75
+ matches ! (
76
+ violation,
77
+ ObjectSafetyViolation :: Method ( _, MethodViolationCode :: WhereClauseReferencesSelf , _)
78
+ )
79
+ } ) {
80
+ for violation in violations {
81
+ if let ObjectSafetyViolation :: Method (
82
+ _,
83
+ MethodViolationCode :: WhereClauseReferencesSelf ,
84
+ span,
85
+ ) = violation
86
+ {
87
+ lint_object_unsafe_trait ( tcx, * span, trait_def_id, & violation) ;
88
+ }
89
+ }
90
+ return true ;
91
+ }
92
+
93
+ false
94
+ }
95
+
65
96
/// We say a method is *vtable safe* if it can be invoked on a trait
66
97
/// object. Note that object-safe traits can have some
67
98
/// non-vtable-safe methods, so long as they require `Self: Sized` or
@@ -93,19 +124,6 @@ fn object_safety_violations_for_trait(
93
124
object_safety_violation_for_method ( tcx, trait_def_id, & item)
94
125
. map ( |( code, span) | ObjectSafetyViolation :: Method ( item. name , code, span) )
95
126
} )
96
- . filter ( |violation| {
97
- if let ObjectSafetyViolation :: Method (
98
- _,
99
- MethodViolationCode :: WhereClauseReferencesSelf ,
100
- span,
101
- ) = violation
102
- {
103
- lint_object_unsafe_trait ( tcx, * span, trait_def_id, & violation) ;
104
- false
105
- } else {
106
- true
107
- }
108
- } )
109
127
. collect ( ) ;
110
128
111
129
// Check the trait itself.
@@ -866,5 +884,5 @@ pub fn contains_illegal_impl_trait_in_trait<'tcx>(
866
884
}
867
885
868
886
pub fn provide ( providers : & mut ty:: query:: Providers ) {
869
- * providers = ty:: query:: Providers { object_safety_violations, ..* providers } ;
887
+ * providers = ty:: query:: Providers { object_safety_violations, is_object_safe , ..* providers } ;
870
888
}
0 commit comments