Skip to content

Commit 8293ca6

Browse files
committed
misc improvements
1 parent f1bbfaf commit 8293ca6

File tree

10 files changed

+166
-83
lines changed

10 files changed

+166
-83
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,14 @@ impl Mutability {
822822
}
823823
}
824824

825+
/// Returns `*const ` or `*mut ` depending on the mutability
826+
pub fn ptr_prefix_str(self) -> &'static str {
827+
match self {
828+
Mutability::Not => "*const",
829+
Mutability::Mut => "*mut",
830+
}
831+
}
832+
825833
/// Returns `""` (empty string) or `"mutably "` depending on the mutability.
826834
pub fn mutably_str(self) -> &'static str {
827835
match self {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
9595
type Region = Region<'tcx>;
9696
type Predicate = Predicate<'tcx>;
9797
type RawPtrTy = RawPtr<'tcx>;
98-
type Mutability = hir::Mutability;
9998
type Movability = hir::Movability;
10099
type PolyFnSig = PolyFnSig<'tcx>;
101100
type ListBinderExistentialPredicate = &'tcx List<PolyExistentialPredicate<'tcx>>;

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
14961496
/// identified by both a universe, as well as a name residing within that universe. Distinct bound
14971497
/// regions/types/consts within the same universe simply have an unknown relationship to one
14981498
/// another.
1499-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
1499+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
15001500
#[derive(HashStable, TyEncodable, TyDecodable)]
15011501
pub struct Placeholder<T> {
15021502
pub universe: UniverseIndex,

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

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -679,29 +679,30 @@ pub trait PrettyPrinter<'tcx>:
679679
}
680680
ty::FnPtr(ref bare_fn) => p!(print(bare_fn)),
681681
ty::Infer(infer_ty) => {
682-
let verbose = self.should_print_verbose();
682+
if self.should_print_verbose() {
683+
p!(write("{:?}", ty.kind()));
684+
return Ok(self);
685+
}
686+
683687
if let ty::TyVar(ty_vid) = infer_ty {
684688
if let Some(name) = self.ty_infer_name(ty_vid) {
685689
p!(write("{}", name))
686690
} else {
687-
if verbose {
688-
p!(write("{:?}", infer_ty))
689-
} else {
690-
p!(write("{}", infer_ty))
691-
}
691+
p!(write("{}", infer_ty))
692692
}
693693
} else {
694-
if verbose { p!(write("{:?}", infer_ty)) } else { p!(write("{}", infer_ty)) }
694+
p!(write("{}", infer_ty))
695695
}
696696
}
697697
ty::Error(_) => p!("[type error]"),
698698
ty::Param(ref param_ty) => p!(print(param_ty)),
699699
ty::Bound(debruijn, bound_ty) => match bound_ty.kind {
700-
ty::BoundTyKind::Anon => debug_bound_var(&mut self, debruijn, bound_ty.var)?,
700+
ty::BoundTyKind::Anon => {
701+
rustc_type_ir::debug_bound_var(&mut self, debruijn, bound_ty.var)?
702+
}
701703
ty::BoundTyKind::Param(_, s) => match self.should_print_verbose() {
702-
true if debruijn == ty::INNERMOST => p!(write("^{}", s)),
703-
true => p!(write("^{}_{}", debruijn.index(), s)),
704-
false => p!(write("{}", s)),
704+
true => p!(write("{:?}", ty.kind())),
705+
false => p!(write("{s}")),
705706
},
706707
},
707708
ty::Adt(def, substs) => {
@@ -734,10 +735,11 @@ pub trait PrettyPrinter<'tcx>:
734735
}
735736
}
736737
ty::Placeholder(placeholder) => match placeholder.bound.kind {
737-
ty::BoundTyKind::Anon => {
738-
debug_placeholder_var(&mut self, placeholder.universe, placeholder.bound.var)?;
739-
}
740-
ty::BoundTyKind::Param(_, name) => p!(write("{}", name)),
738+
ty::BoundTyKind::Anon => p!(write("{placeholder:?}")),
739+
ty::BoundTyKind::Param(_, name) => match self.should_print_verbose() {
740+
true => p!(write("{:?}", ty.kind())),
741+
false => p!(write("{name}")),
742+
},
741743
},
742744
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => {
743745
// We use verbose printing in 'NO_QUERIES' mode, to
@@ -1366,11 +1368,9 @@ pub trait PrettyPrinter<'tcx>:
13661368
}
13671369

