Skip to content

Commit

Permalink
misc improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed May 26, 2023
1 parent f1bbfaf commit 8293ca6
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 83 deletions.
8 changes: 8 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,14 @@ impl Mutability {
}
}

/// Returns `*const ` or `*mut ` depending on the mutability
pub fn ptr_prefix_str(self) -> &'static str {
match self {
Mutability::Not => "*const",
Mutability::Mut => "*mut",
}
}

/// Returns `""` (empty string) or `"mutably "` depending on the mutability.
pub fn mutably_str(self) -> &'static str {
match self {
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type Region = Region<'tcx>;
type Predicate = Predicate<'tcx>;
type RawPtrTy = RawPtr<'tcx>;
type Mutability = hir::Mutability;
type Movability = hir::Movability;
type PolyFnSig = PolyFnSig<'tcx>;
type ListBinderExistentialPredicate = &'tcx List<PolyExistentialPredicate<'tcx>>;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
/// identified by both a universe, as well as a name residing within that universe. Distinct bound
/// regions/types/consts within the same universe simply have an unknown relationship to one
/// another.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(HashStable, TyEncodable, TyDecodable)]
pub struct Placeholder<T> {
pub universe: UniverseIndex,
Expand Down
62 changes: 19 additions & 43 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,29 +679,30 @@ pub trait PrettyPrinter<'tcx>:
}
ty::FnPtr(ref bare_fn) => p!(print(bare_fn)),
ty::Infer(infer_ty) => {
let verbose = self.should_print_verbose();
if self.should_print_verbose() {
p!(write("{:?}", ty.kind()));
return Ok(self);
}

if let ty::TyVar(ty_vid) = infer_ty {
if let Some(name) = self.ty_infer_name(ty_vid) {
p!(write("{}", name))
} else {
if verbose {
p!(write("{:?}", infer_ty))
} else {
p!(write("{}", infer_ty))
}
p!(write("{}", infer_ty))
}
} else {
if verbose { p!(write("{:?}", infer_ty)) } else { p!(write("{}", infer_ty)) }
p!(write("{}", infer_ty))
}
}
ty::Error(_) => p!("[type error]"),
ty::Param(ref param_ty) => p!(print(param_ty)),
ty::Bound(debruijn, bound_ty) => match bound_ty.kind {
ty::BoundTyKind::Anon => debug_bound_var(&mut self, debruijn, bound_ty.var)?,
ty::BoundTyKind::Anon => {
rustc_type_ir::debug_bound_var(&mut self, debruijn, bound_ty.var)?
}
ty::BoundTyKind::Param(_, s) => match self.should_print_verbose() {
true if debruijn == ty::INNERMOST => p!(write("^{}", s)),
true => p!(write("^{}_{}", debruijn.index(), s)),
false => p!(write("{}", s)),
true => p!(write("{:?}", ty.kind())),
false => p!(write("{s}")),
},
},
ty::Adt(def, substs) => {
Expand Down Expand Up @@ -734,10 +735,11 @@ pub trait PrettyPrinter<'tcx>:
}
}
ty::Placeholder(placeholder) => match placeholder.bound.kind {
ty::BoundTyKind::Anon => {
debug_placeholder_var(&mut self, placeholder.universe, placeholder.bound.var)?;
}
ty::BoundTyKind::Param(_, name) => p!(write("{}", name)),
ty::BoundTyKind::Anon => p!(write("{placeholder:?}")),
ty::BoundTyKind::Param(_, name) => match self.should_print_verbose() {
true => p!(write("{:?}", ty.kind())),
false => p!(write("{name}")),
},
},
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => {
// We use verbose printing in 'NO_QUERIES' mode, to
Expand Down Expand Up @@ -1366,11 +1368,9 @@ pub trait PrettyPrinter<'tcx>:
}

