Skip to content

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

Open

Description

@JohelEGP

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;
}

Activity

  1. JohelEGP commented on Aug 29, 2023

    @JohelEGP
    ContributorAuthor

    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.

  2. JohelEGP commented on Sep 12, 2023

    @JohelEGP
    ContributorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions