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
Try to write the panic message with a single write_all
call
#122565
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
The Miri subtree was changed cc @rust-lang/miri |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
library/std/src/panicking.rs
Outdated
let pos = cursor.position() as usize; | ||
err.write_all(&buffer[0..pos]) | ||
} else { | ||
write_msg(err) |
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.
write_msg(err) | |
// The message does not fit into the buffer, write it directly to stderr instead. | |
write_msg(err) |
library/std/src/panicking.rs
Outdated
writeln!(dst, "\nthread '{name}' panicked at {location}:\n{msg}") | ||
}; | ||
|
||
let _ = if write_msg(&mut cursor).is_ok() { |
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 it'd be less confusing to move the let _
into the arms (showing more clearly that it's just ignoring IO errors).
let mut cursor = crate::io::Cursor::new(&mut buffer[..]); | ||
|
||
let write_msg = |dst: &mut dyn crate::io::Write| { | ||
writeln!(dst, "\nthread '{name}' panicked at {location}:\n{msg}") |
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'd suggest to separate the functional change (more newlines) from the implementation change. Makes it easier to discuss and decide on these separately.
A leading newline is not very common in Rust output, so if it stays it definitely needs a comment explaining the purpose.
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.
Panic output isn't stable. There's some work to add thread IDs to the panic message for example.
Yeah the extra newline is unusual, but this is an unsynchronized write to a shared output so we don't have many options to clean up the formatting in cases where a panic is emitted in the middle of something else that's printing.
proc_macro_generated_packed.stderr
is a good example of this. It start printing the panic message in the middle of reporting another error.
Do you think it's disruptive enough that it warrants separate discussion?
Some changes occurred in run-make tests. cc @jieyouxu |
☔ The latest upstream changes (presumably #122267) made this pull request unmergeable. Please resolve the merge conflicts. |
assert_eq!(it.next().map(|l| l.is_empty()), Some(true)); | ||
assert_eq!(it.next().map(|l| l.starts_with("thread '<unnamed>' panicked")), Some(true)); | ||
assert_eq!(it.next().is_some(), true); | ||
assert_eq!(it.next(), Some("note: run with `RUST_BACKTRACE=1` \ | ||
environment variable to display a backtrace")); | ||
assert_eq!( | ||
it.next(), | ||
Some( | ||
"note: run with `RUST_BACKTRACE=1` \ | ||
environment variable to display a backtrace" | ||
) | ||
); | ||
assert_eq!(it.next().map(|l| l.is_empty()), Some(true)); |
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 it's better to filter out blank lines here so that the test isn't too sensitive to such changes.
The function name says it's supposed to test for the absence of backtraces, so blank lines shouldn't be relevant.
This writes the panic message to a buffer before writing to stderr. This allows it to be printed with a single
write_all
call, preventing it from being interleaved with other outputs. It also adds newlines before and after the message ensuring that only the panic message will have its own lines.Before:
After:
r? @the8472