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

+15-19
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

+1-1
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

+1-1
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

+2-2
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

+1-1
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

+3-3
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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;

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

+1-1
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-5.stderr

+1-1
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-5.rs:20:25
33
|
44
LL | pub fn lint_me() -> A;

src/test/ui/lint/lint-ctypes-73251-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn assign() -> Qux {
2020
}
2121

2222
extern "C" {
23-
pub fn lint_me() -> <u32 as Foo>::Assoc; //~ ERROR: uses type `impl Baz`
23+
pub fn lint_me() -> <u32 as Foo>::Assoc; //~ ERROR: uses type `Qux`
2424
}
2525

2626
fn main() {}

src/test/ui/lint/lint-ctypes-73251-1.stderr

+1-1
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-73251-1.rs:23:25
33
|
44
LL | pub fn lint_me() -> <u32 as Foo>::Assoc;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn use_of_b() -> AliasB {
3333
}
3434

3535
extern "C" {
36-
pub fn lint_me() -> <AliasB as TraitB>::Assoc; //~ ERROR: uses type `impl TraitA<Assoc = u32>`
36+
pub fn lint_me() -> <AliasB as TraitB>::Assoc; //~ ERROR: uses type `AliasA`
3737
}
3838

3939
fn main() {}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `extern` block uses type `impl TraitA<Assoc = u32>`, which is not FFI-safe
1+
error: `extern` block uses type `AliasA`, which is not FFI-safe
22
--> $DIR/lint-ctypes-73251-2.rs:36:25
33
|
44
LL | pub fn lint_me() -> <AliasB as TraitB>::Assoc;

