@@ -14,7 +14,7 @@ use rustc_errors::{
1414 Applicability , Diag , ErrorGuaranteed , Level , MultiSpan , StashKey , StringPart , Suggestions , msg,
1515 pluralize, struct_span_code_err,
1616} ;
17- use rustc_hir:: attrs:: diagnostic:: { AppendConstMessage , OnUnimplementedNote } ;
17+ use rustc_hir:: attrs:: diagnostic:: OnUnimplementedNote ;
1818use rustc_hir:: def_id:: { DefId , LOCAL_CRATE , LocalDefId } ;
1919use rustc_hir:: intravisit:: Visitor ;
2020use rustc_hir:: { self as hir, LangItem , Node , find_attr} ;
@@ -193,10 +193,16 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
193193 label,
194194 notes,
195195 parent_label,
196- append_const_msg,
197196 } = self . on_unimplemented_note ( main_trait_predicate, main_obligation, & mut long_ty_file) ;
198197
199198 let have_alt_message = message. is_some ( ) || label. is_some ( ) ;
199+
200+ let message = message. unwrap_or_else ( || self . get_standard_error_message (
201+ main_trait_predicate,
202+ None ,
203+ post_message,
204+ & mut long_ty_file,
205+ ) ) ;
200206 let is_try_conversion = self . is_try_conversion ( span, main_trait_predicate. def_id ( ) ) ;
201207 let is_question_mark = matches ! (
202208 root_obligation. cause. code( ) . peel_derives( ) ,
@@ -210,16 +216,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
210216 let question_mark_message = "the question mark operation (`?`) implicitly \
211217 performs a conversion on the error value \
212218 using the `From` trait";
213- let ( message, notes, append_const_msg ) = if is_try_conversion {
219+ let ( message, notes) = if is_try_conversion {
214220 let ty = self . tcx . short_string (
215221 main_trait_predicate. skip_binder ( ) . self_ty ( ) ,
216222 & mut long_ty_file,
217223 ) ;
218224 // We have a `-> Result<_, E1>` and `gives_E2()?`.
219225 (
220- Some ( format ! ( "`?` couldn't convert the error to `{ty}`" ) ) ,
226+ format ! ( "`?` couldn't convert the error to `{ty}`" ) ,
221227 vec ! [ question_mark_message. to_owned( ) ] ,
222- Some ( AppendConstMessage :: Default ) ,
223228 )
224229 } else if is_question_mark {
225230 let main_trait_predicate =
@@ -228,26 +233,16 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
228233 // trait object: `-> Result<_, Box<dyn Error>` and `gives_E()?` when
229234 // `E: Error` isn't met.
230235 (
231- Some ( format ! (
236+ format ! (
232237 "`?` couldn't convert the error: `{main_trait_predicate}` is \
233238 not satisfied",
234- ) ) ,
239+ ) ,
235240 vec ! [ question_mark_message. to_owned( ) ] ,
236- Some ( AppendConstMessage :: Default ) ,
237241 )
238242 } else {
239- ( message, notes, append_const_msg )
243+ ( message, notes)
240244 } ;
241245
242- let default_err_msg = || self . get_standard_error_message (
243- main_trait_predicate,
244- message,
245- None ,
246- append_const_msg,
247- post_message,
248- & mut long_ty_file,
249- ) ;
250-
251246 let ( err_msg, safe_transmute_explanation) = if self . tcx . is_lang_item (
252247 main_trait_predicate. def_id ( ) ,
253248 LangItem :: TransmuteTrait ,
@@ -271,15 +266,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
271266 ) ;
272267 }
273268 GetSafeTransmuteErrorAndReason :: Default => {
274- ( default_err_msg ( ) , None )
269+ ( message , None )
275270 }
276271 GetSafeTransmuteErrorAndReason :: Error {
277272 err_msg,
278273 safe_transmute_explanation,
279274 } => ( err_msg, safe_transmute_explanation) ,
280275 }
281276 } else {
282- ( default_err_msg ( ) , None )
277+ ( message , None )
283278 } ;
284279
285280 let mut err = struct_span_code_err ! ( self . dcx( ) , span, E0277 , "{}" , err_msg) ;
@@ -393,7 +388,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
393388 if let Some ( s) = label {
394389 // If it has a custom `#[rustc_on_unimplemented]`
395390 // error message, let's display it as the label!
396- err. span_label ( span, s. as_str ( ) . to_owned ( ) ) ;
391+ err. span_label ( span, s) ;
397392 if !matches ! ( leaf_trait_predicate. skip_binder( ) . self_ty( ) . kind( ) , ty:: Param ( _) )
398393 // When the self type is a type param We don't need to "the trait
399394 // `std::marker::Sized` is not implemented for `T`" as we will point
@@ -862,9 +857,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
862857
863858 let err_msg = self . get_standard_error_message (
864859 trait_ref,
865- None ,
866860 Some ( predicate. constness ( ) ) ,
867- None ,
868861 String :: new ( ) ,
869862 & mut file,
870863 ) ;
@@ -924,7 +917,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
924917 label,
925918 notes,
926919 parent_label,
927- append_const_msg : _,
928920 } = note;
929921
930922 if let Some ( message) = message {
@@ -2841,40 +2833,17 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
28412833 fn get_standard_error_message (
28422834 & self ,
28432835 trait_predicate : ty:: PolyTraitPredicate < ' tcx > ,
2844- message : Option < String > ,
28452836 predicate_constness : Option < ty:: BoundConstness > ,
2846- append_const_msg : Option < AppendConstMessage > ,
28472837 post_message : String ,
28482838 long_ty_path : & mut Option < PathBuf > ,
28492839 ) -> String {
2850- message
2851- . and_then ( |cannot_do_this| {
2852- match ( predicate_constness, append_const_msg) {
2853- // do nothing if predicate is not const
2854- ( None , _) => Some ( cannot_do_this) ,
2855- // suggested using default post message
2856- (
2857- Some ( ty:: BoundConstness :: Const | ty:: BoundConstness :: Maybe ) ,
2858- Some ( AppendConstMessage :: Default ) ,
2859- ) => Some ( format ! ( "{cannot_do_this} in const contexts" ) ) ,
2860- // overridden post message
2861- (
2862- Some ( ty:: BoundConstness :: Const | ty:: BoundConstness :: Maybe ) ,
2863- Some ( AppendConstMessage :: Custom ( custom_msg, _) ) ,
2864- ) => Some ( format ! ( "{cannot_do_this}{custom_msg}" ) ) ,
2865- // fallback to generic message
2866- ( Some ( ty:: BoundConstness :: Const | ty:: BoundConstness :: Maybe ) , None ) => None ,
2867- }
2868- } )
2869- . unwrap_or_else ( || {
2870- format ! (
2871- "the trait bound `{}` is not satisfied{post_message}" ,
2872- self . tcx. short_string(
2873- trait_predicate. print_with_bound_constness( predicate_constness) ,
2874- long_ty_path,
2875- ) ,
2876- )
2877- } )
2840+ format ! (
2841+ "the trait bound `{}` is not satisfied{post_message}" ,
2842+ self . tcx. short_string(
2843+ trait_predicate. print_with_bound_constness( predicate_constness) ,
2844+ long_ty_path,
2845+ ) ,
2846+ )
28782847 }
28792848
28802849 fn select_transmute_obligation_for_reporting (
0 commit comments