Skip to content

Commit dd67187

Browse files
committed
Auto merge of rust-lang#67133 - oli-obk:it_must_be_a_sign, r=eddyb
Deduplicate pretty printing of constants r? @eddyb for the pretty printing logic cc @RalfJung
2 parents 59f4ba9 + 6ca65bd commit dd67187

15 files changed

+379
-189
lines changed

src/librustc/mir/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2562,15 +2562,15 @@ impl<'tcx> Debug for Constant<'tcx> {
25622562

25632563
impl<'tcx> Display for Constant<'tcx> {
25642564
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2565+
use crate::ty::print::PrettyPrinter;
25652566
write!(fmt, "const ")?;
2566-
// FIXME make the default pretty printing of raw pointers more detailed. Here we output the
2567-
// debug representation of raw pointers, so that the raw pointers in the mir dump output are
2568-
// detailed and just not '{pointer}'.
2569-
if let ty::RawPtr(_) = self.literal.ty.kind {
2570-
write!(fmt, "{:?} : {}", self.literal.val, self.literal.ty)
2571-
} else {
2572-
write!(fmt, "{}", self.literal)
2573-
}
2567+
ty::tls::with(|tcx| {
2568+
let literal = tcx.lift(&self.literal).unwrap();
2569+
let mut cx = FmtPrinter::new(tcx, fmt, Namespace::ValueNS);
2570+
cx.print_alloc_ids = true;
2571+
cx.pretty_print_const(literal, true)?;
2572+
Ok(())
2573+
})
25742574
}
25752575
}
25762576

src/librustc/ty/print/pretty.rs

+295-105
Large diffs are not rendered by default.

src/librustc_mir/interpret/intrinsics/type_name.rs

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
157157
}
158158
}
159159
}
160+
160161
impl PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
161162
fn region_should_not_be_omitted(&self, _region: ty::Region<'_>) -> bool {
162163
false

src/librustc_mir/interpret/operand.rs

+41-42
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
44
use std::convert::{TryFrom, TryInto};
55

6-
use rustc::ty::layout::{
7-
self, HasDataLayout, IntegerExt, LayoutOf, PrimitiveExt, Size, TyLayout, VariantIdx,
8-
};
9-
use rustc::{mir, ty};
10-
116
use super::{InterpCx, MPlaceTy, Machine, MemPlace, Place, PlaceTy};
127
pub use rustc::mir::interpret::ScalarMaybeUndef;
138
use rustc::mir::interpret::{
149
sign_extend, truncate, AllocId, ConstValue, GlobalId, InterpResult, Pointer, Scalar,
1510
};
16-
use rustc_ast::ast;
11+
use rustc::ty::layout::{
12+
self, HasDataLayout, IntegerExt, LayoutOf, PrimitiveExt, Size, TyLayout, VariantIdx,
13+
};
14+
use rustc::ty::print::{FmtPrinter, PrettyPrinter, Printer};
15+
use rustc::ty::Ty;
16+
use rustc::{mir, ty};
17+
use rustc_hir::def::Namespace;
1718
use rustc_macros::HashStable;
19+
use std::fmt::Write;
1820

1921
/// An `Immediate` represents a single immediate self-contained Rust value.
2022
///
@@ -92,47 +94,44 @@ pub struct ImmTy<'tcx, Tag = ()> {
9294
pub layout: TyLayout<'tcx>,
9395
}
9496

