Skip to content
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

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

Closed
jruderman opened this issue Jul 20, 2012 · 2 comments
Closed

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

jruderman opened this issue Jul 20, 2012 · 2 comments
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Milestone

Comments

@jruderman
Copy link
Contributor

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.

@catamorphism
Copy link
Contributor

Reproduced as of b60a0be

@pnkfelix
Copy link
Member

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.

RalfJung pushed a commit to RalfJung/rust that referenced this issue Jul 8, 2023
Stop parsing ui_test annotations in `run-dep` mode

fixes rust-lang#2967
celinval pushed a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
Relevant PRs:
- rust-lang#119601
- rust-lang#119146
- rust-lang#119174

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

3 participants