Skip to content

Commit 46b18a9

Browse files
authored
Rollup merge of #136477 - lqd:nll-tls-spans, r=matthewjasper
Fix a couple NLL TLS spans Some NLL TLS tests show incorrect spans for the end of function. It seems that the `TerminatorKind::Return` source info span can sometimes point at the single character after the end of the function. Completely changing the span where the terminator is built also changes a bunch of diagnostics: small functions have more code shown unrelated to the errors at hand, wrapping symbols appear and weird-looking arrows point to the end of function, etc. So it seems this is somehow unexpectedly relied upon in making diagnostics look better and their heuristics. So I just changed it where it matters for these few tests: the diagnostics specialized to conflict errors with thread locals. r? `@matthewjasper`
2 parents b07fa76 + d2a30f7 commit 46b18a9

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -3112,12 +3112,24 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
31123112
drop_span, borrow_span
31133113
);
31143114

3115+
// `TerminatorKind::Return`'s span (the `drop_span` here) `lo` can be subtly wrong and point
3116+
// at a single character after the end of the function. This is somehow relied upon in
3117+
// existing diagnostics, and changing this in `rustc_mir_build` makes diagnostics worse in
3118+
// general. We fix these here.
3119+
let sm = self.infcx.tcx.sess.source_map();
3120+
let end_of_function = if drop_span.is_empty()
3121+
&& let Ok(adjusted_span) = sm.span_extend_prev_while(drop_span, |c| c == '}')
3122+
{
3123+
adjusted_span
3124+
} else {
3125+
drop_span
3126+
};
31153127
self.thread_local_value_does_not_live_long_enough(borrow_span)
31163128
.with_span_label(
31173129
borrow_span,
31183130
"thread-local variables cannot be borrowed beyond the end of the function",
31193131
)
3120-
.with_span_label(drop_span, "end of enclosing function is here")
3132+
.with_span_label(end_of_function, "end of enclosing function is here")
31213133
}
31223134

31233135
#[instrument(level = "debug", skip(self))]

tests/ui/borrowck/borrowck-thread-local-static-borrow-outlives-fn.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0712]: thread-local variable borrowed past end of function
44
LL | assert_static(&FOO);
55
| ^^^^ thread-local variables cannot be borrowed beyond the end of the function
66
LL | }
7-
| - end of enclosing function is here
7+
| - end of enclosing function is here
88

99
error: aborting due to 1 previous error
1010

tests/ui/issues/issue-17954.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let a = &FOO;
55
| ^^^^ thread-local variables cannot be borrowed beyond the end of the function
66
...
77
LL | }
8-
| - end of enclosing function is here
8+
| - end of enclosing function is here
99

1010
error: aborting due to 1 previous error
1111

0 commit comments

Comments
 (0)