@@ -1806,6 +1806,7 @@ static unsigned getRecordDiagFromTagKind(TagTypeKind Tag) {
1806
1806
static bool CheckConstexprFunctionBody(Sema &SemaRef, const FunctionDecl *Dcl,
1807
1807
Stmt *Body,
1808
1808
Sema::CheckConstexprKind Kind);
1809
+ static bool CheckConstexprMissingReturn(Sema &SemaRef, const FunctionDecl *Dcl);
1809
1810
1810
1811
// Check whether a function declaration satisfies the requirements of a
1811
1812
// constexpr function definition or a constexpr constructor definition. If so,
@@ -2411,20 +2412,9 @@ static bool CheckConstexprFunctionBody(Sema &SemaRef, const FunctionDecl *Dcl,
2411
2412
}
2412
2413
} else {
2413
2414
if (ReturnStmts.empty()) {
2414
- // C++1y doesn't require constexpr functions to contain a 'return'
2415
- // statement. We still do, unless the return type might be void, because
2416
- // otherwise if there's no return statement, the function cannot
2417
- // be used in a core constant expression.
2418
- bool OK = SemaRef.getLangOpts().CPlusPlus14 &&
2419
- (Dcl->getReturnType()->isVoidType() ||
2420
- Dcl->getReturnType()->isDependentType());
2421
2415
switch (Kind) {
2422
2416
case Sema::CheckConstexprKind::Diagnose:
2423
- SemaRef.Diag(Dcl->getLocation(),
2424
- OK ? diag::warn_cxx11_compat_constexpr_body_no_return
2425
- : diag::err_constexpr_body_no_return)
2426
- << Dcl->isConsteval();
2427
- if (!OK)
2417
+ if (!CheckConstexprMissingReturn(SemaRef, Dcl))
2428
2418
return false;
2429
2419
break;
2430
2420
@@ -2487,6 +2477,26 @@ static bool CheckConstexprFunctionBody(Sema &SemaRef, const FunctionDecl *Dcl,
2487
2477
return true;
2488
2478
}
2489
2479
2480
+ static bool CheckConstexprMissingReturn(Sema &SemaRef,
2481
+ const FunctionDecl *Dcl) {
2482
+ bool IsVoidOrDependentType = Dcl->getReturnType()->isVoidType() ||
2483
+ Dcl->getReturnType()->isDependentType();
2484
+
2485
+ if (SemaRef.getLangOpts().CPlusPlus23 && !IsVoidOrDependentType)
2486
+ return true;
2487
+
2488
+ // C++1y doesn't require constexpr functions to contain a 'return'
2489
+ // statement. We still do, unless the return type might be void, because
2490
+ // otherwise if there's no return statement, the function cannot
2491
+ // be used in a core constant expression.
2492
+ bool OK = SemaRef.getLangOpts().CPlusPlus14 && IsVoidOrDependentType;
2493
+ SemaRef.Diag(Dcl->getLocation(),
2494
+ OK ? diag::warn_cxx11_compat_constexpr_body_no_return
2495
+ : diag::err_constexpr_body_no_return)
2496
+ << Dcl->isConsteval();
2497
+ return OK;
2498
+ }
2499
+
2490
2500
bool Sema::CheckImmediateEscalatingFunctionDefinition(
2491
2501
FunctionDecl *FD, const sema::FunctionScopeInfo *FSI) {
2492
2502
if (!getLangOpts().CPlusPlus20 || !FD->isImmediateEscalating())
0 commit comments