@@ -21,7 +21,7 @@ use rustc_infer::traits::{
2121} ;
2222use rustc_middle:: ty:: print:: { PrintTraitRefExt as _, with_no_trimmed_paths} ;
2323use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
24- use rustc_span:: { ErrorGuaranteed , ExpnKind , Span , sym } ;
24+ use rustc_span:: { DesugaringKind , ErrorGuaranteed , ExpnKind , Span } ;
2525use tracing:: { info, instrument} ;
2626
2727pub use self :: overflow:: * ;
@@ -136,17 +136,6 @@ pub enum DefIdOrName {
136136 Name ( & ' static str ) ,
137137}
138138
139- #[ derive( Debug , PartialEq , Eq , PartialOrd , Ord ) ]
140- enum ErrorSortKey {
141- SubtypeFormat ( usize , usize ) ,
142- OtherKind ,
143- SizedTrait ,
144- MetaSizedTrait ,
145- PointeeSizedTrait ,
146- Coerce ,
147- ClauseWellFormed ,
148- }
149-
150139impl < ' a , ' tcx > TypeErrCtxt < ' a , ' tcx > {
151140 pub fn report_fulfillment_errors (
152141 & self ,
@@ -171,41 +160,50 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
171160 } )
172161 . collect ( ) ;
173162
174- // Ensure `T: Sized`, `T: MetaSized`, `T: PointeeSized` and `T: WF` obligations come last.
163+ // Ensure `T: Sized`, `T: MetaSized`, `T: PointeeSized` and `T: WF` obligations come last,
164+ // and `Subtype` obligations from `FormatLiteral` desugarings come first.
175165 // This lets us display diagnostics with more relevant type information and hide redundant
176166 // E0282 errors.
167+ #[ derive( Debug , PartialEq , Eq , PartialOrd , Ord ) ]
168+ enum ErrorSortKey {
169+ SubtypeFormat ( usize , usize ) ,
170+ OtherKind ,
171+ SizedTrait ,
172+ MetaSizedTrait ,
173+ PointeeSizedTrait ,
174+ Coerce ,
175+ WellFormed ,
176+ }
177177 errors. sort_by_key ( |e| {
178178 let maybe_sizedness_did = match e. obligation . predicate . kind ( ) . skip_binder ( ) {
179179 ty:: PredicateKind :: Clause ( ty:: ClauseKind :: Trait ( pred) ) => Some ( pred. def_id ( ) ) ,
180180 ty:: PredicateKind :: Clause ( ty:: ClauseKind :: HostEffect ( pred) ) => Some ( pred. def_id ( ) ) ,
181181 _ => None ,
182182 } ;
183183
184-
185- let span = e. obligation . cause . span ;
186- let outer_expn_data = span. ctxt ( ) . outer_expn_data ( ) ;
187- let source_span = outer_expn_data. call_site . source_callsite ( ) ;
188- let source_map = self . tcx . sess . source_map ( ) ;
189-
190184 match e. obligation . predicate . kind ( ) . skip_binder ( ) {
191185 ty:: PredicateKind :: Subtype ( _)
192- if let Some ( def_id) = outer_expn_data. macro_def_id
193- && let ( Some ( span_file) , row, col, ..) =
194- source_map. span_to_location_info ( span)
195- && let ( Some ( source_file) , ..) =
196- source_map. span_to_location_info ( source_span)
197- && ( self . tcx . is_diagnostic_item ( sym:: format_args_nl_macro, def_id)
198- || self . tcx . is_diagnostic_item ( sym:: format_args_macro, def_id) )
199- && span_file. src_hash == source_file. src_hash =>
186+ if matches ! (
187+ e. obligation. cause. span. desugaring_kind( ) ,
188+ Some ( DesugaringKind :: FormatLiteral { .. } )
189+ ) =>
200190 {
191+ let ( _, row, col, ..) =
192+ self . tcx . sess . source_map ( ) . span_to_location_info ( e. obligation . cause . span ) ;
201193 ErrorSortKey :: SubtypeFormat ( row, col)
202194 }
203- _ if maybe_sizedness_did == self . tcx . lang_items ( ) . sized_trait ( ) => ErrorSortKey :: SizedTrait ,
204- _ if maybe_sizedness_did == self . tcx . lang_items ( ) . meta_sized_trait ( ) => ErrorSortKey :: MetaSizedTrait ,
205- _ if maybe_sizedness_did == self . tcx . lang_items ( ) . pointee_sized_trait ( ) => ErrorSortKey :: PointeeSizedTrait ,
195+ _ if maybe_sizedness_did == self . tcx . lang_items ( ) . sized_trait ( ) => {
196+ ErrorSortKey :: SizedTrait
197+ }
198+ _ if maybe_sizedness_did == self . tcx . lang_items ( ) . meta_sized_trait ( ) => {
199+ ErrorSortKey :: MetaSizedTrait
200+ }
201+ _ if maybe_sizedness_did == self . tcx . lang_items ( ) . pointee_sized_trait ( ) => {
202+ ErrorSortKey :: PointeeSizedTrait
203+ }
206204 ty:: PredicateKind :: Coerce ( _) => ErrorSortKey :: Coerce ,
207205 ty:: PredicateKind :: Clause ( ty:: ClauseKind :: WellFormed ( _) ) => {
208- ErrorSortKey :: ClauseWellFormed
206+ ErrorSortKey :: WellFormed
209207 }
210208 _ => ErrorSortKey :: OtherKind ,
211209 }
0 commit comments