[clang] [Clang] Restrict CWG1504 devirtualization to pointer arithmetic on record types. (PR #209593)

Yanzuo Liu via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 14 22:01:28 PDT 2026


================
@@ -11,6 +11,25 @@
 // cxx98-error at -1 {{variadic macros are a C99 feature}}
 #endif
 
+namespace cwg1504 { // cwg1504: 23
+#if __cplusplus >= 201103L
+  // CWG1504: Pointer arithmetic after derived-base conversion
+  struct Base { int x; };
+  struct Derived : Base { int y; };
+  constexpr Derived arr[2] = {};
+
+  // Pointer arithmetic on a base pointer into a derived array is UB,
+  // and the constexpr evaluator must diagnose it.
+  constexpr int test(int n) {
+    return ((const Base*)arr)[n].x; // #cwg1504-x
+  }
+  constexpr int bad = test(1);
+  // since-cxx11-error at -1 {{constexpr variable 'bad' must be initialized by a constant expression}}
+  // since-cxx11-note@#cwg1504-x {{cannot access field of pointer past the end of object}}
+  // since-cxx11-note at -3 {{in call to 'test(1)'}}
----------------
zwuis wrote:

```suggestion
    // since-cxx11-note@#cwg1504-x {{cannot access field of pointer past the end of object}}
    // since-cxx11-note at -3 {{in call to 'test(1)'}}
```

https://clang.llvm.org/docs/InternalsManual.html#c-defect-report-tests

https://github.com/llvm/llvm-project/pull/209593


More information about the cfe-commits mailing list