Skip to content

Commit 7e98aa5

Browse files
Dont suggest use<APIT>
1 parent b73478b commit 7e98aa5

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

compiler/rustc_borrowck/src/diagnostics/opaque_suggestions.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,18 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
9797
})
9898
.map(|arg| arg.to_string()),
9999
);
100-
if opaque_def_id.is_local() {
100+
if seen_generics.iter().any(|name| name.starts_with("impl ")) {
101+
// FIXME: This should be a dedicated suggestion, but that
102+
// requires a lot of moving parts :/
103+
diag.span_help(
104+
tcx.def_span(opaque_def_id),
105+
format!(
106+
"add a precise capturing bound to avoid overcapturing, \
107+
after turning all argument-position impl traits into \
108+
type parameters: `+ use</* Args */>`",
109+
),
110+
);
111+
} else if opaque_def_id.is_local() {
101112
diag.span_suggestion_verbose(
102113
tcx.def_span(opaque_def_id).shrink_to_hi(),
103114
"add a precise capturing bound to avoid overcapturing",

tests/ui/impl-trait/precise-capturing/migration-note.rs

+15
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,19 @@ fn returned() -> impl Sized {
187187
}
188188
//~^ NOTE `x` dropped here while still borrowed
189189

190+
fn capture_apit(x: &impl Sized) -> impl Sized {}
191+
//~^ NOTE in this expansion of desugaring of `impl Trait`
192+
193+
fn test_apit() {
194+
let x = String::new();
195+
//~^ NOTE binding `x` declared here
196+
let y = capture_apit(&x);
197+
//~^ NOTE borrow of `x` occurs here
198+
//~| NOTE this call may capture more lifetimes than intended
199+
drop(x);
200+
//~^ ERROR cannot move out of `x` because it is borrowed
201+
//~| NOTE move out of `x` occurs here
202+
}
203+
//~^ NOTE borrow might be used here, when `y` is dropped
204+
190205
fn main() {}

tests/ui/impl-trait/precise-capturing/migration-note.stderr

+31-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,37 @@ help: add a precise capturing bound to avoid overcapturing
278278
LL | fn display_len<T>(x: &Vec<T>) -> impl Display + use<T> {
279279
| ++++++++
280280

281-
error: aborting due to 12 previous errors
281+
error[E0505]: cannot move out of `x` because it is borrowed
282+
--> $DIR/migration-note.rs:199:10
283+
|
284+
LL | let x = String::new();
285+
| - binding `x` declared here
286+
LL |
287+
LL | let y = capture_apit(&x);
288+
| -- borrow of `x` occurs here
289+
...
290+
LL | drop(x);
291+
| ^ move out of `x` occurs here
292+
...
293+
LL | }
294+
| - borrow might be used here, when `y` is dropped and runs the destructor for type `impl Sized`
295+
|
296+
note: this call may capture more lifetimes than intended, because Rust 2024 has adjusted the `impl Trait` lifetime capture rules
297+
--> $DIR/migration-note.rs:196:13
298+
|
299+
LL | let y = capture_apit(&x);
300+
| ^^^^^^^^^^^^^^^^
301+
help: add a precise capturing bound to avoid overcapturing, after turning all argument-position impl traits into type parameters: `+ use</* Args */>`
302+
--> $DIR/migration-note.rs:190:36
303+
|
304+
LL | fn capture_apit(x: &impl Sized) -> impl Sized {}
305+
| ^^^^^^^^^^
306+
help: consider cloning the value if the performance cost is acceptable
307+
|
308+
LL | let y = capture_apit(&x.clone());
309+
| ++++++++
310+
311+
error: aborting due to 13 previous errors
282312

283313
Some errors have detailed explanations: E0499, E0502, E0503, E0505, E0506, E0597, E0716.
284314
For more information about an error, try `rustc --explain E0499`.

0 commit comments

Comments
 (0)