-
Notifications
You must be signed in to change notification settings - Fork 13.8k
[beta-1.91] Warn on future errors from temporary lifetimes shortening in Rust 1.92 #147056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: beta
Are you sure you want to change the base?
Changes from all commits
f1fbf1f
b7d8dfb
74ab7bd
5d1246e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ use rustc_middle::ty::print::Print; | |
use rustc_middle::ty::{self, Ty, TyCtxt}; | ||
use rustc_middle::{bug, span_bug}; | ||
use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult, MoveOutIndex}; | ||
use rustc_session::lint::builtin::MACRO_EXTENDED_TEMPORARY_SCOPES; | ||
use rustc_span::def_id::LocalDefId; | ||
use rustc_span::source_map::Spanned; | ||
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym}; | ||
|
@@ -1580,4 +1581,32 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { | |
pub(crate) fn local_excluded_from_unused_mut_lint(&self, index: Local) -> bool { | ||
self.local_name(index).is_none_or(|name| name.as_str().starts_with('_')) | ||
} | ||
|
||
/// Report a temporary whose scope will shorten in Rust 1.92 due to #145838. | ||
pub(crate) fn lint_macro_extended_temporary_scope( | ||
&self, | ||
future_drop: Location, | ||
borrow: &BorrowData<'tcx>, | ||
) { | ||
let tcx = self.infcx.tcx; | ||
let temp_decl = &self.body.local_decls[borrow.borrowed_place.local]; | ||
let temp_span = temp_decl.source_info.span; | ||
let lint_root = self.body.source_scopes[temp_decl.source_info.scope] | ||
.local_data | ||
.as_ref() | ||
.unwrap_crate_local() | ||
.lint_root; | ||
|
||
let mut labels = MultiSpan::from_span(temp_span); | ||
labels.push_span_label(temp_span, "this expression creates a temporary value..."); | ||
labels.push_span_label( | ||
self.body.source_info(future_drop).span, | ||
"...which will be dropped at the end of this block in Rust 1.92", | ||
); | ||
|
||
tcx.node_span_lint(MACRO_EXTENDED_TEMPORARY_SCOPES, lint_root, labels, |diag| { | ||
diag.primary_message("temporary lifetime shortening in Rust 1.92"); | ||
diag.note("consider using a `let` binding to create a longer lived value"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've opted to use the "consider using a
Note the differing spans. In many instances, the conflicting use reported in diagnostics is an implicit reborrow rather than the actual use in the macro expansion. I think getting more helpful diagnostics would either require:
Another awkward aspect of that span imprecision is (without doing either of those things) it's hard to tell when adding a |
||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message seems like it'd be difficult to understand without context — to me, it almost seems like it's been cut off partway through. It might be better to be explicit here:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a bit difficult to understand as-is.. though I wonder if there's a way to write it without "this" in the primary message. My original take was "this temporary value's lifetime will shorten in Rust 1.92", but I moved away from it because something didn't feel right to me about the "this" and it felt redundant with the label. Maybe it would be fine?