@@ -86,7 +86,7 @@ impl TypeError<'_> {
86
86
/// afterwards to present additional details, particularly when it comes to lifetime-related
87
87
/// errors.
88
88
impl < ' tcx > TypeError < ' tcx > {
89
- pub fn to_string ( self , tcx : TyCtxt < ' tcx > ) -> String {
89
+ pub fn to_string ( self , tcx : TyCtxt < ' tcx > ) -> Cow < ' static , str > {
90
90
use self :: TypeError :: * ;
91
91
fn report_maybe_different ( expected : & str , found : & str ) -> String {
92
92
// A naive approach to making sure that we're not reporting silly errors such as:
@@ -104,56 +104,60 @@ impl<'tcx> TypeError<'tcx> {
104
104
} ;
105
105
106
106
match self {
107
- CyclicTy ( _) => format ! ( "cyclic type of infinite size" ) ,
108
- CyclicConst ( _) => format ! ( "encountered a self-referencing constant" ) ,
109
- Mismatch => format ! ( "types differ" ) ,
107
+ CyclicTy ( _) => "cyclic type of infinite size" . into ( ) ,
108
+ CyclicConst ( _) => "encountered a self-referencing constant" . into ( ) ,
109
+ Mismatch => "types differ" . into ( ) ,
110
110
ConstnessMismatch ( values) => {
111
- format ! ( "expected {} bound, found {} bound" , values. expected, values. found)
111
+ format ! ( "expected {} bound, found {} bound" , values. expected, values. found) . into ( )
112
112
}
113
113
PolarityMismatch ( values) => {
114
114
format ! ( "expected {} polarity, found {} polarity" , values. expected, values. found)
115
+ . into ( )
115
116
}
116
117
UnsafetyMismatch ( values) => {
117
- format ! ( "expected {} fn, found {} fn" , values. expected, values. found)
118
+ format ! ( "expected {} fn, found {} fn" , values. expected, values. found) . into ( )
118
119
}
119
120
AbiMismatch ( values) => {
120
- format ! ( "expected {} fn, found {} fn" , values. expected, values. found)
121
+ format ! ( "expected {} fn, found {} fn" , values. expected, values. found) . into ( )
121
122
}
122
- ArgumentMutability ( _) | Mutability => format ! ( "types differ in mutability" ) ,
123
+ ArgumentMutability ( _) | Mutability => "types differ in mutability" . into ( ) ,
123
124
TupleSize ( values) => format ! (
124
125
"expected a tuple with {} element{}, found one with {} element{}" ,
125
126
values. expected,
126
127
pluralize!( values. expected) ,
127
128
values. found,
128
129
pluralize!( values. found)
129
- ) ,
130
+ )
131
+ . into ( ) ,
130
132
FixedArraySize ( values) => format ! (
131
133
"expected an array with a fixed size of {} element{}, found one with {} element{}" ,
132
134
values. expected,
133
135
pluralize!( values. expected) ,
134
136
values. found,
135
137
pluralize!( values. found)
136
- ) ,
137
- ArgCount => format ! ( "incorrect number of function parameters" ) ,
138
- FieldMisMatch ( adt, field) => format ! ( "field type mismatch: {}.{}" , adt, field) ,
139
- RegionsDoesNotOutlive ( ..) => format ! ( "lifetime mismatch" ) ,
138
+ )
139
+ . into ( ) ,
140
+ ArgCount => "incorrect number of function parameters" . into ( ) ,
141
+ FieldMisMatch ( adt, field) => format ! ( "field type mismatch: {}.{}" , adt, field) . into ( ) ,
142
+ RegionsDoesNotOutlive ( ..) => "lifetime mismatch" . into ( ) ,
140
143
// Actually naming the region here is a bit confusing because context is lacking
141
144
RegionsInsufficientlyPolymorphic ( ..) => {
142
- format ! ( "one type is more general than the other" )
145
+ "one type is more general than the other" . into ( )
143
146
}
144
147
RegionsOverlyPolymorphic ( br, _) => format ! (
145
148
"expected concrete lifetime, found bound lifetime parameter{}" ,
146
149
br_string( br)
147
- ) ,
148
- RegionsPlaceholderMismatch => format ! ( "one type is more general than the other" ) ,
150
+ )
151
+ . into ( ) ,
152
+ RegionsPlaceholderMismatch => "one type is more general than the other" . into ( ) ,
149
153
ArgumentSorts ( values, _) | Sorts ( values) => {
150
154
let mut expected = values. expected . sort_string ( tcx) ;
151
155
let mut found = values. found . sort_string ( tcx) ;
152
156
if expected == found {
153
157
expected = values. expected . sort_string ( tcx) ;
154
158
found = values. found . sort_string ( tcx) ;
155
159
}
156
- report_maybe_different ( & expected, & found)
160
+ report_maybe_different ( & expected, & found) . into ( )
157
161
}
158
162
Traits ( values) => {
159
163
let ( mut expected, mut found) = with_forced_trimmed_paths ! ( (
@@ -165,6 +169,7 @@ impl<'tcx> TypeError<'tcx> {
165
169
found = tcx. def_path_str ( values. found ) ;
166
170
}
167
171
report_maybe_different ( & format ! ( "trait `{expected}`" ) , & format ! ( "trait `{found}`" ) )
172
+ . into ( )
168
173
}
169
174
IntMismatch ( ref values) => {
170
175
let expected = match values. expected {
@@ -175,36 +180,38 @@ impl<'tcx> TypeError<'tcx> {
175
180
ty:: IntVarValue :: IntType ( ty) => ty. name_str ( ) ,
176
181
ty:: IntVarValue :: UintType ( ty) => ty. name_str ( ) ,
177
182
} ;
178
- format ! ( "expected `{}`, found `{}`" , expected, found)
179
- }
180
- FloatMismatch ( ref values) => {
181
- format ! (
182
- "expected `{}`, found `{}`" ,
183
- values. expected. name_str( ) ,
184
- values. found. name_str( )
185
- )
183
+ format ! ( "expected `{}`, found `{}`" , expected, found) . into ( )
186
184
}
185
+ FloatMismatch ( ref values) => format ! (
186
+ "expected `{}`, found `{}`" ,
187
+ values. expected. name_str( ) ,
188
+ values. found. name_str( )
189
+ )
190
+ . into ( ) ,
187
191
VariadicMismatch ( ref values) => format ! (
188
192
"expected {} fn, found {} function" ,
189
193
if values. expected { "variadic" } else { "non-variadic" } ,
190
194
if values. found { "variadic" } else { "non-variadic" }
191
- ) ,
195
+ )
196
+ . into ( ) ,
192
197
ProjectionMismatched ( ref values) => format ! (
193
198
"expected {}, found {}" ,
194
199
tcx. def_path_str( values. expected) ,
195
200
tcx. def_path_str( values. found)
196
- ) ,
201
+ )
202
+ . into ( ) ,
197
203
ExistentialMismatch ( ref values) => report_maybe_different (
198
204
& format ! ( "trait `{}`" , values. expected) ,
199
205
& format ! ( "trait `{}`" , values. found) ,
200
- ) ,
206
+ )
207
+ . into ( ) ,
201
208
ConstMismatch ( ref values) => {
202
- format ! ( "expected `{}`, found `{}`" , values. expected, values. found)
209
+ format ! ( "expected `{}`, found `{}`" , values. expected, values. found) . into ( )
210
+ }
211
+ IntrinsicCast => "cannot coerce intrinsics to function pointers" . into ( ) ,
212
+ TargetFeatureCast ( _) => {
213
+ "cannot coerce functions with `#[target_feature]` to safe function pointers" . into ( )
203
214
}
204
- IntrinsicCast => format ! ( "cannot coerce intrinsics to function pointers" ) ,
205
- TargetFeatureCast ( _) => format ! (
206
- "cannot coerce functions with `#[target_feature]` to safe function pointers"
207
- ) ,
208
215
}
209
216
}
210
217
}
@@ -237,7 +244,7 @@ impl<'tcx> TypeError<'tcx> {
237
244
}
238
245
239
246
impl < ' tcx > Ty < ' tcx > {
240
- pub fn sort_string ( self , tcx : TyCtxt < ' tcx > ) -> String {
247
+ pub fn sort_string ( self , tcx : TyCtxt < ' tcx > ) -> Cow < ' static , str > {
241
248
match * self . kind ( ) {
242
249
ty:: Foreign ( def_id) => format ! ( "extern type `{}`" , tcx. def_path_str( def_id) ) . into ( ) ,
243
250
ty:: FnDef ( def_id, ..) => match tcx. def_kind ( def_id) {
@@ -247,7 +254,7 @@ impl<'tcx> Ty<'tcx> {
247
254
} ,
248
255
ty:: FnPtr ( _) => "fn pointer" . into ( ) ,
249
256
ty:: Dynamic ( ref inner, ..) if let Some ( principal) = inner. principal ( ) => {
250
- format ! ( "`dyn {}`" , tcx. def_path_str( principal. def_id( ) ) )
257
+ format ! ( "`dyn {}`" , tcx. def_path_str( principal. def_id( ) ) ) . into ( )
251
258
}
252
259
ty:: Dynamic ( ..) => "trait object" . into ( ) ,
253
260
ty:: Closure ( ..) => "closure" . into ( ) ,
@@ -269,7 +276,7 @@ impl<'tcx> Ty<'tcx> {
269
276
_ => {
270
277
let width = tcx. sess . diagnostic_width ( ) ;
271
278
let length_limit = std:: cmp:: max ( width / 4 , 15 ) ;
272
- format ! ( "`{}`" , tcx. ty_string_with_limit( self , length_limit) )
279
+ format ! ( "`{}`" , tcx. ty_string_with_limit( self , length_limit) ) . into ( )
273
280
}
274
281
}
275
282
}
0 commit comments