https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97566

            Bug ID: 97566
           Summary: [[no_unique_address]] causes miscompiles when mixed
                    with EBO in constexpr context
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: natattak at gmail dot com
  Target Milestone: ---

Example: (https://godbolt.org/z/Y5q5br)

  #include <memory>
  #include <cassert>

  // error disappears if E doesn't inherit from B
  struct B {};
  struct E : B {};

  struct counter {
    constexpr counter() = default;
    constexpr void inc() {
      size++;
    }

    // error disappears if you remove or reorder this value
    int unused = 0;
    int size = 0;
    [[no_unique_address]] E empty = {};
  };

  constexpr int test() {
    counter x;
    x.inc();
    return x.size;
  }

  int main() {
    assert(test() == 1);        // works, unless optimisations enabled
    static_assert(test() == 1); // fails, always
  }

In particular, the runtime assertion only passes on -O0; any higher
optimisation e.g. -O1 causes it to fail.

Compiled as `g++ -std=c++20`.

Reply via email to