Skip to content

Commit b1b388e

Browse files
committed
fix NLL TLS end of function spans
1 parent c705b7d commit b1b388e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
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))]

0 commit comments

Comments
 (0)