Skip to content

Commit 8e068b9

Browse files
Recurse into APITs in impl_trait_overcaptures
1 parent de27914 commit 8e068b9

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

compiler/rustc_lint/src/impl_trait_overcaptures.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,11 @@ where
262262
// If it's owned by this function
263263
&& let opaque =
264264
self.tcx.hir_node_by_def_id(opaque_def_id).expect_opaque_ty()
265-
&& let hir::OpaqueTyOrigin::FnReturn { parent, .. } = opaque.origin
265+
// We want to recurse into RPITs and async fns, even though the latter
266+
// doesn't overcapture on its own, it may mention additional RPITs
267+
// in its bounds.
268+
&& let hir::OpaqueTyOrigin::FnReturn { parent, .. }
269+
| hir::OpaqueTyOrigin::AsyncFn { parent, .. } = opaque.origin
266270
&& parent == self.parent_def_id
267271
{
268272
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)]
@@ -37,4 +38,8 @@ fn apit2<U, T: Sized>(_: &T, _: U) -> impl Sized + use<U, T> {}
3738
//~^ ERROR `impl Sized` will capture more lifetimes than possibly intended in edition 2024
3839
//~| WARN this changes meaning in Rust 2024
3940

41+
async fn async_fn<'a>(x: &'a ()) -> impl Sized + use<> {}
42+
//~^ ERROR `impl Sized` will capture more lifetimes than possibly intended in edition 2024
43+
//~| WARN this changes meaning in Rust 2024
44+
4045
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)]
@@ -37,4 +38,8 @@ fn apit2<U>(_: &impl Sized, _: U) -> impl Sized {}
3738
//~^ ERROR `impl Sized` will capture more lifetimes than possibly intended in edition 2024
3839
//~| WARN this changes meaning in Rust 2024
3940

41+
async fn async_fn<'a>(x: &'a ()) -> impl Sized {}
42+
//~^ ERROR `impl Sized` will capture more lifetimes than possibly intended in edition 2024
43+
//~| WARN this changes meaning in Rust 2024
44+
4045
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
| ^^
@@ -80,21 +80,21 @@ LL | fn hrtb() -> impl for<'a> Higher<'a, Output = impl Sized + use<>> {}
8080
| +++++++
8181

8282
error: `impl Sized` will capture more lifetimes than possibly intended in edition 2024
83-
--> $DIR/overcaptures-2024.rs:32:28
83+
--> $DIR/overcaptures-2024.rs:33:28
8484
|
8585
LL | fn apit(_: &impl Sized) -> impl Sized {}
8686
| ^^^^^^^^^^
8787
|
8888
= warning: this changes meaning in Rust 2024
8989
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/rpit-lifetime-capture.html>
9090
note: specifically, this lifetime is in scope but not mentioned in the type's bounds
91-
--> $DIR/overcaptures-2024.rs:32:12
91+
--> $DIR/overcaptures-2024.rs:33:12
9292
|
9393
LL | fn apit(_: &impl Sized) -> impl Sized {}
9494
| ^
9595
= note: all lifetimes in scope will be captured by `impl Trait`s in edition 2024
9696
note: you could use a `use<...>` bound to explicitly specify captures, but argument-position `impl Trait`s are not nameable
97-
--> $DIR/overcaptures-2024.rs:32:13
97+
--> $DIR/overcaptures-2024.rs:33:13
9898
|
9999
LL | fn apit(_: &impl Sized) -> impl Sized {}
100100
| ^^^^^^^^^^
@@ -104,21 +104,21 @@ LL | fn apit<T: Sized>(_: &T) -> impl Sized + use<T> {}
104104
| ++++++++++ ~ ++++++++
105105

106106
error: `impl Sized` will capture more lifetimes than possibly intended in edition 2024
107-
--> $DIR/overcaptures-2024.rs:36:38
107+
--> $DIR/overcaptures-2024.rs:37:38
108108
|
109109
LL | fn apit2<U>(_: &impl Sized, _: U) -> impl Sized {}
110110
| ^^^^^^^^^^
111111
|
112112
= warning: this changes meaning in Rust 2024
113113
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/rpit-lifetime-capture.html>
114114
note: specifically, this lifetime is in scope but not mentioned in the type's bounds
115-
--> $DIR/overcaptures-2024.rs:36:16
115+
--> $DIR/overcaptures-2024.rs:37:16
116116
|
117117
LL | fn apit2<U>(_: &impl Sized, _: U) -> impl Sized {}
118118
| ^
119119
= note: all lifetimes in scope will be captured by `impl Trait`s in edition 2024
120120
note: you could use a `use<...>` bound to explicitly specify captures, but argument-position `impl Trait`s are not nameable
121-
--> $DIR/overcaptures-2024.rs:36:17
121+
--> $DIR/overcaptures-2024.rs:37:17
122122
|
123123
LL | fn apit2<U>(_: &impl Sized, _: U) -> impl Sized {}
124124
| ^^^^^^^^^^
@@ -127,5 +127,24 @@ help: use the precise capturing `use<...>` syntax to make the captures explicit
127127
LL | fn apit2<U, T: Sized>(_: &T, _: U) -> impl Sized + use<U, T> {}
128128
| ++++++++++ ~ +++++++++++
129129

130-
error: aborting due to 6 previous errors
130+
error: `impl Sized` will capture more lifetimes than possibly intended in edition 2024
131+
--> $DIR/overcaptures-2024.rs:41:37
132+
|
133+
LL | async fn async_fn<'a>(x: &'a ()) -> impl Sized {}
134+
| ^^^^^^^^^^
135+
|
136+
= warning: this changes meaning in Rust 2024
137+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/rpit-lifetime-capture.html>
138+
note: specifically, this lifetime is in scope but not mentioned in the type's bounds
139+
--> $DIR/overcaptures-2024.rs:41:19
140+
|
141+
LL | async fn async_fn<'a>(x: &'a ()) -> impl Sized {}
142+
| ^^
143+
= note: all lifetimes in scope will be captured by `impl Trait`s in edition 2024
144+
help: use the precise capturing `use<...>` syntax to make the captures explicit
145+
|
146+
LL | async fn async_fn<'a>(x: &'a ()) -> impl Sized + use<> {}
147+
| +++++++
148+
149+
error: aborting due to 7 previous errors
131150

0 commit comments

Comments
 (0)