-
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
Add an unwrap! macro #1669
Add an unwrap! macro #1669
Conversation
18188ce
to
c37a267
Compare
We just moved from Alternative suggestion: add compiler support for forced MIR inlining of functions + some new MIR rvalue that gets turned into the function/line/col position of the inline location during such a forced inlining. This way no user code needs to be changed and we get the same advantage. |
Well, it doesn't effect code that uses
How would that look? Would the
|
And code that uses
Should be possible the way you described it. Maybe even more like the let line: u32;
let col: u32;
mir!(line = Inline(Line); col = Inline(Col)); |
Another alternative (from C++) is default function arguments1 (if they are evaluated at point of use):
1Which of course have numerous other applications. |
@petrochenkov That doesn't really work. The |
What specifically is the issue with |
Then |
Macros in method position would be nice for this too |
@BurntSushi: The issue with |
@TimNN Is that a deficiency in the current implementation that can be fixed? |
@BurntSushi even if it is fixed, if compiling in release mode, the backtrace might not yield any info that helps you, if enough functions got inlined. |
@BurntSushi I probably can be fixed and is tracked at rust-lang/rust#24346, to quote
|
I've wanted this before too, so I'm in favor of the basic idea. Have not read the RFC though. I'd much prefer if the macro was in method position though. The 'unwrappiness' is not the most important part of the whole expression, would rather not have it in front. |
I'm also very interested in distinguishing the 'expect' case from the 'sloppy coding' case. IOW, today I write 'unwrap' when I'm being sloppy, and 'expect' (often with an empty string) to mean, "I've thought about this and unwrapping is correct here". I would not want a single macro to mean both. |
Why would you use |
expect vs unwrap signals intent. When I write |
@petrochenkov was onto something, although this would have to be done with type parameters in Rust: impl<T> Option<T> {
fn unwrap<C: SourceContext = CallerSourceContext>(self) -> T {
match self {
Some(t) => t,
None => {
panic!("unwrap on empty Option at location: {}", C::default());
}
}
}
}
I've been told Haskell does something similar with type parameters. cc @solson |
@eddyb What I told you about before (I think) was the Another relevant thing I found uses GHC implicit parameters to provide call site location information, like your example: https://ghc.haskell.org/trac/ghc/wiki/ExplicitCallStack/ImplicitLocations Implicit parameters themselves are described at https://www.haskell.org/hugs/pages/users_guide/implicit-parameters.html, but call site locations are a compiler-magic special case of them. |
One problem is when you run your application normally, and it crashes unexpectedly in a not immediately reproducible way. Now you have to rerun with RUST_BACKTRACE over and over again until you can find a way to reproduce the crash before you can get source information about where the crash happened. |
Another prior art crate is the inner crate, which contains the |
Exactly. And anyway, what's the point of printing some random location inside libcore when we could instead print somewhere useful.
Interesting, but I'm a little uncomfortable that |
This is a rather small and bearable cost IMO (in the context of
I can appreciate that this is indeed a benefit. |
As a data point, we're having problems getting good crash reports in Servo because |
That can be fixed without changing the API, though. It might involve ugliness like adding a custom calling convention for unwrap-like functions (which would, of course, remain unstable forever), but I'd rather that than adding an Not to say the format string isn't cool, though. |
I think the line info is very my wanted. |
I'm kinda surprised by some of the negativity towards this. @notriddle would rather introduce a new calling convention then use a macro, @oli-obk would rather use What's so bad about using a macro? Is it just because it goes in-front rather than in the method position? That seems like a very minor complaint to me. |
@brson would you rather there was both an |
@canndrew Existing code gets 0 benefits with a macro, and you have to turn every caller into a macro too if you want to get the benefits transitively. |
Well it's not ideal that there'd be two ways of doing things - the old way and the new way. But adding a new calling convention still seems like a heavy-handed way of solving something that we already have a solution for. |
I wonder if it is possible to have an This would require the result of these three macros remain non-constant until the inline-always pass though (or becomes an expression referring to the CallerSourceContext), which could be a huge hack. |
Sorry, I didn't see the last part of your comment. Yes, that makes sense in the context of a MIR inlining pass. |
If we had a macro |
That wouldn't work with existing code, and it would be harder to use than a plain |
@notriddle true, it wouldn't help with existing code. Not sure about ease of use, e.g. compared to worrying about the order of inlining vs macro expansion. |
That should be transparent to the user; the only people who should have to worry about that are the libs and compiler teams. |
Yes. |
Adding a
I don't think it's hacky or unreasonably complex to add code that will be transformed by the |
My 2c: I agree with many on the thread that:
In general, I suspect that building consensus around macros as the stabilized solution to this problem is an uphill battle. I wonder if it's worth branching off a thread on internals to talk about other ways of solving this problem within |
🔔 This RFC is now entering its week-long final comment period 🔔 The libs team is leaning towards closing this along the line of @aturon's previous comment. It seems too late in the game to shift conventions as |
Rendered