44use crate :: errors;
55use rustc_errors:: ErrorGuaranteed ;
66use rustc_hir as hir;
7- use rustc_middle:: ty:: { self , AliasKind , Ty , TyCtxt , TypeVisitableExt } ;
7+ use rustc_middle:: ty:: { self , AliasKind , TyCtxt , TypeVisitableExt } ;
88use rustc_span:: def_id:: LocalDefId ;
99use rustc_span:: Span ;
1010use rustc_trait_selection:: traits:: { self , IsFirstInputType } ;
@@ -283,9 +283,14 @@ fn emit_orphan_check_error<'tcx>(
283283 let self_ty = trait_ref. self_ty ( ) ;
284284 Err ( match err {
285285 traits:: OrphanCheckErr :: NonLocalInputType ( tys) => {
286- let ( mut opaque, mut foreign, mut name, mut pointer, mut ty_diag) =
287- ( Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
288- let mut sugg = None ;
286+ let mut diag = tcx. dcx ( ) . create_err ( match self_ty. kind ( ) {
287+ ty:: Adt ( ..) => errors:: OnlyCurrentTraits :: Outside { span : sp, note : ( ) } ,
288+ _ if self_ty. is_primitive ( ) => {
289+ errors:: OnlyCurrentTraits :: Primitive { span : sp, note : ( ) }
290+ }
291+ _ => errors:: OnlyCurrentTraits :: Arbitrary { span : sp, note : ( ) } ,
292+ } ) ;
293+
289294 for & ( mut ty, is_target_ty) in & tys {
290295 let span = if matches ! ( is_target_ty, IsFirstInputType :: Yes ) {
291296 // Point at `D<A>` in `impl<A, B> for C<B> in D<A>`
@@ -296,113 +301,86 @@ fn emit_orphan_check_error<'tcx>(
296301 } ;
297302
298303 ty = tcx. erase_regions ( ty) ;
299- ty = match ty. kind ( ) {
300- // Remove the type arguments from the output, as they are not relevant.
301- // You can think of this as the reverse of `resolve_vars_if_possible`.
302- // That way if we had `Vec<MyType>`, we will properly attribute the
303- // problem to `Vec<T>` and avoid confusing the user if they were to see
304- // `MyType` in the error.
305- ty:: Adt ( def, _) => Ty :: new_adt ( tcx, * def, ty:: List :: empty ( ) ) ,
306- _ => ty,
307- } ;
308-
309- fn push_to_foreign_or_name < ' tcx > (
310- is_foreign : bool ,
311- foreign : & mut Vec < errors:: OnlyCurrentTraitsForeign > ,
312- name : & mut Vec < errors:: OnlyCurrentTraitsName < ' tcx > > ,
313- span : Span ,
314- sname : & ' tcx str ,
315- ) {
316- if is_foreign {
317- foreign. push ( errors:: OnlyCurrentTraitsForeign { span } )
318- } else {
319- name. push ( errors:: OnlyCurrentTraitsName { span, name : sname } ) ;
320- }
321- }
322304
323305 let is_foreign =
324306 !trait_ref. def_id . is_local ( ) && matches ! ( is_target_ty, IsFirstInputType :: No ) ;
325307
326308 match * ty. kind ( ) {
327309 ty:: Slice ( _) => {
328- push_to_foreign_or_name (
329- is_foreign,
330- & mut foreign,
331- & mut name,
332- span,
333- "slices" ,
334- ) ;
310+ if is_foreign {
311+ diag. subdiagnostic (
312+ tcx. dcx ( ) ,
313+ errors:: OnlyCurrentTraitsForeign { span } ,
314+ ) ;
315+ } else {
316+ diag. subdiagnostic (
317+ tcx. dcx ( ) ,
318+ errors:: OnlyCurrentTraitsName { span, name : "slices" } ,
319+ ) ;
320+ }
335321 }
336322 ty:: Array ( ..) => {
337- push_to_foreign_or_name (
338- is_foreign,
339- & mut foreign,
340- & mut name,
341- span,
342- "arrays" ,
343- ) ;
323+ if is_foreign {
324+ diag. subdiagnostic (
325+ tcx. dcx ( ) ,
326+ errors:: OnlyCurrentTraitsForeign { span } ,
327+ ) ;
328+ } else {
329+ diag. subdiagnostic (
330+ tcx. dcx ( ) ,
331+ errors:: OnlyCurrentTraitsName { span, name : "arrays" } ,
332+ ) ;
333+ }
344334 }
345335 ty:: Tuple ( ..) => {
346- push_to_foreign_or_name (
347- is_foreign,
348- & mut foreign,
349- & mut name,
350- span,
351- "tuples" ,
352- ) ;
336+ if is_foreign {
337+ diag. subdiagnostic (
338+ tcx. dcx ( ) ,
339+ errors:: OnlyCurrentTraitsForeign { span } ,
340+ ) ;
341+ } else {
342+ diag. subdiagnostic (
343+ tcx. dcx ( ) ,
344+ errors:: OnlyCurrentTraitsName { span, name : "tuples" } ,
345+ ) ;
346+ }
353347 }
354348 ty:: Alias ( ty:: Opaque , ..) => {
355- opaque . push ( errors:: OnlyCurrentTraitsOpaque { span } )
349+ diag . subdiagnostic ( tcx . dcx ( ) , errors:: OnlyCurrentTraitsOpaque { span } ) ;
356350 }
357351 ty:: RawPtr ( ptr_ty, mutbl) => {
358352 if !self_ty. has_param ( ) {
359- let mut_key = mutbl. prefix_str ( ) ;
360- sugg = Some ( errors:: OnlyCurrentTraitsPointerSugg {
361- wrapper_span : self_ty_span,
362- struct_span : full_impl_span. shrink_to_lo ( ) ,
363- mut_key,
364- ptr_ty,
365- } ) ;
353+ diag. subdiagnostic (
354+ tcx. dcx ( ) ,
355+ errors:: OnlyCurrentTraitsPointerSugg {
356+ wrapper_span : self_ty_span,
357+ struct_span : full_impl_span. shrink_to_lo ( ) ,
358+ mut_key : mutbl. prefix_str ( ) ,
359+ ptr_ty,
360+ } ,
361+ ) ;
366362 }
367- pointer. push ( errors:: OnlyCurrentTraitsPointer { span, pointer : ty } ) ;
363+ diag. subdiagnostic (
364+ tcx. dcx ( ) ,
365+ errors:: OnlyCurrentTraitsPointer { span, pointer : ty } ,
366+ ) ;
367+ }
368+ ty:: Adt ( adt_def, _) => {
369+ diag. subdiagnostic (
370+ tcx. dcx ( ) ,
371+ errors:: OnlyCurrentTraitsAdt {
372+ span,
373+ name : tcx. def_path_str ( adt_def. did ( ) ) ,
374+ } ,
375+ ) ;
376+ }
377+ _ => {
378+ diag. subdiagnostic ( tcx. dcx ( ) , errors:: OnlyCurrentTraitsTy { span, ty } ) ;
368379 }
369- _ => ty_diag. push ( errors:: OnlyCurrentTraitsTy { span, ty } ) ,
370380 }
371381 }
372382
373- let err_struct = match self_ty. kind ( ) {
374- ty:: Adt ( ..) => errors:: OnlyCurrentTraits :: Outside {
375- span : sp,
376- note : ( ) ,
377- opaque,
378- foreign,
379- name,
380- pointer,
381- ty : ty_diag,
382- sugg,
383- } ,
384- _ if self_ty. is_primitive ( ) => errors:: OnlyCurrentTraits :: Primitive {
385- span : sp,
386- note : ( ) ,
387- opaque,
388- foreign,
389- name,
390- pointer,
391- ty : ty_diag,
392- sugg,
393- } ,
394- _ => errors:: OnlyCurrentTraits :: Arbitrary {
395- span : sp,
396- note : ( ) ,
397- opaque,
398- foreign,
399- name,
400- pointer,
401- ty : ty_diag,
402- sugg,
403- } ,
404- } ;
405- tcx. dcx ( ) . emit_err ( err_struct)
383+ diag. emit ( )
406384 }
407385 traits:: OrphanCheckErr :: UncoveredTy ( param_ty, local_type) => {
408386 let mut sp = sp;
0 commit comments