Skip to content

Point to definition when modifying field of immutable variable #40767

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
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
@@ -688,6 +688,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {

match err.cause {
MutabilityViolation => {
local_def = err.cmt.get_def().map(|nid| self.tcx.hir.span(nid));
format!("cannot assign to {}", descr)
}
BorrowViolation(euv::ClosureCapture(_)) => {
@@ -1132,6 +1133,7 @@ before rustc 1.16, this temporary lived longer - see issue #39283 \
}
}
}

pub fn append_loan_path_to_string(&self,
loan_path: &LoanPath<'tcx>,
out: &mut String) {
31 changes: 31 additions & 0 deletions src/test/ui/did_you_mean/issue-35937.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

struct Foo {
pub v: Vec<String>
}

fn main() {
let f = Foo { v: Vec::new() };
f.v.push("cat".to_string());
}


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

fn bar(s: S) {
s.x += 1;
}
26 changes: 26 additions & 0 deletions src/test/ui/did_you_mean/issue-35937.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error: cannot borrow immutable field `f.v` as mutable
--> $DIR/issue-35937.rs:17:5
|
16 | let f = Foo { v: Vec::new() };
| - consider changing this to `mut f`
17 | f.v.push("cat".to_string());
| ^^^ cannot mutably borrow immutable field

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

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

error: aborting due to 3 previous errors