Skip to content

Error on use of parameter in lambda outside function body #111840

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

Closed
Eczbek opened this issue Oct 10, 2024 · 4 comments
Closed

Error on use of parameter in lambda outside function body #111840

Eczbek opened this issue Oct 10, 2024 · 4 comments
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" invalid Resolved as invalid, i.e. not a bug

Comments

@Eczbek
Copy link

Eczbek commented Oct 10, 2024

Compiler Explorer link: https://godbolt.org/z/118sGzbbM

void foo(auto x) noexcept(noexcept([&x] { x; })) {}

int main() {
	foo(3);
}
<source>:1:43: error: variable 'x' cannot be implicitly captured in a lambda with no capture-default specified
    1 | void foo(auto x) noexcept(noexcept([&x] { x; })) {}
      |                                           ^
<source>:1:41: note: while substituting into a lambda expression here
    1 | void foo(auto x) noexcept(noexcept([&x] { x; })) {}
      |                                         ^
<source>:4:2: note: in instantiation of exception specification for 'foo<int>' requested here
    4 |         foo(3);
      |         ^
<source>:1:15: note: 'x' declared here
    1 | void foo(auto x) noexcept(noexcept([&x] { x; })) {}
      |               ^
<source>:1:36: note: lambda expression begins here
    1 | void foo(auto x) noexcept(noexcept([&x] { x; })) {}
      |                                    ^
<source>:1:39: note: capture 'x' by value
    1 | void foo(auto x) noexcept(noexcept([&x] { x; })) {}
      |                                       ^
      |                                       x
<source>:1:39: note: capture 'x' by reference
    1 | void foo(auto x) noexcept(noexcept([&x] { x; })) {}
      |                                       ^
      |                                       &x
<source>:1:37: note: default capture by value
    1 | void foo(auto x) noexcept(noexcept([&x] { x; })) {}
      |                                     ^
      |                                     =
<source>:1:37: note: default capture by reference
    1 | void foo(auto x) noexcept(noexcept([&x] { x; })) {}
      |                                     ^
      |                                     &
<source>:1:43: warning: expression result unused [-Wunused-value]
    1 | void foo(auto x) noexcept(noexcept([&x] { x; })) {}
      |                                           ^
<source>:4:2: note: in instantiation of exception specification for 'foo<int>' requested here
    4 |         foo(3);
      |         ^
1 warning and 1 error generated.
Compiler returned: 1
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed new issue labels Oct 10, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 10, 2024

@llvm/issue-subscribers-clang-frontend

Author: None (Eczbek)

Compiler Explorer link: https://godbolt.org/z/118sGzbbM
void foo(auto x) noexcept(noexcept([&amp;x] { x; })) {}

int main() {
	foo(3);
}
&lt;source&gt;:1:43: error: variable 'x' cannot be implicitly captured in a lambda with no capture-default specified
    1 | void foo(auto x) noexcept(noexcept([&amp;x] { x; })) {}
      |                                           ^
&lt;source&gt;:1:41: note: while substituting into a lambda expression here
    1 | void foo(auto x) noexcept(noexcept([&amp;x] { x; })) {}
      |                                         ^
&lt;source&gt;:4:2: note: in instantiation of exception specification for 'foo&lt;int&gt;' requested here
    4 |         foo(3);
      |         ^
&lt;source&gt;:1:15: note: 'x' declared here
    1 | void foo(auto x) noexcept(noexcept([&amp;x] { x; })) {}
      |               ^
&lt;source&gt;:1:36: note: lambda expression begins here
    1 | void foo(auto x) noexcept(noexcept([&amp;x] { x; })) {}
      |                                    ^
&lt;source&gt;:1:39: note: capture 'x' by value
    1 | void foo(auto x) noexcept(noexcept([&amp;x] { x; })) {}
      |                                       ^
      |                                       x
&lt;source&gt;:1:39: note: capture 'x' by reference
    1 | void foo(auto x) noexcept(noexcept([&amp;x] { x; })) {}
      |                                       ^
      |                                       &amp;x
&lt;source&gt;:1:37: note: default capture by value
    1 | void foo(auto x) noexcept(noexcept([&amp;x] { x; })) {}
      |                                     ^
      |                                     =
&lt;source&gt;:1:37: note: default capture by reference
    1 | void foo(auto x) noexcept(noexcept([&amp;x] { x; })) {}
      |                                     ^
      |                                     &amp;
&lt;source&gt;:1:43: warning: expression result unused [-Wunused-value]
    1 | void foo(auto x) noexcept(noexcept([&amp;x] { x; })) {}
      |                                           ^
&lt;source&gt;:4:2: note: in instantiation of exception specification for 'foo&lt;int&gt;' requested here
    4 |         foo(3);
      |         ^
1 warning and 1 error generated.
Compiler returned: 1

@zyn0217
Copy link
Contributor

zyn0217 commented Oct 10, 2024

@mizvekov you probably want to test your patch with this one

@mizvekov
Copy link
Contributor

#107942 has no effect yet on this one, but it probably should.
I'll put this on my list to double check later.

@Eczbek
Copy link
Author

Eczbek commented Mar 28, 2025

This might be invalid because lambdas can only have captures in block scope, and the noexcept specifier/requires clause are outside it :(

https://eel.is/c++draft/expr.prim.lambda.capture#3
https://eel.is/c++draft/dcl.fct.def#nt:function-body
https://eel.is/c++draft/basic.scope.block#1

@Eczbek Eczbek closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 2025
@EugeneZelenko EugeneZelenko added the invalid Resolved as invalid, i.e. not a bug label Mar 28, 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" invalid Resolved as invalid, i.e. not a bug
Projects
None yet
Development

No branches or pull requests

5 participants