@@ -62,6 +62,37 @@ fn object_safety_violations(tcx: TyCtxt<'_>, trait_def_id: DefId) -> &'_ [Object
6262 )
6363}
6464
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+
6596/// We say a method is *vtable safe* if it can be invoked on a trait
6697/// object. Note that object-safe traits can have some
6798/// non-vtable-safe methods, so long as they require `Self: Sized` or
@@ -93,19 +124,6 @@ fn object_safety_violations_for_trait(
93124 object_safety_violation_for_method ( tcx, trait_def_id, & item)
94125 . map ( |( code, span) | ObjectSafetyViolation :: Method ( item. name , code, span) )
95126 } )
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- } )
109127 . collect ( ) ;
110128
111129 // Check the trait itself.
@@ -866,5 +884,5 @@ pub fn contains_illegal_impl_trait_in_trait<'tcx>(
866884}
867885
868886pub 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 } ;
870888}
0 commit comments