Skip to content

Commit

Permalink
Improve self-referential diagnostic somewhat
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Feb 3, 2022
1 parent 6807d37 commit 7546163
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
31 changes: 19 additions & 12 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Expand Up @@ -644,20 +644,18 @@ pub trait PrettyPrinter<'tcx>:
return Ok(self);
}

return with_no_queries(|| {
let def_key = self.tcx().def_key(def_id);
if let Some(name) = def_key.disambiguated_data.data.get_opt_name() {
p!(write("{}", name));
// FIXME(eddyb) print this with `print_def_path`.
if !substs.is_empty() {
p!("::");
p!(generic_delimiters(|cx| cx.comma_sep(substs.iter())));
}
return Ok(self);
let def_key = self.tcx().def_key(def_id);
if let Some(name) = def_key.disambiguated_data.data.get_opt_name() {
p!(write("{}", name));
// FIXME(eddyb) print this with `print_def_path`.
if !substs.is_empty() {
p!("::");
p!(generic_delimiters(|cx| cx.comma_sep(substs.iter())));
}
return Ok(self);
}

self.pretty_print_opaque_impl_type(def_id, substs)
});
return self.pretty_print_opaque_impl_type(def_id, substs);
}
ty::Str => p!("str"),
ty::Generator(did, substs, movability) => {
Expand Down Expand Up @@ -900,6 +898,15 @@ pub trait PrettyPrinter<'tcx>:
if !first {
p!(", ");
}
if let GenericArgKind::Type(ty) = ty.unpack() {
if let ty::Opaque(d, substs) = *ty.kind() {
if d == def_id {
p!(print_def_path(d, substs));
first = false;
continue;
}
}
}
p!(print(trait_ref.rebind(*ty)));
first = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/type-alias-impl-trait/nested.rs
Expand Up @@ -13,5 +13,5 @@ fn bar() -> Bar {

fn main() {
println!("{:?}", bar());
//~^ ERROR `impl Trait<Opaque(DefId(0:4 ~ nested[14f6]::Foo::{opaque#0}), [])>` doesn't implement `Debug`
//~^ ERROR `impl Trait<impl Debug>` doesn't implement `Debug`
}
6 changes: 3 additions & 3 deletions src/test/ui/type-alias-impl-trait/nested.stderr
@@ -1,10 +1,10 @@
error[E0277]: `impl Trait<Opaque(DefId(0:4 ~ nested[14f6]::Foo::{opaque#0}), [])>` doesn't implement `Debug`
error[E0277]: `impl Trait<impl Debug>` doesn't implement `Debug`
--> $DIR/nested.rs:15:22
|
LL | println!("{:?}", bar());
| ^^^^^ `impl Trait<Opaque(DefId(0:4 ~ nested[14f6]::Foo::{opaque#0}), [])>` cannot be formatted using `{:?}` because it doesn't implement `Debug`
| ^^^^^ `impl Trait<impl Debug>` cannot be formatted using `{:?}` because it doesn't implement `Debug`
|
= help: the trait `Debug` is not implemented for `impl Trait<Opaque(DefId(0:4 ~ nested[14f6]::Foo::{opaque#0}), [])>`
= help: the trait `Debug` is not implemented for `impl Trait<impl Debug>`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/type-alias-impl-trait/self-referential.rs
Expand Up @@ -3,7 +3,7 @@
type Bar<'a, 'b> = impl PartialEq<Bar<'b, 'a>> + std::fmt::Debug;

fn bar<'a, 'b>(i: &'a i32) -> Bar<'a, 'b> {
i //~ ERROR can't compare `&i32` with `impl PartialEq<Opaque
i //~ ERROR can't compare `&i32` with `impl PartialEq<Bar<'a, 'b>
}

fn main() {
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/type-alias-impl-trait/self-referential.stderr
@@ -1,10 +1,10 @@
error[E0277]: can't compare `&i32` with `impl PartialEq<Opaque(DefId(0:6 ~ self_referential[5b7d]::Bar::{opaque#0}), [ReFree(DefId(0:7 ~ self_referential[5b7d]::bar), BrNamed(DefId(0:8 ~ self_referential[5b7d]::bar::'a), 'a)), ReEarlyBound(0, 'b)])> + Debug`
error[E0277]: can't compare `&i32` with `impl PartialEq<Bar<'a, 'b>::{opaque#0}> + Debug`
--> $DIR/self-referential.rs:6:5
|
LL | i
| ^ no implementation for `&i32 == impl PartialEq<Opaque(DefId(0:6 ~ self_referential[5b7d]::Bar::{opaque#0}), [ReFree(DefId(0:7 ~ self_referential[5b7d]::bar), BrNamed(DefId(0:8 ~ self_referential[5b7d]::bar::'a), 'a)), ReEarlyBound(0, 'b)])> + Debug`
| ^ no implementation for `&i32 == impl PartialEq<Bar<'a, 'b>::{opaque#0}> + Debug`
|
= help: the trait `PartialEq<impl PartialEq<Opaque(DefId(0:6 ~ self_referential[5b7d]::Bar::{opaque#0}), [ReFree(DefId(0:7 ~ self_referential[5b7d]::bar), BrNamed(DefId(0:8 ~ self_referential[5b7d]::bar::'a), 'a)), ReEarlyBound(0, 'b)])> + Debug>` is not implemented for `&i32`
= help: the trait `PartialEq<impl PartialEq<Bar<'a, 'b>::{opaque#0}> + Debug>` is not implemented for `&i32`

error: aborting due to previous error

Expand Down

0 comments on commit 7546163

Please sign in to comment.