wchilders created this revision.
wchilders added reviewers: Tyker, rsmith.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This fixes an issue where immediate invocations were queued inside of decltype, 
resulting in decltype's operand being evaluated.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76724

Files:
  clang/lib/Sema/SemaExpr.cpp
  clang/test/SemaCXX/decltype-consteval.cpp


Index: clang/test/SemaCXX/decltype-consteval.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/decltype-consteval.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s
+// expected-no-diagnostics
+
+struct MaybeConsteval {
+  MaybeConsteval* ptr = nullptr;
+
+  consteval MaybeConsteval(bool Valid) : ptr(this) {
+    if (Valid) {
+      ptr = nullptr;
+    }
+  }
+};
+
+consteval decltype(MaybeConsteval(false)) foo() {
+  return { true };
+}
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -15377,9 +15377,16 @@
   }
 }
 
+// Check to see if this expression is part of a decltype specifier.
+// We're not interested in evaluating this expression, immediately or 
otherwise.
+static bool isInDeclType(Sema &SemaRef) {
+  return SemaRef.ExprEvalContexts.back().ExprContext ==
+         Sema::ExpressionEvaluationContextRecord::EK_Decltype;
+}
+
 ExprResult Sema::CheckForImmediateInvocation(ExprResult E, FunctionDecl *Decl) 
{
   if (!E.isUsable() || !Decl || !Decl->isConsteval() || isConstantEvaluated() 
||
-      RebuildingImmediateInvocation)
+      isInDeclType(*this) || RebuildingImmediateInvocation)
     return E;
 
   /// Opportunistically remove the callee from ReferencesToConsteval if we can.


Index: clang/test/SemaCXX/decltype-consteval.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/decltype-consteval.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s
+// expected-no-diagnostics
+
+struct MaybeConsteval {
+  MaybeConsteval* ptr = nullptr;
+
+  consteval MaybeConsteval(bool Valid) : ptr(this) {
+    if (Valid) {
+      ptr = nullptr;
+    }
+  }
+};
+
+consteval decltype(MaybeConsteval(false)) foo() {
+  return { true };
+}
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -15377,9 +15377,16 @@
   }
 }
 
+// Check to see if this expression is part of a decltype specifier.
+// We're not interested in evaluating this expression, immediately or otherwise.
+static bool isInDeclType(Sema &SemaRef) {
+  return SemaRef.ExprEvalContexts.back().ExprContext ==
+         Sema::ExpressionEvaluationContextRecord::EK_Decltype;
+}
+
 ExprResult Sema::CheckForImmediateInvocation(ExprResult E, FunctionDecl *Decl) {
   if (!E.isUsable() || !Decl || !Decl->isConsteval() || isConstantEvaluated() ||
-      RebuildingImmediateInvocation)
+      isInDeclType(*this) || RebuildingImmediateInvocation)
     return E;
 
   /// Opportunistically remove the callee from ReferencesToConsteval if we can.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to