@@ -2776,7 +2776,7 @@ impl<'tcx> Display for ConstantKind<'tcx> {
27762776 fn fmt ( & self , fmt : & mut Formatter < ' _ > ) -> fmt:: Result {
27772777 match * self {
27782778 ConstantKind :: Ty ( c) => pretty_print_const ( c, fmt, true ) ,
2779- ConstantKind :: Val ( val, ty) => pretty_print_const_value ( val, ty, fmt, true ) ,
2779+ ConstantKind :: Val ( val, ty) => pretty_print_const_value ( val, ty, fmt) ,
27802780 // FIXME(valtrees): Correctly print mir constants.
27812781 ConstantKind :: Unevaluated ( ..) => {
27822782 fmt. write_str ( "_" ) ?;
@@ -2806,13 +2806,16 @@ fn pretty_print_byte_str(fmt: &mut Formatter<'_>, byte_str: &[u8]) -> fmt::Resul
28062806 write ! ( fmt, "b\" {}\" " , byte_str. escape_ascii( ) )
28072807}
28082808
2809- fn comma_sep < ' tcx > ( fmt : & mut Formatter < ' _ > , elems : Vec < ConstantKind < ' tcx > > ) -> fmt:: Result {
2809+ fn comma_sep < ' tcx > (
2810+ fmt : & mut Formatter < ' _ > ,
2811+ elems : Vec < ( ConstValue < ' tcx > , Ty < ' tcx > ) > ,
2812+ ) -> fmt:: Result {
28102813 let mut first = true ;
2811- for elem in elems {
2814+ for ( ct , ty ) in elems {
28122815 if !first {
28132816 fmt. write_str ( ", " ) ?;
28142817 }
2815- fmt . write_str ( & format ! ( "{}" , elem ) ) ?;
2818+ pretty_print_const_value ( ct , ty , fmt ) ?;
28162819 first = false ;
28172820 }
28182821 Ok ( ( ) )
@@ -2823,7 +2826,6 @@ fn pretty_print_const_value<'tcx>(
28232826 ct : ConstValue < ' tcx > ,
28242827 ty : Ty < ' tcx > ,
28252828 fmt : & mut Formatter < ' _ > ,
2826- print_ty : bool ,
28272829) -> fmt:: Result {
28282830 use crate :: ty:: print:: PrettyPrinter ;
28292831
@@ -2882,16 +2884,11 @@ fn pretty_print_const_value<'tcx>(
28822884 // introducing ICEs (e.g. via `layout_of`) from missing bounds.
28832885 // E.g. `transmute([0usize; 2]): (u8, *mut T)` needs to know `T: Sized`
28842886 // to be able to destructure the tuple into `(0u8, *mut T)`
2885- //
2886- // FIXME(eddyb) for `--emit=mir`/`-Z dump-mir`, we should provide the
2887- // correct `ty::ParamEnv` to allow printing *all* constant values.
28882887 ( _, ty:: Array ( ..) | ty:: Tuple ( ..) | ty:: Adt ( ..) ) if !ty. has_non_region_param ( ) => {
28892888 let ct = tcx. lift ( ct) . unwrap ( ) ;
28902889 let ty = tcx. lift ( ty) . unwrap ( ) ;
2891- if let Some ( contents) = tcx. try_destructure_mir_constant (
2892- ty:: ParamEnv :: reveal_all ( ) . and ( ConstantKind :: Val ( ct, ty) ) ,
2893- ) {
2894- let fields = contents. fields . to_vec ( ) ;
2890+ if let Some ( contents) = tcx. try_destructure_mir_constant_for_diagnostics ( ( ct, ty) ) {
2891+ let fields: Vec < ( ConstValue < ' _ > , Ty < ' _ > ) > = contents. fields . to_vec ( ) ;
28952892 match * ty. kind ( ) {
28962893 ty:: Array ( ..) => {
28972894 fmt. write_str ( "[" ) ?;
@@ -2930,12 +2927,14 @@ fn pretty_print_const_value<'tcx>(
29302927 None => {
29312928 fmt. write_str ( " {{ " ) ?;
29322929 let mut first = true ;
2933- for ( field_def, field) in iter:: zip ( & variant_def. fields , fields)
2930+ for ( field_def, ( ct, ty) ) in
2931+ iter:: zip ( & variant_def. fields , fields)
29342932 {
29352933 if !first {
29362934 fmt. write_str ( ", " ) ?;
29372935 }
2938- fmt. write_str ( & format ! ( "{}: {}" , field_def. name, field) ) ?;
2936+ write ! ( fmt, "{}: " , field_def. name) ?;
2937+ pretty_print_const_value ( ct, ty, fmt) ?;
29392938 first = false ;
29402939 }
29412940 fmt. write_str ( " }}" ) ?;
@@ -2945,20 +2944,13 @@ fn pretty_print_const_value<'tcx>(
29452944 _ => unreachable ! ( ) ,
29462945 }
29472946 return Ok ( ( ) ) ;
2948- } else {
2949- // Fall back to debug pretty printing for invalid constants.
2950- fmt. write_str ( & format ! ( "{:?}" , ct) ) ?;
2951- if print_ty {
2952- fmt. write_str ( & format ! ( ": {}" , ty) ) ?;
2953- }
2954- return Ok ( ( ) ) ;
2955- } ;
2947+ }
29562948 }
29572949 ( ConstValue :: Scalar ( scalar) , _) => {
29582950 let mut cx = FmtPrinter :: new ( tcx, Namespace :: ValueNS ) ;
29592951 cx. print_alloc_ids = true ;
29602952 let ty = tcx. lift ( ty) . unwrap ( ) ;
2961- cx = cx. pretty_print_const_scalar ( scalar, ty, print_ty ) ?;
2953+ cx = cx. pretty_print_const_scalar ( scalar, ty) ?;
29622954 fmt. write_str ( & cx. into_buffer ( ) ) ?;
29632955 return Ok ( ( ) ) ;
29642956 }
@@ -2973,12 +2965,8 @@ fn pretty_print_const_value<'tcx>(
29732965 // their fields instead of just dumping the memory.
29742966 _ => { }
29752967 }
2976- // fallback
2977- fmt. write_str ( & format ! ( "{:?}" , ct) ) ?;
2978- if print_ty {
2979- fmt. write_str ( & format ! ( ": {}" , ty) ) ?;
2980- }
2981- Ok ( ( ) )
2968+ // Fall back to debug pretty printing for invalid constants.
2969+ write ! ( fmt, "{ct:?}: {ty}" )
29822970 } )
29832971}
29842972
0 commit comments