Skip to content
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

[clang][Sema] Skip the RequiresExprBodyDecls for lambda dependencies #83997

Merged
merged 5 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -311,6 +311,8 @@ Bug Fixes to C++ Support
Fixes (#GH80630)
- Fix a crash when an explicit template argument list is used with a name for which lookup
finds a non-template function and a dependent using declarator.
- Fixed an issue where the ``RequiresExprBody`` was involved in the lambda dependency
calculation. (#GH56556), (#GH82849).

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
23 changes: 21 additions & 2 deletions clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -13649,10 +13649,29 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
// use evaluation contexts to distinguish the function parameter case.
CXXRecordDecl::LambdaDependencyKind DependencyKind =
CXXRecordDecl::LDK_Unknown;
DeclContext *DC = getSema().CurContext;
// A RequiresExprBodyDecl is not interesting for dependencies.
// For the following case,
//
// template <typename>
// concept C = requires { [] {}; };
//
// template <class F>
// struct Widget;
//
// template <C F>
// struct Widget<F> {};
//
// While we are here in substitution for Widget<F>, the parent of DC would be
zyn0217 marked this conversation as resolved.
Show resolved Hide resolved
// the template specialization itself. Thus, the lambda expression
// will be deemed as dependent even if we have non-dependent template
// arguments.
zyn0217 marked this conversation as resolved.
Show resolved Hide resolved
// (A ClassTemplateSpecializationDecl is always a dependent context.)
if (DC->getDeclKind() == Decl::Kind::RequiresExprBody)
DC = DC->getParent();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we get into the same issue if there are multiple nested requires?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, this should be a while loop, I think

if ((getSema().isUnevaluatedContext() ||
getSema().isConstantEvaluatedContext()) &&
(getSema().CurContext->isFileContext() ||
!getSema().CurContext->getParent()->isDependentContext()))
(DC->isFileContext() || !DC->getParent()->isDependentContext()))
DependencyKind = CXXRecordDecl::LDK_NeverDependent;

CXXRecordDecl *OldClass = E->getLambdaClass();
Expand Down
52 changes: 52 additions & 0 deletions clang/test/SemaTemplate/concepts-lambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,58 @@ struct Foo {

static_assert(ConstructibleWithN<Foo>);

namespace GH56556 {

template <typename It>
inline constexpr It declare ();

template <typename It, template <typename> typename Template>
concept D = requires {
{ [] <typename T1> (Template<T1> &) {}(declare<It &>()) };
};

template <typename T>
struct B {};

template <typename T>
struct Adapter;

template <D<B> T>
struct Adapter<T> {};

template struct Adapter<B<int>>;

} // namespace GH56556

namespace GH82849 {

template <class T>
concept C = requires(T t) {
[](T) {}(t);
};

template <class From>
struct Widget;

template <C F>
struct Widget<F> {
static F create(F from) {
return from;
}
};

template <class>
bool foo() {
return C<int>;
}

void bar() {
// https://github.com/llvm/llvm-project/issues/49570#issuecomment-1664966972
Widget<char>::create(0);
}

} // namespace GH82849

}

// GH60642 reported an assert being hit, make sure we don't assert.
Expand Down
Loading