-
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
RFC: Quick dbg!(expr) macro #2173
Conversation
text/0000-quick-debug-macro.md
Outdated
|
||
## On release builds: | ||
|
||
The same examples above will print nothing to `STDERR` and will instead simply |
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.
Why? dbg!()
calls don't seem like something that you'd want to keep around other than when you're looking into a specific bug.
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.
I think looking into a specific bug is the most likely scenario. However...
There might be some fix you made, but you are not 100% sure, and you'd still like to include it into release builds. With the macro having no effect on release
, this is possible.
Does this have any downsides?
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.
Spewing a bunch of stuff to stderr when a downstream crate isn't building with --release
is a downside.
Asserts are a more common way of handling those kinds of checks IME.
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.
I buy the argument regarding assert
for at least libraries. However, it might have some value for programs?
Does something like this exist in other languages? |
@sfackler Haskell has |
@sfackler (You could argue that) It is similar to: |
Yes please. I think the lesser verbosity compared to |
I want to restate the concern I had on IRC - I think that perhaps this macro should take a single format string argument, with a privileged "first" parameter: e.g. |
@sdleffler this seems more like traceShowId than trace, right? None of those examples wrap the output in |
@sfackler no, because |
@sfackler The format, including |
@sfackler see the "Reference-level Explanation": ideally I'd want to avoid that formatting and just let the user do it. It doesn't actually wrap the output in |
And also thus avoid that bikeshedding :P |
text/0000-quick-debug-macro.md
Outdated
the identity function on the expression, but the call will be inlined away such | ||
that the overhead is zero. | ||
|
||
The file name, line number and column is included for increased utility when included in production quality code. The expression is also stringified, so that |
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 does "production quality code" mean?
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.
Things ready to be included in programs & libraries run in production... This is arguably vague and subjective. It could be interpreted as things ready for cargo publish
, but probably not.
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.
I would argue that things that should be included in programs & libraries run in production should never be writing something like
[DEBUGGING, foobar.rs]
some-expr => 15
to stderr. If you want people to use your library, let them decide if and how they want to consume the debugging information you produce.
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 would probably only be included when developing/debugging a library, which the developer of the lib does.
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.
The "when included in production quality code" bit seems out of place if this wouldn't be used in production quality code.
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.
Should probably be reworded.
The intent is:
- In the case of a library: If the library is used in production and you are the developer/contributor to it, and you want to make some changes and debug those, the macro should be useful to you when doing debug builds.
- In the case of a program: The macro should be useful to you when doing debug builds and you shouldn't have to remove them in
--release
.
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.
But how is the production-ness of the library relevant in the first case?
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.
Changed production quality
to non-trivial
per discussion here and on IRC.
ping @bluss for non- |
text/0000-quick-debug-macro.md
Outdated
|
||
By using `dbg!(expr);`, the burden of a common papercut: writing `println!("{:?}", expr);` every time you want to see the evaluted-to value of an expression, can be significantly reduced. The developer no longer has to remember the formatting args and has to type significantly less (12 characters to be exact). | ||
|
||
The shortness is also beneficial when asking `playbot-mini` on #rust@irc.mozilla.org to evaluate and print an expression. |
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.
I think IRC bots would rather a very different output format from what's described currently:
- The
[DEBUGGING, src/main.rs:1:12]:
is clearly of no value there - Most bots only keep one or two lines (to avoid spamming), so
{:#?}
is worse than{:?}
- The expression is one the line right above, so isn't really worth showing again
playbot
(RIP) just used fn show<T:Debug>(x: T) -> T { print!("{:?}", x); x }
which worked great for it.
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.
I buy this =) I'll remove the argument from the RFC.
… notes on bikeshed
|
Maybe it should be sensitive to environment variables, like in the Can it be made enabled (by default) only from crate being actively developed (e.g current crate, it's examples, |
FWIW my use case for this is to sprinkle it during debugging and remove it before making a git commit. So any extra step required to get some visible output (setting an env variable, adding a crate dependency, typing a formatting string, …) is a hindrance. |
@SimonSapin , Then why bother removing it in release? What if debug and release take different code paths (due to timing, for example) and you are debugging it? |
@vi If timing matters, I think a considerably more rigorous approach to debugging & verification for the code under test is required. This makes this macro less interesting for those use cases, which is OK. |
Hello, I wrote some time ago a crate called One interesting thing about Another thing is that you can prefix with a Another is that you can do And another thing is being able to print many lines, like
|
@vi If an environment variable is used to opt-in to printing the macro will also be quite useless for the Rust Playground as there is no way to specify environment variables there (AFAIK), and if there were, it wouldn't be ergonomic for newcomers to the language or experienced rustaceans. |
Does that work? If EDIT: Of course, one can also see this as a benefit... |
Standard library macros can bypass stability. I would argue that WrapDebug should be omitted entirely in an initial proposal.
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable]
macro_rules! print {
($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)));
}
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
macro_rules! println {
() => (print!("\n"));
($fmt:expr) => (print!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
} |
Interesting... But what is the rationale for omitting WrapDebug initially? Also, if you expand
|
It can only be implemented through specialization which is not stable. There is currently nothing in the standard library API that exposes specialization.
It does not seem particularly reasonable to consider the complexity of an API by the transitive size of its dependencies. Are we going to include the |
Including If we don't include
Then simply consider the complexity of the |
I was specifically referring to macro_rules macros, but I think it's hopefully clear that format_args earns its complexity. It's one of the fundamental and most commonly used components of the standard library. |
Fair enough =) |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
This is a simpler and more opinionated counter-proposal to [RFC 2173](rust-lang#2173).
I’ve expressed before that I’d prefer this macro to be kept very simple (and in particular configuration-less). I wrote up a full counter-proposal at #2361. |
Is it a good idea to restrict |
@vi That sounds like a fine lint for Clippy or other project-specific linter, but maybe doesn’t belong in rustc or cargo. |
Irrespective of this RFC, I want to note that I find this comment to be unhelpful and also wrong.
|
@Centril: Meh. you're overreacting. I'm not here to bicker. I'm simply pointing out that I think that what started as a good idea has grown too much in complexity. It's easy to see how the complexity of the proposal has grown if you read the commit history. I think this is a valid thing to point out. Did not mean to hurt your feelings. |
This formulation is much better.
Don't worry; they were not hurt - while I would have wanted a more feature complete macro, I am excited to see My comment was more general about RFCs in general and not specific to this RFC. I think these points are important to understand how the RFC process works so that people don't see a lot of upvotes or downvotes as indicative of what the responsible subteam will do; if people have this perception, and argue about popularity, they will become disappointed. Moreover, arguments about popularity do not advance discussion or bring clarity into trade-offs. |
@Centril: I should choose my words more carefully. Thank you for all the work you put into this. |
@Centril I just want to thank you for the huge amount of work and patience here, regardless of the outcome. The detail in "formerly unresolved questions" is super valuable, and I wish that all RFCs contained such great summarizations. Your comparison to Over time, we've come to be increasingly conservative in our approach to
On the flip side, our fantastic packaging story with Cargo, together with other ongoing work on improving discoverability and curation in the ecosystem, allow us to provide a very strong user experience despite a lean So to be clear: I think everyone acknowledges that debugging ergonomics are extremely important, and worth investing in. But I think folks are skeptical that, even with this lengthy discussion, we've arrived at the Best Way that will work for everyone (even taking into account the provided knobs). To me, this seems like the perfect kind of thing to provide in the crates.io ecosystem, joining the ranks of many other crates providing highly sophisticated, ergonomic -- and competing -- answers to important development questions. |
The final comment period is now complete. |
As the FCP is now complete with a motion to close this RFC is now closed. The discussion on a more streamlined macro, roughly based on #2173 (comment) continues in RFC #2361. |
See RFC #2361 for the "second version" of this RFC
Adds a macro
dbg!(expr1 [, expr2, .., exprN])
for quick and dirtyDebug
:ing of expressions to the terminal. The macro evaluates expressions, prints it toSTDERR
, and finally yields a flat tuple of(expr1 [, expr2, .. exprN])
. On release builds, the macro is the identity function and has no side effects. The macro is added to the prelude of the standard library.Rendered.
Acknowledgements & ping
@scottmcm, @mbrubeck, @sdleffler, @setharnold