Skip to content
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

[BUG] Trying to 'std::move' when using with std::cin a last use variable #609

Open
soraphis opened this issue Aug 21, 2023 · 5 comments
Open
Labels
bug Something isn't working

Comments

@soraphis
Copy link

Describe the bug
If the guaranteed last use of a variable is in a std::cin a compiler error is thrown, since the cppfront generated code tries to std::move the variable.

First I did not realize it was because of the guaranteed last use, and while it is kinda nonsensical to read into a variable without ever reading from it, it was kinda confusing to get the cpp compiler error, as that meant I'd had to look into the cpp file as the cpp2 file was kinda "fine".

To Reproduce
https://cpp2.godbolt.org/z/d9e4bKGKW

@soraphis soraphis added the bug Something isn't working label Aug 21, 2023
@JohelEGP
Copy link
Contributor

The fix is to use it again by discarding it, e.g., _ = read_variable;.
See #466 (comment).
As you can see, this is a duplicate of quite a few other issues.

@SebastianTroy
Copy link

SebastianTroy commented Aug 21, 2023 via email

@soraphis
Copy link
Author

Is this not actually highlighting an error in your code?

I'm usign VS Code for toying a bit around, so ... no, not really. Especially not in the cpp2 code. cppfront also runs through just fine.

and as usual, c++ error messages being perfectly understandable for humans:
It took me quite a while and looking into the cpp file and the understanding that the std::move did not make any sense there.

...\project\src\helloworld\helloworld.cpp2(7): error C2679: binary '>>': no operator found which takes a right-hand operand of type 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' (or there is no acceptable conversion)
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\istream(299): note: could be 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_streambuf<char,std::char_traits<char>> *)'
...

The fix is to use it again by discarding it, e.g., _ = read_variable;.

yeah, I kinda understand that now, but the error message I see will not always be same and it kinda confused me quite a bit.

I mean ... it says here (https://github.com/hsutter/cppfront/wiki/Design-note:-Explicit-discard)

This is a feature, not a bug -- Cpp2 is correctly diagnosing that a last use of value is not compatible with a parameter that has an out component, because it means no later code looks at the output value.

But I kinda fail to see where the feature is, as it didn't tell me anything, tbh.
wouldn't it be awesome, when cppfront would spit out an error?

last use of value "name" is not compatible with a parameter that has an out component. If this is intended discard the value like this: _ = name

then I'd know where the error is coming from (and how to resolve it). because if i don't get this hint, the errors which I get don't help me reaching the wiki page where it is explained.

@JohelEGP
Copy link
Contributor

Unfortunately, Cppfront is a transpiler.
And you need a Cpp1 compiler to be able to tell a call will fail due to a missing explicit discard.

@tsoj
Copy link
Contributor

tsoj commented Oct 19, 2023

Maybe the error message could be improved in cases where a cpp2 function with an inout parameter is called? Like "variable must be used at least once after returning from inout".

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

4 participants