13681370
ty::ConstKind::Bound(debruijn, bound_var) => {
1369-
debug_bound_var(&mut self, debruijn, bound_var)?
1371+
rustc_type_ir::debug_bound_var(&mut self, debruijn, bound_var)?
13701372
}
1371-
ty::ConstKind::Placeholder(placeholder) => {
1372-
debug_placeholder_var(&mut self, placeholder.universe, placeholder.bound)?;
1373-
},
1373+
ty::ConstKind::Placeholder(placeholder) => p!(write("{placeholder:?}")),
13741374
// FIXME(generic_const_exprs):
13751375
// write out some legible representation of an abstract const?
13761376
ty::ConstKind::Expr(_) => p!("[const expr]"),
@@ -3059,27 +3059,3 @@ pub struct OpaqueFnEntry<'tcx> {
30593059
fn_trait_ref: Option<ty::PolyTraitRef<'tcx>>,
30603060
return_ty: Option<ty::Binder<'tcx, Term<'tcx>>>,
30613061
}
3062-
3063-
pub fn debug_bound_var<T: std::fmt::Write>(
3064-
fmt: &mut T,
3065-
debruijn: ty::DebruijnIndex,
3066-
var: ty::BoundVar,
3067-
) -> Result<(), std::fmt::Error> {
3068-
if debruijn == ty::INNERMOST {
3069-
write!(fmt, "^{}", var.index())
3070-
} else {
3071-
write!(fmt, "^{}_{}", debruijn.index(), var.index())
3072-
}
3073-
}
3074-
3075-
pub fn debug_placeholder_var<T: std::fmt::Write>(
3076-
fmt: &mut T,
3077-
universe: ty::UniverseIndex,
3078-
bound: ty::BoundVar,
3079-
) -> Result<(), std::fmt::Error> {
3080-
if universe == ty::UniverseIndex::ROOT {
3081-
write!(fmt, "!{}", bound.index())
3082-
} else {
3083-
write!(fmt, "!{}_{}", universe.index(), bound.index())
3084-
}
3085-
}

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,33 @@ impl fmt::Debug for ty::FreeRegion {
8888

8989
impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
9090
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91-
write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
91+
let ty::FnSig { inputs_and_output: _, c_variadic, unsafety, abi } = self;
92+
93+
write!(f, "{}", unsafety.prefix_str())?;
94+
match abi {
95+
rustc_target::spec::abi::Abi::Rust => (),
96+
abi => write!(f, "extern \"{abi:?}\" ")?,
97+
};
98+
if *c_variadic {
99+
write!(f, "c_variadic ")?;
100+
};
101+
102+
write!(f, "fn(")?;
103+
let inputs = self.inputs();
104+
match inputs.len() {
105+
0 => write!(f, ")")?,
106+
_ => {
107+
for ty in &self.inputs()[0..(self.inputs().len() - 1)] {
108+
write!(f, "{ty:?}, ")?;
109+
}
110+
write!(f, "{:?})", self.inputs().last().unwrap())?;
111+
}
112+
}
113+
114+
match self.output().kind() {
115+
ty::Tuple(list) if list.is_empty() => Ok(()),
116+
_ => write!(f, " -> {:?}", self.output()),
117+
}
92118
}
93119
}
94120

@@ -216,20 +242,43 @@ impl<'tcx> fmt::Debug for ty::ConstKind<'tcx> {
216242
match self {
217243
Param(param) => write!(f, "{param:?}"),
218244
Infer(var) => write!(f, "{var:?}"),
219-
Bound(debruijn, var) => ty::print::debug_bound_var(f, *debruijn, *var),
220-
Placeholder(placeholder) => {
221-
ty::print::debug_placeholder_var(f, placeholder.universe, placeholder.bound)
222-
}
245+
Bound(debruijn, var) => rustc_type_ir::debug_bound_var(f, *debruijn, *var),
246+
Placeholder(placeholder) => write!(f, "{placeholder:?}"),
223247
Unevaluated(uv) => {
224248
f.debug_tuple("Unevaluated").field(&uv.substs).field(&uv.def).finish()
225249
}
226250
Value(valtree) => write!(f, "{valtree:?}"),
227-
Error(_) => write!(f, "[const error]"),
251+
Error(_) => write!(f, "{{const error}}"),
228252
Expr(expr) => write!(f, "{expr:?}"),
229253
}
230254
}
231255
}
232256

