Skip to content

Revert "[Clang] Instantiate local constexpr functions eagerly (#95660)" #98991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,6 @@ Bug Fixes to C++ Support
- Fixed several bugs in capturing variables within unevaluated contexts. (#GH63845), (#GH67260), (#GH69307),
(#GH88081), (#GH89496), (#GH90669) and (#GH91633).
- Fixed handling of brace ellison when building deduction guides. (#GH64625), (#GH83368).
- Clang now instantiates local constexpr functions eagerly for constant evaluators. (#GH35052), (#GH94849)
- Fixed a failed assertion when attempting to convert an integer representing the difference
between the addresses of two labels (a GNU extension) to a pointer within a constant expression. (#GH95366).
- Fix immediate escalation bugs in the presence of dependent call arguments. (#GH94935)
Expand Down
13 changes: 6 additions & 7 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17938,17 +17938,16 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func,

if (FirstInstantiation || TSK != TSK_ImplicitInstantiation ||
Func->isConstexpr()) {
if (Func->isConstexpr())
if (isa<CXXRecordDecl>(Func->getDeclContext()) &&
cast<CXXRecordDecl>(Func->getDeclContext())->isLocalClass() &&
CodeSynthesisContexts.size())
PendingLocalImplicitInstantiations.push_back(
std::make_pair(Func, PointOfInstantiation));
else if (Func->isConstexpr())
// Do not defer instantiations of constexpr functions, to avoid the
// expression evaluator needing to call back into Sema if it sees a
// call to such a function.
InstantiateFunctionDefinition(PointOfInstantiation, Func);
else if (isa<CXXRecordDecl>(Func->getDeclContext()) &&
cast<CXXRecordDecl>(Func->getDeclContext())
->isLocalClass() &&
CodeSynthesisContexts.size())
PendingLocalImplicitInstantiations.push_back(
std::make_pair(Func, PointOfInstantiation));
else {
Func->setInstantiationIsPending(true);
PendingInstantiations.push_back(
Expand Down
28 changes: 15 additions & 13 deletions clang/test/SemaTemplate/instantiate-local-class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,24 +512,26 @@ namespace LambdaInDefaultMemberInitializer {
}

#if __cplusplus >= 201703L
namespace GH35052 {

template <typename F> constexpr int func(F f) {
if constexpr (f(1UL)) {
return 1;
// Reduced from https://github.com/llvm/llvm-project/issues/98526
// This relies on the deferral instantiation of the local lambda, otherwise we would fail in DeduceReturnType().
namespace local_recursive_lambda {

template <typename F> struct recursive_lambda {
template <typename... Args> auto operator()(Args &&...args) const {
return fn(*this, args...);
}
return 0;
}
F fn;
};

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

static_assert(predicate(1));
return func(predicate);
void foo() {
recursive_lambda{[&](auto &self_fn, int) -> int {
return self_fn(0);
}}(0);
}

} // namespace GH35052
} // namespace local_recursive_lambda

#endif
Loading