95-
// `Tag: Copy` because some methods on `Scalar` consume them by value
9697
impl<Tag: Copy> std::fmt::Display for ImmTy<'tcx, Tag> {
97-
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
98-
match &self.imm {
99-
// We cannot use `to_bits_or_ptr` as we do not have a `tcx`.
100-
// So we use `is_bits` and circumvent a bunch of sanity checking -- but
101-
// this is anyway only for printing.
102-
Immediate::Scalar(ScalarMaybeUndef::Scalar(s)) if s.is_ptr() => {
103-
fmt.write_str("{pointer}")
98+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99+
/// Helper function for printing a scalar to a FmtPrinter
100+
fn p<'a, 'tcx, F: std::fmt::Write, Tag>(
101+
cx: FmtPrinter<'a, 'tcx, F>,
102+
s: ScalarMaybeUndef<Tag>,
103+
ty: Ty<'tcx>,
104+
) -> Result<FmtPrinter<'a, 'tcx, F>, std::fmt::Error> {
105+
match s {
106+
ScalarMaybeUndef::Scalar(s) => {
107+
cx.pretty_print_const_scalar(s.erase_tag(), ty, true)
108+
}
109+
ScalarMaybeUndef::Undef => cx.typed_value(
110+
|mut this| {
111+
this.write_str("{undef ")?;
112+
Ok(this)
113+
},
114+
|this| this.print_type(ty),
115+
" ",
116+
),
104117
}
105-
Immediate::Scalar(ScalarMaybeUndef::Scalar(s)) => {
106-
let s = s.assert_bits(self.layout.size);
107-
match self.layout.ty.kind {
108-
ty::Int(_) => {
109-
return write!(fmt, "{}", super::sign_extend(s, self.layout.size) as i128,);
110-
}
111-
ty::Uint(_) => return write!(fmt, "{}", s),
112-
ty::Bool if s == 0 => return fmt.write_str("false"),
113-
ty::Bool if s == 1 => return fmt.write_str("true"),
114-
ty::Char => {
115-
if let Some(c) = u32::try_from(s).ok().and_then(std::char::from_u32) {
116-
return write!(fmt, "{}", c);
117-
}
118-
}
119-
ty::Float(ast::FloatTy::F32) => {
120-
if let Ok(u) = u32::try_from(s) {
121-
return write!(fmt, "{}", f32::from_bits(u));
122-
}
123-
}
124-
ty::Float(ast::FloatTy::F64) => {
125-
if let Ok(u) = u64::try_from(s) {
126-
return write!(fmt, "{}", f64::from_bits(u));
127-
}
118+
}
119+
ty::tls::with(|tcx| {
120+
match self.imm {
121+
Immediate::Scalar(s) => {
122+
if let Some(ty) = tcx.lift(&self.layout.ty) {
123+
let cx = FmtPrinter::new(tcx, f, Namespace::ValueNS);
124+
p(cx, s, ty)?;
125+
return Ok(());
128126
}
129-
_ => {}
127+
write!(f, "{:?}: {}", s.erase_tag(), self.layout.ty)
128+
}
129+
Immediate::ScalarPair(a, b) => {
130+
// FIXME(oli-obk): at least print tuples and slices nicely
131+
write!(f, "({:?}, {:?}): {}", a.erase_tag(), b.erase_tag(), self.layout.ty,)
130132
}
131-
write!(fmt, "{:x}", s)
132133
}
133-
Immediate::Scalar(ScalarMaybeUndef::Undef) => fmt.write_str("{undef}"),
134-
Immediate::ScalarPair(..) => fmt.write_str("{wide pointer or tuple}"),
135-
}
134+
})
136135
}
137136
}
138137

src/test/mir-opt/const-promotion-extern-static.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {}
1414
// START rustc.FOO.PromoteTemps.before.mir
1515
// bb0: {
1616
// ...
17-
// _5 = const Scalar(alloc1+0) : &i32;
17+
// _5 = const {alloc1+0: &i32};
1818
// _4 = &(*_5);
1919
// _3 = [move _4];
2020
// _2 = &_3;
@@ -31,7 +31,7 @@ fn main() {}
3131
// START rustc.BAR.PromoteTemps.before.mir
3232
// bb0: {
3333
// ...
34-
// _5 = const Scalar(alloc0+0) : &i32;
34+
// _5 = const {alloc0+0: &i32};
3535
// _4 = &(*_5);
3636
// _3 = [move _4];
3737
// _2 = &_3;

src/test/mir-opt/const_prop/discriminant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn main() {
3131
// START rustc.main.ConstProp.after.mir
3232
// bb0: {
3333
// ...
34-
// _3 = const Scalar(0x01) : std::option::Option<bool>;
34+
// _3 = const {transmute(0x01): std::option::Option<bool>};
3535
// _4 = const 1isize;
3636
// switchInt(const 1isize) -> [1isize: bb2, otherwise: bb1];
3737
// }

src/test/mir-opt/const_prop/issue-66971.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn main() {
2929
// START rustc.main.ConstProp.after.mir
3030
// bb0: {
3131
// ...
32-
// _3 = const Scalar(<ZST>) : ();
32+
// _3 = const ();
3333
// _2 = (move _3, const 0u8, const 0u8);
3434
// ...
3535
// _1 = const encode(move _2) -> bb1;

src/test/mir-opt/const_prop/read_immutable_static.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ fn main() {
1010
// START rustc.main.ConstProp.before.mir
1111
// bb0: {
1212
// ...
13-
// _3 = const Scalar(alloc0+0) : &u8;
13+
// _3 = const {alloc0+0: &u8};
1414
// _2 = (*_3);
1515
// ...
16-
// _5 = const Scalar(alloc0+0) : &u8;
16+
// _5 = const {alloc0+0: &u8};
1717
// _4 = (*_5);
1818
// _1 = Add(move _2, move _4);
1919
// ...

src/test/mir-opt/simplify-locals-removes-unused-consts.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// compile-flags: -C overflow-checks=no
22

3-
fn use_zst(_: ((), ())) { }
3+
fn use_zst(_: ((), ())) {}
44

55
struct Temp {
6-
x: u8
6+
x: u8,
77
}
88

9-
fn use_u8(_: u8) { }
9+
fn use_u8(_: u8) {}
1010

1111
fn main() {
1212
let ((), ()) = ((), ());
1313
use_zst(((), ()));
1414

15-
use_u8((Temp { x : 40 }).x + 2);
15+
use_u8((Temp { x: 40 }).x + 2);
1616
}
1717

1818
// END RUST SOURCE
@@ -35,28 +35,28 @@ fn main() {
3535
// bb0: {
3636
// StorageLive(_1);
3737
// StorageLive(_2);
38-
// _2 = const Scalar(<ZST>) : ();
38+
// _2 = const ();
3939
// StorageLive(_3);
40-
// _3 = const Scalar(<ZST>) : ();
41-
// _1 = const Scalar(<ZST>) : ((), ());
40+
// _3 = const ();
41+
// _1 = const {transmute(()): ((), ())};
4242
// StorageDead(_3);
4343
// StorageDead(_2);
4444
// StorageDead(_1);
4545
// StorageLive(_4);
4646
// StorageLive(_6);
47-
// _6 = const Scalar(<ZST>) : ();
47+
// _6 = const ();
4848
// StorageLive(_7);
49-
// _7 = const Scalar(<ZST>) : ();
49+
// _7 = const ();
5050
// StorageDead(_7);
5151
// StorageDead(_6);
52-
// _4 = const use_zst(const Scalar(<ZST>) : ((), ())) -> bb1;
52+
// _4 = const use_zst(const {transmute(()): ((), ())}) -> bb1;
5353
// }
5454
// bb1: {
5555
// StorageDead(_4);
5656
// StorageLive(_8);
5757
// StorageLive(_10);
5858
// StorageLive(_11);
59-
// _11 = const Scalar(0x28) : Temp;
59+
// _11 = const {transmute(0x28) : Temp};
6060
// _10 = const 40u8;
6161
// StorageDead(_10);
6262
// _8 = const use_u8(const 42u8) -> bb2;
@@ -75,7 +75,7 @@ fn main() {
7575
// }
7676
// bb0: {
7777
// StorageLive(_1);
78-
// _1 = const use_zst(const Scalar(<ZST>) : ((), ())) -> bb1;
78+
// _1 = const use_zst(const {transmute(()): ((), ())}) -> bb1;
7979
// }
8080
// bb1: {
8181
// StorageDead(_1);

src/test/ui/const-generics/cannot-infer-const-args.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0282]: type annotations needed
1010
--> $DIR/cannot-infer-const-args.rs:9:5
1111
|
1212
LL | foo();
13-
| ^^^ cannot infer type for fn item `fn() -> usize {foo::<_: usize>}`
13+
| ^^^ cannot infer type for fn item `fn() -> usize {foo::<{_: usize}>}`
1414

1515
error: aborting due to previous error
1616

src/test/ui/const-generics/const-generic-type_name.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
struct S<const N: usize>;
88

99
fn main() {
10-
assert_eq!(std::any::type_name::<S<3>>(), "const_generic_type_name::S<3usize>");
10+
assert_eq!(std::any::type_name::<S<3>>(), "const_generic_type_name::S<3>");
1111
}

src/test/ui/const-generics/fn-const-param-infer.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ error[E0308]: mismatched types
1010
--> $DIR/fn-const-param-infer.rs:16:31
1111
|
1212
LL | let _: Checked<not_one> = Checked::<not_two>;
13-
| ---------------- ^^^^^^^^^^^^^^^^^^ expected `not_one`, found `not_two`
13+
| ---------------- ^^^^^^^^^^^^^^^^^^ expected `{not_one as fn(usize) -> bool}`, found `{not_two as fn(usize) -> bool}`
1414
| |
1515
| expected due to this
1616
|
17-
= note: expected struct `Checked<not_one>`
18-
found struct `Checked<not_two>`
17+
= note: expected struct `Checked<{not_one as fn(usize) -> bool}>`
18+
found struct `Checked<{not_two as fn(usize) -> bool}>`
1919

2020
error[E0308]: mismatched types
2121
--> $DIR/fn-const-param-infer.rs:20:24
@@ -36,12 +36,12 @@ error[E0308]: mismatched types
3636
--> $DIR/fn-const-param-infer.rs:25:40
3737
|
3838
LL | let _: Checked<{generic::<u32>}> = Checked::<{generic::<u16>}>;
39-
| ------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `generic::<u32>`, found `generic::<u16>`
39+
| ------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{generic::<u32> as fn(usize) -> bool}`, found `{generic::<u16> as fn(usize) -> bool}`
4040
| |
4141
| expected due to this
4242
|
43-
= note: expected struct `Checked<generic::<u32>>`
44-
found struct `Checked<generic::<u16>>`
43+
= note: expected struct `Checked<{generic::<u32> as fn(usize) -> bool}>`
44+
found struct `Checked<{generic::<u16> as fn(usize) -> bool}>`
4545

4646
error: aborting due to 4 previous errors
4747

src/test/ui/const-generics/raw-ptr-const-param.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
struct Const<const P: *const u32>;
55

66
fn main() {
7-
let _: Const<{15 as *const _}> = Const::<{10 as *const _}>; //~ mismatched types
8-
let _: Const<{10 as *const _}> = Const::<{10 as *const _}>;
7+
let _: Const<{ 15 as *const _ }> = Const::<{ 10 as *const _ }>; //~ mismatched types
8+
let _: Const<{ 10 as *const _ }> = Const::<{ 10 as *const _ }>;
99
}

src/test/ui/const-generics/raw-ptr-const-param.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ LL | #![feature(const_generics, const_compare_raw_pointers)]
77
= note: `#[warn(incomplete_features)]` on by default
88

99
error[E0308]: mismatched types
10-
--> $DIR/raw-ptr-const-param.rs:7:38
10+
--> $DIR/raw-ptr-const-param.rs:7:40
1111
|
12-
LL | let _: Const<{15 as *const _}> = Const::<{10 as *const _}>;
13-
| ----------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{pointer}`, found `{pointer}`
12+
LL | let _: Const<{ 15 as *const _ }> = Const::<{ 10 as *const _ }>;
13+
| ------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{0xf as *const u32}`, found `{0xa as *const u32}`
1414
| |
1515
| expected due to this
1616
|
17-
= note: expected struct `Const<{pointer}>`
18-
found struct `Const<{pointer}>`
17+
= note: expected struct `Const<{0xf as *const u32}>`
18+
found struct `Const<{0xa as *const u32}>`
1919

2020
error: aborting due to previous error
2121

src/test/ui/consts/offset_from_ub.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ error: any use of this value will cause an error
4343
LL | intrinsics::ptr_offset_from(self, origin)
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4545
| |
46-
| exact_div: 1 cannot be divided by 2 without remainder
46+
| exact_div: 1isize cannot be divided by 2isize without remainder
4747
| inside call to `std::ptr::const_ptr::<impl *const u16>::offset_from` at $DIR/offset_from_ub.rs:36:14
4848
|
4949
::: $DIR/offset_from_ub.rs:31:1

0 commit comments

Comments
 (0)