Skip to content

Commit 039e9b6

Browse files
authored
Rollup merge of #103414 - compiler-errors:rpit-print-lt, r=cjgillot
Pretty print lifetimes captured by RPIT This specifically makes the output in #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
2 parents 9f06fbd + c5df620 commit 039e9b6

File tree

6 files changed

+31
-21
lines changed

6 files changed

+31
-21
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_session::cstore::{ExternCrate, ExternCrateSource};
1616
use rustc_span::symbol::{kw, Ident, Symbol};
1717
use rustc_target::abi::Size;
1818
use rustc_target::spec::abi::Abi;
19+
use smallvec::SmallVec;
1920

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

798800
for (predicate, _) in bounds.subst_iter_copied(tcx, substs) {
799801
let bound_predicate = predicate.kind();
@@ -824,6 +826,9 @@ pub trait PrettyPrinter<'tcx>:
824826
&mut fn_traits,
825827
);
826828
}
829+
ty::PredicateKind::TypeOutlives(outlives) => {
830+
lifetimes.push(outlives.1);
831+
}
827832
_ => {}
828833
}
829834
}
@@ -977,6 +982,11 @@ pub trait PrettyPrinter<'tcx>:
977982
write!(self, "Sized")?;
978983
}
979984

985+
for re in lifetimes {
986+
write!(self, " + ")?;
987+
self = self.print_region(re)?;
988+
}
989+
980990
Ok(self)
981991
}
982992

