Skip to content

Fix a couple NLL TLS spans #136477

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3112,12 +3112,24 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
drop_span, borrow_span
);

// `TerminatorKind::Return`'s span (the `drop_span` here) `lo` can be subtly wrong and point
// at a single character after the end of the function. This is somehow relied upon in
// existing diagnostics, and changing this in `rustc_mir_build` makes diagnostics worse in
// general. We fix these here.
let sm = self.infcx.tcx.sess.source_map();
let end_of_function = if drop_span.is_empty()
&& let Ok(adjusted_span) = sm.span_extend_prev_while(drop_span, |c| c == '}')
{
adjusted_span
} else {
drop_span
};
self.thread_local_value_does_not_live_long_enough(borrow_span)
.with_span_label(
borrow_span,
"thread-local variables cannot be borrowed beyond the end of the function",
)
.with_span_label(drop_span, "end of enclosing function is here")
.with_span_label(end_of_function, "end of enclosing function is here")
}

#[instrument(level = "debug", skip(self))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0712]: thread-local variable borrowed past end of function
LL | assert_static(&FOO);
| ^^^^ thread-local variables cannot be borrowed beyond the end of the function
LL | }
| - end of enclosing function is here
| - end of enclosing function is here

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-17954.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | let a = &FOO;
| ^^^^ thread-local variables cannot be borrowed beyond the end of the function
...
LL | }
| - end of enclosing function is here
| - end of enclosing function is here

error: aborting due to 1 previous error

Expand Down
Loading