rust icon indicating copy to clipboard operation
rust copied to clipboard

Arrays not coercing a mutable reference in the first position

Open ChrisJefferson opened this issue 5 years ago • 4 comments

Given i, j and k of type &mut isize, then &mut [i,j,k] moves i, but not j or k. I discovered this when calling vec![i,j,k].

Similar issues: Arise from let r = &mut 0; [&mut 0, r]; *r += 1; works, but let r = &mut 0; [r, &mut 0]; *r += 1; complains about use of moved value *r.

See an example full program here: https://play.rust-lang.org/?gist=13c2a934ae9d3f6c22b62c433fee4244&version=stable&mode=debug&edition=2015

ChrisJefferson avatar Sep 23 '18 11:09 ChrisJefferson

CC @eddyb @nikomatsakis

ChrisJefferson avatar Sep 23 '18 11:09 ChrisJefferson

Both [&mut 0, &mut &mut 0] and [&mut &mut 0, &mut 0] work, so I'm guessing deref coercions happen when the "LUB (aka join) coercion" mechanism "goes back" in the array, but reborrows don't. Probably just a whitelist? But I don't remember the code exactly.

eddyb avatar Sep 23 '18 11:09 eddyb

Just as an update, I tried this in the latest nightly and it still present.

ChrisJefferson avatar Oct 01 '19 18:10 ChrisJefferson

triage: no change

Spoonbender avatar Oct 17 '22 10:10 Spoonbender