Skip to content

Commit c93f571

Browse files
committed
Print opaque types from type aliases via their path
1 parent 7546163 commit c93f571

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+153
-77
lines changed

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -644,18 +644,23 @@ pub trait PrettyPrinter<'tcx>:
644644
return Ok(self);
645645
}
646646

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())));
647+
let parent = self.tcx().parent(def_id).expect("opaque types always have a parent");
648+
match self.tcx().def_kind(parent) {
649+
DefKind::TyAlias | DefKind::AssocTy => {
650+
if let ty::Opaque(d, _) = *self.tcx().type_of(parent).kind() {
651+
if d == def_id {
652+
// If the type alias directly starts with the `impl` of the
653+
// opaque type we're printing, then skip the `::{opaque#1}`.
654+
p!(print_def_path(parent, substs));
655+
return Ok(self);
656+
}
657+
}
658+
// Complex opaque type, e.g. `type Foo = (i32, impl Debug);`
659+
p!(print_def_path(def_id, substs));
660+
return Ok(self);
654661
}
655-
return Ok(self);
662+
_ => return self.pretty_print_opaque_impl_type(def_id, substs),
656663
}
657-
658-
return self.pretty_print_opaque_impl_type(def_id, substs);
659664
}
660665
ty::Str => p!("str"),
661666
ty::Generator(did, substs, movability) => {
@@ -898,15 +903,6 @@ pub trait PrettyPrinter<'tcx>:
898903
if !first {
899904
p!(", ");
900905
}
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-
}
910906
p!(print(trait_ref.rebind(*ty)));
911907
first = false;
912908
}

src/test/ui/associated-type-bounds/assoc-type-eq-with-dyn-atb-fail.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0277]: the trait bound `String: Copy` is not satisfied
44
LL | Box::new(AssocNoCopy)
55
| ^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
66
|
7-
= note: required for the cast to the object type `dyn Bar<Assoc = impl Copy>`
7+
= note: required for the cast to the object type `dyn Bar<Assoc = <AssocNoCopy as Thing>::Out::{opaque#0}>`
88

99
error: aborting due to previous error
1010

src/test/ui/impl-trait/auto-trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl<T: Send> AnotherTrait for T {}
1919
// (We treat opaque types as "foreign types" that could grow more impls
2020
// in the future.)
2121
impl AnotherTrait for D<OpaqueType> {
22-
//~^ ERROR conflicting implementations of trait `AnotherTrait` for type `D<impl OpaqueTrait>`
22+
//~^ ERROR conflicting implementations of trait `AnotherTrait` for type `D<OpaqueType>`
2323
}
2424

2525
fn main() {}

src/test/ui/impl-trait/auto-trait.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0119]: conflicting implementations of trait `AnotherTrait` for type `D<impl OpaqueTrait>`
1+
error[E0119]: conflicting implementations of trait `AnotherTrait` for type `D<OpaqueType>`
22
--> $DIR/auto-trait.rs:21:1
33
|
44
LL | impl<T: Send> AnotherTrait for T {}
55
| -------------------------------- first implementation here
66
...
77
LL | impl AnotherTrait for D<OpaqueType> {
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `D<impl OpaqueTrait>`
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `D<OpaqueType>`
99

1010
error: aborting due to previous error
1111

src/test/ui/impl-trait/negative-reasoning.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl<T: std::fmt::Debug> AnotherTrait for T {}
1717

1818
// This is in error, because we cannot assume that `OpaqueType: !Debug`
1919
impl AnotherTrait for D<OpaqueType> {
20-
//~^ ERROR conflicting implementations of trait `AnotherTrait` for type `D<impl OpaqueTrait>`
20+
//~^ ERROR conflicting implementations of trait `AnotherTrait` for type `D<OpaqueType>`
2121
}
2222

2323
fn main() {}

src/test/ui/impl-trait/negative-reasoning.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error[E0119]: conflicting implementations of trait `AnotherTrait` for type `D<impl OpaqueTrait>`
1+
error[E0119]: conflicting implementations of trait `AnotherTrait` for type `D<OpaqueType>`
22
--> $DIR/negative-reasoning.rs:19:1
33
|
44
LL | impl<T: std::fmt::Debug> AnotherTrait for T {}
55
| ------------------------------------------- first implementation here
66
...
77
LL | impl AnotherTrait for D<OpaqueType> {
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `D<impl OpaqueTrait>`
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `D<OpaqueType>`
99
|
10-
= note: upstream crates may add a new impl of trait `std::fmt::Debug` for type `impl OpaqueTrait` in future versions
10+
= note: upstream crates may add a new impl of trait `std::fmt::Debug` for type `OpaqueType` in future versions
1111

1212
error: aborting due to previous error
1313

src/test/ui/lint/lint-ctypes-73249-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct A<T: Foo> {
2323
}
2424

2525
extern "C" {
26-
pub fn lint_me() -> A<()>; //~ ERROR: uses type `impl Baz`
26+
pub fn lint_me() -> A<()>; //~ ERROR: uses type `Qux`
2727
}
2828

2929
fn main() {}

src/test/ui/lint/lint-ctypes-73249-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `extern` block uses type `impl Baz`, which is not FFI-safe
1+
error: `extern` block uses type `Qux`, which is not FFI-safe
22
--> $DIR/lint-ctypes-73249-2.rs:26:25
33
|
44
LL | pub fn lint_me() -> A<()>;

src/test/ui/lint/lint-ctypes-73249-3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct A {
1717
}
1818

1919
extern "C" {
20-
pub fn lint_me() -> A; //~ ERROR: uses type `impl Baz`
20+
pub fn lint_me() -> A; //~ ERROR: uses type `Qux`
2121
}
2222

2323
fn main() {}

src/test/ui/lint/lint-ctypes-73249-3.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `extern` block uses type `impl Baz`, which is not FFI-safe
1+
error: `extern` block uses type `Qux`, which is not FFI-safe
22
--> $DIR/lint-ctypes-73249-3.rs:20:25
33
|
44
LL | pub fn lint_me() -> A;

0 commit comments

Comments
 (0)