This is an archive of the discontinued LLVM Phabricator instance.

[clang][Interp] This pointers are writable in constructors
ClosedPublic

Authored by tbaeder on Oct 26 2022, 2:32 AM.

Diff Detail

Event Timeline

tbaeder created this revision.Oct 26 2022, 2:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 26 2022, 2:32 AM
tbaeder requested review of this revision.Oct 26 2022, 2:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 26 2022, 2:32 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaron.ballman added inline comments.Feb 6 2023, 7:43 AM
clang/lib/AST/Interp/Interp.cpp
227–233

The same is true for destructors as well: https://godbolt.org/z/a49aEErz8

tbaeder updated this revision to Diff 495147.Feb 6 2023, 8:19 AM
tbaeder marked an inline comment as done.
tbaeder added inline comments.
clang/lib/AST/Interp/Interp.cpp
227–233

Oh, good catch, thanks.

shafik added inline comments.Feb 16 2023, 7:18 PM
clang/lib/AST/Interp/Interp.cpp
227–233

Interesting case: https://godbolt.org/z/5r5fdh9jr

Implementation divergence, have to figure out who is correct here.

shafik added inline comments.Feb 16 2023, 7:31 PM
clang/lib/AST/Interp/Interp.cpp
227–233

I believe clang is correct: https://eel.is/c++draft/expr.const#4.8

tbaeder added inline comments.Feb 16 2023, 11:43 PM
clang/lib/AST/Interp/Interp.cpp
227–233

The output is the same with the new constant interpreter; that case is probably handled before it's constant evaluated.

aaron.ballman added inline comments.Feb 28 2023, 11:51 AM
clang/lib/AST/Interp/Interp.cpp
228–232

Combining predicates; NFC

clang/test/AST/Interp/cxx20.cpp
221–230

I'd feel more comfortable if we had some way to validate that the write to a in both of these cases actually caused the correct value to appear in a. We're just validating that this compiles and doesn't crash, but we're not validating that the actual behavior occurs. How about adding tests like:

template <bool Good>
struct ctor_test {
  int a = 0;

  constexpr ctor_test() {
    if (Good)
      a = 10;
    int local = 100 / a;
  }
};

template <bool Good>
struct dtor_test {
  int a = 0;

  constexpr dtor_test() = default;
  constexpr ~dtor_test() {
    if (Good)
      a = 10;
    int local = 100 / a;
  }
};

constexpr ctor_test<true> good_ctor;
constexpr dtor_test<true> good_dtor;

constexpr ctor_test<false> bad_ctor;
constexpr dtor_test<false> bad_dtor;
tbaeder updated this revision to Diff 501380.Feb 28 2023, 10:53 PM
tbaeder marked 2 inline comments as done.

Added the new test case.

This revision is now accepted and ready to land.Mar 1 2023, 6:15 AM