1
Fork 0

Disentangle Debug and Display for Ty.

The `Debug` impl for `Ty` just calls the `Display` impl for `Ty`. This
is surprising and annoying. In particular, it means `Debug` doesn't show
as much information as `Debug` for `TyKind` does. And `Debug` is used in
some user-facing error messages, which seems bad.

This commit changes the `Debug` impl for `Ty` to call the `Debug` impl
for `TyKind`. It also does a number of follow-up changes to preserve
existing output, many of which involve inserting
`with_no_trimmed_paths!` calls. It also adds `Display` impls for
`UserType` and `Canonical`.

Some tests have changes to expected output:
- Those that use the `rustc_abi(debug)` attribute.
- Those that use the `EMIT_MIR` annotation.

In each case the output is slightly uglier than before. This isn't
ideal, but it's pretty weird (particularly for the attribute) that the
output is using `Debug` in the first place. They're fairly obscure
attributes (I hadn't heard of them) so I'm not worried by this.

For `async-is-unwindsafe.stderr`, there is one line that now lacks a
full path. This is a consistency improvement, because all the other
mentions of `Context` in this test lack a path.
This commit is contained in:
Nicholas Nethercote 2023-09-07 15:14:40 +10:00
parent c0583a0221
commit 64ea8eb1a9
27 changed files with 502 additions and 77 deletions

View file

@ -10,6 +10,7 @@ use rustc_middle::mir::{
Body, ClosureOutlivesSubject, ClosureRegionRequirements, LocalKind, Location, Promoted,
START_BLOCK,
};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{self, OpaqueHiddenType, TyCtxt};
use rustc_span::symbol::sym;
use std::env;
@ -441,7 +442,10 @@ fn for_each_region_constraint<'tcx>(
let subject = match req.subject {
ClosureOutlivesSubject::Region(subject) => format!("{subject:?}"),
ClosureOutlivesSubject::Ty(ty) => {
format!("{:?}", ty.instantiate(tcx, |vid| ty::Region::new_var(tcx, vid)))
with_no_trimmed_paths!(format!(
"{}",
ty.instantiate(tcx, |vid| ty::Region::new_var(tcx, vid))
))
}
};
with_msg(format!("where {}: {:?}", subject, req.outlived_free_region,))?;

View file

@ -21,6 +21,7 @@ use rustc_hir::BodyOwnerKind;
use rustc_index::IndexVec;
use rustc_infer::infer::NllRegionVariableOrigin;
use rustc_middle::ty::fold::TypeFoldable;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{self, InlineConstArgs, InlineConstArgsParts, RegionVid, Ty, TyCtxt};
use rustc_middle::ty::{GenericArgs, GenericArgsRef};
use rustc_span::symbol::{kw, sym};
@ -332,10 +333,16 @@ impl<'tcx> UniversalRegions<'tcx> {
pub(crate) fn annotate(&self, tcx: TyCtxt<'tcx>, err: &mut Diagnostic) {
match self.defining_ty {
DefiningTy::Closure(def_id, args) => {
let v = with_no_trimmed_paths!(
args[tcx.generics_of(def_id).parent_count..]
.iter()
.map(|arg| arg.to_string())
.collect::<Vec<_>>()
);
err.note(format!(
"defining type: {} with closure args {:#?}",
"defining type: {} with closure args [\n {},\n]",
tcx.def_path_str_with_args(def_id, args),
&args[tcx.generics_of(def_id).parent_count..],
v.join(",\n "),
));
// FIXME: It'd be nice to print the late-bound regions
@ -348,10 +355,16 @@ impl<'tcx> UniversalRegions<'tcx> {
});
}
DefiningTy::Generator(def_id, args, _) => {
let v = with_no_trimmed_paths!(
args[tcx.generics_of(def_id).parent_count..]
.iter()
.map(|arg| arg.to_string())
.collect::<Vec<_>>()
);
err.note(format!(
"defining type: {} with generator args {:#?}",
"defining type: {} with generator args [\n {},\n]",
tcx.def_path_str_with_args(def_id, args),
&args[tcx.generics_of(def_id).parent_count..],
v.join(",\n "),
));
// FIXME: As above, we'd like to print out the region