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] Implicit move before taking address of member #558

Open
JohelEGP opened this issue Jul 26, 2023 · 2 comments · May be fixed by #676
Open

[BUG] Implicit move before taking address of member #558

JohelEGP opened this issue Jul 26, 2023 · 2 comments · May be fixed by #676
Labels
bug Something isn't working

Comments

@JohelEGP
Copy link
Contributor

JohelEGP commented Jul 26, 2023

Title: Implicit move before taking address of member.

Minimal reproducer (https://cpp2.godbolt.org/z/651rnTT7b):

t: @struct type = {
  x: i32;
}
main: () = {
  v: t = ();
  _ = v.x&;
}
Commands:
cppfront main.cpp2
clang++17 -std=c++23 -stdlib=libc++ -lc++abi -pedantic-errors -Wall -Wextra -Wconversion -fsanitize=undefined -Werror=unused-result -I . main.cpp

Expected result: No implicit move, like in other problematic cases.

Actual result and error:

main.cpp2:6:10: error: cannot take the address of an rvalue of type 'cpp2::i32' (aka 'int')
    6 |   (void) &std::move(v).x;
      |          ^~~~~~~~~~~~~~~
Cpp2 lowered to Cpp1:
//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"

class t;
  

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

class t {
  public: cpp2::i32 x; 
};
auto main() -> int;
  

//=== Cpp2 function definitions =================================================


auto main() -> int{
  t v {}; 
  (void) &std::move(v).x;
}
@JohelEGP JohelEGP added the bug Something isn't working label Jul 26, 2023
@JohelEGP
Copy link
Contributor Author

Note that you could say that this is fixed by appending the statement _ = v;.
IIRC there have been exceptions added to not generate a move on last use
in a case like the one in this issue, e.g., moving is statically known to be always ill-formed.
Which is why I opened this issue instead.

@JohelEGP
Copy link
Contributor Author

This is what I'm referring to:

cppfront/source/cppfront.cpp

Lines 3100 to 3109 in ecd3726

// If this is an --, ++, or &, don't add std::move on the lhs
// even if this is a definite last use (only do that when an rvalue is okay)
if (
n.ops.front().op->type() == lexeme::MinusMinus
|| n.ops.front().op->type() == lexeme::PlusPlus
|| n.ops.front().op->type() == lexeme::Ampersand
)
{
suppress_move_from_last_use = true;
}

It works for variables, but not when accessing a variable's data member.

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

Successfully merging a pull request may close this issue.

1 participant