Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove lots of generics from ty::print #116815

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove "subprinter" types from Printer
These are `Self` in almost all printers except one, which can just store
the state as a field instead. This simplifies the printer and allows for
further simplifications, for example using `&mut self` instead of
passing around the printer.
  • Loading branch information
Noratrieb committed Oct 17, 2023
commit 3895f0e9affaab800f5f2cdc06726aeee9a7ff10
30 changes: 12 additions & 18 deletions compiler/rustc_const_eval/src/util/type_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,15 @@ struct AbsolutePathPrinter<'tcx> {
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
type Error = std::fmt::Error;

type Path = Self;
type Region = Self;
type Type = Self;
type DynExistential = Self;
type Const = Self;

fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}

fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
Ok(self)
}

fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self, Self::Error> {
match *ty.kind() {
// Types without identity.
ty::Bool
Expand Down Expand Up @@ -68,18 +62,18 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
}
}

fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
self.pretty_print_const(ct, false)
}

fn print_dyn_existential(
self,
predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
) -> Result<Self, Self::Error> {
self.pretty_print_dyn_existential(predicates)
}

fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
self.path.push_str(self.tcx.crate_name(cnum).as_str());
Ok(self)
}
Expand All @@ -88,17 +82,17 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
self.pretty_path_qualified(self_ty, trait_ref)
}

fn path_append_impl(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
_disambiguated_data: &DisambiguatedDefPathData,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
self.pretty_path_append_impl(
|mut cx| {
cx = print_prefix(cx)?;
Expand All @@ -114,9 +108,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {

fn path_append(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
self = print_prefix(self)?;

write!(self.path, "::{}", disambiguated_data.data).unwrap();
Expand All @@ -126,9 +120,9 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {

fn path_generic_args(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
args: &[GenericArg<'tcx>],
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
self = print_prefix(self)?;
let args =
args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
Expand Down
49 changes: 24 additions & 25 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,76 +580,72 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {

struct AbsolutePathPrinter<'tcx> {
tcx: TyCtxt<'tcx>,
segments: Vec<String>,
}

struct NonTrivialPath;

impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
type Error = NonTrivialPath;

type Path = Vec<String>;
type Region = !;
type Type = !;
type DynExistential = !;
type Const = !;

fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
self.tcx
}

fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
}

fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
}

fn print_dyn_existential(
self,
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
}

fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
}

fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
Ok(vec![self.tcx.crate_name(cnum).to_string()])
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
self.segments = vec![self.tcx.crate_name(cnum).to_string()];
Ok(self)
}
fn path_qualified(
self,
_self_ty: Ty<'tcx>,
_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
}

fn path_append_impl(
self,
_print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
_print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
_disambiguated_data: &DisambiguatedDefPathData,
_self_ty: Ty<'tcx>,
_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
Err(NonTrivialPath)
}
fn path_append(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
let mut path = print_prefix(self)?;
path.push(disambiguated_data.to_string());
Ok(path)
) -> Result<Self, Self::Error> {
self = print_prefix(self)?;
self.segments.push(disambiguated_data.to_string());
Ok(self)
}
fn path_generic_args(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
_args: &[GenericArg<'tcx>],
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
print_prefix(self)
}
}
Expand All @@ -659,8 +655,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// are from a local module we could have false positives, e.g.
// let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
if did1.krate != did2.krate {
let abs_path =
|def_id| AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]);
let abs_path = |def_id| {
AbsolutePathPrinter { tcx: self.tcx, segments: vec![] }
.print_def_path(def_id, &[])
.map(|p| p.segments)
};

// We compare strings because DefPath can be different
// for imported and non-imported crates
Expand Down
62 changes: 31 additions & 31 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,51 +1200,47 @@ impl<'tcx> LateContext<'tcx> {
/// }
/// ```
pub fn get_def_path(&self, def_id: DefId) -> Vec<Symbol> {
pub struct AbsolutePathPrinter<'tcx> {
pub tcx: TyCtxt<'tcx>,
struct AbsolutePathPrinter<'tcx> {
tcx: TyCtxt<'tcx>,
path: Vec<Symbol>,
}

impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
type Error = !;

type Path = Vec<Symbol>;
type Region = ();
type Type = ();
type DynExistential = ();
type Const = ();

fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}

fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
Ok(())
fn print_region(self, _region: ty::Region<'_>) -> Result<Self, Self::Error> {
Ok(self)
}

fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
Ok(())
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self, Self::Error> {
Ok(self)
}

fn print_dyn_existential(
self,
_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
Ok(())
) -> Result<Self, Self::Error> {
Ok(self)
}

fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
Ok(())
fn print_const(self, _ct: ty::Const<'tcx>) -> Result<Self, Self::Error> {
Ok(self)
}

fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
Ok(vec![self.tcx.crate_name(cnum)])
fn path_crate(mut self, cnum: CrateNum) -> Result<Self, Self::Error> {
self.path = vec![self.tcx.crate_name(cnum)];
Ok(self)
}

fn path_qualified(
self,
mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
if trait_ref.is_none() {
if let ty::Adt(def, args) = self_ty.kind() {
return self.print_def_path(def.did(), args);
Expand All @@ -1253,24 +1249,25 @@ impl<'tcx> LateContext<'tcx> {

// This shouldn't ever be needed, but just in case:
with_no_trimmed_paths!({
Ok(vec![match trait_ref {
self.path = vec![match trait_ref {
Some(trait_ref) => Symbol::intern(&format!("{trait_ref:?}")),
None => Symbol::intern(&format!("<{self_ty}>")),
}])
}];
Ok(self)
})
}

fn path_append_impl(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
_disambiguated_data: &DisambiguatedDefPathData,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
let mut path = print_prefix(self)?;

// This shouldn't ever be needed, but just in case:
path.push(match trait_ref {
path.path.push(match trait_ref {
Some(trait_ref) => {
with_no_trimmed_paths!(Symbol::intern(&format!(
"<impl {} for {}>",
Expand All @@ -1288,30 +1285,33 @@ impl<'tcx> LateContext<'tcx> {

fn path_append(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
let mut path = print_prefix(self)?;

// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
return Ok(path);
}

path.push(Symbol::intern(&disambiguated_data.data.to_string()));
path.path.push(Symbol::intern(&disambiguated_data.data.to_string()));
Ok(path)
}

fn path_generic_args(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
print_prefix: impl FnOnce(Self) -> Result<Self, Self::Error>,
_args: &[GenericArg<'tcx>],
) -> Result<Self::Path, Self::Error> {
) -> Result<Self, Self::Error> {
print_prefix(self)
}
}

AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]).unwrap()
AbsolutePathPrinter { tcx: self.tcx, path: vec![] }
.print_def_path(def_id, &[])
.unwrap()
.path
}

/// Returns the associated type `name` for `self_ty` as an implementation of `trait_id`.
Expand Down
Loading