@@ -64,8 +64,11 @@ pub enum UnconstrainedNumeric {
64
64
impl < ' tcx > fmt:: Display for TypeError < ' tcx > {
65
65
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
66
66
use self :: TypeError :: * ;
67
- fn report_maybe_different ( f : & mut fmt:: Formatter < ' _ > ,
68
- expected : & str , found : & str ) -> fmt:: Result {
67
+ fn report_maybe_different (
68
+ f : & mut fmt:: Formatter < ' _ > ,
69
+ expected : & str ,
70
+ found : & str ,
71
+ ) -> fmt:: Result {
69
72
// A naive approach to making sure that we're not reporting silly errors such as:
70
73
// (expected closure, found closure).
71
74
if expected == found {
@@ -183,39 +186,70 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
183
186
}
184
187
}
185
188
189
+ impl < ' tcx > TypeError < ' tcx > {
190
+ pub fn must_include_note ( & self ) -> bool {
191
+ use self :: TypeError :: * ;
192
+ match self {
193
+ CyclicTy ( _) |
194
+ UnsafetyMismatch ( _) |
195
+ Mismatch |
196
+ AbiMismatch ( _) |
197
+ FixedArraySize ( _) |
198
+ Sorts ( _) |
199
+ IntMismatch ( _) |
200
+ FloatMismatch ( _) |
201
+ VariadicMismatch ( _) => false ,
202
+
203
+ Mutability |
204
+ TupleSize ( _) |
205
+ ArgCount |
206
+ RegionsDoesNotOutlive ( ..) |
207
+ RegionsInsufficientlyPolymorphic ( ..) |
208
+ RegionsOverlyPolymorphic ( ..) |
209
+ RegionsPlaceholderMismatch |
210
+ Traits ( _) |
211
+ ProjectionMismatched ( _) |
212
+ ProjectionBoundsLength ( _) |
213
+ ExistentialMismatch ( _) |
214
+ ConstMismatch ( _) |
215
+ IntrinsicCast |
216
+ ObjectUnsafeCoercion ( _) => true ,
217
+ }
218
+ }
219
+ }
220
+
186
221
impl < ' tcx > ty:: TyS < ' tcx > {
187
222
pub fn sort_string ( & self , tcx : TyCtxt < ' _ > ) -> Cow < ' static , str > {
188
223
match self . kind {
189
224
ty:: Bool | ty:: Char | ty:: Int ( _) |
190
- ty:: Uint ( _) | ty:: Float ( _) | ty:: Str | ty:: Never => self . to_string ( ) . into ( ) ,
191
- ty:: Tuple ( ref tys) if tys. is_empty ( ) => self . to_string ( ) . into ( ) ,
225
+ ty:: Uint ( _) | ty:: Float ( _) | ty:: Str | ty:: Never => format ! ( "{}" , self ) . into ( ) ,
226
+ ty:: Tuple ( ref tys) if tys. is_empty ( ) => format ! ( "{}" , self ) . into ( ) ,
192
227
193
228
ty:: Adt ( def, _) => format ! ( "{} `{}`" , def. descr( ) , tcx. def_path_str( def. did) ) . into ( ) ,
194
229
ty:: Foreign ( def_id) => format ! ( "extern type `{}`" , tcx. def_path_str( def_id) ) . into ( ) ,
195
- ty:: Array ( _ , n) => {
230
+ ty:: Array ( t , n) => {
196
231
let n = tcx. lift ( & n) . unwrap ( ) ;
197
232
match n. try_eval_usize ( tcx, ty:: ParamEnv :: empty ( ) ) {
198
- Some ( n) => {
199
- format ! ( "array of {} element{}" , n, pluralize!( n) ) . into ( )
200
- }
233
+ _ if t. is_simple_ty ( ) => format ! ( "array `{}`" , self ) . into ( ) ,
234
+ Some ( n) => format ! ( "array of {} element{} " , n, pluralize!( n) ) . into ( ) ,
201
235
None => "array" . into ( ) ,
202
236
}
203
237
}
238
+ ty:: Slice ( ty) if ty. is_simple_ty ( ) => format ! ( "slice `{}`" , self ) . into ( ) ,
204
239
ty:: Slice ( _) => "slice" . into ( ) ,
205
240
ty:: RawPtr ( _) => "*-ptr" . into ( ) ,
206
- ty:: Ref ( region , ty, mutbl) => {
241
+ ty:: Ref ( _ , ty, mutbl) => {
207
242
let tymut = ty:: TypeAndMut { ty, mutbl } ;
208
243
let tymut_string = tymut. to_string ( ) ;
209
- if tymut_string == "_" || //unknown type name,
210
- tymut_string. len ( ) > 10 || //name longer than saying "reference",
211
- region. to_string ( ) != "'_" //... or a complex type
212
- {
213
- format ! ( "{}reference" , match mutbl {
214
- hir:: Mutability :: Mutable => "mutable " ,
215
- _ => ""
216
- } ) . into ( )
217
- } else {
244
+ if tymut_string != "_" && (
245
+ ty. is_simple_text ( ) || tymut_string. len ( ) < "mutable reference" . len ( )
246
+ ) {
218
247
format ! ( "&{}" , tymut_string) . into ( )
248
+ } else { // Unknown type name, it's long or has type arguments
249
+ match mutbl {
250
+ hir:: Mutability :: Mutable => "mutable reference" ,
251
+ _ => "reference" ,
252
+ } . into ( )
219
253
}
220
254
}
221
255
ty:: FnDef ( ..) => "fn item" . into ( ) ,
@@ -248,7 +282,6 @@ impl<'tcx> ty::TyS<'tcx> {
248
282
}
249
283
250
284
pub fn prefix_string ( & self ) -> Cow < ' static , str > {
251
- debug ! ( "prefix_string {:?} {} {:?}" , self , self , self . kind) ;
252
285
match self . kind {
253
286
ty:: Infer ( _) | ty:: Error | ty:: Bool | ty:: Char | ty:: Int ( _) |
254
287
ty:: Uint ( _) | ty:: Float ( _) | ty:: Str | ty:: Never => "type" . into ( ) ,
0 commit comments