Skip to content

Commit ac8c8d2

Browse files
William UtterWilliam Utter
William Utter
authored and
William Utter
committed
added pretty_print_const_expr
formatting
1 parent aaef5fe commit ac8c8d2

File tree

4 files changed

+79
-8
lines changed

4 files changed

+79
-8
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::query::Providers;
44
use crate::traits::util::supertraits_for_pretty_printing;
55
use crate::ty::GenericArgKind;
66
use crate::ty::{
7-
ConstInt, ParamConst, ScalarInt, Term, TermKind, TypeFoldable, TypeSuperFoldable,
7+
ConstInt, Expr, ParamConst, ScalarInt, Term, TermKind, TypeFoldable, TypeSuperFoldable,
88
TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
99
};
1010
use rustc_apfloat::ieee::{Double, Single};
@@ -1382,12 +1382,83 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
13821382
ty::ConstKind::Placeholder(placeholder) => p!(write("{placeholder:?}")),
13831383
// FIXME(generic_const_exprs):
13841384
// write out some legible representation of an abstract const?
1385-
ty::ConstKind::Expr(_) => p!("{{const expr}}"),
1385+
ty::ConstKind::Expr(expr) => self.pretty_print_const_expr(expr, print_ty)?,
13861386
ty::ConstKind::Error(_) => p!("{{const error}}"),
13871387
};
13881388
Ok(())
13891389
}
13901390

1391+
fn pretty_print_const_expr(
1392+
&mut self,
1393+
expr: Expr<'tcx>,
1394+
print_ty: bool,
1395+
) -> Result<(), PrintError> {
1396+
define_scoped_cx!(self);
1397+
match expr {
1398+
Expr::Binop(op, c1, c2) => {
1399+
let formatted_op = op.to_hir_binop().as_str();
1400+
p!(print(c1), write(" {formatted_op} "), print(c2));
1401+
}
1402+
Expr::UnOp(op, ct) => {
1403+
use rustc_middle::mir::UnOp;
1404+
let formatted_op = match op {
1405+
UnOp::Not => "!",
1406+
UnOp::Neg => "-",
1407+
};
1408+
p!(write("{formatted_op}"), print(ct));
1409+
}
1410+
Expr::FunctionCall(fn_def, fn_args) => {
1411+
use ty::TyKind;
1412+
match fn_def.ty().kind() {
1413+
TyKind::FnDef(def_id, gen_args) => {
1414+
p!(print_value_path(*def_id, gen_args), "(");
1415+
if print_ty {
1416+
let tcx = self.tcx();
1417+
let sig = tcx.fn_sig(def_id).instantiate(tcx, gen_args).skip_binder();
1418+
1419+
let mut args_with_ty = fn_args.iter().map(|ct| (ct, ct.ty()));
1420+
let output_ty = sig.output();
1421+
1422+
if let Some((ct, ty)) = args_with_ty.next() {
1423+
self.typed_value(
1424+
|this| this.pretty_print_const(ct, print_ty),
1425+
|this| this.pretty_print_type(ty),
1426+
": ",
1427+
)?;
1428+
for (ct, ty) in args_with_ty {
1429+
self.typed_value(
1430+
|this| this.pretty_print_const(ct, print_ty),
1431+
|this| this.pretty_print_type(ty),
1432+
": ",
1433+
)?;
1434+
}
1435+
}
1436+
p!(write(") -> {output_ty}"));
1437+
} else {
1438+
p!(comma_sep(fn_args.iter()), ")");
1439+
}
1440+
}
1441+
_ => bug!("unexpected type of fn def"),
1442+
}
1443+
}
1444+
Expr::Cast(kind, ct, ty) => {
1445+
use ty::abstract_const::CastKind;
1446+
match kind {
1447+
CastKind::As => {
1448+
self.typed_value(
1449+
|this| this.pretty_print_const(ct, print_ty),
1450+
|this| this.pretty_print_type(ty),
1451+
"as",
1452+
)?;
1453+
}
1454+
// FIXME?: some implicit coercions might be useful to print
1455+
CastKind::Use => self.pretty_print_const(ct, print_ty)?,
1456+
}
1457+
}
1458+
}
1459+
Ok(())
1460+
}
1461+
13911462
fn pretty_print_const_scalar(
13921463
&mut self,
13931464
scalar: Scalar,

tests/ui/const-generics/generic_const_exprs/const_kind_expr/issue_114151.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ where
1919
//~^^ ERROR: unconstrained generic constant
2020
//~^^^ ERROR: function takes 1 generic argument but 2 generic arguments were supplied
2121
//~^^^^ ERROR: unconstrained generic constant
22-
//~^^^^^ ERROR: unconstrained generic constant `{const expr}`
22+
//~^^^^^ ERROR: unconstrained generic constant `L + 1 + L`
2323
}
2424

2525
fn main() {}

tests/ui/const-generics/generic_const_exprs/const_kind_expr/issue_114151.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ LL | | }
5252
LL | | }],
5353
| |_____^ required by this bound in `foo`
5454

55-
error: unconstrained generic constant `{const expr}`
55+
error: unconstrained generic constant `L + 1 + L`
5656
--> $DIR/issue_114151.rs:17:5
5757
|
5858
LL | foo::<_, L>([(); L + 1 + L]);

tests/ui/const-generics/transmute-fail.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
44
LL | std::mem::transmute(v)
55
| ^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: source type: `[[u32; H+1]; W]` (generic size {const expr})
8-
= note: target type: `[[u32; W+1]; H]` (generic size {const expr})
7+
= note: source type: `[[u32; H+1]; W]` (generic size H + 1 * 4 * W)
8+
= note: target type: `[[u32; W+1]; H]` (generic size W + 1 * 4 * H)
99

1010
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
1111
--> $DIR/transmute-fail.rs:16:5
@@ -22,8 +22,8 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
2222
LL | std::mem::transmute(v)
2323
| ^^^^^^^^^^^^^^^^^^^
2424
|
25-
= note: source type: `[[u32; H]; W]` (generic size {const expr})
26-
= note: target type: `[u32; W * H * H]` (generic size {const expr})
25+
= note: source type: `[[u32; H]; W]` (generic size 4 * H * W)
26+
= note: target type: `[u32; W * H * H]` (generic size 4 * H * H * W)
2727

2828
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2929
--> $DIR/transmute-fail.rs:30:5

0 commit comments

Comments
 (0)