Skip to content

Commit ce626b9

Browse files
Recurse into APITs in impl_trait_overcaptures
1 parent b73478b commit ce626b9

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

compiler/rustc_lint/src/impl_trait_overcaptures.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,11 @@ where
259259
// If it's owned by this function
260260
&& let opaque =
261261
self.tcx.hir_node_by_def_id(opaque_def_id).expect_opaque_ty()
262-
&& let hir::OpaqueTyOrigin::FnReturn { parent, .. } = opaque.origin
262+
// We want to recurse into RPITs and async fns, even though the latter
263+
// doesn't overcapture on its own, it may mention additional RPITs
264+
// in its bounds.
265+
&& let hir::OpaqueTyOrigin::FnReturn { parent, .. }
266+
| hir::OpaqueTyOrigin::AsyncFn { parent, .. } = opaque.origin
263267
&& parent == self.parent_def_id
264268
{
265269
let opaque_span = self.tcx.def_span(opaque_def_id);

tests/ui/impl-trait/precise-capturing/overcaptures-2024.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ run-rustfix
2+
//@ edition: 2018
23

34
#![allow(unused)]
45
#![deny(impl_trait_overcaptures)]
@@ -29,4 +30,8 @@ fn hrtb() -> impl for<'a> Higher<'a, Output = impl Sized + use<>> {}
2930
//~^ ERROR `impl Sized` will capture more lifetimes than possibly intended in edition 2024
3031
//~| WARN this changes meaning in Rust 2024
3132

33+
async fn async_fn<'a>(x: &'a ()) -> impl Sized + use<> {}
34+
//~^ ERROR `impl Sized` will capture more lifetimes than possibly intended in edition 2024
35+
//~| WARN this changes meaning in Rust 2024
36+
3237
fn main() {}

tests/ui/impl-trait/precise-capturing/overcaptures-2024.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ run-rustfix
2+
//@ edition: 2018
23

34
#![allow(unused)]
45
#![deny(impl_trait_overcaptures)]
@@ -29,4 +30,8 @@ fn hrtb() -> impl for<'a> Higher<'a, Output = impl Sized> {}
2930
//~^ ERROR `impl Sized` will capture more lifetimes than possibly intended in edition 2024
3031
//~| WARN this changes meaning in Rust 2024
3132

33+
async fn async_fn<'a>(x: &'a ()) -> impl Sized {}
34+
//~^ ERROR `impl Sized` will capture more lifetimes than possibly intended in edition 2024
35+
//~| WARN this changes meaning in Rust 2024
36+
3237
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
error: `impl Sized` will capture more lifetimes than possibly intended in edition 2024
2-
--> $DIR/overcaptures-2024.rs:6:29
2+
--> $DIR/overcaptures-2024.rs:7:29
33
|
44
LL | fn named<'a>(x: &'a i32) -> impl Sized { *x }
55
| ^^^^^^^^^^
66
|
77
= warning: this changes meaning in Rust 2024
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/rpit-lifetime-capture.html>
99
note: specifically, this lifetime is in scope but not mentioned in the type's bounds
10-
--> $DIR/overcaptures-2024.rs:6:10
10+
--> $DIR/overcaptures-2024.rs:7:10
1111
|
1212
LL | fn named<'a>(x: &'a i32) -> impl Sized { *x }
1313
| ^^
1414
= note: all lifetimes in scope will be captured by `impl Trait`s in edition 2024
1515
note: the lint level is defined here
16-
--> $DIR/overcaptures-2024.rs:4:9
16+
--> $DIR/overcaptures-2024.rs:5:9
1717
|
1818
LL | #![deny(impl_trait_overcaptures)]
1919
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -23,15 +23,15 @@ LL | fn named<'a>(x: &'a i32) -> impl Sized + use<> { *x }
2323
| +++++++
2424

