@@ -6,7 +6,7 @@ use crate::mir::interpret::{AllocRange, ConstAllocation, ConstValue, Scalar};
6
6
use crate :: mir:: visit:: MirVisitable ;
7
7
use crate :: ty:: codec:: { TyDecoder , TyEncoder } ;
8
8
use crate :: ty:: fold:: { FallibleTypeFolder , TypeFoldable } ;
9
- use crate :: ty:: print:: with_no_trimmed_paths;
9
+ use crate :: ty:: print:: { pretty_print_const , with_no_trimmed_paths} ;
10
10
use crate :: ty:: print:: { FmtPrinter , Printer } ;
11
11
use crate :: ty:: visit:: TypeVisitableExt ;
12
12
use crate :: ty:: { self , List , Ty , TyCtxt } ;
@@ -20,7 +20,7 @@ use rustc_hir::def_id::{DefId, CRATE_DEF_ID};
20
20
use rustc_hir:: { self , GeneratorKind , ImplicitSelfKind } ;
21
21
use rustc_hir:: { self as hir, HirId } ;
22
22
use rustc_session:: Session ;
23
- use rustc_target:: abi:: { FieldIdx , Size , VariantIdx } ;
23
+ use rustc_target:: abi:: { FieldIdx , VariantIdx } ;
24
24
25
25
use polonius_engine:: Atom ;
26
26
pub use rustc_ast:: Mutability ;
@@ -46,7 +46,6 @@ pub use basic_blocks::BasicBlocks;
46
46
47
47
mod basic_blocks;
48
48
mod consts;
49
- pub use consts:: * ;
50
49
pub mod coverage;
51
50
mod generic_graph;
52
51
pub mod generic_graphviz;
@@ -58,10 +57,8 @@ pub mod pretty;
58
57
mod query;
59
58
pub mod spanview;
60
59
mod syntax;
61
- pub use syntax:: * ;
62
60
pub mod tcx;
63
61
mod terminator;
64
- pub use terminator:: * ;
65
62
66
63
pub mod traversal;
67
64
mod type_foldable;
@@ -72,6 +69,10 @@ pub use self::graphviz::write_mir_graphviz;
72
69
pub use self :: pretty:: {
73
70
create_dump_file, display_allocation, dump_enabled, dump_mir, write_mir_pretty, PassWhere ,
74
71
} ;
72
+ pub use consts:: * ;
73
+ pub use pretty:: pretty_print_const_value;
74
+ pub use syntax:: * ;
75
+ pub use terminator:: * ;
75
76
76
77
/// Types for locals
77
78
pub type LocalDecls < ' tcx > = IndexSlice < Local , LocalDecl < ' tcx > > ;
@@ -2431,176 +2432,6 @@ rustc_index::newtype_index! {
2431
2432
pub struct Promoted { }
2432
2433
}
2433
2434
2434
- fn pretty_print_const < ' tcx > (
2435
- c : ty:: Const < ' tcx > ,
2436
- fmt : & mut Formatter < ' _ > ,
2437
- print_types : bool ,
2438
- ) -> fmt:: Result {
2439
- use crate :: ty:: print:: PrettyPrinter ;
2440
- ty:: tls:: with ( |tcx| {
2441
- let literal = tcx. lift ( c) . unwrap ( ) ;
2442
- let mut cx = FmtPrinter :: new ( tcx, Namespace :: ValueNS ) ;
2443
- cx. print_alloc_ids = true ;
2444
- let cx = cx. pretty_print_const ( literal, print_types) ?;
2445
- fmt. write_str ( & cx. into_buffer ( ) ) ?;
2446
- Ok ( ( ) )
2447
- } )
2448
- }
2449
-
2450
- fn pretty_print_byte_str ( fmt : & mut Formatter < ' _ > , byte_str : & [ u8 ] ) -> fmt:: Result {
2451
- write ! ( fmt, "b\" {}\" " , byte_str. escape_ascii( ) )
2452
- }
2453
-
2454
- fn comma_sep < ' tcx > (
2455
- fmt : & mut Formatter < ' _ > ,
2456
- elems : Vec < ( ConstValue < ' tcx > , Ty < ' tcx > ) > ,
2457
- ) -> fmt:: Result {
2458
- let mut first = true ;
2459
- for ( ct, ty) in elems {
2460
- if !first {
2461
- fmt. write_str ( ", " ) ?;
2462
- }
2463
- pretty_print_const_value ( ct, ty, fmt) ?;
2464
- first = false ;
2465
- }
2466
- Ok ( ( ) )
2467
- }
2468
-
2469
- // FIXME: Move that into `mir/pretty.rs`.
2470
- fn pretty_print_const_value < ' tcx > (
2471
- ct : ConstValue < ' tcx > ,
2472
- ty : Ty < ' tcx > ,
2473
- fmt : & mut Formatter < ' _ > ,
2474
- ) -> fmt:: Result {
2475
- use crate :: ty:: print:: PrettyPrinter ;
2476
-
2477
- ty:: tls:: with ( |tcx| {
2478
- let ct = tcx. lift ( ct) . unwrap ( ) ;
2479
- let ty = tcx. lift ( ty) . unwrap ( ) ;
2480
-
2481
- if tcx. sess . verbose ( ) {
2482
- fmt. write_str ( & format ! ( "ConstValue({ct:?}: {ty})" ) ) ?;
2483
- return Ok ( ( ) ) ;
2484
- }
2485
-
2486
- let u8_type = tcx. types . u8 ;
2487
- match ( ct, ty. kind ( ) ) {
2488
- // Byte/string slices, printed as (byte) string literals.
2489
- ( _, ty:: Ref ( _, inner_ty, _) ) if matches ! ( inner_ty. kind( ) , ty:: Str ) => {
2490
- if let Some ( data) = ct. try_get_slice_bytes_for_diagnostics ( tcx) {
2491
- fmt. write_str ( & format ! ( "{:?}" , String :: from_utf8_lossy( data) ) ) ?;
2492
- return Ok ( ( ) ) ;
2493
- }
2494
- }
2495
- ( _, ty:: Ref ( _, inner_ty, _) ) if matches ! ( inner_ty. kind( ) , ty:: Slice ( t) if * t == u8_type) => {
2496
- if let Some ( data) = ct. try_get_slice_bytes_for_diagnostics ( tcx) {
2497
- pretty_print_byte_str ( fmt, data) ?;
2498
- return Ok ( ( ) ) ;
2499
- }
2500
- }
2501
- ( ConstValue :: Indirect { alloc_id, offset } , ty:: Array ( t, n) ) if * t == u8_type => {
2502
- let n = n. try_to_target_usize ( tcx) . unwrap ( ) ;
2503
- let alloc = tcx. global_alloc ( alloc_id) . unwrap_memory ( ) ;
2504
- // cast is ok because we already checked for pointer size (32 or 64 bit) above
2505
- let range = AllocRange { start : offset, size : Size :: from_bytes ( n) } ;
2506
- let byte_str = alloc. inner ( ) . get_bytes_strip_provenance ( & tcx, range) . unwrap ( ) ;
2507
- fmt. write_str ( "*" ) ?;
2508
- pretty_print_byte_str ( fmt, byte_str) ?;
2509
- return Ok ( ( ) ) ;
2510
- }
2511
- // Aggregates, printed as array/tuple/struct/variant construction syntax.
2512
- //
2513
- // NB: the `has_non_region_param` check ensures that we can use
2514
- // the `destructure_const` query with an empty `ty::ParamEnv` without
2515
- // introducing ICEs (e.g. via `layout_of`) from missing bounds.
2516
- // E.g. `transmute([0usize; 2]): (u8, *mut T)` needs to know `T: Sized`
2517
- // to be able to destructure the tuple into `(0u8, *mut T)`
2518
- ( _, ty:: Array ( ..) | ty:: Tuple ( ..) | ty:: Adt ( ..) ) if !ty. has_non_region_param ( ) => {
2519
- let ct = tcx. lift ( ct) . unwrap ( ) ;
2520
- let ty = tcx. lift ( ty) . unwrap ( ) ;
2521
- if let Some ( contents) = tcx. try_destructure_mir_constant_for_diagnostics ( ( ct, ty) ) {
2522
- let fields: Vec < ( ConstValue < ' _ > , Ty < ' _ > ) > = contents. fields . to_vec ( ) ;
2523
- match * ty. kind ( ) {
2524
- ty:: Array ( ..) => {
2525
- fmt. write_str ( "[" ) ?;
2526
- comma_sep ( fmt, fields) ?;
2527
- fmt. write_str ( "]" ) ?;
2528
- }
2529
- ty:: Tuple ( ..) => {
2530
- fmt. write_str ( "(" ) ?;
2531
- comma_sep ( fmt, fields) ?;
2532
- if contents. fields . len ( ) == 1 {
2533
- fmt. write_str ( "," ) ?;
2534
- }
2535
- fmt. write_str ( ")" ) ?;
2536
- }
2537
- ty:: Adt ( def, _) if def. variants ( ) . is_empty ( ) => {
2538
- fmt. write_str ( & format ! ( "{{unreachable(): {ty}}}" ) ) ?;
2539
- }
2540
- ty:: Adt ( def, args) => {
2541
- let variant_idx = contents
2542
- . variant
2543
- . expect ( "destructed mir constant of adt without variant idx" ) ;
2544
- let variant_def = & def. variant ( variant_idx) ;
2545
- let args = tcx. lift ( args) . unwrap ( ) ;
2546
- let mut cx = FmtPrinter :: new ( tcx, Namespace :: ValueNS ) ;
2547
- cx. print_alloc_ids = true ;
2548
- let cx = cx. print_value_path ( variant_def. def_id , args) ?;
2549
- fmt. write_str ( & cx. into_buffer ( ) ) ?;
2550
-
2551
- match variant_def. ctor_kind ( ) {
2552
- Some ( CtorKind :: Const ) => { }
2553
- Some ( CtorKind :: Fn ) => {
2554
- fmt. write_str ( "(" ) ?;
2555
- comma_sep ( fmt, fields) ?;
2556
- fmt. write_str ( ")" ) ?;
2557
- }
2558
- None => {
2559
- fmt. write_str ( " {{ " ) ?;
2560
- let mut first = true ;
2561
- for ( field_def, ( ct, ty) ) in
2562
- iter:: zip ( & variant_def. fields , fields)
2563
- {
2564
- if !first {
2565
- fmt. write_str ( ", " ) ?;
2566
- }
2567
- write ! ( fmt, "{}: " , field_def. name) ?;
2568
- pretty_print_const_value ( ct, ty, fmt) ?;
2569
- first = false ;
2570
- }
2571
- fmt. write_str ( " }}" ) ?;
2572
- }
2573
- }
2574
- }
2575
- _ => unreachable ! ( ) ,
2576
- }
2577
- return Ok ( ( ) ) ;
2578
- }
2579
- }
2580
- ( ConstValue :: Scalar ( scalar) , _) => {
2581
- let mut cx = FmtPrinter :: new ( tcx, Namespace :: ValueNS ) ;
2582
- cx. print_alloc_ids = true ;
2583
- let ty = tcx. lift ( ty) . unwrap ( ) ;
2584
- cx = cx. pretty_print_const_scalar ( scalar, ty) ?;
2585
- fmt. write_str ( & cx. into_buffer ( ) ) ?;
2586
- return Ok ( ( ) ) ;
2587
- }
2588
- ( ConstValue :: ZeroSized , ty:: FnDef ( d, s) ) => {
2589
- let mut cx = FmtPrinter :: new ( tcx, Namespace :: ValueNS ) ;
2590
- cx. print_alloc_ids = true ;
2591
- let cx = cx. print_value_path ( * d, s) ?;
2592
- fmt. write_str ( & cx. into_buffer ( ) ) ?;
2593
- return Ok ( ( ) ) ;
2594
- }
2595
- // FIXME(oli-obk): also pretty print arrays and other aggregate constants by reading
2596
- // their fields instead of just dumping the memory.
2597
- _ => { }
2598
- }
2599
- // Fall back to debug pretty printing for invalid constants.
2600
- write ! ( fmt, "{ct:?}: {ty}" )
2601
- } )
2602
- }
2603
-
2604
2435
/// `Location` represents the position of the start of the statement; or, if
2605
2436
/// `statement_index` equals the number of statements, then the start of the
2606
2437
/// terminator.
0 commit comments