1010
1111use hir:: def_id:: DefId ;
1212use ty:: { self , BoundRegion , Region , Ty , TyCtxt } ;
13+ use std:: borrow:: Cow ;
1314use std:: fmt;
1415use rustc_target:: spec:: abi;
1516use syntax:: ast;
@@ -71,7 +72,7 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
7172 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
7273 use self :: TypeError :: * ;
7374 fn report_maybe_different ( f : & mut fmt:: Formatter < ' _ > ,
74- expected : String , found : String ) -> fmt:: Result {
75+ expected : & str , found : & str ) -> fmt:: Result {
7576 // A naive approach to making sure that we're not reporting silly errors such as:
7677 // (expected closure, found closure).
7778 if expected == found {
@@ -126,15 +127,15 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
126127 br)
127128 }
128129 Sorts ( values) => ty:: tls:: with ( |tcx| {
129- report_maybe_different ( f, values. expected . sort_string ( tcx) ,
130- values. found . sort_string ( tcx) )
130+ report_maybe_different ( f, & values. expected . sort_string ( tcx) ,
131+ & values. found . sort_string ( tcx) )
131132 } ) ,
132133 Traits ( values) => ty:: tls:: with ( |tcx| {
133134 report_maybe_different ( f,
134- format ! ( "trait `{}`" ,
135- tcx. item_path_str( values. expected) ) ,
136- format ! ( "trait `{}`" ,
137- tcx. item_path_str( values. found) ) )
135+ & format ! ( "trait `{}`" ,
136+ tcx. item_path_str( values. expected) ) ,
137+ & format ! ( "trait `{}`" ,
138+ tcx. item_path_str( values. found) ) )
138139 } ) ,
139140 IntMismatch ( ref values) => {
140141 write ! ( f, "expected `{:?}`, found `{:?}`" ,
@@ -162,8 +163,8 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
162163 values. found)
163164 } ,
164165 ExistentialMismatch ( ref values) => {
165- report_maybe_different ( f, format ! ( "trait `{}`" , values. expected) ,
166- format ! ( "trait `{}`" , values. found) )
166+ report_maybe_different ( f, & format ! ( "trait `{}`" , values. expected) ,
167+ & format ! ( "trait `{}`" , values. found) )
167168 }
168169 OldStyleLUB ( ref err) => {
169170 write ! ( f, "{}" , err)
@@ -173,22 +174,22 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
173174}
174175
175176impl < ' a , ' gcx , ' lcx , ' tcx > ty:: TyS < ' tcx > {
176- pub fn sort_string ( & self , tcx : TyCtxt < ' a , ' gcx , ' lcx > ) -> String {
177+ pub fn sort_string ( & self , tcx : TyCtxt < ' a , ' gcx , ' lcx > ) -> Cow < ' static , str > {
177178 match self . sty {
178179 ty:: Bool | ty:: Char | ty:: Int ( _) |
179- ty:: Uint ( _) | ty:: Float ( _) | ty:: Str | ty:: Never => self . to_string ( ) ,
180- ty:: Tuple ( ref tys) if tys. is_empty ( ) => self . to_string ( ) ,
180+ ty:: Uint ( _) | ty:: Float ( _) | ty:: Str | ty:: Never => self . to_string ( ) . into ( ) ,
181+ ty:: Tuple ( ref tys) if tys. is_empty ( ) => self . to_string ( ) . into ( ) ,
181182
182- ty:: Adt ( def, _) => format ! ( "{} `{}`" , def. descr( ) , tcx. item_path_str( def. did) ) ,
183- ty:: Foreign ( def_id) => format ! ( "extern type `{}`" , tcx. item_path_str( def_id) ) ,
183+ ty:: Adt ( def, _) => format ! ( "{} `{}`" , def. descr( ) , tcx. item_path_str( def. did) ) . into ( ) ,
184+ ty:: Foreign ( def_id) => format ! ( "extern type `{}`" , tcx. item_path_str( def_id) ) . into ( ) ,
184185 ty:: Array ( _, n) => {
185186 match n. assert_usize ( tcx) {
186- Some ( n) => format ! ( "array of {} elements" , n) ,
187- None => "array" . to_string ( ) ,
187+ Some ( n) => format ! ( "array of {} elements" , n) . into ( ) ,
188+ None => "array" . into ( ) ,
188189 }
189190 }
190- ty:: Slice ( _) => "slice" . to_string ( ) ,
191- ty:: RawPtr ( _) => "*-ptr" . to_string ( ) ,
191+ ty:: Slice ( _) => "slice" . into ( ) ,
192+ ty:: RawPtr ( _) => "*-ptr" . into ( ) ,
192193 ty:: Ref ( region, ty, mutbl) => {
193194 let tymut = ty:: TypeAndMut { ty, mutbl } ;
194195 let tymut_string = tymut. to_string ( ) ;
@@ -199,39 +200,39 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
199200 format ! ( "{}reference" , match mutbl {
200201 hir:: Mutability :: MutMutable => "mutable " ,
201202 _ => ""
202- } )
203+ } ) . into ( )
203204 } else {
204- format ! ( "&{}" , tymut_string)
205+ format ! ( "&{}" , tymut_string) . into ( )
205206 }
206207 }
207- ty:: FnDef ( ..) => "fn item" . to_string ( ) ,
208- ty:: FnPtr ( _) => "fn pointer" . to_string ( ) ,
208+ ty:: FnDef ( ..) => "fn item" . into ( ) ,
209+ ty:: FnPtr ( _) => "fn pointer" . into ( ) ,
209210 ty:: Dynamic ( ref inner, ..) => {
210- inner. principal ( ) . map_or_else ( || "trait" . to_string ( ) ,
211- |p| format ! ( "trait {}" , tcx. item_path_str( p. def_id( ) ) ) )
211+ inner. principal ( ) . map_or_else ( || "trait" . into ( ) ,
212+ |p| format ! ( "trait {}" , tcx. item_path_str( p. def_id( ) ) ) . into ( ) )
212213 }
213- ty:: Closure ( ..) => "closure" . to_string ( ) ,
214- ty:: Generator ( ..) => "generator" . to_string ( ) ,
215- ty:: GeneratorWitness ( ..) => "generator witness" . to_string ( ) ,
216- ty:: Tuple ( ..) => "tuple" . to_string ( ) ,
217- ty:: Infer ( ty:: TyVar ( _) ) => "inferred type" . to_string ( ) ,
218- ty:: Infer ( ty:: IntVar ( _) ) => "integral variable" . to_string ( ) ,
219- ty:: Infer ( ty:: FloatVar ( _) ) => "floating-point variable" . to_string ( ) ,
214+ ty:: Closure ( ..) => "closure" . into ( ) ,
215+ ty:: Generator ( ..) => "generator" . into ( ) ,
216+ ty:: GeneratorWitness ( ..) => "generator witness" . into ( ) ,
217+ ty:: Tuple ( ..) => "tuple" . into ( ) ,
218+ ty:: Infer ( ty:: TyVar ( _) ) => "inferred type" . into ( ) ,
219+ ty:: Infer ( ty:: IntVar ( _) ) => "integral variable" . into ( ) ,
220+ ty:: Infer ( ty:: FloatVar ( _) ) => "floating-point variable" . into ( ) ,
220221 ty:: Infer ( ty:: CanonicalTy ( _) ) |
221- ty:: Infer ( ty:: FreshTy ( _) ) => "fresh type" . to_string ( ) ,
222- ty:: Infer ( ty:: FreshIntTy ( _) ) => "fresh integral type" . to_string ( ) ,
223- ty:: Infer ( ty:: FreshFloatTy ( _) ) => "fresh floating-point type" . to_string ( ) ,
224- ty:: Projection ( _) => "associated type" . to_string ( ) ,
225- ty:: UnnormalizedProjection ( _) => "non-normalized associated type" . to_string ( ) ,
222+ ty:: Infer ( ty:: FreshTy ( _) ) => "fresh type" . into ( ) ,
223+ ty:: Infer ( ty:: FreshIntTy ( _) ) => "fresh integral type" . into ( ) ,
224+ ty:: Infer ( ty:: FreshFloatTy ( _) ) => "fresh floating-point type" . into ( ) ,
225+ ty:: Projection ( _) => "associated type" . into ( ) ,
226+ ty:: UnnormalizedProjection ( _) => "non-normalized associated type" . into ( ) ,
226227 ty:: Param ( ref p) => {
227228 if p. is_self ( ) {
228- "Self" . to_string ( )
229+ "Self" . into ( )
229230 } else {
230- "type parameter" . to_string ( )
231+ "type parameter" . into ( )
231232 }
232233 }
233- ty:: Opaque ( ..) => "opaque type" . to_string ( ) ,
234- ty:: Error => "type error" . to_string ( ) ,
234+ ty:: Opaque ( ..) => "opaque type" . into ( ) ,
235+ ty:: Error => "type error" . into ( ) ,
235236 }
236237 }
237238}
@@ -251,20 +252,19 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
251252 db. note ( "no two closures, even if identical, have the same type" ) ;
252253 db. help ( "consider boxing your closure and/or using it as a trait object" ) ;
253254 }
254- match ( & values . found . sty , & values . expected . sty ) { // Issue #53280
255- ( ty :: Infer ( ty :: IntVar ( _ ) ) , ty :: Float ( _ ) ) => {
256- if let Ok ( snippet ) = self . sess . source_map ( ) . span_to_snippet ( sp ) {
257- if snippet . chars ( ) . all ( |c| c . is_digit ( 10 ) || c == '-' || c == '_' ) {
258- db . span_suggestion_with_applicability (
259- sp ,
260- "use a float literal" ,
261- format ! ( "{}.0" , snippet ) ,
262- Applicability :: MachineApplicable
263- ) ;
264- }
255+ if let ( ty :: Infer ( ty :: IntVar ( _ ) ) , ty :: Float ( _ ) ) =
256+ ( & values . found . sty , & values . expected . sty ) // Issue #53280
257+ {
258+ if let Ok ( snippet ) = self . sess . source_map ( ) . span_to_snippet ( sp ) {
259+ if snippet . chars ( ) . all ( |c| c . is_digit ( 10 ) || c == '-' || c == '_' ) {
260+ db . span_suggestion_with_applicability (
261+ sp ,
262+ "use a float literal" ,
263+ format ! ( "{}.0" , snippet ) ,
264+ Applicability :: MachineApplicable
265+ ) ;
265266 }
266- } ,
267- _ => { }
267+ }
268268 }
269269 } ,
270270 OldStyleLUB ( err) => {
0 commit comments