Skip to content

Commit 4b4f6db

Browse files
committed
Add pretty printer support for ConstValue without going through ty::Const
1 parent 4830527 commit 4b4f6db

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
@@ -64,6 +64,7 @@ use rustc_hir as hir;
6464
use rustc_hir::def_id::DefId;
6565
use rustc_hir::lang_items::LangItem;
6666
use rustc_hir::{Item, ItemKind, Node};
67+
use rustc_middle::mir::interpret::ConstValue;
6768
use rustc_middle::ty::error::TypeError;
6869
use rustc_middle::ty::{
6970
self,
@@ -523,6 +524,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
523524
Err(NonTrivialPath)
524525
}
525526

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

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};
@@ -915,6 +916,14 @@ impl<'tcx> LateContext<'tcx> {
915916
Ok(())
916917
}
917918

919+
fn print_const_value(
920+
self,
921+
_val: ConstValue<'tcx>,
922+
_ty: Ty<'tcx>,
923+
) -> Result<Self::Const, Self::Error> {
924+
Ok(())
925+
}
926+
918927
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
919928
Ok(vec![self.tcx.original_crate_name(cnum)])
920929
}

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
@@ -1441,6 +1441,14 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
14411441
self.pretty_print_const(ct, true)
14421442
}
14431443

1444+
fn print_const_value(
1445+
self,
1446+
val: ConstValue<'tcx>,
1447+
ty: Ty<'tcx>,
1448+
) -> Result<Self::Const, Self::Error> {
1449+
self.pretty_print_const_value(val, ty, true)
1450+
}
1451+
14441452
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
14451453
self.empty_path = true;
14461454
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::{DefPathData, 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<ty::ExistentialPredicate<'tcx>>>,

compiler/rustc_symbol_mangling/src/legacy.rs

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

257+
fn print_const_value(
258+
self,
259+
value: ConstValue<'tcx>,
260+
ty: Ty<'tcx>,
261+
) -> Result<Self::Const, Self::Error> {
262+
bug!("symbol mangling should never see MIR constants, but saw {:?}:{:?}", value, ty)
263+
}
264+
257265
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
258266
self.write_str(&self.tcx.original_crate_name(cnum).as_str())?;
259267
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::print::{Print, Printer};
78
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst};
89
use rustc_middle::ty::{self, FloatTy, Instance, IntTy, Ty, TyCtxt, TypeFoldable, UintTy};
@@ -559,6 +560,14 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
559560
Ok(self)
560561
}
561562

563+
fn print_const_value(
564+
self,
565+
value: ConstValue<'tcx>,
566+
ty: Ty<'tcx>,
567+
) -> Result<Self::Const, Self::Error> {
568+
bug!("symbol mangling should never see MIR constants, but saw {:?}:{:?}", value, ty)
569+
}
570+
562571
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
563572
self.push("C");
564573
let fingerprint = self.tcx.crate_disambiguator(cnum).to_fingerprint();

0 commit comments

Comments
 (0)