@@ -86,7 +86,7 @@ impl TypeError<'_> {
8686/// afterwards to present additional details, particularly when it comes to lifetime-related
8787/// errors.
8888impl < ' 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 > {
9090 use self :: TypeError :: * ;
9191 fn report_maybe_different ( expected : & str , found : & str ) -> String {
9292 // A naive approach to making sure that we're not reporting silly errors such as:
@@ -104,56 +104,60 @@ impl<'tcx> TypeError<'tcx> {
104104 } ;
105105
106106 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 ( ) ,
110110 ConstnessMismatch ( values) => {
111- format ! ( "expected {} bound, found {} bound" , values. expected, values. found)
111+ format ! ( "expected {} bound, found {} bound" , values. expected, values. found) . into ( )
112112 }
113113 PolarityMismatch ( values) => {
114114 format ! ( "expected {} polarity, found {} polarity" , values. expected, values. found)
115+ . into ( )
115116 }
116117 UnsafetyMismatch ( values) => {
117- format ! ( "expected {} fn, found {} fn" , values. expected, values. found)
118+ format ! ( "expected {} fn, found {} fn" , values. expected, values. found) . into ( )
118119 }
119120 AbiMismatch ( values) => {
120- format ! ( "expected {} fn, found {} fn" , values. expected, values. found)
121+ format ! ( "expected {} fn, found {} fn" , values. expected, values. found) . into ( )
121122 }
122- ArgumentMutability ( _) | Mutability => format ! ( "types differ in mutability" ) ,
123+ ArgumentMutability ( _) | Mutability => "types differ in mutability" . into ( ) ,
123124 TupleSize ( values) => format ! (
124125 "expected a tuple with {} element{}, found one with {} element{}" ,
125126 values. expected,
126127 pluralize!( values. expected) ,
127128 values. found,
128129 pluralize!( values. found)
129- ) ,
130+ )
131+ . into ( ) ,
130132 FixedArraySize ( values) => format ! (
131133 "expected an array with a fixed size of {} element{}, found one with {} element{}" ,
132134 values. expected,
133135 pluralize!( values. expected) ,
134136 values. found,
135137 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 ( ) ,
140143 // Actually naming the region here is a bit confusing because context is lacking
141144 RegionsInsufficientlyPolymorphic ( ..) => {
142- format ! ( "one type is more general than the other" )
145+ "one type is more general than the other" . into ( )
143146 }
144147 RegionsOverlyPolymorphic ( br, _) => format ! (
145148 "expected concrete lifetime, found bound lifetime parameter{}" ,
146149 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 ( ) ,
149153 ArgumentSorts ( values, _) | Sorts ( values) => {
150154 let mut expected = values. expected . sort_string ( tcx) ;
151155 let mut found = values. found . sort_string ( tcx) ;
152156 if expected == found {
153157 expected = values. expected . sort_string ( tcx) ;
154158 found = values. found . sort_string ( tcx) ;
155159 }
156- report_maybe_different ( & expected, & found)
160+ report_maybe_different ( & expected, & found) . into ( )
157161 }
158162 Traits ( values) => {
159163 let ( mut expected, mut found) = with_forced_trimmed_paths ! ( (
@@ -165,6 +169,7 @@ impl<'tcx> TypeError<'tcx> {
165169 found = tcx. def_path_str ( values. found ) ;
166170 }
167171 report_maybe_different ( & format ! ( "trait `{expected}`" ) , & format ! ( "trait `{found}`" ) )
172+ . into ( )
168173 }
169174 IntMismatch ( ref values) => {
170175 let expected = match values. expected {
@@ -175,36 +180,38 @@ impl<'tcx> TypeError<'tcx> {
175180 ty:: IntVarValue :: IntType ( ty) => ty. name_str ( ) ,
176181 ty:: IntVarValue :: UintType ( ty) => ty. name_str ( ) ,
177182 } ;
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 ( )
186184 }
185+ FloatMismatch ( ref values) => format ! (
186+ "expected `{}`, found `{}`" ,
187+ values. expected. name_str( ) ,
188+ values. found. name_str( )
189+ )
190+ . into ( ) ,
187191 VariadicMismatch ( ref values) => format ! (
188192 "expected {} fn, found {} function" ,
189193 if values. expected { "variadic" } else { "non-variadic" } ,
190194 if values. found { "variadic" } else { "non-variadic" }
191- ) ,
195+ )
196+ . into ( ) ,
192197 ProjectionMismatched ( ref values) => format ! (
193198 "expected {}, found {}" ,
194199 tcx. def_path_str( values. expected) ,
195200 tcx. def_path_str( values. found)
196- ) ,
201+ )
202+ . into ( ) ,
197203 ExistentialMismatch ( ref values) => report_maybe_different (
198204 & format ! ( "trait `{}`" , values. expected) ,
199205 & format ! ( "trait `{}`" , values. found) ,
200- ) ,
206+ )
207+ . into ( ) ,
201208 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 ( )
203214 }
204- IntrinsicCast => format ! ( "cannot coerce intrinsics to function pointers" ) ,
205- TargetFeatureCast ( _) => format ! (
206- "cannot coerce functions with `#[target_feature]` to safe function pointers"
207- ) ,
208215 }
209216 }
210217}
@@ -237,7 +244,7 @@ impl<'tcx> TypeError<'tcx> {
237244}
238245
239246impl < ' 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 > {
241248 match * self . kind ( ) {
242249 ty:: Foreign ( def_id) => format ! ( "extern type `{}`" , tcx. def_path_str( def_id) ) . into ( ) ,
243250 ty:: FnDef ( def_id, ..) => match tcx. def_kind ( def_id) {
@@ -247,7 +254,7 @@ impl<'tcx> Ty<'tcx> {
247254 } ,
248255 ty:: FnPtr ( _) => "fn pointer" . into ( ) ,
249256 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 ( )
251258 }
252259 ty:: Dynamic ( ..) => "trait object" . into ( ) ,
253260 ty:: Closure ( ..) => "closure" . into ( ) ,
@@ -269,7 +276,7 @@ impl<'tcx> Ty<'tcx> {
269276 _ => {
270277 let width = tcx. sess . diagnostic_width ( ) ;
271278 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 ( )
273280 }
274281 }
275282 }
0 commit comments