Skip to content

Commit

Permalink
Make it translatable too
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jul 11, 2024
1 parent 03bee1e commit 42653c0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_infer/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ infer_opaque_hidden_type =
infer_outlives_bound = lifetime of the source pointer does not outlive lifetime bound of the object type
infer_outlives_content = lifetime of reference outlives lifetime of borrowed content...
infer_precise_capturing_existing = add `{$new_lifetime}` to the `use<...>` bound to explicitly capture it
infer_precise_capturing_new = add a `use<...>` bound to explicitly capture `{$new_lifetime}`
infer_prlf_defined_with_sub = the lifetime `{$sub_symbol}` defined here...
infer_prlf_defined_without_sub = the lifetime defined here...
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)
Expand Down
29 changes: 29 additions & 0 deletions compiler/rustc_infer/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1581,3 +1581,32 @@ pub enum ObligationCauseFailureCode {
subdiags: Vec<TypeErrorAdditionalDiags>,
},
}

#[derive(Subdiagnostic)]
pub enum AddPreciseCapturing {
#[suggestion(
infer_precise_capturing_new,
style = "verbose",
code = " + use<{concatenated_bounds}>",
applicability = "machine-applicable"
)]
New {
#[primary_span]
span: Span,
new_lifetime: Symbol,
concatenated_bounds: String,
},
#[suggestion(
infer_precise_capturing_existing,
style = "verbose",
code = "{pre}{new_lifetime}{post}",
applicability = "machine-applicable"
)]
Existing {
#[primary_span]
span: Span,
new_lifetime: Symbol,
pre: &'static str,
post: &'static str,
},
}
23 changes: 10 additions & 13 deletions compiler/rustc_infer/src/infer/error_reporting/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1288,20 +1288,18 @@ fn suggest_precise_capturing<'tcx>(
_ => None,
});

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

diag.span_suggestion_verbose(
insertion_span,
format!("add `{new_lifetime}` to the `use<...>` bound to explicitly capture it",),
format!("{pre}{new_lifetime}{post}"),
Applicability::MachineApplicable,
);
diag.subdiagnostic(errors::AddPreciseCapturing::Existing { span, new_lifetime, pre, post });
} else {
let mut captured_lifetimes = FxIndexSet::default();
let mut captured_non_lifetimes = FxIndexSet::default();
Expand Down Expand Up @@ -1349,11 +1347,10 @@ fn suggest_precise_capturing<'tcx>(
.collect::<Vec<_>>()
.join(", ");

diag.span_suggestion_verbose(
tcx.def_span(opaque_def_id).shrink_to_hi(),
format!("add a `use<...>` bound to explicitly capture `{new_lifetime}`",),
format!(" + use<{concatenated_bounds}>"),
Applicability::MachineApplicable,
);
diag.subdiagnostic(errors::AddPreciseCapturing::New {
span: tcx.def_span(opaque_def_id).shrink_to_hi(),
new_lifetime,
concatenated_bounds,
});
}
}

0 comments on commit 42653c0

Please sign in to comment.