Skip to content

Ignore Inlined spans when computing caller location. #109307

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

Merged
merged 1 commit into from
Mar 20, 2023
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
6 changes: 5 additions & 1 deletion compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
) -> OperandRef<'tcx, Bx::Value> {
let tcx = bx.tcx();

let mut span_to_caller_location = |span: Span| {
let mut span_to_caller_location = |mut span: Span| {
// Remove `Inlined` marks as they pollute `expansion_cause`.
while span.is_inlined() {
span.remove_mark();
}
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo());
let const_loc = tcx.const_caller_location((
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
location
}

pub(crate) fn location_triple_for_span(&self, span: Span) -> (Symbol, u32, u32) {
pub(crate) fn location_triple_for_span(&self, mut span: Span) -> (Symbol, u32, u32) {
// Remove `Inlined` marks as they pollute `expansion_cause`.
while span.is_inlined() {
span.remove_mark();
}
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_span/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ impl Span {
pub fn fresh_expansion(self, expn_id: LocalExpnId) -> Span {
HygieneData::with(|data| {
self.with_ctxt(data.apply_mark(
SyntaxContext::root(),
self.ctxt(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this has any (probably positive) effects on diagnostics.

expn_id.to_expn_id(),
Transparency::Transparent,
))
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/rfc-2091-track-caller/intrinsic-wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-pass
// revisions: default mir-opt
//[default] compile-flags: -Zinline-mir=no
//[mir-opt] compile-flags: -Zmir-opt-level=4

macro_rules! caller_location_from_macro {
Expand All @@ -9,13 +10,13 @@ macro_rules! caller_location_from_macro {
fn main() {
let loc = core::panic::Location::caller();
assert_eq!(loc.file(), file!());
assert_eq!(loc.line(), 10);
assert_eq!(loc.line(), 11);
assert_eq!(loc.column(), 15);

// `Location::caller()` in a macro should behave similarly to `file!` and `line!`,
// i.e. point to where the macro was invoked, instead of the macro itself.
let loc2 = caller_location_from_macro!();
assert_eq!(loc2.file(), file!());
assert_eq!(loc2.line(), 17);
assert_eq!(loc2.line(), 18);
assert_eq!(loc2.column(), 16);
}
23 changes: 23 additions & 0 deletions tests/ui/rfc-2091-track-caller/mir-inlined-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// run-pass
// revisions: default mir-opt
//[default] compile-flags: -Zinline-mir=no
//[mir-opt] compile-flags: -Zmir-opt-level=4

use std::panic::Location;

macro_rules! f {
() => {
Location::caller()
};
}

#[inline(always)]
fn g() -> &'static Location<'static> {
f!()
}

fn main() {
let loc = g();
assert_eq!(loc.line(), 16);
assert_eq!(loc.column(), 5);
}