rust-lang / rust-lang/rust-analyzer

mutably borrowed Type Alias incorrectly marked as immutable

Open
#21,168 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-mir C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

this is a weird one. When asserting the type of the length of an array with as usize a bug occurs where passing a variable that is &mut Alias into a function that take &Alias marks that variable as immutable.

This does not happen when the type alias is changed to type ArrayAlias = [u16; 8];

type ArrayAlias = [u16; 8 as usize];

fn main() {}

fn produce_error(array: &mut ArrayAlias) {
    mark_register_immutable(array);
    array[0] = 5; // cannot mutate immutable variable `array`
}

fn mark_register_immutable(_: &ArrayAlias){
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided Rust snippet and compare it with the version using type ArrayAlias = [u16; 8];. Trace how the type alias with 8 as usize is handled when passing &mut ArrayAlias to &ArrayAlias. Done means array[0] = 5 remains mutable in the reproducer, with coverage for both alias definitions.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.