@@ -3,7 +3,6 @@ use crate::ty::subst::{GenericArg, GenericArgKind, Subst};
33use crate :: ty:: { self , ConstInt , DefIdTree , ParamConst , ScalarInt , Term , Ty , TyCtxt , TypeFoldable } ;
44use rustc_apfloat:: ieee:: { Double , Single } ;
55use rustc_data_structures:: fx:: FxHashMap ;
6- use rustc_data_structures:: intern:: { Interned , WithStableHash } ;
76use rustc_data_structures:: sso:: SsoHashSet ;
87use rustc_hir as hir;
98use rustc_hir:: def:: { self , CtorKind , DefKind , Namespace } ;
@@ -1263,71 +1262,52 @@ pub trait PrettyPrinter<'tcx>:
12631262 let ( alloc_id, offset) = ptr. into_parts ( ) ;
12641263 match ty. kind ( ) {
12651264 // Byte strings (&[u8; N])
1266- ty:: Ref (
1267- _,
1268- Ty ( Interned (
1269- WithStableHash {
1270- internee :
1271- ty:: TyS {
1272- kind :
1273- ty:: Array (
1274- Ty ( Interned (
1275- WithStableHash {
1276- internee :
1277- ty:: TyS { kind : ty:: Uint ( ty:: UintTy :: U8 ) , .. } ,
1278- ..
1279- } ,
1280- _,
1281- ) ) ,
1282- ty:: Const ( Interned (
1283- ty:: ConstS {
1284- val : ty:: ConstKind :: Value ( ConstValue :: Scalar ( int) ) ,
1285- ..
1286- } ,
1287- _,
1288- ) ) ,
1289- ) ,
1290- ..
1291- } ,
1292- ..
1293- } ,
1294- _,
1295- ) ) ,
1296- _,
1297- ) => match self . tcx ( ) . get_global_alloc ( alloc_id) {
1298- Some ( GlobalAlloc :: Memory ( alloc) ) => {
1299- let len = int. assert_bits ( self . tcx ( ) . data_layout . pointer_size ) ;
1300- let range = AllocRange { start : offset, size : Size :: from_bytes ( len) } ;
1301- if let Ok ( byte_str) = alloc. inner ( ) . get_bytes ( & self . tcx ( ) , range) {
1302- p ! ( pretty_print_byte_str( byte_str) )
1303- } else {
1304- p ! ( "<too short allocation>" )
1265+ ty:: Ref ( _, inner, _) => {
1266+ if let ty:: Array ( elem, len) = inner. kind ( ) {
1267+ if let ty:: Uint ( ty:: UintTy :: U8 ) = elem. kind ( ) {
1268+ if let ty:: ConstKind :: Value ( ConstValue :: Scalar ( int) ) = len. val ( ) {
1269+ match self . tcx ( ) . get_global_alloc ( alloc_id) {
1270+ Some ( GlobalAlloc :: Memory ( alloc) ) => {
1271+ let len = int. assert_bits ( self . tcx ( ) . data_layout . pointer_size ) ;
1272+ let range =
1273+ AllocRange { start : offset, size : Size :: from_bytes ( len) } ;
1274+ if let Ok ( byte_str) =
1275+ alloc. inner ( ) . get_bytes ( & self . tcx ( ) , range)
1276+ {
1277+ p ! ( pretty_print_byte_str( byte_str) )
1278+ } else {
1279+ p ! ( "<too short allocation>" )
1280+ }
1281+ }
1282+ // FIXME: for statics and functions, we could in principle print more detail.
1283+ Some ( GlobalAlloc :: Static ( def_id) ) => {
1284+ p ! ( write( "<static({:?})>" , def_id) )
1285+ }
1286+ Some ( GlobalAlloc :: Function ( _) ) => p ! ( "<function>" ) ,
1287+ None => p ! ( "<dangling pointer>" ) ,
1288+ }
1289+ return Ok ( self ) ;
1290+ }
13051291 }
13061292 }
1307- // FIXME: for statics and functions, we could in principle print more detail.
1308- Some ( GlobalAlloc :: Static ( def_id) ) => p ! ( write( "<static({:?})>" , def_id) ) ,
1309- Some ( GlobalAlloc :: Function ( _) ) => p ! ( "<function>" ) ,
1310- None => p ! ( "<dangling pointer>" ) ,
1311- } ,
1293+ }
13121294 ty:: FnPtr ( _) => {
13131295 // FIXME: We should probably have a helper method to share code with the "Byte strings"
13141296 // printing above (which also has to handle pointers to all sorts of things).
1315- match self . tcx ( ) . get_global_alloc ( alloc_id) {
1316- Some ( GlobalAlloc :: Function ( instance) ) => {
1317- self = self . typed_value (
1318- |this| this. print_value_path ( instance. def_id ( ) , instance. substs ) ,
1319- |this| this. print_type ( ty) ,
1320- " as " ,
1321- ) ?;
1322- }
1323- _ => self = self . pretty_print_const_pointer ( ptr, ty, print_ty) ?,
1297+ if let Some ( GlobalAlloc :: Function ( instance) ) = self . tcx ( ) . get_global_alloc ( alloc_id)
1298+ {
1299+ self = self . typed_value (
1300+ |this| this. print_value_path ( instance. def_id ( ) , instance. substs ) ,
1301+ |this| this. print_type ( ty) ,
1302+ " as " ,
1303+ ) ?;
1304+ return Ok ( self ) ;
13241305 }
13251306 }
1326- // Any pointer values not covered by a branch above
1327- _ => {
1328- self = self . pretty_print_const_pointer ( ptr, ty, print_ty) ?;
1329- }
1307+ _ => { }
13301308 }
1309+ // Any pointer values not covered by a branch above
1310+ self = self . pretty_print_const_pointer ( ptr, ty, print_ty) ?;
13311311 Ok ( self )
13321312 }
13331313
@@ -1448,39 +1428,31 @@ pub trait PrettyPrinter<'tcx>:
14481428
14491429 match ( ct, ty. kind ( ) ) {
14501430 // Byte/string slices, printed as (byte) string literals.
1451- (
1452- ConstValue :: Slice { data, start, end } ,
1453- ty:: Ref (
1454- _,
1455- Ty ( Interned (
1456- WithStableHash { internee : ty:: TyS { kind : ty:: Slice ( t) , .. } , .. } ,
1457- _,
1458- ) ) ,
1459- _,
1460- ) ,
1461- ) if * t == u8_type => {
1462- // The `inspect` here is okay since we checked the bounds, and there are
1463- // no relocations (we have an active slice reference here). We don't use
1464- // this result to affect interpreter execution.
1465- let byte_str =
1466- data. inner ( ) . inspect_with_uninit_and_ptr_outside_interpreter ( start..end) ;
1467- self . pretty_print_byte_str ( byte_str)
1468- }
1469- (
1470- ConstValue :: Slice { data, start, end } ,
1471- ty:: Ref (
1472- _,
1473- Ty ( Interned ( WithStableHash { internee : ty:: TyS { kind : ty:: Str , .. } , .. } , _) ) ,
1474- _,
1475- ) ,
1476- ) => {
1477- // The `inspect` here is okay since we checked the bounds, and there are no
1478- // relocations (we have an active `str` reference here). We don't use this
1479- // result to affect interpreter execution.
1480- let slice =
1481- data. inner ( ) . inspect_with_uninit_and_ptr_outside_interpreter ( start..end) ;
1482- p ! ( write( "{:?}" , String :: from_utf8_lossy( slice) ) ) ;
1483- Ok ( self )
1431+ ( ConstValue :: Slice { data, start, end } , ty:: Ref ( _, inner, _) ) => {
1432+ match inner. kind ( ) {
1433+ ty:: Slice ( t) => {
1434+ if * t == u8_type {
1435+ // The `inspect` here is okay since we checked the bounds, and there are
1436+ // no relocations (we have an active slice reference here). We don't use
1437+ // this result to affect interpreter execution.
1438+ let byte_str = data
1439+ . inner ( )
1440+ . inspect_with_uninit_and_ptr_outside_interpreter ( start..end) ;
1441+ return self . pretty_print_byte_str ( byte_str) ;
1442+ }
1443+ }
1444+ ty:: Str => {
1445+ // The `inspect` here is okay since we checked the bounds, and there are no
1446+ // relocations (we have an active `str` reference here). We don't use this
1447+ // result to affect interpreter execution.
1448+ let slice = data
1449+ . inner ( )
1450+ . inspect_with_uninit_and_ptr_outside_interpreter ( start..end) ;
1451+ p ! ( write( "{:?}" , String :: from_utf8_lossy( slice) ) ) ;
1452+ return Ok ( self ) ;
1453+ }
1454+ _ => { }
1455+ }
14841456 }
14851457 ( ConstValue :: ByRef { alloc, offset } , ty:: Array ( t, n) ) if * t == u8_type => {
14861458 let n = n. val ( ) . try_to_bits ( self . tcx ( ) . data_layout . pointer_size ) . unwrap ( ) ;
@@ -1490,7 +1462,7 @@ pub trait PrettyPrinter<'tcx>:
14901462 let byte_str = alloc. inner ( ) . get_bytes ( & self . tcx ( ) , range) . unwrap ( ) ;
14911463 p ! ( "*" ) ;
14921464 p ! ( pretty_print_byte_str( byte_str) ) ;
1493- Ok ( self )
1465+ return Ok ( self ) ;
14941466 }
14951467
14961468 // Aggregates, printed as array/tuple/struct/variant construction syntax.
@@ -1567,22 +1539,24 @@ pub trait PrettyPrinter<'tcx>:
15671539 _ => unreachable ! ( ) ,
15681540 }
15691541
1570- Ok ( self )
1542+ return Ok ( self ) ;
15711543 }
15721544
1573- ( ConstValue :: Scalar ( scalar) , _) => self . pretty_print_const_scalar ( scalar, ty, print_ty) ,
1545+ ( ConstValue :: Scalar ( scalar) , _) => {
1546+ return self . pretty_print_const_scalar ( scalar, ty, print_ty) ;
1547+ }
15741548
15751549 // FIXME(oli-obk): also pretty print arrays and other aggregate constants by reading
15761550 // their fields instead of just dumping the memory.
1577- _ => {
1578- // fallback
1579- p ! ( write( "{:?}" , ct) ) ;
1580- if print_ty {
1581- p ! ( ": " , print( ty) ) ;
1582- }
1583- Ok ( self )
1584- }
1551+ _ => { }
15851552 }
1553+
1554+ // fallback
1555+ p ! ( write( "{:?}" , ct) ) ;
1556+ if print_ty {
1557+ p ! ( ": " , print( ty) ) ;
1558+ }
1559+ Ok ( self )
15861560 }
15871561}
15881562
0 commit comments