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

They are immutable, so diagnose accordingly.

>From 5d7a1d30bf72bfc11e0d3d3e2eb71227c50d411d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]>
Date: Tue, 29 Sep 2026 13:52:46 +0200
Subject: [PATCH] [clang][bytecode] Diagnose destructors called on string
 pointers

They are immutable, so diagnose accordingly.
---
 clang/lib/AST/ByteCode/Interp.cpp   |  8 ++++++++
 clang/test/AST/ByteCode/invalid.cpp | 14 ++++++++++++++
 2 files changed, 22 insertions(+)

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}}
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to