@@ -2776,7 +2776,7 @@ impl<'tcx> Display for ConstantKind<'tcx> {
2776
2776
fn fmt ( & self , fmt : & mut Formatter < ' _ > ) -> fmt:: Result {
2777
2777
match * self {
2778
2778
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) ,
2780
2780
// FIXME(valtrees): Correctly print mir constants.
2781
2781
ConstantKind :: Unevaluated ( ..) => {
2782
2782
fmt. write_str ( "_" ) ?;
@@ -2806,13 +2806,16 @@ fn pretty_print_byte_str(fmt: &mut Formatter<'_>, byte_str: &[u8]) -> fmt::Resul
2806
2806
write ! ( fmt, "b\" {}\" " , byte_str. escape_ascii( ) )
2807
2807
}
2808
2808
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 {
2810
2813
let mut first = true ;
2811
- for elem in elems {
2814
+ for ( ct , ty ) in elems {
2812
2815
if !first {
2813
2816
fmt. write_str ( ", " ) ?;
2814
2817
}
2815
- fmt . write_str ( & format ! ( "{}" , elem ) ) ?;
2818
+ pretty_print_const_value ( ct , ty , fmt ) ?;
2816
2819
first = false ;
2817
2820
}
2818
2821
Ok ( ( ) )
@@ -2823,7 +2826,6 @@ fn pretty_print_const_value<'tcx>(
2823
2826
ct : ConstValue < ' tcx > ,
2824
2827
ty : Ty < ' tcx > ,
2825
2828
fmt : & mut Formatter < ' _ > ,
2826
- print_ty : bool ,
2827
2829
) -> fmt:: Result {
2828
2830
use crate :: ty:: print:: PrettyPrinter ;
2829
2831
@@ -2882,16 +2884,11 @@ fn pretty_print_const_value<'tcx>(
2882
2884
// introducing ICEs (e.g. via `layout_of`) from missing bounds.
2883
2885
// E.g. `transmute([0usize; 2]): (u8, *mut T)` needs to know `T: Sized`
2884
2886
// 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.
2888
2887
( _, ty:: Array ( ..) | ty:: Tuple ( ..) | ty:: Adt ( ..) ) if !ty. has_non_region_param ( ) => {
2889
2888
let ct = tcx. lift ( ct) . unwrap ( ) ;
2890
2889
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 ( ) ;
2895
2892
match * ty. kind ( ) {
2896
2893
ty:: Array ( ..) => {
2897
2894
fmt. write_str ( "[" ) ?;
@@ -2930,12 +2927,14 @@ fn pretty_print_const_value<'tcx>(
2930
2927
None => {
2931
2928
fmt. write_str ( " {{ " ) ?;
2932
2929
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)
2934
2932
{
2935
2933
if !first {
2936
2934
fmt. write_str ( ", " ) ?;
2937
2935
}
2938
- fmt. write_str ( & format ! ( "{}: {}" , field_def. name, field) ) ?;
2936
+ write ! ( fmt, "{}: " , field_def. name) ?;
2937
+ pretty_print_const_value ( ct, ty, fmt) ?;
2939
2938
first = false ;
2940
2939
}
2941
2940
fmt. write_str ( " }}" ) ?;
@@ -2945,20 +2944,13 @@ fn pretty_print_const_value<'tcx>(
2945
2944
_ => unreachable ! ( ) ,
2946
2945
}
2947
2946
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
+ }
2956
2948
}
2957
2949
( ConstValue :: Scalar ( scalar) , _) => {
2958
2950
let mut cx = FmtPrinter :: new ( tcx, Namespace :: ValueNS ) ;
2959
2951
cx. print_alloc_ids = true ;
2960
2952
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) ?;
2962
2954
fmt. write_str ( & cx. into_buffer ( ) ) ?;
2963
2955
return Ok ( ( ) ) ;
2964
2956
}
@@ -2973,12 +2965,8 @@ fn pretty_print_const_value<'tcx>(
2973
2965
// their fields instead of just dumping the memory.
2974
2966
_ => { }
2975
2967
}
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}" )
2982
2970
} )
2983
2971
}
2984
2972
0 commit comments