Skip to content

[BUG] Multiple move, reassign or cout var after move - works however expected to fail #684

Closed
@shemeshg

Description

@shemeshg

Describe the bug
The following example works, however it is expected to fail.

I can

  1. re-move, already moved string.
  2. reassign moved string (unremark the //s = "test2";)
  3. cout already moved string

To Reproduce
Steps to reproduce the behavior:

  1. Sample code - distilled down to minimal essentials please
class Wraper {
    public:
    Wraper(const std::string &&p):s{std::move(p)}{
       
       std::cout << "Moved !!!\n"; 
    }

    void foo(std::string &&x){
        s=std::move(x);
    }

    void doPrint(){
        std::cout << s; 
    }

    std::string s;
};

main: () -> int = {
    s:std::string ="Hello,!\n";
    w:Wraper = move(s);
    w.foo(move(s));
    s="asd";

    std::cout << "after move ** "<<s<<"**\n"; 
    w.doPrint();
    //s = "test2";
    w.doPrint();    
}

  1. Command lines including which C++ compiler you are using
    clanf 14, macos
FetchContent_Declare(
    cppfront
    GIT_REPOSITORY https://github.com/modern-cmake/cppfront.git
    GIT_TAG main  # or an actual git SHA if you don't like to live dangerously
)

FetchContent_MakeAvailable(cppfront)
  1. Expected result - what you expected to happen
  • error, can not move already moved var.
  • |Error can not reassign moved var
  • Error can not cout already moved var
  1. Actual result/error
  • no error - that is the problem

Additional context
Add any other context about the problem here.

Compiled into

#line 23 "/Volumes/RAM_Disk_4G/g/main.cpp2"
[[nodiscard]] auto main() -> int{
    std::string s {"Hello,!\n"}; 
    Wraper w {move(s)}; 
    CPP2_UFCS(foo, w, move(s));
    s = "asd";

    std::cout << "after move ** " << std::move(s) << "**\n";
    CPP2_UFCS_0(doPrint, w);
    //s = "test2";
    CPP2_UFCS_0(doPrint, std::move(w));
}

clang, don't prevent this, so it is just unexpected behavior (s could be empty, or ref to old value)

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