708 icon indicating copy to clipboard operation
708 copied to clipboard

Heisenbug when compiling `f(move auto a, in auto b)` invoked with `f(move(s), s)`

Open hsutter opened this issue 5 years ago • 0 comments

The basic case is:

void move_in(move auto a, in auto b) { copy_from(a, b); }
// ...
move_in(std::move(s), s);

This bug seems fragile... each of the following repros sometimes crashes the compiler, sometimes fails compilation without crashing, and sometimes succeeds:

  • Simple case: https://cppx.godbolt.org/z/58ced4
  • Minor whitespace change of moving main's {: https://cppx.godbolt.org/z/e1e1cG
  • Minor renaming from move_in to f: https://cppx.godbolt.org/z/bf363e
  • Both renaming the function and moving the {: https://cppx.godbolt.org/z/3qcWP4

Just keep hitting Godbolt's "clear cache and recompile" button, and the answer for each changes from run to run of the compiler.

Original report: https://www.reddit.com/r/cpp/comments/j8tf9k/empirically_measuring_reducing_cs_accidental/g8zfd9x/

Note that with the Lifetime rules we'd get a warning that the two arguments alias. Still, even without that diagnostic we should not crash, but still translate the well-formed program with the semantics that the last use of parameter a (the copy_from(a,b) call) moves from a, and because b is passed by pointer (so will alias a in this code) and the evaluation of a and b are not indeterminately sequenced then the evaluation of b should copy either the original or moved-from state of the object.

hsutter avatar Nov 30 '20 19:11 hsutter