@@ -64,8 +64,11 @@ pub enum UnconstrainedNumeric {
6464impl < ' tcx > fmt:: Display for TypeError < ' tcx > {
6565 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
6666 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 {
6972 // A naive approach to making sure that we're not reporting silly errors such as:
7073 // (expected closure, found closure).
7174 if expected == found {
@@ -183,39 +186,70 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
183186 }
184187}
185188
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+
186221impl < ' tcx > ty:: TyS < ' tcx > {
187222 pub fn sort_string ( & self , tcx : TyCtxt < ' _ > ) -> Cow < ' static , str > {
188223 match self . kind {
189224 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 ( ) ,
192227
193228 ty:: Adt ( def, _) => format ! ( "{} `{}`" , def. descr( ) , tcx. def_path_str( def. did) ) . into ( ) ,
194229 ty:: Foreign ( def_id) => format ! ( "extern type `{}`" , tcx. def_path_str( def_id) ) . into ( ) ,
195- ty:: Array ( _ , n) => {
230+ ty:: Array ( t , n) => {
196231 let n = tcx. lift ( & n) . unwrap ( ) ;
197232 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 ( ) ,
201235 None => "array" . into ( ) ,
202236 }
203237 }
238+ ty:: Slice ( ty) if ty. is_simple_ty ( ) => format ! ( "slice `{}`" , self ) . into ( ) ,
204239 ty:: Slice ( _) => "slice" . into ( ) ,
205240 ty:: RawPtr ( _) => "*-ptr" . into ( ) ,
206- ty:: Ref ( region , ty, mutbl) => {
241+ ty:: Ref ( _ , ty, mutbl) => {
207242 let tymut = ty:: TypeAndMut { ty, mutbl } ;
208243 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+ ) {
218247 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 ( )
219253 }
220254 }
221255 ty:: FnDef ( ..) => "fn item" . into ( ) ,
@@ -248,7 +282,6 @@ impl<'tcx> ty::TyS<'tcx> {
248282 }
249283
250284 pub fn prefix_string ( & self ) -> Cow < ' static , str > {
251- debug ! ( "prefix_string {:?} {} {:?}" , self , self , self . kind) ;
252285 match self . kind {
253286 ty:: Infer ( _) | ty:: Error | ty:: Bool | ty:: Char | ty:: Int ( _) |
254287 ty:: Uint ( _) | ty:: Float ( _) | ty:: Str | ty:: Never => "type" . into ( ) ,
0 commit comments