Skip to content

Commit 6a99326

Browse files
[clang] Consistently handle consteval constructors for variables. (#144970)
443377a handled simple variable definitions, but it didn't handle uninitialized variables with a consteval constructor, and it didn't handle template instantiation. Fixes #135281 .
1 parent d47c126 commit 6a99326

File tree

8 files changed

+64
-26
lines changed

8 files changed

+64
-26
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,9 @@ Bug Fixes to C++ Support
920920
- Correctly handle allocations in the condition of a ``if constexpr``.(#GH120197) (#GH134820)
921921
- Fixed a crash when handling invalid member using-declaration in C++20+ mode. (#GH63254)
922922
- Fix a crash when trying to instantiate an ambiguous specialization. (#GH51866)
923+
- Improved handling of variables with ``consteval`` constructors, to
924+
consistently treat the initializer as manifestly constant-evaluated.
925+
(#GH135281)
923926

924927
Bug Fixes to AST Handling
925928
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6761,6 +6761,7 @@ class Sema final : public SemaBase {
67616761
EK_Decltype,
67626762
EK_TemplateArgument,
67636763
EK_AttrArgument,
6764+
EK_VariableInit,
67646765
EK_Other
67656766
} ExprContext;
67666767

clang/lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,6 +2710,7 @@ Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
27102710
break;
27112711
}
27122712
case InitKind::Uninitialized: {
2713+
InitializerScopeRAII InitScope(*this, D, ThisDecl);
27132714
Actions.ActOnUninitializedDecl(ThisDecl);
27142715
break;
27152716
}

clang/lib/Sema/SemaCoroutine.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,8 @@ static bool checkSuspensionContext(Sema &S, SourceLocation Loc,
783783
const auto ExprContext = S.currentEvaluationContext().ExprContext;
784784
const bool BadContext =
785785
S.isUnevaluatedContext() ||
786-
ExprContext != Sema::ExpressionEvaluationContextRecord::EK_Other;
786+
(ExprContext != Sema::ExpressionEvaluationContextRecord::EK_Other &&
787+
ExprContext != Sema::ExpressionEvaluationContextRecord::EK_VariableInit);
787788
if (BadContext) {
788789
S.Diag(Loc, diag::err_coroutine_unevaluated_context) << Keyword;
789790
return false;

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18892,7 +18892,8 @@ void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) {
1889218892
EnterDeclaratorContext(S, D->getDeclContext());
1889318893

1889418894
PushExpressionEvaluationContext(
18895-
ExpressionEvaluationContext::PotentiallyEvaluated, D);
18895+
ExpressionEvaluationContext::PotentiallyEvaluated, D,
18896+
ExpressionEvaluationContextRecord::EK_VariableInit);
1889618897
}
1889718898

1889818899
void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) {
@@ -18901,29 +18902,6 @@ void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) {
1890118902
if (S && D->isOutOfLine())
1890218903
ExitDeclaratorContext(S);
1890318904

18904-
if (getLangOpts().CPlusPlus23) {
18905-
// An expression or conversion is 'manifestly constant-evaluated' if it is:
18906-
// [...]
18907-
// - the initializer of a variable that is usable in constant expressions or
18908-
// has constant initialization.
18909-
if (auto *VD = dyn_cast<VarDecl>(D);
18910-
VD && (VD->isUsableInConstantExpressions(Context) ||
18911-
VD->hasConstantInitialization())) {
18912-
// An expression or conversion is in an 'immediate function context' if it
18913-
// is potentially evaluated and either:
18914-
// [...]
18915-
// - it is a subexpression of a manifestly constant-evaluated expression
18916-
// or conversion.
18917-
ExprEvalContexts.back().InImmediateFunctionContext = true;
18918-
}
18919-
}
18920-
18921-
// Unless the initializer is in an immediate function context (as determined
18922-
// above), this will evaluate all contained immediate function calls as
18923-
// constant expressions. If the initializer IS an immediate function context,
18924-
// the initializer has been determined to be a constant expression, and all
18925-
// such evaluations will be elided (i.e., as if we "knew the whole time" that
18926-
// it was a constant expression).
1892718905
PopExpressionEvaluationContext();
1892818906
}
1892918907

clang/lib/Sema/SemaExpr.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17934,6 +17934,25 @@ HandleImmediateInvocations(Sema &SemaRef,
1793417934
Rec.isImmediateFunctionContext() || SemaRef.RebuildingImmediateInvocation)
1793517935
return;
1793617936

17937+
// An expression or conversion is 'manifestly constant-evaluated' if it is:
17938+
// [...]
17939+
// - the initializer of a variable that is usable in constant expressions or
17940+
// has constant initialization.
17941+
if (SemaRef.getLangOpts().CPlusPlus23 &&
17942+
Rec.ExprContext ==
17943+
Sema::ExpressionEvaluationContextRecord::EK_VariableInit) {
17944+
auto *VD = cast<VarDecl>(Rec.ManglingContextDecl);
17945+
if (VD->isUsableInConstantExpressions(SemaRef.Context) ||
17946+
VD->hasConstantInitialization()) {
17947+
// An expression or conversion is in an 'immediate function context' if it
17948+
// is potentially evaluated and either:
17949+
// [...]
17950+
// - it is a subexpression of a manifestly constant-evaluated expression
17951+
// or conversion.
17952+
return;
17953+
}
17954+
}
17955+
1793717956
/// When we have more than 1 ImmediateInvocationCandidates or previously
1793817957
/// failed immediate invocations, we need to check for nested
1793917958
/// ImmediateInvocationCandidates in order to avoid duplicate diagnostics.

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6103,7 +6103,8 @@ void Sema::InstantiateVariableInitializer(
61036103
ContextRAII SwitchContext(*this, Var->getDeclContext());
61046104

61056105
EnterExpressionEvaluationContext Evaluated(
6106-
*this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, Var);
6106+
*this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, Var,
6107+
ExpressionEvaluationContextRecord::EK_VariableInit);
61076108
currentEvaluationContext().InLifetimeExtendingContext =
61086109
parentEvaluationContext().InLifetimeExtendingContext;
61096110
currentEvaluationContext().RebuildDefaultArgOrDefaultInit =

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,3 +576,37 @@ int f() {
576576
//expected-note@-2 {{read of non-const variable 'a' is not allowed in a constant expression}}
577577
}
578578
}
579+
580+
#if __cplusplus >= 202302L
581+
namespace GH135281 {
582+
struct B {
583+
const void* p;
584+
consteval B() : p{this} {}
585+
};
586+
B b;
587+
B b2{};
588+
B &&b3{};
589+
void f() {
590+
static B b4;
591+
B b5; // expected-error {{call to consteval function 'GH135281::B::B' is not a constant expression}} \
592+
// expected-note {{pointer to temporary is not a constant expression}} \
593+
// expected-note {{temporary created here}}
594+
}
595+
template<typename T> T temp_var_uninit;
596+
template<typename T> T temp_var_brace_init{};
597+
B* b6 = &temp_var_uninit<B>;
598+
B* b7 = &temp_var_brace_init<B>;
599+
B* b8 = &temp_var_brace_init<B&&>;
600+
template<typename T> void f2() {
601+
static T b9;
602+
T b10; // expected-error {{call to consteval function 'GH135281::B::B' is not a constant expression}} \
603+
// expected-note {{pointer to temporary is not a constant expression}} \
604+
// expected-note {{temporary created here}}
605+
static B b11;
606+
B b12; // expected-error 2 {{call to consteval function 'GH135281::B::B' is not a constant expression}} \
607+
// expected-note 2 {{pointer to temporary is not a constant expression}} \
608+
// expected-note 2 {{temporary created here}}
609+
}
610+
void (*ff)() = f2<B>; // expected-note {{instantiation of function template specialization}}
611+
}
612+
#endif

0 commit comments

Comments
 (0)