257+
impl<'tcx> fmt::Debug for ty::RawPtr<'tcx> {
258+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
259+
write!(f, "{} {:?}", self.mutbl.ptr_prefix_str(), self.ty)
260+
}
261+
}
262+
263+
impl fmt::Debug for ty::BoundTy {
264+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
265+
match self.kind {
266+
ty::BoundTyKind::Anon => write!(f, "{:?}", self.var),
267+
ty::BoundTyKind::Param(_, sym) => write!(f, "{sym:?}"),
268+
}
269+
}
270+
}
271+
272+
impl<T: fmt::Debug> fmt::Debug for ty::Placeholder<T> {
273+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
274+
if self.universe == ty::UniverseIndex::ROOT {
275+
write!(f, "!{:?}", self.bound)
276+
} else {
277+
write!(f, "!{}_{:?}", self.universe.index(), self.bound)
278+
}
279+
}
280+
}
281+
233282
///////////////////////////////////////////////////////////////////////////
234283
// Atomic structs
235284
//
@@ -295,6 +344,7 @@ TrivialTypeTraversalAndLiftImpls! {
295344
crate::ty::AliasRelationDirection,
296345
crate::ty::Placeholder<crate::ty::BoundRegion>,
297346
crate::ty::Placeholder<crate::ty::BoundTy>,
347+
crate::ty::Placeholder<ty::BoundVar>,
298348
crate::ty::ClosureKind,
299349
crate::ty::FreeRegion,
300350
crate::ty::InferTy,
@@ -311,7 +361,6 @@ TrivialTypeTraversalAndLiftImpls! {
311361
interpret::Scalar,
312362
rustc_target::abi::Size,
313363
ty::BoundVar,
314-
ty::Placeholder<ty::BoundVar>,
315364
}
316365

317366
TrivialTypeTraversalAndLiftImpls! {

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_type_ir::TyKind as IrTyKind;
4141
pub type TyKind<'tcx> = IrTyKind<TyCtxt<'tcx>>;
4242
pub type RegionKind<'tcx> = IrRegionKind<TyCtxt<'tcx>>;
4343

44-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
44+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)]
4545
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
4646
pub struct RawPtr<'tcx> {
4747
pub ty: Ty<'tcx>,
@@ -1511,10 +1511,11 @@ impl Atom for RegionVid {
15111511

15121512
rustc_index::newtype_index! {
15131513
#[derive(HashStable)]
1514+
#[debug_format = "{}"]
15141515
pub struct BoundVar {}
15151516
}
15161517

1517-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
1518+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)]
15181519
#[derive(HashStable)]
15191520
pub struct BoundTy {
15201521
pub var: BoundVar,

compiler/rustc_type_ir/src/lib.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,11 @@ pub trait Interner: Sized {
4747
type Region: Clone + Debug + Hash + Ord;
4848
type Predicate;
4949
type RawPtrTy: Clone + Debug + Hash + Ord;
50-
type Mutability: Clone + Debug + Hash + Ord;
5150
type Movability: Clone + Debug + Hash + Ord;
5251
type PolyFnSig: Clone + Debug + Hash + Ord;
5352
type ListBinderExistentialPredicate: Clone + Debug + Hash + Ord;
5453
type BinderListTy: Clone + Debug + Hash + Ord;
55-
type ListTy: Clone + Debug + Hash + Ord;
54+
type ListTy: Clone + Debug + Hash + Ord + IntoIterator<Item = Self::Ty>;
5655
type AliasTy: Clone + Debug + Hash + Ord;
5756
type ParamTy: Clone + Debug + Hash + Ord;
5857
type BoundTy: Clone + Debug + Hash + Ord;
@@ -390,7 +389,19 @@ impl DebruijnIndex {
390389
}
391390
}
392391

393-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
392+
pub fn debug_bound_var<T: std::fmt::Write>(
393+
fmt: &mut T,
394+
debruijn: DebruijnIndex,
395+
var: impl std::fmt::Debug,
396+
) -> Result<(), std::fmt::Error> {
397+
if debruijn == INNERMOST {
398+
write!(fmt, "^{:?}", var)
399+
} else {
400+
write!(fmt, "^{}_{:?}", debruijn.index(), var)
401+
}
402+
}
403+
404+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
394405
#[derive(Encodable, Decodable, HashStable_Generic)]
395406
pub enum IntTy {
396407
Isize,
@@ -448,7 +459,7 @@ impl IntTy {
448459
}
449460
}
450461

451-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Debug)]
462+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
452463
#[derive(Encodable, Decodable, HashStable_Generic)]
453464
pub enum UintTy {
454465
Usize,
@@ -506,7 +517,7 @@ impl UintTy {
506517
}
507518
}
508519

509-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
520+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
510521
#[derive(Encodable, Decodable, HashStable_Generic)]
511522
pub enum FloatTy {
512523
F32,

compiler/rustc_type_ir/src/structural_impls.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
55
use crate::fold::{FallibleTypeFolder, TypeFoldable};
66
use crate::visit::{TypeVisitable, TypeVisitor};
7-
use crate::Interner;
7+
use crate::{FloatTy, IntTy, Interner, UintTy};
88
use rustc_data_structures::functor::IdFunctor;
99
use rustc_data_structures::sync::Lrc;
1010
use rustc_index::{Idx, IndexVec};
1111

12+
use core::fmt;
1213
use std::ops::ControlFlow;
1314

1415
///////////////////////////////////////////////////////////////////////////
@@ -163,3 +164,21 @@ impl<I: Interner, T: TypeVisitable<I>, Ix: Idx> TypeVisitable<I> for IndexVec<Ix
163164
self.iter().try_for_each(|t| t.visit_with(visitor))
164165
}
165166
}
167+
168+
impl fmt::Debug for IntTy {
169+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170+
write!(f, "{}", self.name_str())
171+
}
172+
}
173+
174+
impl fmt::Debug for UintTy {
175+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
176+
write!(f, "{}", self.name_str())
177+
}
178+
}
179+
180+
impl fmt::Debug for FloatTy {
181+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182+
write!(f, "{}", self.name_str())
183+
}
184+
}

0 commit comments

Comments
 (0)