src/test/ui/lint/opaque-ty-ffi-unsafe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn ret_closure() -> A {
99

1010
extern "C" {
1111
pub fn a(_: A);
12-
//~^ ERROR `extern` block uses type `impl Fn()`, which is not FFI-safe [improper_ctypes]
12+
//~^ ERROR `extern` block uses type `A`, which is not FFI-safe [improper_ctypes]
1313
}
1414

1515
fn main() {}

src/test/ui/lint/opaque-ty-ffi-unsafe.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `extern` block uses type `impl Fn()`, which is not FFI-safe
1+
error: `extern` block uses type `A`, which is not FFI-safe
22
--> $DIR/opaque-ty-ffi-unsafe.rs:11:17
33
|
44
LL | pub fn a(_: A);

src/test/ui/traits/alias/issue-83613.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ fn mk_opaque() -> OpaqueType {
88
trait AnotherTrait {}
99
impl<T: Send> AnotherTrait for T {}
1010
impl AnotherTrait for OpaqueType {}
11-
//~^ ERROR conflicting implementations of trait `AnotherTrait` for type `impl OpaqueTrait`
11+
//~^ ERROR conflicting implementations of trait `AnotherTrait` for type `OpaqueType`
1212
//~| ERROR cannot implement trait on type alias impl trait
1313
fn main() {}

src/test/ui/traits/alias/issue-83613.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ note: type alias impl trait defined here
1010
LL | type OpaqueType = impl OpaqueTrait;
1111
| ^^^^^^^^^^^^^^^^
1212

13-
error[E0119]: conflicting implementations of trait `AnotherTrait` for type `impl OpaqueTrait`
13+
error[E0119]: conflicting implementations of trait `AnotherTrait` for type `OpaqueType`
1414
--> $DIR/issue-83613.rs:10:1
1515
|
1616
LL | impl<T: Send> AnotherTrait for T {}
1717
| -------------------------------- first implementation here
1818
LL | impl AnotherTrait for OpaqueType {}
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `impl OpaqueTrait`
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `OpaqueType`
2020

2121
error: aborting due to 2 previous errors
2222

src/test/ui/type-alias-impl-trait/auto-trait-leakage2.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ error[E0277]: `Rc<u32>` cannot be sent between threads safely
22
--> $DIR/auto-trait-leakage2.rs:17:13
33
|
44
LL | type Foo = impl std::fmt::Debug;
5-
| -------------------- within this `impl Debug`
5+
| -------------------- within this `Foo`
66
...
77
LL | is_send(m::foo());
88
| ------- ^^^^^^^^ `Rc<u32>` cannot be sent between threads safely
99
| |
1010
| required by a bound introduced by this call
1111
|
12-
= help: within `impl Debug`, the trait `Send` is not implemented for `Rc<u32>`
13-
= note: required because it appears within the type `impl Debug`
12+
= help: within `Foo`, the trait `Send` is not implemented for `Rc<u32>`
13+
= note: required because it appears within the type `Foo`
1414
note: required by a bound in `is_send`
1515
--> $DIR/auto-trait-leakage2.rs:14:15
1616
|

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL | pub type Boo = impl ::std::fmt::Debug;
1515
LL | ""
1616
| ^^ expected opaque type, found `&str`
1717
|
18-
= note: expected opaque type `impl Debug`
18+
= note: expected opaque type `Boo`
1919
found reference `&str`
2020

2121
error: aborting due to 2 previous errors

src/test/ui/type-alias-impl-trait/issue-63279.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LL | type Closure = impl FnOnce();
2525
LL | || -> Closure { || () }
2626
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found closure
2727
|
28-
= note: expected opaque type `impl FnOnce()`
28+
= note: expected opaque type `Closure`
2929
found closure `[closure@$DIR/issue-63279.rs:8:5: 8:28]`
3030

3131
error: aborting due to 3 previous errors

src/test/ui/type-alias-impl-trait/issue-74280.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | type Test = impl Copy;
77
LL | 7
88
| ^ expected `()`, found integer
99
|
10-
= note: expected opaque type `impl Copy`
10+
= note: expected opaque type `Test`
1111
found type `{integer}`
1212

1313
error: aborting due to previous error

src/test/ui/type-alias-impl-trait/issue-77179.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn test() -> Pointer<_> {
55
| --------^-
66
| | |
77
| | not allowed in type signatures
8-
| help: replace with the correct return type: `impl Deref<Target = i32>`
8+
| help: replace with the correct return type: `Pointer<i32>`
99

1010
error: aborting due to previous error
1111

src/test/ui/type-alias-impl-trait/multiple-def-uses-in-one-fn3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | fn g<A: ToString + Clone, B: ToString + Clone>(a: A, b: B) -> (X<A, B>, X<A
1111
LL | (a, b)
1212
| ^ expected type parameter `A`, found type parameter `B`
1313
|
14-
= note: expected opaque type `impl ToString`
14+
= note: expected opaque type `X<A, B>`
1515
found type parameter `B`
1616
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
1717
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl Foo<()> for () { }
1212
fn foo() -> impl Foo<FooX> {
1313
// FIXME(type-alias-impl-trait): We could probably make this work.
1414
()
15-
//~^ ERROR: the trait bound `(): Foo<impl Debug>` is not satisfied
15+
//~^ ERROR: the trait bound `(): Foo<FooX>` is not satisfied
1616
}
1717

1818
fn main() { }

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0277]: the trait bound `(): Foo<impl Debug>` is not satisfied
1+
error[E0277]: the trait bound `(): Foo<FooX>` is not satisfied
22
--> $DIR/nested-tait-inference.rs:14:5
33
|
44
LL | ()
5-
| ^^ the trait `Foo<impl Debug>` is not implemented for `()`
5+
| ^^ the trait `Foo<FooX>` is not implemented for `()`
66
|
77
= help: the following implementations were found:
88
<() as Foo<()>>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl Foo<u32> for () {}
1212

1313
fn foo() -> impl Foo<FooX> {
1414
()
15-
//~^ ERROR: the trait bound `(): Foo<impl Debug>` is not satisfied
15+
//~^ ERROR: the trait bound `(): Foo<FooX>` is not satisfied
1616
}
1717

1818
fn main() {}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0277]: the trait bound `(): Foo<impl Debug>` is not satisfied
1+
error[E0277]: the trait bound `(): Foo<FooX>` is not satisfied
22
--> $DIR/nested-tait-inference2.rs:14:5
33
|
44
LL | ()
5-
| ^^ the trait `Foo<impl Debug>` is not implemented for `()`
5+
| ^^ the trait `Foo<FooX>` is not implemented for `()`
66
|
77
= help: the following implementations were found:
88
<() as Foo<()>>

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<impl Debug>` doesn't implement `Debug`
16+
//~^ ERROR `Bar` 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<impl Debug>` doesn't implement `Debug`
1+
error[E0277]: `Bar` doesn't implement `Debug`
22
--> $DIR/nested.rs:15:22
33
|
44
LL | println!("{:?}", bar());
5-
| ^^^^^ `impl Trait<impl Debug>` cannot be formatted using `{:?}` because it doesn't implement `Debug`
5+
| ^^^^^ `Bar` cannot be formatted using `{:?}` because it doesn't implement `Debug`
66
|
7-
= help: the trait `Debug` is not implemented for `impl Trait<impl Debug>`
7+
= help: the trait `Debug` is not implemented for `Bar`
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/no_revealing_outside_defining_module.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | let _: &str = bomp();
88
| ^^^^^^ expected `&str`, found opaque type
99
|
1010
= note: expected reference `&str`
11-
found opaque type `impl Debug`
11+
found opaque type `Boo`
1212

1313
error[E0308]: mismatched types
1414
--> $DIR/no_revealing_outside_defining_module.rs:19:5
@@ -19,7 +19,7 @@ LL | pub type Boo = impl ::std::fmt::Debug;
1919
LL | ""
2020
| ^^ expected opaque type, found `&str`
2121
|
22-
= note: expected opaque type `impl Debug`
22+
= note: expected opaque type `Boo`
2323
found reference `&str`
2424

2525
error: aborting due to 2 previous errors

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type Foo = impl std::fmt::Debug;
44
type Bar = impl PartialEq<Foo>;
55

66
fn bar() -> Bar {
7-
42_i32 //~ ERROR can't compare `i32` with `impl Debug`
7+
42_i32 //~ ERROR can't compare `i32` with `Foo`
88
}
99

1010
fn main() {}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0277]: can't compare `i32` with `impl Debug`
1+
error[E0277]: can't compare `i32` with `Foo`
22
--> $DIR/self-referential-2.rs:7:5
33
|
44
LL | 42_i32
5-
| ^^^^^^ no implementation for `i32 == impl Debug`
5+
| ^^^^^^ no implementation for `i32 == Foo`
66
|
7-
= help: the trait `PartialEq<impl Debug>` is not implemented for `i32`
7+
= help: the trait `PartialEq<Foo>` is not implemented for `i32`
88

99
error: aborting due to previous error
1010

0 commit comments

Comments
 (0)