aaron.ballman created this revision.
aaron.ballman added reviewers: cor3ntin, erichkeane, bruno, rsmith.
Herald added a project: All.
aaron.ballman requested review of this revision.
Herald added a project: clang.

We were not creating an evaluation context for the TU scope, so we never popped 
an evaluation context for it. Popping the evaluation context triggers a number 
of diagnostics, including warnings about immediate invocations that we were 
previously missing.

Note: I think we have an additional issue that we should solve, but not as part 
of this patch. I don't think Clang is properly modeling static initialization 
as happening before constant expression evaluation. I think structure members 
members are zero initialized per http://eel.is/c++draft/basic.start.static#1, 
https://eel.is/c++draft/basic.start.static#2.sentence-2, and 
http://eel.is/c++draft/dcl.init#general-6.2 and the new test case actually 
should be accepted. However, it's also worth noting that other compilers behave 
the way this patch makes Clang behave: https://godbolt.org/z/T7noqhdPr


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D121211

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Parse/ParseAST.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp


Index: clang/test/SemaCXX/cxx2a-consteval.cpp
===================================================================
--- clang/test/SemaCXX/cxx2a-consteval.cpp
+++ clang/test/SemaCXX/cxx2a-consteval.cpp
@@ -689,3 +689,13 @@
   }
 };
 } // PR48235
+
+namespace NamespaceScopeConsteval {
+struct S {
+  int Val; // expected-note {{subobject declared here}}
+  consteval S() {}
+};
+
+S s; // expected-error {{call to consteval function 
'NamespaceScopeConsteval::S::S' is not a constant expression}} \
+        expected-note {{subobject of type 'int' is not initialized}}
+} // namespace NamespaceScopeConsteval
Index: clang/lib/Parse/ParseAST.cpp
===================================================================
--- clang/lib/Parse/ParseAST.cpp
+++ clang/lib/Parse/ParseAST.cpp
@@ -155,6 +155,9 @@
     P.Initialize();
     Parser::DeclGroupPtrTy ADecl;
     Sema::ModuleImportState ImportState;
+    EnterExpressionEvaluationContext PotentiallyEvaluated(
+        S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
+
     for (bool AtEOF = P.ParseFirstTopLevelDecl(ADecl, ImportState); !AtEOF;
          AtEOF = P.ParseTopLevelDecl(ADecl, ImportState)) {
       // If we got a null return and something *was* parsed, ignore it.  This
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -118,6 +118,8 @@
 
 C++20 Feature Support
 ^^^^^^^^^^^^^^^^^^^^^
+- Diagnose consteval and constexpr issues that happen at namespace scope. This
+  partially addresses `Issue 51593 
<https://github.com/llvm/llvm-project/issues/51593>`_.
 
 C++2b Feature Support
 ^^^^^^^^^^^^^^^^^^^^^


Index: clang/test/SemaCXX/cxx2a-consteval.cpp
===================================================================
--- clang/test/SemaCXX/cxx2a-consteval.cpp
+++ clang/test/SemaCXX/cxx2a-consteval.cpp
@@ -689,3 +689,13 @@
   }
 };
 } // PR48235
+
+namespace NamespaceScopeConsteval {
+struct S {
+  int Val; // expected-note {{subobject declared here}}
+  consteval S() {}
+};
+
+S s; // expected-error {{call to consteval function 'NamespaceScopeConsteval::S::S' is not a constant expression}} \
+        expected-note {{subobject of type 'int' is not initialized}}
+} // namespace NamespaceScopeConsteval
Index: clang/lib/Parse/ParseAST.cpp
===================================================================
--- clang/lib/Parse/ParseAST.cpp
+++ clang/lib/Parse/ParseAST.cpp
@@ -155,6 +155,9 @@
     P.Initialize();
     Parser::DeclGroupPtrTy ADecl;
     Sema::ModuleImportState ImportState;
+    EnterExpressionEvaluationContext PotentiallyEvaluated(
+        S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
+
     for (bool AtEOF = P.ParseFirstTopLevelDecl(ADecl, ImportState); !AtEOF;
          AtEOF = P.ParseTopLevelDecl(ADecl, ImportState)) {
       // If we got a null return and something *was* parsed, ignore it.  This
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -118,6 +118,8 @@
 
 C++20 Feature Support
 ^^^^^^^^^^^^^^^^^^^^^
+- Diagnose consteval and constexpr issues that happen at namespace scope. This
+  partially addresses `Issue 51593 <https://github.com/llvm/llvm-project/issues/51593>`_.
 
 C++2b Feature Support
 ^^^^^^^^^^^^^^^^^^^^^
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to