-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
* Lambdas were not considered immediate escalating in a template * Calls to an immediate function whose arguments were dependent was incorrectly treated as immediate escalating. Fixes llvm#94935
@llvm/pr-subscribers-clang Author: cor3ntin (cor3ntin) Changes
Fixes #94935 Full diff: https://github.com/llvm/llvm-project/pull/95233.diff 6 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index cf1ba02cbc4b2..c5e9363a12119 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -846,6 +846,7 @@ Bug Fixes to C++ Support
- Fix a crash caused by improper use of ``__array_extent``. (#GH80474)
- Fixed several bugs in capturing variables within unevaluated contexts. (#GH63845), (#GH67260), (#GH69307),
(#GH88081), (#GH89496), (#GH90669) and (#GH91633).
+- Fix immediate escalation bugs in the presence of dependent call arguments. (#GH94935)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 9bf01417186c3..69ac2e804fa75 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5239,6 +5239,12 @@ class Sema final : public SemaBase {
return ExprEvalContexts.back();
};
+ ExpressionEvaluationContextRecord ¤tEvaluationContext() {
+ 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");
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 44f886bf54e3a..2c221fd9c828a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -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;
}
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index f117fe98d142b..cf4e80399632b 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -14550,6 +14550,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;
diff --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp
index 622ec31c459dd..fb8385eb02051 100644
--- a/clang/test/SemaCXX/cxx2a-consteval.cpp
+++ b/clang/test/SemaCXX/cxx2a-consteval.cpp
@@ -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 {
diff --git a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
index b70c02201ac3c..378414f136172 100644
--- a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
+++ b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
@@ -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>();
+
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sema.h
changes look good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
* Lambdas were not considered immediate escalating in a template * Calls to an immediate function whose arguments were dependent were incorrectly treated as immediate escalating. Fixes llvm#94935
Fixes #94935