Skip to content

Commit 42653c0

Browse files
Make it translatable too
1 parent 03bee1e commit 42653c0

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

compiler/rustc_infer/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ infer_opaque_hidden_type =
221221
222222
infer_outlives_bound = lifetime of the source pointer does not outlive lifetime bound of the object type
223223
infer_outlives_content = lifetime of reference outlives lifetime of borrowed content...
224+
225+
infer_precise_capturing_existing = add `{$new_lifetime}` to the `use<...>` bound to explicitly capture it
226+
infer_precise_capturing_new = add a `use<...>` bound to explicitly capture `{$new_lifetime}`
227+
224228
infer_prlf_defined_with_sub = the lifetime `{$sub_symbol}` defined here...
225229
infer_prlf_defined_without_sub = the lifetime defined here...
226230
infer_prlf_known_limitation = this is a known limitation that will be removed in the future (see issue #100013 <https://github.com/rust-lang/rust/issues/100013> for more information)

compiler/rustc_infer/src/errors/mod.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1581,3 +1581,32 @@ pub enum ObligationCauseFailureCode {
15811581
subdiags: Vec<TypeErrorAdditionalDiags>,
15821582
},
15831583
}
1584+
1585+
#[derive(Subdiagnostic)]
1586+
pub enum AddPreciseCapturing {
1587+
#[suggestion(
1588+
infer_precise_capturing_new,
1589+
style = "verbose",
1590+
code = " + use<{concatenated_bounds}>",
1591+
applicability = "machine-applicable"
1592+
)]
1593+
New {
1594+
#[primary_span]
1595+
span: Span,
1596+
new_lifetime: Symbol,
1597+
concatenated_bounds: String,
1598+
},
1599+
#[suggestion(
1600+
infer_precise_capturing_existing,
1601+
style = "verbose",
1602+
code = "{pre}{new_lifetime}{post}",
1603+
applicability = "machine-applicable"
1604+
)]
1605+
Existing {
1606+
#[primary_span]
1607+
span: Span,
1608+
new_lifetime: Symbol,
1609+
pre: &'static str,
1610+
post: &'static str,
1611+
},
1612+
}

compiler/rustc_infer/src/infer/error_reporting/region.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -1288,20 +1288,18 @@ fn suggest_precise_capturing<'tcx>(
12881288
_ => None,
12891289
});
12901290

1291-
let (insertion_span, pre, post) = if let Some(last_lifetime_span) = last_lifetime_span {
1291+
let (span, pre, post) = if let Some(last_lifetime_span) = last_lifetime_span {
12921292
(last_lifetime_span.shrink_to_hi(), ", ", "")
12931293
} else if let Some(first_param_span) = first_param_span {
12941294
(first_param_span.shrink_to_lo(), "", ", ")
12951295
} else {
1296+
// If we have no args, then have `use<>` and need to fall back to using
1297+
// span math. This sucks, but should be reliable due to the construction
1298+
// of the `use<>` span.
12961299
(span.with_hi(span.hi() - BytePos(1)).shrink_to_hi(), "", "")
12971300
};
12981301

1299-
diag.span_suggestion_verbose(
1300-
insertion_span,
1301-
format!("add `{new_lifetime}` to the `use<...>` bound to explicitly capture it",),
1302-
format!("{pre}{new_lifetime}{post}"),
1303-
Applicability::MachineApplicable,
1304-
);
1302+
diag.subdiagnostic(errors::AddPreciseCapturing::Existing { span, new_lifetime, pre, post });
13051303
} else {
13061304
let mut captured_lifetimes = FxIndexSet::default();
13071305
let mut captured_non_lifetimes = FxIndexSet::default();
@@ -1349,11 +1347,10 @@ fn suggest_precise_capturing<'tcx>(
13491347
.collect::<Vec<_>>()
13501348
.join(", ");
13511349

1352-
diag.span_suggestion_verbose(
1353-
tcx.def_span(opaque_def_id).shrink_to_hi(),
1354-
format!("add a `use<...>` bound to explicitly capture `{new_lifetime}`",),
1355-
format!(" + use<{concatenated_bounds}>"),
1356-
Applicability::MachineApplicable,
1357-
);
1350+
diag.subdiagnostic(errors::AddPreciseCapturing::New {
1351+
span: tcx.def_span(opaque_def_id).shrink_to_hi(),
1352+
new_lifetime,
1353+
concatenated_bounds,
1354+
});
13581355
}
13591356
}

0 commit comments

Comments
 (0)