Skip to content

Wrong error message with managed boxes #5717

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

Closed
dhardy opened this issue Apr 4, 2013 · 6 comments
Closed

Wrong error message with managed boxes #5717

dhardy opened this issue Apr 4, 2013 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@dhardy
Copy link
Contributor

dhardy commented Apr 4, 2013

The compiler reports the wrong error message in this sample (which I don't know whether should be errornous or not; I'm just learning Rust):

fn main() {
    let a = @5;         // immutable

    let mut d = @mut 5; // mutable variable, mutable box
    *d += 5;    // error message about this line, though this is fine
    d = a;      // error doesn't appear if this isn't mentioned

    io::println(fmt!("a, d:\t%d, %d", *a,*d));
}

The message reported is:

dhardy@L10036:~/code/small/rust$ rust run mut.rs 
mut.rs:5:4: 5:6 error: assigning to dereference of const @ pointer
mut.rs:5     *d += 5;
            ^~
error: aborting due to previous error

I'm using the compiler from commit 5f13e9c.

@dhardy
Copy link
Contributor Author

dhardy commented Apr 4, 2013

While we're at it, could someone explain whether "box mutability" is a property of the handle (named variable) or the box? Because:

let mut e = @mut 1;
e = d;  // fine (d from above example)
let mut f = @ 2;
f = d;   // same poor error message as above

@thestinger
Copy link
Contributor

Owned types inherit their mutability from the handle (owner), but managed boxes aren't owned so the mutability is determined by the box's type (@mut vs. @). You could consider the handle to be merely observing it.

@mut and @ are different types, which is why there's an error. The tutorial should be more clear about this. The error message is of course totally wrong :).

@dhardy
Copy link
Contributor Author

dhardy commented Apr 4, 2013

Clear, thanks. So it's a property of both the handle and the box.

Can I be really annoying and ask how to specify that in the type? I.e.

let mut g : @int;

appears to be a mutable handle to an immutable box, but how do I specify a type with a mutable managed box (without assinging it a value)?

@thestinger
Copy link
Contributor

You can use @mut there too, like let x: @mut int;. This also applies to & and &mut since those also just observe a value instead of expressing ownership.

@bstrie
Copy link
Contributor

bstrie commented Jun 27, 2013

Reproduced. I presume this would be obviated by moving GC into a library (as it would make it more clear that @ and @mut are separate types).

@alexcrichton
Copy link
Member

The error message now accurately states

foo.rs:7:8: 7:9 error: mismatched types: expected `@mut <VI1>` but found `@<VI0>` (values differ in mutability)
foo.rs:7     d = a;      // error doesn't appear if this isn't mentioned
                 ^
error: aborting due to previous error

which is correct because @mut T and @T are different types. Closing this because GC is very soon moving into a library, @-boxes are behind a feature gate, and this seems to be working well enough now (although a test isn't really that necessary with all the in-flight changes)

flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 30, 2022
Don't lint `while_let_loop` when significant drop order would change

fixes rust-lang#7226
fixes rust-lang#7913
fixes rust-lang#5717

For rust-lang#5717 it may not stay fully fixed. This is only completely fixed right now due to all the allowed drop impls have `#[may_dangle]` on their drop impls. This may get changed in the future based on how significant drops are determined, but the example listed with `RefCell` shouldn't break.

changelog: Don't lint `while_let_loop` when the order of significant drops would change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

4 participants