[clang] 213e03c - [Clang] Fix handling of immediate escalation for inherited constructors (#112860)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 22 13:00:20 PST 2025
Author: cor3ntin
Date: 2025-01-22T22:00:17+01:00
New Revision: 213e03ca1174177370715a8776a6423ee29b10ca
URL: https://github.com/llvm/llvm-project/commit/213e03ca1174177370715a8776a6423ee29b10ca
DIFF: https://github.com/llvm/llvm-project/commit/213e03ca1174177370715a8776a6423ee29b10ca.diff
LOG: [Clang] Fix handling of immediate escalation for inherited constructors (#112860)
Fixes #112677
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/include/clang/Sema/Sema.h
clang/lib/AST/Decl.cpp
clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index cad17c1b3957b6..c749e34d6d2c5d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -971,6 +971,7 @@ Bug Fixes to C++ Support
- Fixed canonicalization of pack indexing types - Clang did not always recognized identical pack indexing. (#GH123033)
- Fixed a nested lambda substitution issue for constraint evaluation. (#GH123441)
- Fixed various false diagnostics related to the use of immediate functions. (#GH123472)
+- Fix immediate escalation not propagating through inherited constructors. (#GH112677)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 9fa33d6ca76ba5..9a9998b114e0f7 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -13086,7 +13086,7 @@ class Sema final : public SemaBase {
auto *FD = dyn_cast<FunctionDecl>(DC);
S.PushFunctionScope();
S.PushExpressionEvaluationContext(
- (FD && FD->isConsteval())
+ (FD && FD->isImmediateFunction())
? ExpressionEvaluationContext::ImmediateFunctionContext
: ExpressionEvaluationContext::PotentiallyEvaluated);
if (FD) {
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index a1a51d38b93e1f..ddde16ada5af88 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -3283,6 +3283,11 @@ bool FunctionDecl::isImmediateEscalating() const {
// consteval specifier,
if (isDefaulted() && !isConsteval())
return true;
+
+ if (auto *CD = dyn_cast<CXXConstructorDecl>(this);
+ CD && CD->isInheritingConstructor())
+ return CD->getInheritedConstructor().getConstructor();
+
// - a function that results from the instantiation of a templated entity
// defined with the constexpr specifier.
TemplatedKind TK = getTemplatedKind();
@@ -3303,6 +3308,12 @@ bool FunctionDecl::isImmediateFunction() const {
if (isImmediateEscalating() && BodyContainsImmediateEscalatingExpressions())
return true;
+ if (auto *CD = dyn_cast<CXXConstructorDecl>(this);
+ CD && CD->isInheritingConstructor())
+ return CD->getInheritedConstructor()
+ .getConstructor()
+ ->isImmediateFunction();
+
if (const auto *MD = dyn_cast<CXXMethodDecl>(this);
MD && MD->isLambdaStaticInvoker())
return MD->getParent()->getLambdaCallOperator()->isImmediateFunction();
diff --git a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
index 378414f1361729..3f3123eaee76b6 100644
--- a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
+++ b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
@@ -496,3 +496,35 @@ struct Y {
template void g<Y>();
}
+
+namespace GH112677 {
+
+class ConstEval {
+ public:
+ consteval ConstEval(int); // expected-note 2{{declared here}}
+};
+
+struct TemplateCtor {
+ ConstEval val;
+ template <class Anything = int> constexpr
+ TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
+};
+struct C : TemplateCtor {
+ using TemplateCtor::TemplateCtor; // expected-note {{in call to 'TemplateCtor<int>(0)'}}
+};
+
+C c(0); // expected-note{{in implicit initialization for inherited constructor of 'C'}}
+// expected-error at -1 {{call to immediate function 'GH112677::C::TemplateCtor' is not a constant expression}}
+
+struct SimpleCtor { constexpr SimpleCtor(int) {}};
+struct D : SimpleCtor {
+ int y = 10;
+ ConstEval x = y; // expected-note {{undefined constructor 'ConstEval'}}
+ using SimpleCtor::SimpleCtor;
+ //expected-note at -1 {{'SimpleCtor' is an immediate constructor because the default initializer of 'x' contains a call to a consteval constructor 'ConstEval' and that call is not a constant expression}}
+};
+
+D d(0); // expected-note {{in implicit initialization for inherited constructor of 'D'}}
+// expected-error at -1 {{call to immediate function 'GH112677::D::SimpleCtor' is not a constant expression}}
+
+}
More information about the cfe-commits
mailing list