10
10
11
11
use hir:: def_id:: DefId ;
12
12
use ty:: { self , BoundRegion , Region , Ty , TyCtxt } ;
13
+ use std:: borrow:: Cow ;
13
14
use std:: fmt;
14
15
use rustc_target:: spec:: abi;
15
16
use syntax:: ast;
@@ -71,7 +72,7 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
71
72
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
72
73
use self :: TypeError :: * ;
73
74
fn report_maybe_different ( f : & mut fmt:: Formatter < ' _ > ,
74
- expected : String , found : String ) -> fmt:: Result {
75
+ expected : & str , found : & str ) -> fmt:: Result {
75
76
// A naive approach to making sure that we're not reporting silly errors such as:
76
77
// (expected closure, found closure).
77
78
if expected == found {
@@ -126,15 +127,15 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
126
127
br)
127
128
}
128
129
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) )
131
132
} ) ,
132
133
Traits ( values) => ty:: tls:: with ( |tcx| {
133
134
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) ) )
138
139
} ) ,
139
140
IntMismatch ( ref values) => {
140
141
write ! ( f, "expected `{:?}`, found `{:?}`" ,
@@ -162,8 +163,8 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
162
163
values. found)
163
164
} ,
164
165
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) )
167
168
}
168
169
OldStyleLUB ( ref err) => {
169
170
write ! ( f, "{}" , err)
@@ -173,22 +174,22 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
173
174
}
174
175
175
176
impl < ' 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 > {
177
178
match self . sty {
178
179
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 ( ) ,
181
182
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 ( ) ,
184
185
ty:: Array ( _, n) => {
185
186
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 ( ) ,
188
189
}
189
190
}
190
- ty:: Slice ( _) => "slice" . to_string ( ) ,
191
- ty:: RawPtr ( _) => "*-ptr" . to_string ( ) ,
191
+ ty:: Slice ( _) => "slice" . into ( ) ,
192
+ ty:: RawPtr ( _) => "*-ptr" . into ( ) ,
192
193
ty:: Ref ( region, ty, mutbl) => {
193
194
let tymut = ty:: TypeAndMut { ty, mutbl } ;
194
195
let tymut_string = tymut. to_string ( ) ;
@@ -199,39 +200,39 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
199
200
format ! ( "{}reference" , match mutbl {
200
201
hir:: Mutability :: MutMutable => "mutable " ,
201
202
_ => ""
202
- } )
203
+ } ) . into ( )
203
204
} else {
204
- format ! ( "&{}" , tymut_string)
205
+ format ! ( "&{}" , tymut_string) . into ( )
205
206
}
206
207
}
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 ( ) ,
209
210
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 ( ) )
212
213
}
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 ( ) ,
220
221
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 ( ) ,
226
227
ty:: Param ( ref p) => {
227
228
if p. is_self ( ) {
228
- "Self" . to_string ( )
229
+ "Self" . into ( )
229
230
} else {
230
- "type parameter" . to_string ( )
231
+ "type parameter" . into ( )
231
232
}
232
233
}
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 ( ) ,
235
236
}
236
237
}
237
238
}
@@ -251,20 +252,19 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
251
252
db. note ( "no two closures, even if identical, have the same type" ) ;
252
253
db. help ( "consider boxing your closure and/or using it as a trait object" ) ;
253
254
}
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
+ ) ;
265
266
}
266
- } ,
267
- _ => { }
267
+ }
268
268
}
269
269
} ,
270
270
OldStyleLUB ( err) => {
0 commit comments