ty::ConstKind::Bound(debruijn, bound_var) => {
debug_bound_var(&mut self, debruijn, bound_var)?
rustc_type_ir::debug_bound_var(&mut self, debruijn, bound_var)?
}
ty::ConstKind::Placeholder(placeholder) => {
debug_placeholder_var(&mut self, placeholder.universe, placeholder.bound)?;
},
ty::ConstKind::Placeholder(placeholder) => p!(write("{placeholder:?}")),
// FIXME(generic_const_exprs):
// write out some legible representation of an abstract const?
ty::ConstKind::Expr(_) => p!("[const expr]"),
Expand Down Expand Up @@ -3059,27 +3059,3 @@ pub struct OpaqueFnEntry<'tcx> {
fn_trait_ref: Option<ty::PolyTraitRef<'tcx>>,
return_ty: Option<ty::Binder<'tcx, Term<'tcx>>>,
}

pub fn debug_bound_var<T: std::fmt::Write>(
fmt: &mut T,
debruijn: ty::DebruijnIndex,
var: ty::BoundVar,
) -> Result<(), std::fmt::Error> {
if debruijn == ty::INNERMOST {
write!(fmt, "^{}", var.index())
} else {
write!(fmt, "^{}_{}", debruijn.index(), var.index())
}
}

pub fn debug_placeholder_var<T: std::fmt::Write>(
fmt: &mut T,
universe: ty::UniverseIndex,
bound: ty::BoundVar,
) -> Result<(), std::fmt::Error> {
if universe == ty::UniverseIndex::ROOT {
write!(fmt, "!{}", bound.index())
} else {
write!(fmt, "!{}_{}", universe.index(), bound.index())
}
}
63 changes: 56 additions & 7 deletions compiler/rustc_middle/src/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,33 @@ impl fmt::Debug for ty::FreeRegion {

impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
let ty::FnSig { inputs_and_output: _, c_variadic, unsafety, abi } = self;

write!(f, "{}", unsafety.prefix_str())?;
match abi {
rustc_target::spec::abi::Abi::Rust => (),
abi => write!(f, "extern \"{abi:?}\" ")?,
};
if *c_variadic {
write!(f, "c_variadic ")?;
};

write!(f, "fn(")?;
let inputs = self.inputs();
match inputs.len() {
0 => write!(f, ")")?,
_ => {
for ty in &self.inputs()[0..(self.inputs().len() - 1)] {
write!(f, "{ty:?}, ")?;
}
write!(f, "{:?})", self.inputs().last().unwrap())?;
}
}

match self.output().kind() {
ty::Tuple(list) if list.is_empty() => Ok(()),
_ => write!(f, " -> {:?}", self.output()),
}
}
}

Expand Down Expand Up @@ -216,20 +242,43 @@ impl<'tcx> fmt::Debug for ty::ConstKind<'tcx> {
match self {
Param(param) => write!(f, "{param:?}"),
Infer(var) => write!(f, "{var:?}"),
Bound(debruijn, var) => ty::print::debug_bound_var(f, *debruijn, *var),
Placeholder(placeholder) => {
ty::print::debug_placeholder_var(f, placeholder.universe, placeholder.bound)
}
Bound(debruijn, var) => rustc_type_ir::debug_bound_var(f, *debruijn, *var),
Placeholder(placeholder) => write!(f, "{placeholder:?}"),
Unevaluated(uv) => {
f.debug_tuple("Unevaluated").field(&uv.substs).field(&uv.def).finish()
}
Value(valtree) => write!(f, "{valtree:?}"),
Error(_) => write!(f, "[const error]"),
Error(_) => write!(f, "{{const error}}"),
Expr(expr) => write!(f, "{expr:?}"),
}
}
}

impl<'tcx> fmt::Debug for ty::RawPtr<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {:?}", self.mutbl.ptr_prefix_str(), self.ty)
}
}

impl fmt::Debug for ty::BoundTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.kind {
ty::BoundTyKind::Anon => write!(f, "{:?}", self.var),
ty::BoundTyKind::Param(_, sym) => write!(f, "{sym:?}"),
}
}
}

impl<T: fmt::Debug> fmt::Debug for ty::Placeholder<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.universe == ty::UniverseIndex::ROOT {
write!(f, "!{:?}", self.bound)
} else {
write!(f, "!{}_{:?}", self.universe.index(), self.bound)
}
}
}

