-
Notifications
You must be signed in to change notification settings - Fork 259
[BUG] Returning a tuple with unnamed members #325
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
Comments
https://github.com/hsutter/cppfront/blob/main/regression-tests/mixed-multiple-return-values.cpp2 doesn't demonstrate it, but |
@JohelEGP Yes, this is about unnamed members. BTW is there a pure cpp2 way to write |
Ah, I see. IIRC, you're right here.
Not yet, it seems. |
A couple of issues here.
f: () -> _ = std::tuple(5, "hi");
main: () -> int = {
t := f();
assert(t.get<0>() == 5);
assert(t.get<1>() == std::string("hi"));
} Generates: [[nodiscard]] auto f() -> auto { return std::tuple(5, "hi"); }
[[nodiscard]] auto main() -> int{
auto t {f()};
assert(CPP2_UFCS_TEMPLATE_0(get, (<0>), t)==5);
assert(CPP2_UFCS_TEMPLATE_0(get, (<1>), std::move(t))==std::string("hi"));
}
f: () -> (a:int, b:int) = { a = 5; b = 7; }
main: () -> int = {
t := f();
assert(t.a == 5);
assert(t.b == 7);
} Generates: [[nodiscard]] auto f() -> f__ret{
cpp2::deferred_init<int> a;
cpp2::deferred_init<int> b;
#line 1 "../tests/tuple_return.cpp2"
a.construct(5); b.construct(7);
return { std::move(a.value()), std::move(b.value()) }; }
[[nodiscard]] auto main() -> int{
auto t {f()};
assert(t.a==5);
assert(std::move(t).b==7);
} |
Mixed C++ and C++2 is OK though AFAIU, it's done in
Or
OK, though it would be good to error when trying to infer the type of e.g. f: () -> _ = std::tuple(5, "hi");
main: () -> int = {
auto [a,b] = f();
}
cpp2::assert_in_bounds(auto, a, b) = f();
OK, but if we have tuple types, it would be nice if you could use a tuple type as the return type and not have to declare member names. |
That's why I replied:
IOW, this is a feature request. It's not the problem solved by this feature, but sounds like a reasonable extension.
Cpp2 is distinguished from C++1 at the top-level declarations. Once you're in a C++1 top-level declaration, you stay in C++1. Same for Cpp2.
|
@ntrel you can return a tuple type: f1: () -> _ = std::tuple(5, "hi"); // using deduced return type
f2: () -> std::tuple<int, std::string> = std::tuple(5, "hi"); // using explicit return type |
OK, thanks. So deducing an expression constructing a I think these should be diagnosed (better) in a C++2 function: f: () = {
...
auto x = 1; // causes ill-formed initializer error but line is first line of `f`, not this line
auto [x,y] = (1,2); // passes cppfront but errors in C++1
} |
Implemented in #342. |
IIUC, comma expressions are gone from cpp2 so it could be just
Can we have general destructuring assignment to any existing lvalues without necessarily defining new variables? |
@orent Regarding using comma:
Originally posted by @hsutter in #331 (comment) |
Triage:
The error should be that an identifier was expected, not
I think for that parameter the error is correct as
Type inference + initializer list could be diagnosed with an error. There is also another bug:
This should error that no type is declared for a and b. I have a fix for that. |
Note: type cannot be `_`. Fixes most of hsutter#325.
I see that you treat
t : std::tuple = (1,2); // creates tuple
v : std::vector = (1,2); // creates a vector with 1 and 2 |
Or in function calls: f(1, 2); // call callable f with 1 and 2
:(a, b) = {
std::cout << a << b << std::end;
}(1, 2); // call the unnamed function with 1 and 2 |
Yes I understand it's a
Yes it's nice Cpp2 seems to unify them syntactically. |
Actually, it is not |
And named returns, which you initialize by name rather than put in an anonymous
|
I think this thread has answered some of the misunderstanding of Cpp2 syntax and what is supported, but I did think that this case should work:
|
* Check return parameter has identifier and type Note: type cannot be `_`. Fixes most of #325. * Tweak error messages; add tests * Move checks to parameter_declaration * Address review * Fix wrong check for missing type
* Check return parameter has identifier and type Note: type cannot be `_`. Fixes most of hsutter#325. * Tweak error messages; add tests * Move checks to parameter_declaration * Address review * Fix wrong check for missing type
Possibly this should be an enhancement request. Some of these errors may be because the feature is not implemented? Perhaps I'm using the wrong syntax? If so, better diagnostics would be nice, especially for
f
(see 4).To Reproduce
d
can't be initialized by an expression:But
d
doesn't used named return values. Alsof
can be initialized by a tuple expression without needing{}
.d
, spurious compile error fore
:There is no declared variable.
e
, spurious compile error forg
:Probably it's parsing
std
as an identifier.g
,f
compiles OK but doesn't declare or use a tuple struct so the c++ compiler errors:The text was updated successfully, but these errors were encountered: