Skip to content

Commit

Permalink
Rollup merge of rust-lang#103414 - compiler-errors:rpit-print-lt, r=c…
Browse files Browse the repository at this point in the history
…jgillot

Pretty print lifetimes captured by RPIT

This specifically makes the output in rust-lang#103409 change from:

```diff
  error: `impl` item signature doesn't match `trait` item signature
    --> $DIR/signature-mismatch.rs:15:5
     |
  LL |     fn async_fn(&self, buff: &[u8]) -> impl Future<Output = Vec<u8>>;
     |     ----------------------------------------------------------------- expected `fn(&'1 Struct, &'2 [u8]) -> impl Future<Output = Vec<u8>>`
  ...
  LL |     fn async_fn<'a>(&self, buff: &'a [u8]) -> impl Future<Output = Vec<u8>> + 'a {
-  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 Struct, &'2 [u8]) -> impl Future<Output = Vec<u8>>`
+  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 Struct, &'2 [u8]) -> impl Future<Output = Vec<u8>> + '2`
     |
     = note: expected `fn(&'1 Struct, &'2 [u8]) -> impl Future<Output = Vec<u8>>`
-               found `fn(&'1 Struct, &'2 [u8]) -> impl Future<Output = Vec<u8>>`
+               found `fn(&'1 Struct, &'2 [u8]) -> impl Future<Output = Vec<u8>> + '2`
     = help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
     = help: verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output

  error: aborting due to previous error
```

Along with the UI tests in this PR, which I think are all improvements!

r? `@oli-obk` though feel free to re-roll
  • Loading branch information
notriddle authored Oct 23, 2022
2 parents 9f06fbd + c5df620 commit 039e9b6
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
10 changes: 10 additions & 0 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use rustc_session::cstore::{ExternCrate, ExternCrateSource};
use rustc_span::symbol::{kw, Ident, Symbol};
use rustc_target::abi::Size;
use rustc_target::spec::abi::Abi;
use smallvec::SmallVec;

use std::cell::Cell;
use std::char;
Expand Down Expand Up @@ -794,6 +795,7 @@ pub trait PrettyPrinter<'tcx>:
let mut traits = FxIndexMap::default();
let mut fn_traits = FxIndexMap::default();
let mut is_sized = false;
let mut lifetimes = SmallVec::<[ty::Region<'tcx>; 1]>::new();

for (predicate, _) in bounds.subst_iter_copied(tcx, substs) {
let bound_predicate = predicate.kind();
Expand Down Expand Up @@ -824,6 +826,9 @@ pub trait PrettyPrinter<'tcx>:
&mut fn_traits,
);
}
ty::PredicateKind::TypeOutlives(outlives) => {
lifetimes.push(outlives.1);
}
_ => {}
}
}
Expand Down Expand Up @@ -977,6 +982,11 @@ pub trait PrettyPrinter<'tcx>:
write!(self, "Sized")?;
}

for re in lifetimes {
write!(self, " + ")?;
self = self.print_region(re)?;
}

Ok(self)
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/associated-type-bounds/inside-adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ enum E2 { V(Box<dyn Iterator<Item: Copy>>) }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
enum E3 { V(dyn Iterator<Item: 'static>) }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
//~| ERROR the size for values of type `(dyn Iterator<Item = impl Sized> + 'static)`
//~| ERROR the size for values of type `(dyn Iterator<Item = impl Sized + 'static> + 'static)`

union U1 { f: ManuallyDrop<dyn Iterator<Item: Copy>> }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
Expand All @@ -25,6 +25,6 @@ union U2 { f: ManuallyDrop<Box<dyn Iterator<Item: Copy>>> }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
union U3 { f: ManuallyDrop<dyn Iterator<Item: 'static>> }
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
//~| ERROR the size for values of type `(dyn Iterator<Item = impl Sized> + 'static)`
//~| ERROR the size for values of type `(dyn Iterator<Item = impl Sized + 'static> + 'static)`

fn main() {}
10 changes: 5 additions & 5 deletions src/test/ui/associated-type-bounds/inside-adt.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ help: the `Box` type always has a statically known size and allocates its conten
LL | enum E1 { V(Box<dyn Iterator<Item: Copy>>) }
| ++++ +

error[E0277]: the size for values of type `(dyn Iterator<Item = impl Sized> + 'static)` cannot be known at compilation time
error[E0277]: the size for values of type `(dyn Iterator<Item = impl Sized + 'static> + 'static)` cannot be known at compilation time
--> $DIR/inside-adt.rs:17:13
|
LL | enum E3 { V(dyn Iterator<Item: 'static>) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Iterator<Item = impl Sized> + 'static)`
= help: the trait `Sized` is not implemented for `(dyn Iterator<Item = impl Sized + 'static> + 'static)`
= note: no field of an enum variant may have a dynamically sized type
= help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
Expand Down Expand Up @@ -107,14 +107,14 @@ help: the `Box` type always has a statically known size and allocates its conten
LL | union U1 { f: Box<ManuallyDrop<dyn Iterator<Item: Copy>>> }
| ++++ +

error[E0277]: the size for values of type `(dyn Iterator<Item = impl Sized> + 'static)` cannot be known at compilation time
error[E0277]: the size for values of type `(dyn Iterator<Item = impl Sized + 'static> + 'static)` cannot be known at compilation time
--> $DIR/inside-adt.rs:26:15
|
LL | union U3 { f: ManuallyDrop<dyn Iterator<Item: 'static>> }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `ManuallyDrop<(dyn Iterator<Item = impl Sized> + 'static)>`, the trait `Sized` is not implemented for `(dyn Iterator<Item = impl Sized> + 'static)`
= note: required because it appears within the type `ManuallyDrop<(dyn Iterator<Item = impl Sized> + 'static)>`
= help: within `ManuallyDrop<(dyn Iterator<Item = impl Sized + 'static> + 'static)>`, the trait `Sized` is not implemented for `(dyn Iterator<Item = impl Sized + 'static> + 'static)`
= note: required because it appears within the type `ManuallyDrop<(dyn Iterator<Item = impl Sized + 'static> + 'static)>`
= note: no field of a union may have a dynamically sized type
= help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/associated-types/issue-87261.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ where

fn main() {
accepts_trait(returns_opaque());
//~^ ERROR type mismatch resolving `<impl Trait as Trait>::Associated == ()`
//~^ ERROR type mismatch resolving `<impl Trait + 'static as Trait>::Associated == ()`

accepts_trait(returns_opaque_derived());
//~^ ERROR type mismatch resolving `<impl DerivedTrait as Trait>::Associated == ()`
//~^ ERROR type mismatch resolving `<impl DerivedTrait + 'static as Trait>::Associated == ()`

accepts_trait(returns_opaque_foo());
//~^ ERROR type mismatch resolving `<impl Trait + Foo as Trait>::Associated == ()`
Expand All @@ -89,7 +89,7 @@ fn main() {
//~^ ERROR type mismatch resolving `<impl DerivedTrait + Foo as Trait>::Associated == ()`

accepts_generic_trait(returns_opaque_generic());
//~^ ERROR type mismatch resolving `<impl GenericTrait<()> as GenericTrait<()>>::Associated == ()`
//~^ ERROR type mismatch resolving `<impl GenericTrait<()> + 'static as GenericTrait<()>>::Associated == ()`

accepts_generic_trait(returns_opaque_generic_foo());
//~^ ERROR type mismatch resolving `<impl GenericTrait<()> + Foo as GenericTrait<()>>::Associated == ()`
Expand Down
18 changes: 9 additions & 9 deletions src/test/ui/associated-types/issue-87261.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ note: required by a bound in `accepts_generic_trait`
LL | fn accepts_generic_trait<T: GenericTrait<(), Associated = ()>>(_: T) {}
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_generic_trait`

error[E0271]: type mismatch resolving `<impl Trait as Trait>::Associated == ()`
error[E0271]: type mismatch resolving `<impl Trait + 'static as Trait>::Associated == ()`
--> $DIR/issue-87261.rs:79:19
|
LL | fn returns_opaque() -> impl Trait + 'static {
Expand All @@ -144,18 +144,18 @@ LL | accepts_trait(returns_opaque());
| required by a bound introduced by this call
|
= note: expected unit type `()`
found associated type `<impl Trait as Trait>::Associated`
found associated type `<impl Trait + 'static as Trait>::Associated`
note: required by a bound in `accepts_trait`
--> $DIR/issue-87261.rs:43:27
|
LL | fn accepts_trait<T: Trait<Associated = ()>>(_: T) {}
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_trait`
help: consider constraining the associated type `<impl Trait as Trait>::Associated` to `()`
help: consider constraining the associated type `<impl Trait + 'static as Trait>::Associated` to `()`
|
LL | fn returns_opaque() -> impl Trait<Associated = ()> + 'static {
| +++++++++++++++++

error[E0271]: type mismatch resolving `<impl DerivedTrait as Trait>::Associated == ()`
error[E0271]: type mismatch resolving `<impl DerivedTrait + 'static as Trait>::Associated == ()`
--> $DIR/issue-87261.rs:82:19
|
LL | fn returns_opaque_derived() -> impl DerivedTrait + 'static {
Expand All @@ -167,13 +167,13 @@ LL | accepts_trait(returns_opaque_derived());
| required by a bound introduced by this call
|
= note: expected unit type `()`
found associated type `<impl DerivedTrait as Trait>::Associated`
found associated type `<impl DerivedTrait + 'static as Trait>::Associated`
note: required by a bound in `accepts_trait`
--> $DIR/issue-87261.rs:43:27
|
LL | fn accepts_trait<T: Trait<Associated = ()>>(_: T) {}
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_trait`
help: consider constraining the associated type `<impl DerivedTrait as Trait>::Associated` to `()`
help: consider constraining the associated type `<impl DerivedTrait + 'static as Trait>::Associated` to `()`
|
LL | fn returns_opaque_derived() -> impl DerivedTrait<Associated = ()> + 'static {
| +++++++++++++++++
Expand Down Expand Up @@ -222,7 +222,7 @@ note: required by a bound in `accepts_trait`
LL | fn accepts_trait<T: Trait<Associated = ()>>(_: T) {}
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_trait`

error[E0271]: type mismatch resolving `<impl GenericTrait<()> as GenericTrait<()>>::Associated == ()`
error[E0271]: type mismatch resolving `<impl GenericTrait<()> + 'static as GenericTrait<()>>::Associated == ()`
--> $DIR/issue-87261.rs:91:27
|
LL | fn returns_opaque_generic() -> impl GenericTrait<()> + 'static {
Expand All @@ -234,13 +234,13 @@ LL | accepts_generic_trait(returns_opaque_generic());
| required by a bound introduced by this call
|
= note: expected unit type `()`
found associated type `<impl GenericTrait<()> as GenericTrait<()>>::Associated`
found associated type `<impl GenericTrait<()> + 'static as GenericTrait<()>>::Associated`
note: required by a bound in `accepts_generic_trait`
--> $DIR/issue-87261.rs:44:46
|
LL | fn accepts_generic_trait<T: GenericTrait<(), Associated = ()>>(_: T) {}
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_generic_trait`
help: consider constraining the associated type `<impl GenericTrait<()> as GenericTrait<()>>::Associated` to `()`
help: consider constraining the associated type `<impl GenericTrait<()> + 'static as GenericTrait<()>>::Associated` to `()`
|
LL | fn returns_opaque_generic() -> impl GenericTrait<(), Associated = ()> + 'static {
| +++++++++++++++++
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/impl-trait/hidden-lifetimes.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0700]: hidden type for `impl Swap` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Swap + 'a` captures lifetime that does not appear in bounds
--> $DIR/hidden-lifetimes.rs:29:5
|
LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a {
Expand All @@ -11,7 +11,7 @@ help: to declare that the `impl Trait` captures `'b`, you can add an explicit `'
LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a + 'b {
| ++++

error[E0700]: hidden type for `impl Swap` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Swap + 'a` captures lifetime that does not appear in bounds
--> $DIR/hidden-lifetimes.rs:46:5
|
LL | fn hide_rc_refcell<'a, 'b: 'a, T: 'static>(x: Rc<RefCell<&'b T>>) -> impl Swap + 'a {
Expand Down

0 comments on commit 039e9b6

Please sign in to comment.