Skip to content

Commit 561132e

Browse files
authored
[Clang] Fix immediate escalation of template function specializations. (#124404)
We record whether an expression is immediate escalating in the FunctionScope. However, that only happen when parsing or transforming an expression. This might not happen when transforming a non dependent expression. This patch fixes that by considering a function immediate when instantiated from an immediate function. Fixes #123405
1 parent eaa5897 commit 561132e

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

clang/docs/ReleaseNotes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,7 @@ Bug Fixes to C++ Support
10031003
- Fixed assertions or false compiler diagnostics in the case of C++ modules for
10041004
lambda functions or inline friend functions defined inside templates (#GH122493).
10051005
- Clang now rejects declaring an alias template with the same name as its template parameter. (#GH123423)
1006+
- Fixed immediate escalation of non-dependent expressions. (#GH123405)
10061007
- Fix type of expression when calling a template which returns an ``__array_rank`` querying a type depending on a
10071008
template parameter. Now, such expression can be used with ``static_assert`` and ``constexpr``. (#GH123498)
10081009
- Correctly determine the implicit constexprness of lambdas in dependent contexts. (#GH97958) (#GH114234)

clang/lib/AST/Decl.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -3314,6 +3314,10 @@ bool FunctionDecl::isImmediateFunction() const {
33143314
.getConstructor()
33153315
->isImmediateFunction();
33163316

3317+
if (FunctionDecl *P = getTemplateInstantiationPattern();
3318+
P && P->isImmediateFunction())
3319+
return true;
3320+
33173321
if (const auto *MD = dyn_cast<CXXMethodDecl>(this);
33183322
MD && MD->isLambdaStaticInvoker())
33193323
return MD->getParent()->getLambdaCallOperator()->isImmediateFunction();

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

+18
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,21 @@ D d(0); // expected-note {{in implicit initialization for inherited constructor
528528
// expected-error@-1 {{call to immediate function 'GH112677::D::SimpleCtor' is not a constant expression}}
529529

530530
}
531+
532+
namespace GH123405 {
533+
534+
consteval void fn() {}
535+
536+
template <typename>
537+
constexpr int tfn(int) {
538+
auto p = &fn; // expected-note {{'tfn<int>' is an immediate function because its body evaluates the address of a consteval function 'fn'}}
539+
return int(p); // expected-error {{cast from pointer to smaller type 'int' loses information}}
540+
}
541+
542+
int g() {
543+
int a; // expected-note {{declared here}}
544+
return tfn<int>(a); // expected-error {{call to immediate function 'GH123405::tfn<int>' is not a constant expression}}\
545+
// expected-note {{read of non-const variable 'a' is not allowed in a constant expression}}
546+
}
547+
548+
}

0 commit comments

Comments
 (0)