Skip to content

Commit 213e03c

Browse files
authored
[Clang] Fix handling of immediate escalation for inherited constructors (#112860)
Fixes #112677
1 parent 18e9d3d commit 213e03c

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ Bug Fixes to C++ Support
971971
- Fixed canonicalization of pack indexing types - Clang did not always recognized identical pack indexing. (#GH123033)
972972
- Fixed a nested lambda substitution issue for constraint evaluation. (#GH123441)
973973
- Fixed various false diagnostics related to the use of immediate functions. (#GH123472)
974+
- Fix immediate escalation not propagating through inherited constructors. (#GH112677)
974975

975976
Bug Fixes to AST Handling
976977
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13086,7 +13086,7 @@ class Sema final : public SemaBase {
1308613086
auto *FD = dyn_cast<FunctionDecl>(DC);
1308713087
S.PushFunctionScope();
1308813088
S.PushExpressionEvaluationContext(
13089-
(FD && FD->isConsteval())
13089+
(FD && FD->isImmediateFunction())
1309013090
? ExpressionEvaluationContext::ImmediateFunctionContext
1309113091
: ExpressionEvaluationContext::PotentiallyEvaluated);
1309213092
if (FD) {

clang/lib/AST/Decl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,6 +3283,11 @@ bool FunctionDecl::isImmediateEscalating() const {
32833283
// consteval specifier,
32843284
if (isDefaulted() && !isConsteval())
32853285
return true;
3286+
3287+
if (auto *CD = dyn_cast<CXXConstructorDecl>(this);
3288+
CD && CD->isInheritingConstructor())
3289+
return CD->getInheritedConstructor().getConstructor();
3290+
32863291
// - a function that results from the instantiation of a templated entity
32873292
// defined with the constexpr specifier.
32883293
TemplatedKind TK = getTemplatedKind();
@@ -3303,6 +3308,12 @@ bool FunctionDecl::isImmediateFunction() const {
33033308
if (isImmediateEscalating() && BodyContainsImmediateEscalatingExpressions())
33043309
return true;
33053310

3311+
if (auto *CD = dyn_cast<CXXConstructorDecl>(this);
3312+
CD && CD->isInheritingConstructor())
3313+
return CD->getInheritedConstructor()
3314+
.getConstructor()
3315+
->isImmediateFunction();
3316+
33063317
if (const auto *MD = dyn_cast<CXXMethodDecl>(this);
33073318
MD && MD->isLambdaStaticInvoker())
33083319
return MD->getParent()->getLambdaCallOperator()->isImmediateFunction();

clang/test/SemaCXX/cxx2b-consteval-propagate.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,35 @@ struct Y {
496496
template void g<Y>();
497497

498498
}
499+
500+
namespace GH112677 {
501+
502+
class ConstEval {
503+
public:
504+
consteval ConstEval(int); // expected-note 2{{declared here}}
505+
};
506+
507+
struct TemplateCtor {
508+
ConstEval val;
509+
template <class Anything = int> constexpr
510+
TemplateCtor(int arg) : val(arg) {} // expected-note {{undefined constructor 'ConstEval'}}
511+
};
512+
struct C : TemplateCtor {
513+
using TemplateCtor::TemplateCtor; // expected-note {{in call to 'TemplateCtor<int>(0)'}}
514+
};
515+
516+
C c(0); // expected-note{{in implicit initialization for inherited constructor of 'C'}}
517+
// expected-error@-1 {{call to immediate function 'GH112677::C::TemplateCtor' is not a constant expression}}
518+
519+
struct SimpleCtor { constexpr SimpleCtor(int) {}};
520+
struct D : SimpleCtor {
521+
int y = 10;
522+
ConstEval x = y; // expected-note {{undefined constructor 'ConstEval'}}
523+
using SimpleCtor::SimpleCtor;
524+
//expected-note@-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}}
525+
};
526+
527+
D d(0); // expected-note {{in implicit initialization for inherited constructor of 'D'}}
528+
// expected-error@-1 {{call to immediate function 'GH112677::D::SimpleCtor' is not a constant expression}}
529+
530+
}

0 commit comments

Comments
 (0)