Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd an unwrap! macro #1669
Conversation
canndrew
force-pushed the
canndrew:unwrap_macro
branch
2 times, most recently
from
18188ce
to
c37a267
Jul 7, 2016
canndrew
force-pushed the
canndrew:unwrap_macro
branch
from
c37a267
to
26001f3
Jul 7, 2016
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
Well, it doesn't effect code that uses
How would that look? Would the
|
This comment has been minimized.
This comment has been minimized.
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)); |
This comment has been minimized.
This comment has been minimized.
|
Another alternative (from C++) is default function arguments1 (if they are evaluated at point of use):
1Which of course have numerous other applications. |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov That doesn't really work. The |
This comment has been minimized.
This comment has been minimized.
|
What specifically is the issue with |
This comment has been minimized.
This comment has been minimized.
Then |
This comment has been minimized.
This comment has been minimized.
|
Macros in method position would be nice for this too |
This comment has been minimized.
This comment has been minimized.
|
@BurntSushi: The issue with |
This comment has been minimized.
This comment has been minimized.
|
@TimNN Is that a deficiency in the current implementation that can be fixed? |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
@BurntSushi I probably can be fixed and is tracked at rust-lang/rust#24346, to quote
|
alexcrichton
added
the
T-libs
label
Jul 7, 2016
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
Why would you use |
This comment has been minimized.
This comment has been minimized.
expect vs unwrap signals intent. When I write |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
crumblingstatue
commented
Jul 12, 2016
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. |
This comment has been minimized.
This comment has been minimized.
diwic
commented
Jul 12, 2016
|
Another prior art crate is the inner crate, which contains the |
This comment has been minimized.
This comment has been minimized.
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 comment has been minimized.
This comment has been minimized.
This is a rather small and bearable cost IMO (in the context of
I can appreciate that this is indeed a benefit. |
This comment has been minimized.
This comment has been minimized.
asajeffrey
commented
Jul 13, 2016
|
As a data point, we're having problems getting good crash reports in Servo because |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
wuranbo
commented
Jul 15, 2016
•
|
I think the line info is very my wanted. |
wuranbo
referenced this pull request
Jul 15, 2016
Closed
Backtrace does not include file and line number on non-Linux platforms #24346
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
@brson would you rather there was both an |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
Sorry, I didn't see the last part of your comment. Yes, that makes sense in the context of a MIR inlining pass. |
This comment has been minimized.
This comment has been minimized.
asajeffrey
commented
Jul 15, 2016
|
If we had a macro |
This comment has been minimized.
This comment has been minimized.
That wouldn't work with existing code, and it would be harder to use than a plain |
This comment has been minimized.
This comment has been minimized.
asajeffrey
commented
Jul 15, 2016
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
That should be transparent to the user; the only people who should have to worry about that are the libs and compiler teams. |
This comment has been minimized.
This comment has been minimized.
Yes. |
This comment has been minimized.
This comment has been minimized.
Adding a
I don't think it's hacky or unreasonably complex to add code that will be transformed by the |
aturon
self-assigned this
Jul 22, 2016
This comment has been minimized.
This comment has been minimized.
|
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 |
aturon
added
the
I-nominated
label
Jul 25, 2016
This comment has been minimized.
This comment has been minimized.
|
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 |
canndrew commentedJul 7, 2016
Rendered