-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
A macro to get the current function name. #2818
Conversation
# Reference-level explanation | ||
[reference-level-explanation]: #reference-level-explanation | ||
|
||
Use of the `function!` macro expands to the compiler's internal name for the function. This will generally be the name that the user wrote into the file but in the case of a closure or similar it will be something like the function's name with a unique suffix. |
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.
What about a closure in a static?
static F: &(dyn Fn() -> &'static str + Sync) = &|| "1";
(the fully-qualified name is currently playground::F::{{closure}}
)
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.
@eddyb know this one (i hope)
👍 Pie-in-the-sky: It would be nice to have a more flexible feature where you could have a function automatically receive its caller's file/line/function name/etc., like C++ recently added with |
That actually was requested earlier today: #2815 |
@comex This is already being implemented rust-lang/rust#65881 |
For debug information about what's happening within the program there's several useful macros that you might use. One of them is the `function!` macro, which expands to the name of the current function. If used outside of a function it causes a compilation error. | ||
|
||
# Reference-level explanation | ||
[reference-level-explanation]: #reference-level-explanation |
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.
This section needs some elaboration to consider the various cases (e.g. like the one @kennytm raised) and give examples of code and what the result is.
Also, looking at the implementation of module_path!
in libsyntax_ext
https://github.com/rust-lang/rust/blob/9e8c4e6fb1c952048fb823e59f4c9c6487bf9a58/src/libsyntax_ext/source_util.rs#L65-L73 it looks like the information is readily available. In this case however, the information is not available in cx
or reachable fields. Some elaboration on the implementation would be good. cc @petrochenkov since they are the most likely T-compiler reviewer for this RFC. Also, this probably has little to do with "debuginfo" -- that's a different part of the compiler that comes into play much later in the compilation process.
Who is going to implement this RFC by the way? I think we should have someone available to do that, with mentoring if necessary (if so a mentor is required, e.g. @petrochenkov), before accepting the RFC. |
I have a reference implementation of this feature. https://github.com/da-x/rust/tree/function-macro-1.38.0 It's working, but would probably need some changes to conform to the RFC. |
Hmm; it feels fairly invasive to add |
@Centril I agree regarding having |
What! Now that's the kind of pleasant surprise that comes rarely. Hmm... but if that is already being implemented, might it make sense to not add a new macro, but instead just add the function name to It seems like On the other hand, it may be worth adding a macro just to avoid confusing people who see the macros but don't know about |
Co-Authored-By: Mazdak Farrokhzad <twingoow@gmail.com>
Co-Authored-By: Mazdak Farrokhzad <twingoow@gmail.com>
Co-Authored-By: Mazdak Farrokhzad <twingoow@gmail.com>
Updated based on current feedback. I'm extremely convinced by the |
# Prior art | ||
[prior-art]: #prior-art | ||
|
||
* C99 has a `__func__` pre-processor macro that expands to the current funciton name. [link](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1642.html) |
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.
(Luckily) This is not a pre-processor macro, just a predefined identifier provided by the C compiler
I'm in favor of |
This time you write it Josh ;3 |
I wonder if specifying this in terms of |
@nagisa That would presumably need to account for substitutions to polymorphic functions as well as considering nested |
If we're bikeshedding the name, something like |
Also, |
this is in wip? |
No, this is pretty dead I'd have to say. The idea isn't fundamentally bad, it's just we all got busy with other things. I'll close this for now. |
In the meantime we can use |
@ArekPiekarz Is the latest version (0.3.0) still working for that purpose? I'm getting the following while trying it out: % cargo build
Compiling stdext v0.3.0
error[E0391]: cycle detected when caching mir of `num::integer::<impl at /Users/rvalls/.cargo/registry/src/github.com-1ecc6299db9ec823/stdext-0.3.0/src/num/integer.rs:274:9: 440:10>::BITS` for CTFE
--> /Users/rvalls/.cargo/registry/src/github.com-1ecc6299db9ec823/stdext-0.3.0/src/num/integer.rs:277:13
|
277 | const BITS: u32 = Self::BITS;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
445 | impl_integer!(u8, u16, u32, u64, u128);
| --------------------------------------- in this macro invocation
|
= note: ...which requires normalizing `<u8 as num::integer::Integer>::BITS`...
note: ...which requires simplifying constant for the type system `num::integer::<impl at /Users/rvalls/.cargo/registry/src/github.com-1ecc6299db9ec823/stdext-0.3.0/src/num/integer.rs:274:9: 440:10>::BITS`...
--> /Users/rvalls/.cargo/registry/src/github.com-1ecc6299db9ec823/stdext-0.3.0/src/num/integer.rs:277:13
|
277 | const BITS: u32 = Self::BITS;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
445 | impl_integer!(u8, u16, u32, u64, u128);
| --------------------------------------- in this macro invocation
note: ...which requires simplifying constant for the type system `num::integer::<impl at /Users/rvalls/.cargo/registry/src/github.com-1ecc6299db9ec823/stdext-0.3.0/src/num/integer.rs:274:9: 440:10>::BITS`...
--> /Users/rvalls/.cargo/registry/src/github.com-1ecc6299db9ec823/stdext-0.3.0/src/num/integer.rs:277:13
|
277 | const BITS: u32 = Self::BITS;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
445 | impl_integer!(u8, u16, u32, u64, u128);
| --------------------------------------- in this macro invocation
note: ...which requires const-evaluating + checking `num::integer::<impl at /Users/rvalls/.cargo/registry/src/github.com-1ecc6299db9ec823/stdext-0.3.0/src/num/integer.rs:274:9: 440:10>::BITS`...
--> /Users/rvalls/.cargo/registry/src/github.com-1ecc6299db9ec823/stdext-0.3.0/src/num/integer.rs:277:13
|
277 | const BITS: u32 = Self::BITS;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
445 | impl_integer!(u8, u16, u32, u64, u128);
| --------------------------------------- in this macro invocation
= note: ...which again requires caching mir of `num::integer::<impl at /Users/rvalls/.cargo/registry/src/github.com-1ecc6299db9ec823/stdext-0.3.0/src/num/integer.rs:274:9: 440:10>::BITS` for CTFE, completing the cycle
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error
For more information about this error, try `rustc --explain E0391`.
error: could not compile `stdext`
To learn more, run the command again with --verbose. In the meantime I reckon that https://stackoverflow.com/a/63904992/457116 might be a better alternative until this RFC gets merged/implemented for good. |
@brainstorm I'm not the author of that crate, but I just tested that it still works in a simple program. Can you share code which exhibits the problem? |
@ArekPiekarz I don't have much time to cook a proper small reproducer like I used to in https://github.com/brainstorm/dotenv-codegen-rustc-hang, but here's a 2 line start if you want to have a crack at it: git clone https://github.com/umccr/aws-benchmarks && cd aws-benchmarks
git checkout 560e531b9bd263f73bbf547eb27f7e2485461c3c && cargo build ... maybe some interaction of the other crates I'm pulling in? Don't know 🤷🏻♂️ |
@brainstorm I have built your project with your instructions without any errors. Here is the log - https://pastebin.com/m86af0pz |
Yep, that was it! I was running with |
The lack of official support is a bit of a shame. |
So...has Rust just given up on this? Are we doomed to depend on a separate crate for this trivial functionality as well? |
rendered