@@ -6,7 +6,7 @@ use crate::mir::interpret::{AllocRange, ErrorHandled, ConstAllocation, ConstValu
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 > > ;
@@ -2459,176 +2460,6 @@ rustc_index::newtype_index! {
2459
2460
pub struct Promoted { }
2460
2461
}
2461
2462
2462
- fn pretty_print_const < ' tcx > (
2463
- c : ty:: Const < ' tcx > ,
2464
- fmt : & mut Formatter < ' _ > ,
2465
- print_types : bool ,
2466
- ) -> fmt:: Result {
2467
- use crate :: ty:: print:: PrettyPrinter ;
2468
- ty:: tls:: with ( |tcx| {
2469
- let literal = tcx. lift ( c) . unwrap ( ) ;
2470
- let mut cx = FmtPrinter :: new ( tcx, Namespace :: ValueNS ) ;
2471
- cx. print_alloc_ids = true ;
2472
- let cx = cx. pretty_print_const ( literal, print_types) ?;
2473
- fmt. write_str ( & cx. into_buffer ( ) ) ?;
2474
- Ok ( ( ) )
2475
- } )
2476
- }
2477
-
2478
- fn pretty_print_byte_str ( fmt : & mut Formatter < ' _ > , byte_str : & [ u8 ] ) -> fmt:: Result {
2479
- write ! ( fmt, "b\" {}\" " , byte_str. escape_ascii( ) )
2480
- }
2481
-
2482
- fn comma_sep < ' tcx > (
2483
- fmt : & mut Formatter < ' _ > ,
2484
- elems : Vec < ( ConstValue < ' tcx > , Ty < ' tcx > ) > ,
2485
- ) -> fmt:: Result {
2486
- let mut first = true ;
2487
- for ( ct, ty) in elems {
2488
- if !first {
2489
- fmt. write_str ( ", " ) ?;
2490
- }
2491
- pretty_print_const_value ( ct, ty, fmt) ?;
2492
- first = false ;
2493
- }
2494
- Ok ( ( ) )
2495
- }
2496
-
2497
- // FIXME: Move that into `mir/pretty.rs`.
2498
- fn pretty_print_const_value < ' tcx > (
2499
- ct : ConstValue < ' tcx > ,
2500
- ty : Ty < ' tcx > ,
2501
- fmt : & mut Formatter < ' _ > ,
2502
- ) -> fmt:: Result {
2503
- use crate :: ty:: print:: PrettyPrinter ;
2504
-
2505
- ty:: tls:: with ( |tcx| {
2506
- let ct = tcx. lift ( ct) . unwrap ( ) ;
2507
- let ty = tcx. lift ( ty) . unwrap ( ) ;
2508
-
2509
- if tcx. sess . verbose ( ) {
2510
- fmt. write_str ( & format ! ( "ConstValue({ct:?}: {ty})" ) ) ?;
2511
- return Ok ( ( ) ) ;
2512
- }
2513
-
2514
- let u8_type = tcx. types . u8 ;
2515
- match ( ct, ty. kind ( ) ) {
2516
- // Byte/string slices, printed as (byte) string literals.
2517
- ( _, ty:: Ref ( _, inner_ty, _) ) if matches ! ( inner_ty. kind( ) , ty:: Str ) => {
2518
- if let Some ( data) = ct. try_get_slice_bytes_for_diagnostics ( tcx) {
2519
- fmt. write_str ( & format ! ( "{:?}" , String :: from_utf8_lossy( data) ) ) ?;
2520
- return Ok ( ( ) ) ;
2521
- }
2522
- }
2523
- ( _, ty:: Ref ( _, inner_ty, _) ) if matches ! ( inner_ty. kind( ) , ty:: Slice ( t) if * t == u8_type) => {
2524
- if let Some ( data) = ct. try_get_slice_bytes_for_diagnostics ( tcx) {
2525
- pretty_print_byte_str ( fmt, data) ?;
2526
- return Ok ( ( ) ) ;
2527
- }
2528
- }
2529
- ( ConstValue :: Indirect { alloc_id, offset } , ty:: Array ( t, n) ) if * t == u8_type => {
2530
- let n = n. try_to_target_usize ( tcx) . unwrap ( ) ;
2531
- let alloc = tcx. global_alloc ( alloc_id) . unwrap_memory ( ) ;
2532
- // cast is ok because we already checked for pointer size (32 or 64 bit) above
2533
- let range = AllocRange { start : offset, size : Size :: from_bytes ( n) } ;
2534
- let byte_str = alloc. inner ( ) . get_bytes_strip_provenance ( & tcx, range) . unwrap ( ) ;
2535
- fmt. write_str ( "*" ) ?;
2536
- pretty_print_byte_str ( fmt, byte_str) ?;
2537
- return Ok ( ( ) ) ;
2538
- }
2539
- // Aggregates, printed as array/tuple/struct/variant construction syntax.
2540
- //
2541
- // NB: the `has_non_region_param` check ensures that we can use
2542
- // the `destructure_const` query with an empty `ty::ParamEnv` without
2543
- // introducing ICEs (e.g. via `layout_of`) from missing bounds.
2544
- // E.g. `transmute([0usize; 2]): (u8, *mut T)` needs to know `T: Sized`
2545
- // to be able to destructure the tuple into `(0u8, *mut T)`
2546
- ( _, ty:: Array ( ..) | ty:: Tuple ( ..) | ty:: Adt ( ..) ) if !ty. has_non_region_param ( ) => {
2547
- let ct = tcx. lift ( ct) . unwrap ( ) ;
2548
- let ty = tcx. lift ( ty) . unwrap ( ) ;
2549
- if let Some ( contents) = tcx. try_destructure_mir_constant_for_diagnostics ( ( ct, ty) ) {
2550
- let fields: Vec < ( ConstValue < ' _ > , Ty < ' _ > ) > = contents. fields . to_vec ( ) ;
2551
- match * ty. kind ( ) {
2552
- ty:: Array ( ..) => {
2553
- fmt. write_str ( "[" ) ?;
2554
- comma_sep ( fmt, fields) ?;
2555
- fmt. write_str ( "]" ) ?;
2556
- }
2557
- ty:: Tuple ( ..) => {
2558
- fmt. write_str ( "(" ) ?;
2559
- comma_sep ( fmt, fields) ?;
2560
- if contents. fields . len ( ) == 1 {
2561
- fmt. write_str ( "," ) ?;
2562
- }
2563
- fmt. write_str ( ")" ) ?;
2564
- }
2565
- ty:: Adt ( def, _) if def. variants ( ) . is_empty ( ) => {
2566
- fmt. write_str ( & format ! ( "{{unreachable(): {ty}}}" ) ) ?;
2567
- }
2568
- ty:: Adt ( def, args) => {
2569
- let variant_idx = contents
2570
- . variant
2571
- . expect ( "destructed mir constant of adt without variant idx" ) ;
2572
- let variant_def = & def. variant ( variant_idx) ;
2573
- let args = tcx. lift ( args) . unwrap ( ) ;
2574
- let mut cx = FmtPrinter :: new ( tcx, Namespace :: ValueNS ) ;
2575
- cx. print_alloc_ids = true ;
2576
- let cx = cx. print_value_path ( variant_def. def_id , args) ?;
2577
- fmt. write_str ( & cx. into_buffer ( ) ) ?;
2578
-
2579
- match variant_def. ctor_kind ( ) {
2580
- Some ( CtorKind :: Const ) => { }
2581
- Some ( CtorKind :: Fn ) => {
2582
- fmt. write_str ( "(" ) ?;
2583
- comma_sep ( fmt, fields) ?;
2584
- fmt. write_str ( ")" ) ?;
2585
- }
2586
- None => {
2587
- fmt. write_str ( " {{ " ) ?;
2588
- let mut first = true ;
2589
- for ( field_def, ( ct, ty) ) in
2590
- iter:: zip ( & variant_def. fields , fields)
2591
- {
2592
- if !first {
2593
- fmt. write_str ( ", " ) ?;
2594
- }
2595
- write ! ( fmt, "{}: " , field_def. name) ?;
2596
- pretty_print_const_value ( ct, ty, fmt) ?;
2597
- first = false ;
2598
- }
2599
- fmt. write_str ( " }}" ) ?;
2600
- }
2601
- }
2602
- }
2603
- _ => unreachable ! ( ) ,
2604
- }
2605
- return Ok ( ( ) ) ;
2606
- }
2607
- }
2608
- ( ConstValue :: Scalar ( scalar) , _) => {
2609
- let mut cx = FmtPrinter :: new ( tcx, Namespace :: ValueNS ) ;
2610
- cx. print_alloc_ids = true ;
2611
- let ty = tcx. lift ( ty) . unwrap ( ) ;
2612
- cx = cx. pretty_print_const_scalar ( scalar, ty) ?;
2613
- fmt. write_str ( & cx. into_buffer ( ) ) ?;
2614
- return Ok ( ( ) ) ;
2615
- }
2616
- ( ConstValue :: ZeroSized , ty:: FnDef ( d, s) ) => {
2617
- let mut cx = FmtPrinter :: new ( tcx, Namespace :: ValueNS ) ;
2618
- cx. print_alloc_ids = true ;
2619
- let cx = cx. print_value_path ( * d, s) ?;
2620
- fmt. write_str ( & cx. into_buffer ( ) ) ?;
2621
- return Ok ( ( ) ) ;
2622
- }
2623
- // FIXME(oli-obk): also pretty print arrays and other aggregate constants by reading
2624
- // their fields instead of just dumping the memory.
2625
- _ => { }
2626
- }
2627
- // Fall back to debug pretty printing for invalid constants.
2628
- write ! ( fmt, "{ct:?}: {ty}" )
2629
- } )
2630
- }
2631
-
2632
2463
/// `Location` represents the position of the start of the statement; or, if
2633
2464
/// `statement_index` equals the number of statements, then the start of the
2634
2465
/// terminator.
0 commit comments