rust-playground icon indicating copy to clipboard operation
rust-playground copied to clipboard

Writing non-utf8 data to stdout suppresses compiler messages

Open trentj opened this issue 7 years ago • 3 comments

Here's a simple example:

use std::io::{stdout, Write};
fn main() {
    stdout().write(b"\x91");
}

The only output, in the "Errors" section, is the message Sandbox operation failed: Output was not valid UTF-8: invalid utf-8 sequence of 1 bytes from index 0, which, although true, does not contain any compiler warnings or confirmation that compilation was successful. (At first, I thought this might be a connectivity issue or something of that nature.)

I want to see compiler output regardless of whether there is a problem with the actual program stdout. In this case, if you replace \x91 with some valid UTF-8 string, you see the following in the "Standard Error" section, which was absent in the original example:

   Compiling playground v0.0.1 (file:///playground)
warning: unused `std::result::Result` which must be used
 --> src/main.rs:3:5
  |
3 |     stdout().write(b"\x61");
  |     ^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(unused_must_use)] on by default
  = note: this `Result` may be an `Err` variant, which should be handled

    Finished dev [unoptimized + debuginfo] target(s) in 1.31s
     Running `target/debug/playground`

trentj avatar Aug 22 '18 12:08 trentj

This just lead to lots of confusion in https://github.com/rust-lang/rust/issues/58776 because I did not realize that it was playground showing the error, instead of rustc or the application.

@shepmaster what about using from_utf8_lossy to handle non-UTF-8 output more gracefully?

RalfJung avatar Feb 27 '19 14:02 RalfJung

I think something along the lines of from_utf8 and on failure try from_utf8_lossy and add some text to the returned warnings would be reasonable.

shepmaster avatar Feb 27 '19 15:02 shepmaster

Curiously, running the same example program in Miri does not suppress any output. Instead it prints the compiler warnings, and then the program output as

RalfJung avatar Apr 07 '20 12:04 RalfJung