-
Notifications
You must be signed in to change notification settings - Fork 13k
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
caller_location: point to macro invocation sites, like file!/line!, and use in core::panic!. #65973
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,7 +98,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { | |
let intrinsic_name = &self.tcx.item_name(instance.def_id()).as_str()[..]; | ||
match intrinsic_name { | ||
"caller_location" => { | ||
let caller = self.tcx.sess.source_map().lookup_char_pos(span.lo()); | ||
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); | ||
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we de-dup this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice if we could pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could just do: fn caller(&self, span: Span) -> Whatever {
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
self.sess.source_map().lookup_char_pos(topmost.lo())
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where would you put this? Also, "caller" is misleading, in fact the name of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eddyb I think you are taking the snippet too literally; rename it to anything you see fit. It would be placed on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But, like, there is little generality in it. It's literally "caller_location helper" if it does those two things, and that doesn't feel right to put anywhere outside the query for it (which, again, would ideally take There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose, but it does make sure the logics don't grow apart. I guess we'll have to ensure that through reviews instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW "you could just do" is my least favorite way to phrase review feedback. @eddyb is correct that I had ICEs when using a Span as a parameter to a query. I agree with them that two callsites does not a need for deduplication make (yet). |
||
let location = self.alloc_caller_location( | ||
Symbol::intern(&caller.file.name.to_string()), | ||
caller.line as u32, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
// run-pass | ||
|
||
#![feature(core_intrinsics)] | ||
|
||
macro_rules! caller_location_from_macro { | ||
() => (core::intrinsics::caller_location()); | ||
} | ||
|
||
fn main() { | ||
let loc = core::intrinsics::caller_location(); | ||
assert_eq!(loc.file(), file!()); | ||
assert_eq!(loc.line(), 5); | ||
assert_eq!(loc.line(), 10); | ||
assert_eq!(loc.column(), 15); | ||
|
||
// `caller_location()` 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.column(), 16); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, I think a previous PR forgot to change the macro in
libstd
(which is not the same as the one in libcore). cc @anpThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anp told me it was intentional, as they use different entry-points, and we can probably wait to have full
#[track_caller]
before changinglibstd
.