Skip to content

Commit 2d8c6b3

Browse files
zyn0217yuxuanchen1997
authored andcommitted
Revert "[Clang] Instantiate local constexpr functions eagerly (#95660)" (#98991)
Summary: Unfortunately, #95660 has caused a regression in DeduceReturnType(), where some of the local recursive lambdas are getting incorrectly rejected. This reverts commit 5548ea3. Also, this adds an offending case to the test. Closes #98526 Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251570
1 parent f0ac4a4 commit 2d8c6b3

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,6 @@ Bug Fixes to C++ Support
10171017
(#GH88081), (#GH89496), (#GH90669), (#GH91633) and (#GH97453).
10181018
- Fixed a crash in constraint instantiation under nested lambdas with dependent parameters.
10191019
- Fixed handling of brace ellison when building deduction guides. (#GH64625), (#GH83368).
1020-
- Clang now instantiates local constexpr functions eagerly for constant evaluators. (#GH35052), (#GH94849)
10211020
- Fixed a failed assertion when attempting to convert an integer representing the difference
10221021
between the addresses of two labels (a GNU extension) to a pointer within a constant expression. (#GH95366).
10231022
- Fix immediate escalation bugs in the presence of dependent call arguments. (#GH94935)

clang/lib/Sema/SemaExpr.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17958,17 +17958,16 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func,
1795817958

1795917959
if (FirstInstantiation || TSK != TSK_ImplicitInstantiation ||
1796017960
Func->isConstexpr()) {
17961-
if (Func->isConstexpr())
17961+
if (isa<CXXRecordDecl>(Func->getDeclContext()) &&
17962+
cast<CXXRecordDecl>(Func->getDeclContext())->isLocalClass() &&
17963+
CodeSynthesisContexts.size())
17964+
PendingLocalImplicitInstantiations.push_back(
17965+
std::make_pair(Func, PointOfInstantiation));
17966+
else if (Func->isConstexpr())
1796217967
// Do not defer instantiations of constexpr functions, to avoid the
1796317968
// expression evaluator needing to call back into Sema if it sees a
1796417969
// call to such a function.
1796517970
InstantiateFunctionDefinition(PointOfInstantiation, Func);
17966-
else if (isa<CXXRecordDecl>(Func->getDeclContext()) &&
17967-
cast<CXXRecordDecl>(Func->getDeclContext())
17968-
->isLocalClass() &&
17969-
CodeSynthesisContexts.size())
17970-
PendingLocalImplicitInstantiations.push_back(
17971-
std::make_pair(Func, PointOfInstantiation));
1797217971
else {
1797317972
Func->setInstantiationIsPending(true);
1797417973
PendingInstantiations.push_back(

clang/test/SemaTemplate/instantiate-local-class.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -512,24 +512,26 @@ namespace LambdaInDefaultMemberInitializer {
512512
}
513513

514514
#if __cplusplus >= 201703L
515-
namespace GH35052 {
516515

517-
template <typename F> constexpr int func(F f) {
518-
if constexpr (f(1UL)) {
519-
return 1;
516+
// Reduced from https://github.com/llvm/llvm-project/issues/98526
517+
// This relies on the deferral instantiation of the local lambda, otherwise we would fail in DeduceReturnType().
518+
namespace local_recursive_lambda {
519+
520+
template <typename F> struct recursive_lambda {
521+
template <typename... Args> auto operator()(Args &&...args) const {
522+
return fn(*this, args...);
520523
}
521-
return 0;
522-
}
524+
F fn;
525+
};
523526

524-
int main() {
525-
auto predicate = [](auto v) /*implicit constexpr*/ -> bool {
526-
return v == 1;
527-
};
527+
template <typename F> recursive_lambda(F) -> recursive_lambda<F>;
528528

529-
static_assert(predicate(1));
530-
return func(predicate);
529+
void foo() {
530+
recursive_lambda{[&](auto &self_fn, int) -> int {
531+
return self_fn(0);
532+
}}(0);
531533
}
532534

533-
} // namespace GH35052
535+
} // namespace local_recursive_lambda
534536

535537
#endif

0 commit comments

Comments
 (0)