[PATCH] D76724: Prevent immediate evaluations inside of decltype

Wyatt Childers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 24 12:54:04 PDT 2020


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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76724.252401.patch
Type: text/x-patch
Size: 1410 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200324/6f1950bc/attachment.bin>


More information about the cfe-commits mailing list