Skip to content

Commit 359c36c

Browse files
William UtterWilliam Utter
authored andcommitted
added pretty_print_const_expr
1 parent aaef5fe commit 359c36c

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

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

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

0 commit comments

Comments
 (0)