@@ -21,7 +21,7 @@ use rustc_infer::traits::{
21
21
} ;
22
22
use rustc_middle:: ty:: print:: { PrintTraitRefExt as _, with_no_trimmed_paths} ;
23
23
use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
24
- use rustc_span:: { ErrorGuaranteed , ExpnKind , Span , sym } ;
24
+ use rustc_span:: { DesugaringKind , ErrorGuaranteed , ExpnKind , Span } ;
25
25
use tracing:: { info, instrument} ;
26
26
27
27
pub use self :: overflow:: * ;
@@ -136,17 +136,6 @@ pub enum DefIdOrName {
136
136
Name ( & ' static str ) ,
137
137
}
138
138
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
-
150
139
impl < ' a , ' tcx > TypeErrCtxt < ' a , ' tcx > {
151
140
pub fn report_fulfillment_errors (
152
141
& self ,
@@ -171,41 +160,50 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
171
160
} )
172
161
. collect ( ) ;
173
162
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.
175
165
// This lets us display diagnostics with more relevant type information and hide redundant
176
166
// 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
+ }
177
177
errors. sort_by_key ( |e| {
178
178
let maybe_sizedness_did = match e. obligation . predicate . kind ( ) . skip_binder ( ) {
179
179
ty:: PredicateKind :: Clause ( ty:: ClauseKind :: Trait ( pred) ) => Some ( pred. def_id ( ) ) ,
180
180
ty:: PredicateKind :: Clause ( ty:: ClauseKind :: HostEffect ( pred) ) => Some ( pred. def_id ( ) ) ,
181
181
_ => None ,
182
182
} ;
183
183
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
-
190
184
match e. obligation . predicate . kind ( ) . skip_binder ( ) {
191
185
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
+ ) =>
200
190
{
191
+ let ( _, row, col, ..) =
192
+ self . tcx . sess . source_map ( ) . span_to_location_info ( e. obligation . cause . span ) ;
201
193
ErrorSortKey :: SubtypeFormat ( row, col)
202
194
}
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
+ }
206
204
ty:: PredicateKind :: Coerce ( _) => ErrorSortKey :: Coerce ,
207
205
ty:: PredicateKind :: Clause ( ty:: ClauseKind :: WellFormed ( _) ) => {
208
- ErrorSortKey :: ClauseWellFormed
206
+ ErrorSortKey :: WellFormed
209
207
}
210
208
_ => ErrorSortKey :: OtherKind ,
211
209
}
0 commit comments