@@ -16,6 +16,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
16
16
use rustc_middle:: ty:: { self , subst:: Subst , TyCtxt } ;
17
17
use rustc_span:: source_map:: Span ;
18
18
use rustc_target:: abi:: { Abi , LayoutOf } ;
19
+ use std:: borrow:: Cow ;
19
20
use std:: convert:: TryInto ;
20
21
21
22
pub fn note_on_undefined_behavior_error ( ) -> & ' static str {
@@ -328,11 +329,22 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
328
329
) )
329
330
} else {
330
331
let msg = if is_static {
331
- "could not evaluate static initializer"
332
+ Cow :: from ( "could not evaluate static initializer" )
332
333
} else {
333
- "evaluation of constant value failed"
334
+ // If the current item has generics, we'd like to enrich the message with the
335
+ // instance and its substs: to show the actual compile-time values, in addition to
336
+ // the expression, leading to the const eval error.
337
+ let instance = & key. value . instance ;
338
+ if !instance. substs . is_empty ( ) {
339
+ let instance = with_no_trimmed_paths ( || instance. to_string ( ) ) ;
340
+ let msg = format ! ( "evaluation of `{}` failed" , instance) ;
341
+ Cow :: from ( msg)
342
+ } else {
343
+ Cow :: from ( "evaluation of constant value failed" )
344
+ }
334
345
} ;
335
- Err ( err. report_as_error ( ecx. tcx . at ( ecx. cur_span ( ) ) , msg) )
346
+
347
+ Err ( err. report_as_error ( ecx. tcx . at ( ecx. cur_span ( ) ) , & msg) )
336
348
}
337
349
}
338
350
Ok ( mplace) => {
0 commit comments