Skip to content

Clang behaves different with other compilers for constraint nested lambdas #134193

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

Open
zyn0217 opened this issue Apr 3, 2025 · 5 comments
Open
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" concepts C++20 concepts confirmed Verified by a second party crash Prefer [crash-on-valid] or [crash-on-invalid] lambda C++11 lambda expressions

Comments

@zyn0217
Copy link
Contributor

zyn0217 commented Apr 3, 2025

Both were discovered in #133719, but the underlying causes are different from the original issue so I filed a separate one to track these.

  1. The following code causes clang to crash, dating back to Clang 16 and up to trunk:
template <class T>
constexpr auto f{[] (auto arg) {
    return [a{arg}] {
        [] () requires (a == 1) {}();
    };
}};

int main() {
    f<int>(0);
}

https://compiler-explorer.com/z/Tq9xzrPns

  1. Clang accepts the following code, while others don't (discovered by @shafik)
void f() {
    auto f{ 
      [](auto arg) {
        return [a = arg]() 
          requires (a == 1)
         { return a;};
      }
    };

    f(10);
}

https://godbolt.org/z/ncYx8q9f6

This has started since Clang 16.

@zyn0217 zyn0217 added clang:frontend Language frontend issues, e.g. anything involving "Sema" concepts C++20 concepts labels Apr 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 3, 2025

@llvm/issue-subscribers-clang-frontend

Author: Younan Zhang (zyn0217)

Both were discovered in #133719, but the underlying causes are different from the original issue so I filed a separate one to track these.
  1. The following code causes clang to crash, dating back to Clang 16 and up to trunk:
template &lt;class T&gt;
constexpr auto f{[] (auto arg) {
    return [a{arg}] {
        [] () requires (a == 1) {}();
    };
}};

int main() {
    f&lt;int&gt;(0);
}

https://compiler-explorer.com/z/Tq9xzrPns

  1. Clang accepts the following code, while others don't (discovered by @shafik)
void f() {
    auto f{ 
      [](auto arg) {
        return [a = arg]() 
          requires (a == 1)
         { return a;};
      }
    };

    f(10);
}

https://godbolt.org/z/ncYx8q9f6

This has started since Clang 16.

@frederick-vs-ja
Copy link
Contributor

frederick-vs-ja commented Apr 3, 2025

2. Clang accepts the following code, while others don't (discovered by @shafik)

void f() {
    auto f{ 
      [](auto arg) {
        return [a = arg]() 
          requires (a == 1)
         { return a;};
      }
    };

    f(10);
}

https://godbolt.org/z/ncYx8q9f6

The 2nd case is not a bug. It is an expected a possibly result of DR P2036R3 + P2579R0, which is currently only implemented by Clang.

@frederick-vs-ja frederick-vs-ja added lambda C++11 lambda expressions crash-on-valid crash Prefer [crash-on-valid] or [crash-on-invalid] and removed crash-on-valid labels Apr 3, 2025
@frederick-vs-ja
Copy link
Contributor

Looks like that I was confused.

It seems that we should at least reject this

void f() {
  auto l2 = []<class = void>() {
    return [a = 10]() requires (a == 1) { return a; };
  }();
}

@frederick-vs-ja
Copy link
Contributor

This one is perhaps valid, but seemingly causes infinite recursion in Clang (Godbolt link).

struct ConvToOne {
  constexpr operator int() const { return 1; }
};

void g() {
  auto l2 = []<class = void>() {
    return [a = ConvToOne{}]() requires (a == 1) { return a; };
  }();
  l2();
}

@shafik
Copy link
Collaborator

shafik commented Apr 3, 2025

CC @cor3ntin

@shafik shafik added the confirmed Verified by a second party label Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" concepts C++20 concepts confirmed Verified by a second party crash Prefer [crash-on-valid] or [crash-on-invalid] lambda C++11 lambda expressions
Projects
None yet
Development

No branches or pull requests

4 participants