Skip to content

Commit 0dc6c1e

Browse files
Make precise capturing suggestion machine-applicable only if it has not APITs
1 parent 81eef2d commit 0dc6c1e

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

compiler/rustc_trait_selection/src/errors.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1907,10 +1907,18 @@ impl Subdiagnostic for AddPreciseCapturingForOvercapture {
19071907
diag: &mut Diag<'_, G>,
19081908
_f: &F,
19091909
) {
1910+
let applicability = if self.apit_spans.is_empty() {
1911+
Applicability::MachineApplicable
1912+
} else {
1913+
// If there are APIT that are converted to regular parameters,
1914+
// then this may make the API turbofishable in ways that were
1915+
// not intended.
1916+
Applicability::MaybeIncorrect
1917+
};
19101918
diag.multipart_suggestion_verbose(
19111919
fluent::trait_selection_precise_capturing_overcaptures,
19121920
self.suggs,
1913-
Applicability::MaybeIncorrect,
1921+
applicability,
19141922
);
19151923
if !self.apit_spans.is_empty() {
19161924
diag.span_note(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ run-rustfix
2+
//@ rustfix-only-machine-applicable
3+
4+
// Make sure that simple overcapture suggestions remain machine applicable.
5+
6+
#![allow(unused)]
7+
#![deny(impl_trait_overcaptures)]
8+
9+
fn named<'a>(x: &'a i32) -> impl Sized + use<> { *x }
10+
//~^ ERROR `impl Sized` will capture more lifetimes than possibly intended in edition 2024
11+
//~| WARN this changes meaning in Rust 2024
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ run-rustfix
2+
//@ rustfix-only-machine-applicable
3+
4+
// Make sure that simple overcapture suggestions remain machine applicable.
5+
6+
#![allow(unused)]
7+
#![deny(impl_trait_overcaptures)]
8+
9+
fn named<'a>(x: &'a i32) -> impl Sized { *x }
10+
//~^ ERROR `impl Sized` will capture more lifetimes than possibly intended in edition 2024
11+
//~| WARN this changes meaning in Rust 2024
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: `impl Sized` will capture more lifetimes than possibly intended in edition 2024
2+
--> $DIR/overcaptures-2024-machine-applicable.rs:9:29
3+
|
4+
LL | fn named<'a>(x: &'a i32) -> impl Sized { *x }
5+
| ^^^^^^^^^^
6+
|
7+
= warning: this changes meaning in Rust 2024
8+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/rpit-lifetime-capture.html>
9+
note: specifically, this lifetime is in scope but not mentioned in the type's bounds
10+
--> $DIR/overcaptures-2024-machine-applicable.rs:9:10
11+
|
12+
LL | fn named<'a>(x: &'a i32) -> impl Sized { *x }
13+
| ^^
14+
= note: all lifetimes in scope will be captured by `impl Trait`s in edition 2024
15+
note: the lint level is defined here
16+
--> $DIR/overcaptures-2024-machine-applicable.rs:7:9
17+
|
18+
LL | #![deny(impl_trait_overcaptures)]
19+
| ^^^^^^^^^^^^^^^^^^^^^^^
20+
help: use the precise capturing `use<...>` syntax to make the captures explicit
21+
|
22+
LL | fn named<'a>(x: &'a i32) -> impl Sized + use<> { *x }
23+
| +++++++
24+
25+
error: aborting due to 1 previous error
26+

0 commit comments

Comments
 (0)