[clang] 2334314 - Do not check if we are in a discared context in non-immediate contexts

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 7 09:14:18 PST 2021


Author: Corentin Jabot
Date: 2021-12-07T12:13:35-05:00
New Revision: 2334314550724812bb94e36c6b5df7d665bdbd9d

URL: https://github.com/llvm/llvm-project/commit/2334314550724812bb94e36c6b5df7d665bdbd9d
DIFF: https://github.com/llvm/llvm-project/commit/2334314550724812bb94e36c6b5df7d665bdbd9d.diff

LOG: Do not check if we are in a discared context in non-immediate contexts

This fixes in a regression introduced by 6eeda06c1.

When deducing the return type of nested function calls, only the
return type of the outermost expression should be ignored.

Instead of assuming all contextes nested in a discared statements
are themselves discarded, only assume that in immediate contexts.

Similarly, only consider contextes immediately in an immediate or
discarded statement as being themselves immediate.

Added: 
    

Modified: 
    clang/include/clang/Sema/Sema.h
    clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 40c0b549968dc..8182d25295c5f 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -1324,12 +1324,15 @@ class Sema final {
 
     bool isImmediateFunctionContext() const {
       return Context == ExpressionEvaluationContext::ImmediateFunctionContext ||
-             InImmediateFunctionContext;
+             (Context == ExpressionEvaluationContext::DiscardedStatement &&
+              InImmediateFunctionContext);
     }
 
     bool isDiscardedStatementContext() const {
       return Context == ExpressionEvaluationContext::DiscardedStatement ||
-             InDiscardedStatement;
+             (Context ==
+                  ExpressionEvaluationContext::ImmediateFunctionContext &&
+              InDiscardedStatement);
     }
   };
 

diff  --git a/clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp b/clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp
index ed61119dc252a..72a61874ed038 100644
--- a/clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp
+++ b/clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp
@@ -159,4 +159,19 @@ a:  if constexpr(sizeof(n) == 4) // expected-error {{redefinition}} expected-not
       surprise: {}
   }
 }
+
+namespace deduced_return_type_in_discareded_statement {
+
+template <typename T>
+auto a(const T &t) {
+  return t;
+}
+
+void f() {
+  if constexpr (false) {
+    a(a(0));
+  }
+}
+} // namespace deduced_return_type_in_discareded_statement
+
 #endif


        


More information about the cfe-commits mailing list