statiolake / proconio-rs

Apache License 2.0
126 stars 7 forks source link

fastout changes the standard output #14

Closed rchaser53 closed 4 years ago

rchaser53 commented 4 years ago

I use proconio to resolve atcoder problems. I found fastout changes the standard output in some cases. The example is below.

use proconio::fastout;

#[fastout]  // the standard output is changed when this line is commented out
fn main() {
  for i in 0..2 {
    if i % 2 == 0 {
      println!("-1");
      continue
    }

    let success = 0;
    match success {
      _ => { println!("1"); }
    }
  }
}

The following is an output.

1
-1

When I comment out fastout, the standard output is changed like below.

-1
1

I tried this code on the atcoder code test.

statiolake commented 4 years ago

Thanks. I confirmed the issue in AtCoder's code test, although this problem didn't happen in my local PC (Windows). I'll take a closer look later.

Edit With proconio-derive v0.1.6, it successfully (?) reproduced on Windows too. This comment was with proconio-derive v0.2.1.

statiolake commented 4 years ago

I found this bug in proconio-derive v0.1.6 bundled with proconio v0.3.6, the current version used in AtCoder. The newer version of proconio-derive, starting from v0.2.0 bundled with proconio v0.4.1, doesn't seem to have this bug. However this is not usable until AtCoder updates our crate.

The root cause of this problem was that I've forgot to replace println! in match arms: the code at that time was only replacing the expression part of match statement. https://github.com/statiolake/proconio-rs/blob/4b360be54cd23ed9ac2c7d4d7210d18af46e138c/proconio-derive/src/fastout.rs#L130 So inside match arms the println! macro was left original. When this println! is executed the previous output -1 is not yet printed since #[fastout] buffered that output. Then this println! shows 1 immediately (since normal println! does flush the buffer), so the resulting output seems opposite order. In 6fbd7f9c15aec56d046f9a0883d3bd376189a37b the code was rewritten so this mistake no longer exists.

qryxip commented 4 years ago

Same problem to #8? We've written about it in a Wiki in atcoder-rust-resources.

statiolake commented 4 years ago

Exactly the same issue... I completely forgot it, thanks @qryxip.