-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 eprint!
and eprintln!
macros to the prelude.
#41192
Conversation
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
src/libstd/macros.rs
Outdated
/// Macro for printing to the standard error. | ||
/// | ||
/// Equivalent to the `print!` macro, except that output goes to | ||
/// `io::stderr()` instead of `io::stdout()`. See `print!` for |
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.
we don't use ()
after function names
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.
Fixed.
src/libstd/macros.rs
Outdated
/// | ||
/// # Panics | ||
/// | ||
/// Panics if writing to `io::stderr()` fails. |
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.
in general, it'd be good to link up all the stuff here:
print!
io::stderr
io::stdout
io::stderr
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.
Could you be more specific? I don't understand what you have in mind, besides some degree of cross-reference hyperlinks.
src/libstd/macros.rs
Outdated
/// | ||
/// # Panics | ||
/// | ||
/// Panics if writing to `io::stderr()` fails. |
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.
same comments here
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.
Also fixed
src/libstd/io/stdio.rs
Outdated
// 2. If the local stderr is currently in use (e.g. we're in the middle of | ||
// already printing) then accessing again would cause a panic. | ||
// | ||
// If, however, the actual I/O causes an error, we do indeed panic. |
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.
Instead of line-for-line copying the comment above in _print
, can this and _print
bove either share an implementation or reduce duplication?
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.
Yes, the implementation can be factored out.
📌 Commit 1d00098 has been approved by |
src/libstd/io/mod.rs
Outdated
@@ -290,6 +290,8 @@ pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; | |||
pub use self::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; | |||
#[stable(feature = "rust1", since = "1.0.0")] | |||
pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; | |||
#[unstable(feature = "eprint", issue="40528")] |
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 should probably be feature = "eprint_internal"
.
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 believe that is incorrect. See how _print
, immediately above, is marked stable, not unstable(print_internal)?
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.
Well strictly speaking the import for _print
shouldn't be marked stable but I don't think stability attributes on imports have any effect anyway so I guess it doesn't really matter.
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.
Stability on pub use
is ignored, it's just advisory for readers.
src/libstd/macros.rs
Outdated
/// | ||
/// Panics if writing to `io::stderr` fails. | ||
#[macro_export] | ||
#[unstable(feature = "eprint", issue="40528")] |
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'm pretty sure stability attributes don't apply the macro_rules
macros so these should be stable
.
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 dunno if the compiler can enforce it, but it seems worthwhile for documentation reasons anyway?
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.
If you can use these marcos in stable rust then why would you want the documentation to say otherwise?
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.
@zackw the test you added has #![feature(eprint)]
, is that not necessary? (I assumed it was)
If not, let's change this to #[stable]
to reflect the (unforutnate) reality.
src/libstd/macros.rs
Outdated
macro_rules! eprintln { | ||
() => (eprint!("\n")); | ||
($fmt:expr) => (eprint!(concat!($fmt, "\n"))); | ||
($fmt:expr, $($arg:tt)*) => (eprint!(concat!($fmt, "\n"), $($arg)*)); |
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 suffers from #30143 just like println
and writeln
. Maybe the aim was to copy println
bug for bug but in case it wasn't, these last two rules can be replaced with ($($arg:tt)*) => (eprint!("{}\n", format_args!($($arg)*)));
to get sensible behaviour.
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 aim is, in fact, bug-for-bug equivalence with println
. If this is to be changed, it should be changed via #30143, for all of the printing macros at once.
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 disagree. The reason println
and writeln
were not fixed was to avoid breaking existing programs. That isn't a concern here.
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.
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.
@ollie27's suggested change to the macro does work, but I am surprised that it works - it appears to be a type error. I am reluctant to do something that may only work by accident. It also appears to have double formatting overhead, which is not desirable. (It is disappointing that macro_rules! macros have no direct way to specify that a particular token tree should be a string literal. Freaking CPP can do this.)
But more importantly, I really do think eprintln
should be bug-for-bug compatible with println
, because people should be able to go through existing programs and change println
to eprintln
where appropriate, without thinking about it apart from "should this properly go to stderr?" Again, if this is to be changed, it should be changed for both println
and eprintln
at the same time.
@bors: r- ( active comments ) |
☔ The latest upstream changes (presumably #41411) made this pull request unmergeable. Please resolve the merge conflicts. |
Fixed up the merge conflicts and the stability annotations. For now, have not changed the |
I still personally feel that we should fix #30143 if we can for eprint/eprintln. @ollie27's suggestion works and I believe not accidentally so, so it should be fine to insert. I also don't really think performance is much of an issue here, I also disagree that this should have the same bug to make transitioning easier. It's highly unlikely to exhibit #30143 anyway and it's an opportunity to make it more idiomatic Rust by using a format string. |
☔ The latest upstream changes (presumably #41437) made this pull request unmergeable. Please resolve the merge conflicts. |
@zackw & @alexcrichton - is there a decision on the #30143 question? |
@zackw do you feel firmly this should be bug-for-bug compatible? If so I can bring this up with the libs team to see what they think as well. |
It is my considered opinion that either we should implement the bug in If we believe that the bug in On the other hand, if we believe that there is no need for |
Ok, in that case let's get some more opinions! cc @rust-lang/libs |
Even if we don't fix the bug in |
I'm ambivalent, mostly because this seems like a very minor issue. (I personally don't really have a problem with the current Given that, as far as I know, there aren't any footguns here, I'd personally lean toward matching the "bug" in the new macros. (I have yet to see a compelling argument that this "bug" is problematic, aside from being inconsistent with |
Nominating for discussion at the next libs triage (next Tuesday). Sorry for the delay! |
Even if we think the existing println! behavior is undesirable, my inclination is to keep the behavior consistent between println! and eprintln! to reduce the possibility for surprises. |
* Factor out the nigh-identical bodies of `_print` and `_eprint` to a helper function `print_to` (I was sorely tempted to call it `_doprnt`). * Update the issue number for the unstable `eprint` feature. * Add entries to the "unstable book" for `eprint` and `eprint_internal`. * Style corrections to the documentation.
@alexcrichton Rebased now. Let me know if you also want it squashed. |
📌 Commit 4ab3bcb has been approved by |
⌛ Testing commit 4ab3bcb with merge 9e9f527... |
💔 Test failed - status-travis |
Looks like the new test is failing on emscripten only; there's not enough detail for me to be sure, but I suspect the problem is emscripten doesn't actually have a stderr. Any objection to skipping the test there? |
Oh I think it's just that emscripten can't spawn new processes, you'll probably just want to ignore the test on emscripten |
er didn't mean to close |
@bors: r+ |
📌 Commit 72588a2 has been approved by |
Add `eprint!` and `eprintln!` macros to the prelude. These are exactly the same as `print!` and `println!` except that they write to stderr instead of stdout. Issues rust-lang#39228 and rust-lang#40528; previous PR rust-lang#39229; accepted RFC rust-lang/rfcs#1869; proposed revision to The Book rust-lang/book#615. I have _not_ revised this any since the original submission; I will do that later this week. I wanted to get this PR in place since it's been quite a while since the RFC was merged. Known outstanding review comments: * [x] @steveklabnik requested a new chapter for the unstable version of The Book -- please see if the proposed revisions to the second edition cover it. * [x] @nodakai asked if it were possible to merge the internal methods `_print` and `_eprint` - not completely, since they both refer to different internal globals which we don't want to expose, but I will see if some duplication can be factored out. Please let me know if I missed anything.
Add `eprint!` and `eprintln!` macros to the prelude. These are exactly the same as `print!` and `println!` except that they write to stderr instead of stdout. Issues rust-lang#39228 and rust-lang#40528; previous PR rust-lang#39229; accepted RFC rust-lang/rfcs#1869; proposed revision to The Book rust-lang/book#615. I have _not_ revised this any since the original submission; I will do that later this week. I wanted to get this PR in place since it's been quite a while since the RFC was merged. Known outstanding review comments: * [x] @steveklabnik requested a new chapter for the unstable version of The Book -- please see if the proposed revisions to the second edition cover it. * [x] @nodakai asked if it were possible to merge the internal methods `_print` and `_eprint` - not completely, since they both refer to different internal globals which we don't want to expose, but I will see if some duplication can be factored out. Please let me know if I missed anything.
These are exactly the same as
print!
andprintln!
except that they write to stderr instead of stdout. Issues #39228 and #40528; previous PR #39229; accepted RFC rust-lang/rfcs#1869; proposed revision to The Book rust-lang/book#615.I have not revised this any since the original submission; I will do that later this week. I wanted to get this PR in place since it's been quite a while since the RFC was merged.
Known outstanding review comments:
_print
and_eprint
- not completely, since they both refer to different internal globals which we don't want to expose, but I will see if some duplication can be factored out.Please let me know if I missed anything.