///////////////////////////////////////////////////////////////////////////
// Atomic structs
//
Expand Down Expand Up @@ -295,6 +344,7 @@ TrivialTypeTraversalAndLiftImpls! {
crate::ty::AliasRelationDirection,
crate::ty::Placeholder<crate::ty::BoundRegion>,
crate::ty::Placeholder<crate::ty::BoundTy>,
crate::ty::Placeholder<ty::BoundVar>,
crate::ty::ClosureKind,
crate::ty::FreeRegion,
crate::ty::InferTy,
Expand All @@ -311,7 +361,6 @@ TrivialTypeTraversalAndLiftImpls! {
interpret::Scalar,
rustc_target::abi::Size,
ty::BoundVar,
ty::Placeholder<ty::BoundVar>,
}

TrivialTypeTraversalAndLiftImpls! {
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use rustc_type_ir::TyKind as IrTyKind;
pub type TyKind<'tcx> = IrTyKind<TyCtxt<'tcx>>;
pub type RegionKind<'tcx> = IrRegionKind<TyCtxt<'tcx>>;

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

rustc_index::newtype_index! {
#[derive(HashStable)]
#[debug_format = "{}"]
pub struct BoundVar {}
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)]
#[derive(HashStable)]
pub struct BoundTy {
pub var: BoundVar,
Expand Down
21 changes: 16 additions & 5 deletions compiler/rustc_type_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ pub trait Interner: Sized {
type Region: Clone + Debug + Hash + Ord;
type Predicate;
type RawPtrTy: Clone + Debug + Hash + Ord;
type Mutability: Clone + Debug + Hash + Ord;
type Movability: Clone + Debug + Hash + Ord;
type PolyFnSig: Clone + Debug + Hash + Ord;
type ListBinderExistentialPredicate: Clone + Debug + Hash + Ord;
type BinderListTy: Clone + Debug + Hash + Ord;
type ListTy: Clone + Debug + Hash + Ord;
type ListTy: Clone + Debug + Hash + Ord + IntoIterator<Item = Self::Ty>;
type AliasTy: Clone + Debug + Hash + Ord;
type ParamTy: Clone + Debug + Hash + Ord;
type BoundTy: Clone + Debug + Hash + Ord;
Expand Down Expand Up @@ -390,7 +389,19 @@ impl DebruijnIndex {
}
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub fn debug_bound_var<T: std::fmt::Write>(
fmt: &mut T,
debruijn: DebruijnIndex,
var: impl std::fmt::Debug,
) -> Result<(), std::fmt::Error> {
if debruijn == INNERMOST {
write!(fmt, "^{:?}", var)
} else {
write!(fmt, "^{}_{:?}", debruijn.index(), var)
}
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Encodable, Decodable, HashStable_Generic)]
pub enum IntTy {
Isize,
Expand Down Expand Up @@ -448,7 +459,7 @@ impl IntTy {
}
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Debug)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
#[derive(Encodable, Decodable, HashStable_Generic)]
pub enum UintTy {
Usize,
Expand Down Expand Up @@ -506,7 +517,7 @@ impl UintTy {
}
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Encodable, Decodable, HashStable_Generic)]
pub enum FloatTy {
F32,
Expand Down
21 changes: 20 additions & 1 deletion compiler/rustc_type_ir/src/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use crate::fold::{FallibleTypeFolder, TypeFoldable};
use crate::visit::{TypeVisitable, TypeVisitor};
use crate::Interner;
use crate::{FloatTy, IntTy, Interner, UintTy};
use rustc_data_structures::functor::IdFunctor;
use rustc_data_structures::sync::Lrc;
use rustc_index::{Idx, IndexVec};

use core::fmt;
use std::ops::ControlFlow;

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -163,3 +164,21 @@ impl<I: Interner, T: TypeVisitable<I>, Ix: Idx> TypeVisitable<I> for IndexVec<Ix
self.iter().try_for_each(|t| t.visit_with(visitor))
}
}

impl fmt::Debug for IntTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name_str())
}
}

impl fmt::Debug for UintTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name_str())
}
}

impl fmt::Debug for FloatTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name_str())
}
}
Loading

0 comments on commit 8293ca6

Please sign in to comment.