Skip to content

[BUG] UFCS doesn't work at namespace scope #314

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
JohelEGP opened this issue Apr 2, 2023 · 7 comments
Closed

[BUG] UFCS doesn't work at namespace scope #314

JohelEGP opened this issue Apr 2, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@JohelEGP
Copy link
Contributor

JohelEGP commented Apr 2, 2023

Describe the bug
error: non-local lambda expression cannot have a capture-default.

To Reproduce
Steps to reproduce the behavior:

  1. Sample code - distilled down to minimal essentials please
f: (x)->int = 0;
y: int = 0.f();
Generated code
//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"


//=== Cpp2 type definitions and function declarations ===========================

#line 1 "x.cpp2"
[[nodiscard]] auto f(auto const& x) -> int;
extern int y;
//=== Cpp2 function definitions =================================================

#line 1 "x.cpp2"
[[nodiscard]] auto f(auto const& x) -> int { return 0;  }
int y {CPP2_UFCS_0(f, 0)}; 
  1. Command lines including which C++ compiler you are using
~/root/bin/cppfront x.cpp2 
~/root/clang/bin/clang++ -std=c++20 -I $CPPFRONT_INCLUDE_DIR x.cpp
clang version 17.0.0
  1. Expected result - what you expected to happen
    UFCS to work at namespace scope. By using a different macro that doesn't use a capture-default.

  2. Actual result/error

x.cpp2:2:8: error: non-local lambda expression cannot have a capture-default
int y {CPP2_UFCS_0(f, 0)}; 
       ^
/home/johel/Documents/C++/Forks/cppfront/include/cpp2util.h:646:2: note: expanded from macro 'CPP2_UFCS_0'
[&](auto&& obj) CPP2_FORCE_INLINE_LAMBDA -> decltype(auto) { \
 ^
1 error generated.
@JohelEGP JohelEGP added the bug Something isn't working label Apr 2, 2023
@filipsajdak
Copy link
Contributor

@JohelEGP I think it would be good to see what cpp1 code is generated (in such issues).

I will generate it by myself but it will give more context than the error message from cpp1 compiler.

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Apr 2, 2023

Good idea. Updated, rebased on main.

@filipsajdak
Copy link
Contributor

@JohelEGP your lamda returns void.

Please correct your code to:

f: (x) -> _ = 0;
y := 0.f();

@filipsajdak
Copy link
Contributor

Ah, your updated code is fine.

@hsutter
Copy link
Owner

hsutter commented Apr 2, 2023

Thanks! Fixing in next commit...

@hsutter hsutter closed this as completed in 827ed79 Apr 3, 2023
@hsutter
Copy link
Owner

hsutter commented Apr 3, 2023

Incidentally, I think you win the prize for the issue that is resolved by the commit that exactly doubles the line count of cppfront from the day I first introduced it at CppCon 2022 about six months ago when it was 9 KLOC... this commit just took us over the 18 KLOC mark. (Still quite svelte! Circle is over 10x as big I think.)

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Apr 3, 2023

the issue

The first issue so far. ☺

I'm looking forward to the seeing the numbers when cppfront self-hosts.

zaucy pushed a commit to zaucy/cppfront that referenced this issue Dec 5, 2023
The `[&]` introducer is required for the UFCS lambda in case it's invoked in a deeply nested context, but then the `[&]` is illegal when it's outside a function (initializing a namespace scope object)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants