[PATCH] D147621: [clang][Interp] Start handling mutable record members

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu May 4 04:31:40 PDT 2023


tbaeder updated this revision to Diff 519434.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147621/new/

https://reviews.llvm.org/D147621

Files:
  clang/lib/AST/Interp/Interp.cpp
  clang/lib/AST/Interp/Interp.h
  clang/test/AST/Interp/records.cpp


Index: clang/test/AST/Interp/records.cpp
===================================================================
--- clang/test/AST/Interp/records.cpp
+++ clang/test/AST/Interp/records.cpp
@@ -700,3 +700,26 @@
 
 };
 #endif
+
+namespace MutableFields {
+  class Foo {
+  public:
+    constexpr Foo() : I(1) {}
+    mutable int I; // ref-note {{declared here}}
+  };
+
+
+  constexpr int foo() {
+    const Foo F;
+    F.I = 12;
+    return F.I;
+  }
+  static_assert(foo() == 12, "");
+
+  /// FIXME: This should also be rejected in the new interpreter.
+  constexpr Foo GlobalF;
+  constexpr int foo2() { // ref-error {{never produces a constant expression}}
+    return GlobalF.I; // ref-note {{read of mutable member 'I' is not allowed in a constant expression}}
+  }
+}
+
Index: clang/lib/AST/Interp/Interp.h
===================================================================
--- clang/lib/AST/Interp/Interp.h
+++ clang/lib/AST/Interp/Interp.h
@@ -1057,7 +1057,12 @@
     return false;
   if (!CheckRange(S, OpPC, Ptr, CSK_Field))
     return false;
-  S.Stk.push<Pointer>(Ptr.atField(Off));
+
+  const Pointer &FieldPtr = Ptr.atField(Off);
+  if (!CheckMutable(S, OpPC, FieldPtr))
+    return false;
+
+  S.Stk.push<Pointer>(FieldPtr);
   return true;
 }
 
Index: clang/lib/AST/Interp/Interp.cpp
===================================================================
--- clang/lib/AST/Interp/Interp.cpp
+++ clang/lib/AST/Interp/Interp.cpp
@@ -224,6 +224,14 @@
     return true;
   }
 
+  // Writing to mutable fields is fine even if the parent is
+  // declared const.
+  if (S.getLangOpts().CPlusPlus14) {
+    if (const auto *FD = dyn_cast<FieldDecl>(Ptr.getFieldDesc()->asDecl());
+        FD && FD->isMutable())
+      return true;
+  }
+
   const QualType Ty = Ptr.getType();
   const SourceInfo &Loc = S.Current->getSource(OpPC);
   S.FFDiag(Loc, diag::note_constexpr_modify_const_type) << Ty;
@@ -236,6 +244,11 @@
     return true;
   }
 
+  // In C++14 onwards, it is permitted to read a mutable member whose
+  // lifetime began within the evaluation.
+  if (S.getLangOpts().CPlusPlus14)
+    return true;
+
   const SourceInfo &Loc = S.Current->getSource(OpPC);
   const FieldDecl *Field = Ptr.getField();
   S.FFDiag(Loc, diag::note_constexpr_access_mutable, 1) << AK_Read << Field;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147621.519434.patch
Type: text/x-patch
Size: 2293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230504/a37c2e41/attachment.bin>


More information about the cfe-commits mailing list