[PATCH] D143466: [clang][Interp] Fix initializing base class members
Aaron Ballman via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 1 07:49:06 PST 2023
aaron.ballman added inline comments.
================
Comment at: clang/test/AST/Interp/records.cpp:260
+ class _C : public _B {};
+ constexpr _C c{12};
+};
----------------
We should test that the initialization actually happened.
================
Comment at: clang/test/AST/Interp/records.cpp:262
+};
+#endif
+
----------------
Another test would be for invalid base initialization, like:
```
struct Base {
int a;
};
struct Intermediate : Base {
int b;
};
struct Final : Intermediate {
int c;
constexpr Final(int a, int b, int c) : c(c) {}
};
static_assert(Final{1, 2, 3}.c == 3, ""); // OK
static_assert(Final{1, 2, 3}.a == 0, ""); // Error, reads uninitialized member
```
or with multiple bases:
```
struct Base {
int a;
};
struct Mixin {
int b;
constexpr Mixin() = default;
constexpr Mixin(int b) : b(b) {}
};
struct Final : Base, Mixin {
int c;
constexpr Final(int a, int b, int c) : Mixin(b), c(c) {}
constexpr Final(int a, int b, int c, bool) : c(c) {}
};
static_assert(Final{1, 2, 3}.c == 3, ""); // OK
static_assert(Final{1, 2, 3}.b == 2, ""); // OK
static_assert(Final{1, 2, 3}.a == 0, ""); // Error, reads uninitialized member
```
or in a different form:
```
struct Base {
int a;
};
struct Mixin {
int b;
};
struct Final : Base, Mixin {
int c;
constexpr Final(int a, int b, int c) : c(c) { this->b = b; }
constexpr Final(int a, int b, int c, bool) : c(c) {}
};
static_assert(Final{1, 2, 3}.c == 3, ""); // OK
static_assert(Final{1, 2, 3}.b == 2, ""); // OK
static_assert(Final{1, 2, 3}.a == 0, ""); // Error, reads uninitialized member
```
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D143466/new/
https://reviews.llvm.org/D143466
More information about the cfe-commits
mailing list