llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.19k stars 11.13k forks source link

lambda's operator() is non-constexpr function #97958

Open Fedr opened 3 weeks ago

Fedr commented 3 weeks ago

This program

static_assert(
    []<int I=0>() -> decltype([]{ return true; })
    { return {}; }()());

is accepted in GCC, MSVC and Clang 17, but Clang 18 complains:

error: static assertion expression is not an integral constant expression
    2 |     []<int I=0>() -> decltype([]{ return true; })
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    3 |     { return {}; }()());
      |     ~~~~~~~~~~~~~~~~~~
<source>:2:5: note: non-constexpr function 'operator()' cannot be used in a constant expression
<source>:2:31: note: declared here
    2 |     []<int I=0>() -> decltype([]{ return true; })

Online demo: https://gcc.godbolt.org/z/nW74e4q9v

It is a modified example from https://stackoverflow.com/q/77431308/7325599

llvmbot commented 3 weeks ago

@llvm/issue-subscribers-clang-frontend

Author: Fedor Chelnokov (Fedr)

This program ``` static_assert( []<int I=0>() -> decltype([]{ return true; }) { return {}; }()()); ``` is accepted in GCC, MSVC and Clang 17, but Clang 18 complains: ``` error: static assertion expression is not an integral constant expression 2 | []<int I=0>() -> decltype([]{ return true; }) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3 | { return {}; }()()); | ~~~~~~~~~~~~~~~~~~ <source>:2:5: note: non-constexpr function 'operator()' cannot be used in a constant expression <source>:2:31: note: declared here 2 | []<int I=0>() -> decltype([]{ return true; }) ``` Online demo: https://gcc.godbolt.org/z/nW74e4q9v It is a modified example from https://stackoverflow.com/q/77431308/7325599
MitalAshok commented 3 weeks ago

git bisect points to 890f11d60feffed4207afa58229da7bfaaff276e

tbaederr commented 2 weeks ago

CC @cor3ntin