Skip to content

Commit 3efecba

Browse files
authored
Rollup merge of #109307 - cjgillot:inline-location, r=compiler-errors
Ignore `Inlined` spans when computing caller location. Fixes #105538
2 parents d86fd83 + be8b323 commit 3efecba

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
14751475
) -> OperandRef<'tcx, Bx::Value> {
14761476
let tcx = bx.tcx();
14771477

1478-
let mut span_to_caller_location = |span: Span| {
1478+
let mut span_to_caller_location = |mut span: Span| {
1479+
// Remove `Inlined` marks as they pollute `expansion_cause`.
1480+
while span.is_inlined() {
1481+
span.remove_mark();
1482+
}
14791483
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
14801484
let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo());
14811485
let const_loc = tcx.const_caller_location((

compiler/rustc_const_eval/src/interpret/intrinsics/caller_location.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
111111
location
112112
}
113113

114-
pub(crate) fn location_triple_for_span(&self, span: Span) -> (Symbol, u32, u32) {
114+
pub(crate) fn location_triple_for_span(&self, mut span: Span) -> (Symbol, u32, u32) {
115+
// Remove `Inlined` marks as they pollute `expansion_cause`.
116+
while span.is_inlined() {
117+
span.remove_mark();
118+
}
115119
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
116120
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
117121
(

compiler/rustc_span/src/hygiene.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ impl Span {
880880
pub fn fresh_expansion(self, expn_id: LocalExpnId) -> Span {
881881
HygieneData::with(|data| {
882882
self.with_ctxt(data.apply_mark(
883-
SyntaxContext::root(),
883+
self.ctxt(),
884884
expn_id.to_expn_id(),
885885
Transparency::Transparent,
886886
))
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-pass
22
// revisions: default mir-opt
3+
//[default] compile-flags: -Zinline-mir=no
34
//[mir-opt] compile-flags: -Zmir-opt-level=4
45

56
macro_rules! caller_location_from_macro {
@@ -9,13 +10,13 @@ macro_rules! caller_location_from_macro {
910
fn main() {
1011
let loc = core::panic::Location::caller();
1112
assert_eq!(loc.file(), file!());
12-
assert_eq!(loc.line(), 10);
13+
assert_eq!(loc.line(), 11);
1314
assert_eq!(loc.column(), 15);
1415

1516
// `Location::caller()` in a macro should behave similarly to `file!` and `line!`,
1617
// i.e. point to where the macro was invoked, instead of the macro itself.
1718
let loc2 = caller_location_from_macro!();
1819
assert_eq!(loc2.file(), file!());
19-
assert_eq!(loc2.line(), 17);
20+
assert_eq!(loc2.line(), 18);
2021
assert_eq!(loc2.column(), 16);
2122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// run-pass
2+
// revisions: default mir-opt
3+
//[default] compile-flags: -Zinline-mir=no
4+
//[mir-opt] compile-flags: -Zmir-opt-level=4
5+
6+
use std::panic::Location;
7+
8+
macro_rules! f {
9+
() => {
10+
Location::caller()
11+
};
12+
}
13+
14+
#[inline(always)]
15+
fn g() -> &'static Location<'static> {
16+
f!()
17+
}
18+
19+
fn main() {
20+
let loc = g();
21+
assert_eq!(loc.line(), 16);
22+
assert_eq!(loc.column(), 5);
23+
}

0 commit comments

Comments
 (0)