[clang] [clang][bytecode] Diagnose destructors called on string pointers (PR #227283)

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 29 04:55:55 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

They are immutable, so diagnose accordingly.

---
Full diff: https://github.com/llvm/llvm-project/pull/227283.diff


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Interp.cpp (+8) 
- (modified) clang/test/AST/ByteCode/invalid.cpp (+14) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 132c4ae92be41..08b78e2030f13 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1957,6 +1957,14 @@ bool checkDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
   if (S.checkingConstantDestruction(Ptr))
     return true;
 
+  // String pointers are immutable, so can't call a destructor on them.
+  if (Ptr.isStringPointer()) {
+    S.FFDiag(S.Current->getSource(OpPC),
+             diag::note_constexpr_access_unreadable_object)
+        << AK_Destroy << Ptr.toDiagnosticString(S.getASTContext());
+    return false;
+  }
+
   // Can't call a dtor on a global variable.
   if (Ptr.isOpaquePointer() || Ptr.block()->isStatic()) {
     const SourceInfo &E = S.Current->getSource(OpPC);
diff --git a/clang/test/AST/ByteCode/invalid.cpp b/clang/test/AST/ByteCode/invalid.cpp
index 52c91f46ac16d..cf859eb75dd90 100644
--- a/clang/test/AST/ByteCode/invalid.cpp
+++ b/clang/test/AST/ByteCode/invalid.cpp
@@ -313,3 +313,17 @@ namespace UnsizedArrayAndNonEmptyPath {
 
   void bar() { foo(); }
 }
+
+namespace StringLiteralDtor {
+  template <typename T> constexpr void foo(T *t) {
+    t->~T(); // both-note {{destruction of object}}
+  }
+  constexpr bool bar() {
+    foo(&"baz"[0]); // both-note {{in call to}} \
+                    // ref-note {{temporary created here}}
+    return true;
+  }
+
+  static_assert(bar(), ""); // both-error {{not an integral constant expression}} \
+                            // both-note {{in call to}}
+}

``````````

</details>


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


More information about the cfe-commits mailing list