Skip to content

Commit 0907476

Browse files
committed
Add pretty printer support for ConstValue without going through ty::Const
1 parent ee86f96 commit 0907476

File tree

7 files changed

+67
-1
lines changed

7 files changed

+67
-1
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use rustc_hir::def_id::DefId;
6565
use rustc_hir::lang_items::LangItem;
6666
use rustc_hir::{Item, ItemKind, Node};
6767
use rustc_middle::dep_graph::DepContext;
68+
use rustc_middle::mir::interpret::ConstValue;
6869
use rustc_middle::ty::error::TypeError;
6970
use rustc_middle::ty::{
7071
self,
@@ -524,6 +525,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
524525
Err(NonTrivialPath)
525526
}
526527

528+
fn print_const_value(
529+
self,
530+
_value: ConstValue<'tcx>,
531+
_ty: Ty<'tcx>,
532+
) -> Result<Self::Const, Self::Error> {
533+
Err(NonTrivialPath)
534+
}
535+
527536
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
528537
Ok(vec![self.tcx.crate_name(cnum).to_string()])
529538
}

compiler/rustc_lint/src/context.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
3131
use rustc_middle::lint::LintDiagnosticBuilder;
3232
use rustc_middle::middle::privacy::AccessLevels;
3333
use rustc_middle::middle::stability;
34+
use rustc_middle::mir::interpret::ConstValue;
3435
use rustc_middle::ty::layout::{LayoutError, TyAndLayout};
3536
use rustc_middle::ty::print::with_no_trimmed_paths;
3637
use rustc_middle::ty::{self, print::Printer, subst::GenericArg, Ty, TyCtxt};
@@ -943,6 +944,14 @@ impl<'tcx> LateContext<'tcx> {
943944
Ok(())
944945
}
945946

947+
fn print_const_value(
948+
self,
949+
_val: ConstValue<'tcx>,
950+
_ty: Ty<'tcx>,
951+
) -> Result<Self::Const, Self::Error> {
952+
Ok(())
953+
}
954+
946955
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
947956
Ok(vec![self.tcx.crate_name(cnum)])
948957
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::mir::interpret::ConstValue;
12
use crate::ty::subst::{GenericArg, Subst};
23
use crate::ty::{self, DefIdTree, Ty, TyCtxt};
34

@@ -68,6 +69,12 @@ pub trait Printer<'tcx>: Sized {
6869

6970
fn print_const(self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error>;
7071

72+
fn print_const_value(
73+
self,
74+
val: ConstValue<'tcx>,
75+
ty: Ty<'tcx>,
76+
) -> Result<Self::Const, Self::Error>;
77+
7178
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error>;
7279

7380
fn path_qualified(
@@ -362,3 +369,11 @@ impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for &'tcx ty::Const<'tcx> {
362369
cx.print_const(self)
363370
}
364371
}
372+
373+
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for (ConstValue<'tcx>, Ty<'tcx>) {
374+
type Output = P::Const;
375+
type Error = P::Error;
376+
fn print(&self, cx: P) -> Result<Self::Output, Self::Error> {
377+
cx.print_const_value(self.0, self.1)
378+
}
379+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,14 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
14581458
self.pretty_print_const(ct, true)
14591459
}
14601460

1461+
fn print_const_value(
1462+
self,
1463+
val: ConstValue<'tcx>,
1464+
ty: Ty<'tcx>,
1465+
) -> Result<Self::Const, Self::Error> {
1466+
self.pretty_print_const_value(val, ty, true)
1467+
}
1468+
14611469
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
14621470
self.empty_path = true;
14631471
if cnum == LOCAL_CRATE {

compiler/rustc_mir/src/interpret/intrinsics/type_name.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_hir::def_id::CrateNum;
22
use rustc_hir::definitions::DisambiguatedDefPathData;
3-
use rustc_middle::mir::interpret::Allocation;
3+
use rustc_middle::mir::interpret::{Allocation, ConstValue};
44
use rustc_middle::ty::{
55
self,
66
print::{PrettyPrinter, Print, Printer},
@@ -72,6 +72,14 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
7272
self.pretty_print_const(ct, false)
7373
}
7474

75+
fn print_const_value(
76+
self,
77+
val: ConstValue<'tcx>,
78+
ty: Ty<'tcx>,
79+
) -> Result<Self::Const, Self::Error> {
80+
self.pretty_print_const_value(val, ty, false)
81+
}
82+
7583
fn print_dyn_existential(
7684
mut self,
7785
predicates: &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,

compiler/rustc_symbol_mangling/src/legacy.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ impl Printer<'tcx> for SymbolPrinter<'tcx> {
253253
Ok(self)
254254
}
255255

256+
fn print_const_value(
257+
self,
258+
value: ConstValue<'tcx>,
259+
ty: Ty<'tcx>,
260+
) -> Result<Self::Const, Self::Error> {
261+
bug!("symbol mangling should never see MIR constants, but saw {:?}:{:?}", value, ty)
262+
}
263+
256264
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
257265
self.write_str(&self.tcx.crate_name(cnum).as_str())?;
258266
Ok(self)

compiler/rustc_symbol_mangling/src/v0.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
33
use rustc_hir as hir;
44
use rustc_hir::def_id::{CrateNum, DefId};
55
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
6+
use rustc_middle::mir::interpret::ConstValue;
67
use rustc_middle::ty::layout::IntegerExt;
78
use rustc_middle::ty::print::{Print, Printer};
89
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst};
@@ -590,6 +591,14 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
590591
Ok(self)
591592
}
592593

594+
fn print_const_value(
595+
self,
596+
value: ConstValue<'tcx>,
597+
ty: Ty<'tcx>,
598+
) -> Result<Self::Const, Self::Error> {
599+
bug!("symbol mangling should never see MIR constants, but saw {:?}:{:?}", value, ty)
600+
}
601+
593602
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
594603
self.push("C");
595604
let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();

0 commit comments

Comments
 (0)