src/test/ui/associated-type-bounds/inside-adt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ enum E2 { V(Box<dyn Iterator<Item: Copy>>) }
1616
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
1717
enum E3 { V(dyn Iterator<Item: 'static>) }
1818
//~^ ERROR associated type bounds are not allowed within structs, enums, or unions
19-
//~| ERROR the size for values of type `(dyn Iterator<Item = impl Sized> + 'static)`
19+
//~| ERROR the size for values of type `(dyn Iterator<Item = impl Sized + 'static> + 'static)`
2020

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

3030
fn main() {}

src/test/ui/associated-type-bounds/inside-adt.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ help: the `Box` type always has a statically known size and allocates its conten
7070
LL | enum E1 { V(Box<dyn Iterator<Item: Copy>>) }
7171
| ++++ +
7272

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

110-
error[E0277]: the size for values of type `(dyn Iterator<Item = impl Sized> + 'static)` cannot be known at compilation time
110+
error[E0277]: the size for values of type `(dyn Iterator<Item = impl Sized + 'static> + 'static)` cannot be known at compilation time
111111
--> $DIR/inside-adt.rs:26:15
112112
|
113113
LL | union U3 { f: ManuallyDrop<dyn Iterator<Item: 'static>> }
114114
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
115115
|
116-
= help: within `ManuallyDrop<(dyn Iterator<Item = impl Sized> + 'static)>`, the trait `Sized` is not implemented for `(dyn Iterator<Item = impl Sized> + 'static)`
117-
= note: required because it appears within the type `ManuallyDrop<(dyn Iterator<Item = impl Sized> + 'static)>`
116+
= help: within `ManuallyDrop<(dyn Iterator<Item = impl Sized + 'static> + 'static)>`, the trait `Sized` is not implemented for `(dyn Iterator<Item = impl Sized + 'static> + 'static)`
117+
= note: required because it appears within the type `ManuallyDrop<(dyn Iterator<Item = impl Sized + 'static> + 'static)>`
118118
= note: no field of a union may have a dynamically sized type
119119
= help: change the field's type to have a statically known size
120120
help: borrowed types always have a statically known size

src/test/ui/associated-types/issue-87261.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ where
7777

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

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

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

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

9494
accepts_generic_trait(returns_opaque_generic_foo());
9595
//~^ ERROR type mismatch resolving `<impl GenericTrait<()> + Foo as GenericTrait<()>>::Associated == ()`

src/test/ui/associated-types/issue-87261.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ note: required by a bound in `accepts_generic_trait`
132132
LL | fn accepts_generic_trait<T: GenericTrait<(), Associated = ()>>(_: T) {}
133133
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_generic_trait`
134134

135-
error[E0271]: type mismatch resolving `<impl Trait as Trait>::Associated == ()`
135+
error[E0271]: type mismatch resolving `<impl Trait + 'static as Trait>::Associated == ()`
136136
--> $DIR/issue-87261.rs:79:19
137137
|
138138
LL | fn returns_opaque() -> impl Trait + 'static {
@@ -144,18 +144,18 @@ LL | accepts_trait(returns_opaque());
144144
| required by a bound introduced by this call
145145
|
146146
= note: expected unit type `()`
147-
found associated type `<impl Trait as Trait>::Associated`
147+
found associated type `<impl Trait + 'static as Trait>::Associated`
148148
note: required by a bound in `accepts_trait`
149149
--> $DIR/issue-87261.rs:43:27
150150
|
151151
LL | fn accepts_trait<T: Trait<Associated = ()>>(_: T) {}
152152
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_trait`
153-
help: consider constraining the associated type `<impl Trait as Trait>::Associated` to `()`
153+
help: consider constraining the associated type `<impl Trait + 'static as Trait>::Associated` to `()`
154154
|
155155
LL | fn returns_opaque() -> impl Trait<Associated = ()> + 'static {
156156
| +++++++++++++++++
157157

158-
error[E0271]: type mismatch resolving `<impl DerivedTrait as Trait>::Associated == ()`
158+
error[E0271]: type mismatch resolving `<impl DerivedTrait + 'static as Trait>::Associated == ()`
159159
--> $DIR/issue-87261.rs:82:19
160160
|
161161
LL | fn returns_opaque_derived() -> impl DerivedTrait + 'static {
@@ -167,13 +167,13 @@ LL | accepts_trait(returns_opaque_derived());
167167
| required by a bound introduced by this call
168168
|
169169
= note: expected unit type `()`
170-
found associated type `<impl DerivedTrait as Trait>::Associated`
170+
found associated type `<impl DerivedTrait + 'static as Trait>::Associated`
171171
note: required by a bound in `accepts_trait`
172172
--> $DIR/issue-87261.rs:43:27
173173
|
174174
LL | fn accepts_trait<T: Trait<Associated = ()>>(_: T) {}
175175
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_trait`
176-
help: consider constraining the associated type `<impl DerivedTrait as Trait>::Associated` to `()`
176+
help: consider constraining the associated type `<impl DerivedTrait + 'static as Trait>::Associated` to `()`
177177
|
178178
LL | fn returns_opaque_derived() -> impl DerivedTrait<Associated = ()> + 'static {
179179
| +++++++++++++++++
@@ -222,7 +222,7 @@ note: required by a bound in `accepts_trait`
222222
LL | fn accepts_trait<T: Trait<Associated = ()>>(_: T) {}
223223
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_trait`
224224

225-
error[E0271]: type mismatch resolving `<impl GenericTrait<()> as GenericTrait<()>>::Associated == ()`
225+
error[E0271]: type mismatch resolving `<impl GenericTrait<()> + 'static as GenericTrait<()>>::Associated == ()`
226226
--> $DIR/issue-87261.rs:91:27
227227
|
228228
LL | fn returns_opaque_generic() -> impl GenericTrait<()> + 'static {
@@ -234,13 +234,13 @@ LL | accepts_generic_trait(returns_opaque_generic());
234234
| required by a bound introduced by this call
235235
|
236236
= note: expected unit type `()`
237-
found associated type `<impl GenericTrait<()> as GenericTrait<()>>::Associated`
237+
found associated type `<impl GenericTrait<()> + 'static as GenericTrait<()>>::Associated`
238238
note: required by a bound in `accepts_generic_trait`
239239
--> $DIR/issue-87261.rs:44:46
240240
|
241241
LL | fn accepts_generic_trait<T: GenericTrait<(), Associated = ()>>(_: T) {}
242242
| ^^^^^^^^^^^^^^^ required by this bound in `accepts_generic_trait`
243-
help: consider constraining the associated type `<impl GenericTrait<()> as GenericTrait<()>>::Associated` to `()`
243+
help: consider constraining the associated type `<impl GenericTrait<()> + 'static as GenericTrait<()>>::Associated` to `()`
244244
|
245245
LL | fn returns_opaque_generic() -> impl GenericTrait<(), Associated = ()> + 'static {
246246
| +++++++++++++++++

src/test/ui/impl-trait/hidden-lifetimes.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0700]: hidden type for `impl Swap` captures lifetime that does not appear in bounds
1+
error[E0700]: hidden type for `impl Swap + 'a` captures lifetime that does not appear in bounds
22
--> $DIR/hidden-lifetimes.rs:29:5
33
|
44
LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a {
@@ -11,7 +11,7 @@ help: to declare that the `impl Trait` captures `'b`, you can add an explicit `'
1111
LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a + 'b {
1212
| ++++
1313

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

0 commit comments

Comments
 (0)