Skip to content

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

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

Closed
shemeshg opened this issue Sep 15, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@shemeshg
Copy link

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)

@shemeshg shemeshg added the bug Something isn't working label Sep 15, 2023
@shemeshg shemeshg changed the title [BUG] Multiple move, reassign var after move works, expected to fail [BUG] Multiple move, reassign or cout var after move works. - expected to fail Sep 15, 2023
@shemeshg shemeshg changed the title [BUG] Multiple move, reassign or cout var after move works. - expected to fail [BUG] Multiple move, reassign or cout var after move - works however expected to fail Sep 15, 2023
@AbhinavK00
Copy link

This is the desired behaviour that it expected to happen. Moved-from variables are in well-formed state so you can do all those things with a moved-from variable. See comment #246 for more info.
Herb does mentions that he is interested in catching use-of-moved but with static analysis.

@JohelEGP
Copy link
Contributor

Looks like you're asking for what is quoted at #252 (comment) about Rust's borrow checker.

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

3 participants