Skip to content

"assigning... prohibited due to outstanding loan" could be clearer #2969

Description

@jruderman
fn main()
{
   let mut x = [mut 1, 2, 4];
   let v : &int = &x[2];
   x[2] = 6;
   assert *v == 6;
}

gives me

/Users/jruderman/Desktop/a.rs:5:3: 5:6 error: assigning to mutable vec content prohibited due to outstanding loan
/Users/jruderman/Desktop/a.rs:5    x[2] = 6;
                                   ^~~
/Users/jruderman/Desktop/a.rs:4:19: 4:22 note: loan of mutable vec content granted here
/Users/jruderman/Desktop/a.rs:4    let v : &int = &x[2];
                                                   ^~~

I think the error message should emphasize that it is an immutable loan. So it's clear that if you were to change it to a mutable loan, it's okay.

Activity

  1. catamorphism commented on Dec 6, 2012

    @catamorphism
    Contributor

    Reproduced as of b60a0be

  2. pnkfelix commented on Mar 22, 2013

    @pnkfelix
    Contributor

    According to my experiments with the current rustc, the statement "if you were to change it to a mutable loan, it's okay" is false, and therefore changing the error message in the manner described would be a mistake.

    That is, from what I can tell, rustc treats this:

    fn foo01() { // local vector, mutable borrow
        let x = [1, 2, 4];
        let v : &mut int = &mut x[2];
        x[2] = 6; // error: assigning to mutable vec content prohibited due to outstanding loan
        fail_unless!( *v == 6 );
    }

    the same way that it treats this:

    fn foo11() { // owned vector, mutable borrow
        let x = ~[1, 2, 4];
        let v : &mut int = &mut x[2];
        x[2] = 6; // error: assigning to mutable vec content prohibited due to outstanding loan
        fail_unless!( *v == 6 );
    }

    and that is very different from how it treats this:

    fn foo21() { // heap-mutable vector, mutable borrow
        let x = @mut [1, 2, 4];
        let v : &mut int = &mut x[2];
        x[2] = 6;
        fail_unless!( *v == 6 );
    }

    For the case of the owned vector, the error message makes perfect sense, regardless of whether the loan is mutable or immutable. (And I'm pretty sure we have intentionally given stack-local allocation a semantics matching that of owned allocations.)


    The only other scenario that I can imagine this bug is trying to describe, is when we have a heap-mutable vector and an immutable-borrow, like this:

    fn foo20() { // heap-mutable vector, immutable borrow
        let x = @mut [1, 2, 4];
        let v : &int = &x[2]; // error: illegal borrow unless pure: creating immutable alias to mutable vec content
        x[2] = 6; // note: impure due to assigning to mutable vec content
        fail_unless!( *v == 6 );
    }

    But rustc emits different error messages (shown above) for these two cases.


    So, I am closing this bug as it is not-a-bug, AFAICT. Feel free to reopen if I have misunderstood the issue or incorrectly translated the example to the current syntax.

  3. added a commit that references this issue on Aug 19, 2026
  4. added 2 commits that reference this issue on Aug 21, 2026
    f4431d2
    8e0ad99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-type-systemArea: Type systemE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions