Skip to content

Commit 7546163

Browse files
committed
Improve self-referential diagnostic somewhat
1 parent 6807d37 commit 7546163

File tree

5 files changed

+27
-20
lines changed

5 files changed

+27
-20
lines changed

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

+19-12
Original file line numberDiff line numberDiff line change
@@ -644,20 +644,18 @@ pub trait PrettyPrinter<'tcx>:
644644
return Ok(self);
645645
}
646646

647-
return with_no_queries(|| {
648-
let def_key = self.tcx().def_key(def_id);
649-
if let Some(name) = def_key.disambiguated_data.data.get_opt_name() {
650-
p!(write("{}", name));
651-
// FIXME(eddyb) print this with `print_def_path`.
652-
if !substs.is_empty() {
653-
p!("::");
654-
p!(generic_delimiters(|cx| cx.comma_sep(substs.iter())));
655-
}
656-
return Ok(self);
647+
let def_key = self.tcx().def_key(def_id);
648+
if let Some(name) = def_key.disambiguated_data.data.get_opt_name() {
649+
p!(write("{}", name));
650+
// FIXME(eddyb) print this with `print_def_path`.
651+
if !substs.is_empty() {
652+
p!("::");
653+
p!(generic_delimiters(|cx| cx.comma_sep(substs.iter())));
657654
}
655+
return Ok(self);
656+
}
658657

659-
self.pretty_print_opaque_impl_type(def_id, substs)
660-
});
658+
return self.pretty_print_opaque_impl_type(def_id, substs);
661659
}
662660
ty::Str => p!("str"),
663661
ty::Generator(did, substs, movability) => {
@@ -900,6 +898,15 @@ pub trait PrettyPrinter<'tcx>:
900898
if !first {
901899
p!(", ");
902900
}
901+
if let GenericArgKind::Type(ty) = ty.unpack() {
902+
if let ty::Opaque(d, substs) = *ty.kind() {
903+
if d == def_id {
904+
p!(print_def_path(d, substs));
905+
first = false;
906+
continue;
907+
}
908+
}
909+
}
903910
p!(print(trait_ref.rebind(*ty)));
904911
first = false;
905912
}

src/test/ui/type-alias-impl-trait/nested.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ fn bar() -> Bar {
1313

1414
fn main() {
1515
println!("{:?}", bar());
16-
//~^ ERROR `impl Trait<Opaque(DefId(0:4 ~ nested[14f6]::Foo::{opaque#0}), [])>` doesn't implement `Debug`
16+
//~^ ERROR `impl Trait<impl Debug>` doesn't implement `Debug`
1717
}

src/test/ui/type-alias-impl-trait/nested.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0277]: `impl Trait<Opaque(DefId(0:4 ~ nested[14f6]::Foo::{opaque#0}), [])>` doesn't implement `Debug`
1+
error[E0277]: `impl Trait<impl Debug>` doesn't implement `Debug`
22
--> $DIR/nested.rs:15:22
33
|
44
LL | println!("{:?}", bar());
5-
| ^^^^^ `impl Trait<Opaque(DefId(0:4 ~ nested[14f6]::Foo::{opaque#0}), [])>` cannot be formatted using `{:?}` because it doesn't implement `Debug`
5+
| ^^^^^ `impl Trait<impl Debug>` cannot be formatted using `{:?}` because it doesn't implement `Debug`
66
|
7-
= help: the trait `Debug` is not implemented for `impl Trait<Opaque(DefId(0:4 ~ nested[14f6]::Foo::{opaque#0}), [])>`
7+
= help: the trait `Debug` is not implemented for `impl Trait<impl Debug>`
88
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
99

1010
error: aborting due to previous error

src/test/ui/type-alias-impl-trait/self-referential.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
type Bar<'a, 'b> = impl PartialEq<Bar<'b, 'a>> + std::fmt::Debug;
44

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

99
fn main() {

src/test/ui/type-alias-impl-trait/self-referential.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
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`
1+
error[E0277]: can't compare `&i32` with `impl PartialEq<Bar<'a, 'b>::{opaque#0}> + Debug`
22
--> $DIR/self-referential.rs:6:5
33
|
44
LL | i
5-
| ^ 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`
5+
| ^ no implementation for `&i32 == impl PartialEq<Bar<'a, 'b>::{opaque#0}> + Debug`
66
|
7-
= 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`
7+
= help: the trait `PartialEq<impl PartialEq<Bar<'a, 'b>::{opaque#0}> + Debug>` is not implemented for `&i32`
88

99
error: aborting due to previous error
1010

0 commit comments

Comments
 (0)