@@ -16,6 +16,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
1616use rustc_middle:: ty:: { self , subst:: Subst , TyCtxt } ;
1717use rustc_span:: source_map:: Span ;
1818use rustc_target:: abi:: { Abi , LayoutOf } ;
19+ use std:: borrow:: Cow ;
1920use std:: convert:: TryInto ;
2021
2122pub fn note_on_undefined_behavior_error ( ) -> & ' static str {
@@ -328,11 +329,22 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
328329 ) )
329330 } else {
330331 let msg = if is_static {
331- "could not evaluate static initializer"
332+ Cow :: from ( "could not evaluate static initializer" )
332333 } 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+ }
334345 } ;
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) )
336348 }
337349 }
338350 Ok ( mplace) => {
0 commit comments