profile
viewpoint

Ask questionsMIR InstCombine introduces copies of mutable borrows

https://play.rust-lang.org/?gist=9f782298cc5b0f6cd557baef9e443096&version=nightly

fn blah(mut x: String) {
    do_something(&mut x);
}

generates the following MIR:

    let mut _0: ();                      // return pointer
    let mut _2: ();
    let mut _3: &mut std::string::String;
    let mut _4: &mut std::string::String;

    bb0: {                              
        StorageLive(_3);                 // scope 0 at src/main.rs:5:18: 5:24
        StorageLive(_4);                 // scope 0 at src/main.rs:5:18: 5:24
        _4 = &mut _1;                    // scope 0 at src/main.rs:5:18: 5:24
        _3 = _4;                         // scope 0 at src/main.rs:5:18: 5:24
        _2 = const do_something(move _3) -> [return: bb1, unwind: bb3]; // scope 0 at src/main.rs:5:5: 5:25
    }

Which -- though we don't run it again after optimizations -- wouldn't pass TypeckMir since it's a copy of a &mut String.

That came from InstCombine on the following

        _3 = &mut (*_4);                 // scope 0 at lib.rs:33:18: 33:24

This may or may not be a problem, but I wanted to file it in case it impacts any of the assumptions desired from the Copy/Move split on Operand.

Edit: I suppose this instcombine has long potentially changed the type of the operand, since it doesn't check mutability of the outer & or the borrow being *d.

cc @eddyb

rust-lang/rust

Answer questions scottmcm

This is now also fixed by https://github.com/rust-lang/rust/pull/72820, which removed this fold in InstCombine

useful!

Related questions

Spurious NaNs produced by trig functions with valid inputs on Windows GNU toolchains hot 3
using 'cargo install xsv' on windows 10 triggers rustc internal error hot 2
under latest MinGW, cannot link with C code using stdout hot 2
Archive all nightlies hot 2
chain() make collect very slow hot 1
if/while Some(n) = &mut foo sugar will leak a temporary mutable borrow to current scope in particular situation hot 1
build an empty project failed (undefined reference to `__onexitbegin') hot 1
Invalid collision with TryFrom implementation? hot 1
Crater runs for Rust 1.38.0 hot 1
Spurious NaNs produced by trig functions with valid inputs on Windows GNU toolchains hot 1
Building LLVM with Clang fails hot 1
Internal compiler error: can't buffer lints after HIR lowering hot 1
E0373 help suggests `move async` but the correct syntax is `async move` hot 1
Tracking issue for `Option::contains` and `Result::contains` hot 1
async fn + rustfmt don't "just work" inside of RLS hot 1
source:https://uonfu.com/
Github User Rank List