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

Point to definition when modifying field of immutable variable #40767

Closed
wants to merge 1 commit into from

Conversation

estebank
Copy link
Contributor

Given a file

struct S {
    x: i32,
}
fn foo() {
    let s = S { x: 42 };
    s.x += 1;
}
fn bar(s: S) {
    s.x += 1;
}

Provide the following output:

error: cannot assign to immutable field `s.x`
 --> $DIR/issue-35937.rs:16:5
  |
5 |     let s = S { x: 42 };
  |         - consider changing this to `mut s`
6 |     s.x += 1;
  |     ^^^^^^^^ cannot mutably borrow immutable field

error: cannot assign to immutable field `s.x`
 --> $DIR/issue-35937.rs:20:5
  |
8 | fn bar(s: S) {
  |        - consider changing this to `mut s`
9 |     s.x += 1;
  |     ^^^^^^^^ cannot mutably borrow immutable field

Follow up to #40445. Fix #35937.

@rust-highfive
Copy link
Collaborator

r? @pnkfelix

(rust_highfive has picked a reviewer for you, use r? to override)

@Mark-Simulacrum
Copy link
Member

It'd be nice to avoid the phrasing "immutable field" since it's not really the field that's immutable, but more the binding of the struct. I'm not sure I can come up with anything better than "immutable binding," though, which isn't great either.

The help message does alleviate most of the potential pain from this, so while nice to improve, certainly not necessary.

@estebank estebank force-pushed the issue-35937 branch 2 times, most recently from 0f8c678 to 289772f Compare March 24, 2017 04:14
Given a file

```rust
struct S {
    x: i32,
}
fn foo() {
    let s = S { x: 42 };
    s.x += 1;
}
fn bar(s: S) {
    s.x += 1;
}
```

Provide the following output:

```rust
error: cannot assign to immutable field `s.x`
 --> $DIR/issue-35937.rs:16:5
  |
5 |     let s = S { x: 42 };
  |         - consider changing this to `mut s`
6 |     s.x += 1;
  |     ^^^^^^^^ cannot mutably borrow immutable field

error: cannot assign to immutable field `s.x`
 --> $DIR/issue-35937.rs:20:5
  |
8 | fn bar(s: S) {
  |        - consider changing this to `mut s`
9 |     s.x += 1;
  |     ^^^^^^^^ cannot mutably borrow immutable field
```

Follow up to rust-lang#40445. Fix rust-lang#35937.
@pnkfelix
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Mar 30, 2017

📌 Commit 289772f has been approved by pnkfelix

@bors
Copy link
Contributor

bors commented Mar 30, 2017

⌛ Testing commit 289772f with merge 83d832d...

@bors
Copy link
Contributor

bors commented Mar 30, 2017

💔 Test failed - status-travis

@estebank
Copy link
Contributor Author

estebank commented Apr 2, 2017

@bors retry

@bors
Copy link
Contributor

bors commented Apr 2, 2017

⌛ Testing commit 289772f with merge d1023ae...

@bors
Copy link
Contributor

bors commented Apr 2, 2017

💔 Test failed - status-travis

@alexcrichton
Copy link
Member

alexcrichton commented Apr 2, 2017 via email

@bors
Copy link
Contributor

bors commented Apr 3, 2017

⌛ Testing commit 289772f with merge 7f2ec26...

@bors
Copy link
Contributor

bors commented Apr 3, 2017

💔 Test failed - status-travis

@estebank
Copy link
Contributor Author

estebank commented Apr 3, 2017

@bors retry

@bors
Copy link
Contributor

bors commented Apr 3, 2017

⌛ Testing commit 289772f with merge cd0a4f3...

@bors
Copy link
Contributor

bors commented Apr 3, 2017

💔 Test failed - status-travis

@alexcrichton
Copy link
Member

alexcrichton commented Apr 3, 2017 via email

@bors
Copy link
Contributor

bors commented Apr 5, 2017

⌛ Testing commit 289772f with merge ceaaebc...

@bors
Copy link
Contributor

bors commented Apr 5, 2017

💔 Test failed - status-travis

@estebank
Copy link
Contributor Author

estebank commented Apr 5, 2017

@bors retry

@alexcrichton
Copy link
Member

@bors: r-

I think that's a legitimate error unfortunately:

[00:12:10]    Compiling rustc_borrowck v0.0.0 (file:///checkout/src/librustc_borrowck)
[00:12:10] error[E0425]: cannot find value `local_def` in this scope
[00:12:10]    --> /checkout/src/librustc_borrowck/borrowck/mod.rs:690:25
[00:12:10]     |
[00:12:10] 690 |                         local_def = err.cmt.get_def().map(|nid| self.tcx.hir.span(nid));
[00:12:10]     |                         ^^^^^^^^^ not found in this scope
[00:12:10] 
[00:12:11] error: no method named `get_def` found for type `std::rc::Rc<rustc::middle::mem_categorization::cmt_<'tcx>>` in the current scope
[00:12:11]    --> /checkout/src/librustc_borrowck/borrowck/mod.rs:690:45
[00:12:11]     |
[00:12:11] 690 |                         local_def = err.cmt.get_def().map(|nid| self.tcx.hir.span(nid));
[00:12:11]     |                                             ^^^^^^^
[00:12:11] 
[00:12:11] error[E0277]: the trait bound `str: core::marker::Sized` is not satisfied
[00:12:11]    --> /checkout/src/librustc_borrowck/borrowck/mod.rs:666:13
[00:12:11]     |
[00:12:11] 666 |         let msg = match err.code {
[00:12:11]     |             ^^^ the trait `core::marker::Sized` is not implemented for `str`
[00:12:11]     |
[00:12:11]     = note: `str` does not have a constant size known at compile-time
[00:12:11]     = note: all local variables must have a statically known size
[00:12:11] 
[00:12:13] error: aborting due to 2 previous errors
[00:12:13] 
[00:12:13] error: Could not compile `rustc_borrowck`.
[00:12:13] Build failed, waiting for other jobs to finish...
[00:13:06] error: build failed
[00:13:06] 
[00:13:06] 
[00:13:06] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "build" "-j" "4" "--target" "x86_64-unknown-linux-gnu" "--release" "--locked" "--features" " jemalloc" "--manifest-path" "/checkout/src/rustc/Cargo.toml"
[00:13:06] expected success, got: exit code: 101
[00:13:06] 
[00:13:06] 
[00:13:06] Build completed unsuccessfully in 0:10:40
[00:13:06] Makefile:24: recipe for target 'all' failed
[00:13:06] make: *** [all] Error 1

@estebank
Copy link
Contributor Author

estebank commented Apr 5, 2017

@alexcrichton it seems like #40841 supersedes this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants