@@ -15,8 +15,12 @@ use super::{
1515 Obligation ,
1616 ObligationCauseCode ,
1717 OutputTypeParameterMismatch ,
18+ TraitNotObjectSafe ,
1819 PredicateObligation ,
1920 SelectionError ,
21+ ObjectSafetyViolation ,
22+ MethodViolationCode ,
23+ object_safety_violations,
2024} ;
2125
2226use fmt_macros:: { Parser , Piece , Position } ;
@@ -246,6 +250,55 @@ pub fn report_selection_error<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
246250 note_obligation_cause ( infcx, obligation) ;
247251 }
248252 }
253+
254+ TraitNotObjectSafe ( ref trait_ref) => {
255+ span_err ! ( infcx. tcx. sess, obligation. cause. span, E0038 ,
256+ "cannot convert to a trait object because trait `{}` is not object-safe" ,
257+ ty:: item_path_str( infcx. tcx, trait_ref. def_id( ) ) ) ;
258+
259+ for violation in object_safety_violations ( infcx. tcx , trait_ref. clone ( ) ) {
260+ match violation {
261+ ObjectSafetyViolation :: SizedSelf => {
262+ infcx. tcx . sess . span_note (
263+ obligation. cause . span ,
264+ "the trait cannot require that `Self : Sized`" ) ;
265+ }
266+
267+ ObjectSafetyViolation :: SupertraitSelf => {
268+ infcx. tcx . sess . span_note (
269+ obligation. cause . span ,
270+ "the trait cannot use `Self` as a type parameter \
271+ in the supertrait listing") ;
272+ }
273+
274+ ObjectSafetyViolation :: Method ( method,
275+ MethodViolationCode :: StaticMethod ) => {
276+ infcx. tcx . sess . span_note (
277+ obligation. cause . span ,
278+ & format ! ( "method `{}` has no receiver" ,
279+ method. name. user_string( infcx. tcx) ) ) ;
280+ }
281+
282+ ObjectSafetyViolation :: Method ( method,
283+ MethodViolationCode :: ReferencesSelf ) => {
284+ infcx. tcx . sess . span_note (
285+ obligation. cause . span ,
286+ & format ! ( "method `{}` references the `Self` type \
287+ in its arguments or return type",
288+ method. name. user_string( infcx. tcx) ) ) ;
289+ }
290+
291+ ObjectSafetyViolation :: Method ( method,
292+ MethodViolationCode :: Generic ) => {
293+ infcx. tcx . sess . span_note (
294+ obligation. cause . span ,
295+ & format ! ( "method `{}` has generic type parameters" ,
296+ method. name. user_string( infcx. tcx) ) ) ;
297+ }
298+ }
299+ }
300+
301+ }
249302 }
250303}
251304
@@ -397,10 +450,6 @@ fn note_obligation_cause_code<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>,
397450 "only the last field of a struct or enum variant \
398451 may have a dynamically sized type")
399452 }
400- ObligationCauseCode :: ObjectSized => {
401- span_note ! ( tcx. sess, cause_span,
402- "only sized types can be made into objects" ) ;
403- }
404453 ObligationCauseCode :: SharedStatic => {
405454 span_note ! ( tcx. sess, cause_span,
406455 "shared static variables must have a type that implements `Sync`" ) ;
0 commit comments