2525
error: `impl Sized` will capture more lifetimes than possibly intended in edition 2024
26-
--> $DIR/overcaptures-2024.rs:10:25
26+
--> $DIR/overcaptures-2024.rs:11:25
2727
|
2828
LL | fn implicit(x: &i32) -> impl Sized { *x }
2929
| ^^^^^^^^^^
3030
|
3131
= warning: this changes meaning in Rust 2024
3232
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/rpit-lifetime-capture.html>
3333
note: specifically, this lifetime is in scope but not mentioned in the type's bounds
34-
--> $DIR/overcaptures-2024.rs:10:16
34+
--> $DIR/overcaptures-2024.rs:11:16
3535
|
3636
LL | fn implicit(x: &i32) -> impl Sized { *x }
3737
| ^
@@ -42,15 +42,15 @@ LL | fn implicit(x: &i32) -> impl Sized + use<> { *x }
4242
| +++++++
4343

4444
error: `impl Sized + '_` will capture more lifetimes than possibly intended in edition 2024
45-
--> $DIR/overcaptures-2024.rs:16:33
45+
--> $DIR/overcaptures-2024.rs:17:33
4646
|
4747
LL | fn hello(&self, x: &i32) -> impl Sized + '_ { self }
4848
| ^^^^^^^^^^^^^^^
4949
|
5050
= warning: this changes meaning in Rust 2024
5151
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/rpit-lifetime-capture.html>
5252
note: specifically, this lifetime is in scope but not mentioned in the type's bounds
53-
--> $DIR/overcaptures-2024.rs:16:24
53+
--> $DIR/overcaptures-2024.rs:17:24
5454
|
5555
LL | fn hello(&self, x: &i32) -> impl Sized + '_ { self }
5656
| ^
@@ -61,15 +61,15 @@ LL | fn hello(&self, x: &i32) -> impl Sized + '_ + use<'_> { self }
6161
| +++++++++
6262

6363
error: `impl Sized` will capture more lifetimes than possibly intended in edition 2024
64-
--> $DIR/overcaptures-2024.rs:28:47
64+
--> $DIR/overcaptures-2024.rs:29:47
6565
|
6666
LL | fn hrtb() -> impl for<'a> Higher<'a, Output = impl Sized> {}
6767
| ^^^^^^^^^^
6868
|
6969
= warning: this changes meaning in Rust 2024
7070
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/rpit-lifetime-capture.html>
7171
note: specifically, this lifetime is in scope but not mentioned in the type's bounds
72-
--> $DIR/overcaptures-2024.rs:28:23
72+
--> $DIR/overcaptures-2024.rs:29:23
7373
|
7474
LL | fn hrtb() -> impl for<'a> Higher<'a, Output = impl Sized> {}
7575
| ^^
@@ -79,5 +79,24 @@ help: use the precise capturing `use<...>` syntax to make the captures explicit
7979
LL | fn hrtb() -> impl for<'a> Higher<'a, Output = impl Sized + use<>> {}
8080
| +++++++
8181

82-
error: aborting due to 4 previous errors
82+
error: `impl Sized` will capture more lifetimes than possibly intended in edition 2024
83+
--> $DIR/overcaptures-2024.rs:33:37
84+
|
85+
LL | async fn async_fn<'a>(x: &'a ()) -> impl Sized {}
86+
| ^^^^^^^^^^
87+
|
88+
= warning: this changes meaning in Rust 2024
89+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/rpit-lifetime-capture.html>
90+
note: specifically, this lifetime is in scope but not mentioned in the type's bounds
91+
--> $DIR/overcaptures-2024.rs:33:19
92+
|
93+
LL | async fn async_fn<'a>(x: &'a ()) -> impl Sized {}
94+
| ^^
95+
= note: all lifetimes in scope will be captured by `impl Trait`s in edition 2024
96+
help: use the precise capturing `use<...>` syntax to make the captures explicit
97+
|
98+
LL | async fn async_fn<'a>(x: &'a ()) -> impl Sized + use<> {}
99+
| +++++++
100+
101+
error: aborting due to 5 previous errors
83102

0 commit comments

Comments
 (0)