Skip to content

[Clang] Fix immediate escalation in templated entities #95233

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
Jun 18, 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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,8 @@ Bug Fixes to C++ Support
- 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)


Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -5239,6 +5239,12 @@ class Sema final : public SemaBase {
return ExprEvalContexts.back();
};

ExpressionEvaluationContextRecord &currentEvaluationContext() {
assert(!ExprEvalContexts.empty() &&
"Must be in an expression evaluation context");
return ExprEvalContexts.back();
};

ExpressionEvaluationContextRecord &parentEvaluationContext() {
assert(ExprEvalContexts.size() >= 2 &&
"Must be in an expression evaluation context");
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6471,6 +6471,14 @@ ExprResult Sema::ActOnCallExpr(Scope *Scope, Expr *Fn, SourceLocation LParenLoc,
if (LangOpts.CPlusPlus) {
if (const auto *CE = dyn_cast<CallExpr>(Call.get()))
DiagnosedUnqualifiedCallsToStdFunctions(*this, CE);

// If we previously found that the id-expression of this call refers to a
// consteval function but the call is dependent, we should not treat is an
// an invalid immediate call.
if (auto *DRE = dyn_cast<DeclRefExpr>(Fn->IgnoreParens());
DRE && Call.get()->isValueDependent()) {
currentEvaluationContext().ReferenceToConsteval.erase(DRE);
}
}
return Call;
}
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -14545,6 +14545,9 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
E->getCallOperator()->isConsteval() ?
Sema::ExpressionEvaluationContext::ImmediateFunctionContext :
Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
getSema().currentEvaluationContext().InImmediateEscalatingFunctionContext =
getSema().getLangOpts().CPlusPlus20 &&
E->getCallOperator()->isImmediateEscalating();

Sema::CodeSynthesisContext C;
C.Kind = clang::Sema::CodeSynthesisContext::LambdaExpressionSubstitution;
Expand Down
8 changes: 6 additions & 2 deletions clang/test/SemaCXX/cxx2a-consteval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1195,14 +1195,18 @@ namespace GH66562 {

namespace ns
{
consteval int foo(int x) { return 1; } // expected-note {{declared here}}
consteval int foo(int x) { return 1; } // expected-note {{declared here}} \
// expected-note {{passing argument to parameter 'x' here}}
}

template <class A>
struct T {
static constexpr auto xx = ns::foo(A{}); // expected-error {{cannot take address of consteval function 'foo' outside of an immediate invocation}}
static constexpr auto xx = ns::foo(A{}); // expected-error {{cannot take address of consteval function 'foo' outside of an immediate invocation}} \
// expected-error {{cannot initialize a parameter of type 'int' with an rvalue of type 'char *'}}
};

template class T<char*>; // expected-note {{in instantiation}}

}

namespace GH65520 {
Expand Down
42 changes: 42 additions & 0 deletions clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,45 @@ namespace GH91308 {
}
using R1 = decltype(&f<int>);
}

namespace GH94935 {

consteval void f(int) {}
consteval void undef(int); // expected-note {{declared here}}

template<typename T>
struct G {
void g() {
GH94935::f(T::fn());
GH94935::f(T::undef2()); // expected-error {{call to consteval function 'GH94935::f' is not a constant expression}} \
// expected-note {{undefined function 'undef2' cannot be used in a constant expression}}
GH94935::undef(T::fn()); // expected-error {{call to consteval function 'GH94935::undef' is not a constant expression}} \
// expected-note {{undefined function 'undef' cannot be used in a constant expression}}
}
};

struct X {
static consteval int fn() { return 0; }
static consteval int undef2(); // expected-note {{declared here}}

};

void test() {
G<X>{}.g(); // expected-note {{instantiation}}
}


template<typename T>
void g() {
auto l = []{
::f(T::fn());
};
}

struct Y {
static int fn();
};

template void g<Y>();

}
Loading