[clang] [clang][ExprConst] Don't try to evaluate value-dependent DeclRefExprs (PR #67778)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 4 23:27:37 PDT 2023


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/67778

>From 55a0f4abbb9ad9270aa18e2eec08b4a0bfb4dae0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 29 Sep 2023 11:23:55 +0200
Subject: [PATCH] [clang][ExprConst] Don't try to evaluate value-dependent
 DeclRefExprs

The Expression here migth be value dependent, which makes us run into an
assertion later on. Just bail out early.

Fixes #67690
---
 clang/docs/ReleaseNotes.rst                      |  3 +++
 clang/lib/AST/ExprConstant.cpp                   |  3 +++
 clang/test/SemaCXX/constant-expression-cxx1z.cpp | 13 +++++++++++++
 3 files changed, 19 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 21b76292bca965e..2362e479dfbbc5a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -339,6 +339,9 @@ Bug Fixes in This Version
   (`#64462 <https://github.com/llvm/llvm-project/issues/64462>`_)
 - Fixes a regression where the ``UserDefinedLiteral`` was not properly preserved
   while evaluating consteval functions. (`#63898 <https://github.com/llvm/llvm-project/issues/63898>`_).
+- Fix a crash when evaluating value-dependent structured binding
+  variables at compile time.
+  Fixes (`#67690 <https://github.com/llvm/llvm-project/issues/67690>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index a142ea7c47a4730..d0e27de743604da 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -3357,6 +3357,9 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
     return false;
   }
 
+  if (E->isValueDependent())
+    return false;
+
   // Dig out the initializer, and use the declaration which it's attached to.
   // FIXME: We should eventually check whether the variable has a reachable
   // initializing declaration.
diff --git a/clang/test/SemaCXX/constant-expression-cxx1z.cpp b/clang/test/SemaCXX/constant-expression-cxx1z.cpp
index 9335626a5c90a4f..c0766f70cf88158 100644
--- a/clang/test/SemaCXX/constant-expression-cxx1z.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx1z.cpp
@@ -177,3 +177,16 @@ namespace LambdaCallOp {
     p();
   }
 }
+
+// This used to crash due to an assertion failure,
+// see gh#67690
+namespace {
+  struct C {
+    int x;
+  };
+
+  template <const C *p> void f() {
+    const auto &[c] = *p;
+    &c; // expected-warning {{expression result unused}}
+  }
+}



More information about the cfe-commits mailing list