tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, tahonermann, shafik.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147621

Files:
  clang/lib/AST/Interp/Interp.cpp
  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
@@ -664,6 +664,28 @@
 };
 #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}}
+  }
+}
+
 /// FIXME: The following tests are broken.
 ///   They are using CXXDefaultInitExprs which contain a CXXThisExpr. The This 
pointer
 ///   in those refers to the declaration we are currently initializing, *not* 
the
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;


Index: clang/test/AST/Interp/records.cpp
===================================================================
--- clang/test/AST/Interp/records.cpp
+++ clang/test/AST/Interp/records.cpp
@@ -664,6 +664,28 @@
 };
 #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}}
+  }
+}
+
 /// FIXME: The following tests are broken.
 ///   They are using CXXDefaultInitExprs which contain a CXXThisExpr. The This pointer
 ///   in those refers to the declaration we are currently initializing, *not* the
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;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to