Skip to content

Provide more context when mutably borrowing an imutably borrowed value - #148508

Merged
bors merged 1 commit into
rust-lang:mainfrom
estebank:issue-74617
Nov 11, 2025
Merged

Provide more context when mutably borrowing an imutably borrowed value#148508
bors merged 1 commit into
rust-lang:mainfrom
estebank:issue-74617

Conversation

@estebank

@estebankestebank commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

Point at statics and consts being mutable borrowed or written to:

error[E0594]: cannot assign to immutable static item `NUM`
--> $DIR/E0594.rs:4:5
|
LL | static NUM: i32 = 18;
| --------------- this `static` cannot be written to
...
LL | NUM = 20;
| ^^^^^^^^ cannot assign

Point at the expression that couldn't be mutably borrowed from a pattern:

error[E0596]: cannot borrow data in a `&` reference as mutable
--> $DIR/mut-pattern-of-immutable-borrow.rs:19:14
|
LL | match &arg.field {
| ---------- this cannot be borrowed as mutable
LL | Some(ref mut s) => s.push('a'),
| ^^^^^^^^^ cannot borrow as mutable

Partially address #74617.

@rustbot

Copy link
Copy Markdown
Collaborator

This PR modifies tests/ui/issues/. If this PR is adding new tests to tests/ui/issues/,
please refrain from doing so, and instead add it to more descriptive subdirectories.

@rustbotrustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 5, 2025
@rustbot

Copy link
Copy Markdown
Collaborator

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

{
err.span_label(
local.source_info.span,
format!("this cannot be {acted_on}"),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem to have a testcase.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's covered by a few for Boring like in tests/ui/nll/dont-print-desugared.stderr, and tests/ui/pattern/mut-pattern-of-immutable-borrow.rs covers the BlockTailTemp case.

@hkBsthkBstNov 6, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked for the output "this cannot be written to" and did not find it. Since you added the "to", the output should be in the diff, but I did not find it...

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. It is a combination that can't happen. It would trigger with something like

structS;implS{fnfoo(&self){*self = S;}}

but the explicit check for no overlapping spans means that the output is unchanged:

error[E0594]: cannot assign to `*self`, which is behind a `&` reference
--> f16.rs:4:9
|
4 | *self = S;
| ^^^^^^^^^ `self` is a `&` reference, so it cannot be written to
|
help: consider changing this to be a mutable reference
|
3 | fn foo(&mut self) {
| +++

Comment threadtests/ui/borrowck/argument_number_mismatch_ice.stderr Outdated
@nnethercote

Copy link
Copy Markdown
Contributor

r=me with the two commits squashed. Thanks.

Point at statics and consts being mutable borrowed or written to:
```
error[E0594]: cannot assign to immutable static item `NUM`
--> $DIR/E0594.rs:4:5
|
LL | static NUM: i32 = 18;
| --------------- this `static` cannot be written to
...
LL | NUM = 20;
| ^^^^^^^^ cannot assign
```
Point at the expression that couldn't be mutably borrowed from a pattern:
```
error[E0596]: cannot borrow data in a `&` reference as mutable
--> $DIR/mut-pattern-of-immutable-borrow.rs:19:14
|
LL | match &arg.field {
| ---------- this cannot be borrowed as mutable
LL | Some(ref mut s) => s.push('a'),
| ^^^^^^^^^ cannot borrow as mutable
```
@nnethercotennethercote added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 10, 2025
@estebank

Copy link
Copy Markdown
ContributorAuthor

@bors r=nnethercote

@bors

bors commented Nov 10, 2025

Copy link
Copy Markdown
Collaborator

📌 Commit 71b0755 has been approved by nnethercote

It is now in the queue for this repository.

@bors

bors commented Nov 10, 2025

Copy link
Copy Markdown
Collaborator

🌲 The tree is currently closed for pull requests below priority 1000. This pull request will be tested once the tree is reopened.

@borsbors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 10, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 11, 2025
Provide more context when mutably borrowing an imutably borrowed value
Point at statics and consts being mutable borrowed or written to:
```
error[E0594]: cannot assign to immutable static item `NUM`
--> $DIR/E0594.rs:4:5
|
LL | static NUM: i32 = 18;
| --------------- this `static` cannot be written to
...
LL | NUM = 20;
| ^^^^^^^^ cannot assign
```
Point at the expression that couldn't be mutably borrowed from a pattern:
```
error[E0596]: cannot borrow data in a `&` reference as mutable
--> $DIR/mut-pattern-of-immutable-borrow.rs:19:14
|
LL | match &arg.field {
| ---------- this cannot be borrowed as mutable
LL | Some(ref mut s) => s.push('a'),
| ^^^^^^^^^ cannot borrow as mutable
```
Partially address rust-lang#74617.
@ZalatharZalathar mentioned this pull request Nov 11, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 11, 2025
Provide more context when mutably borrowing an imutably borrowed value
Point at statics and consts being mutable borrowed or written to:
```
error[E0594]: cannot assign to immutable static item `NUM`
--> $DIR/E0594.rs:4:5
|
LL | static NUM: i32 = 18;
| --------------- this `static` cannot be written to
...
LL | NUM = 20;
| ^^^^^^^^ cannot assign
```
Point at the expression that couldn't be mutably borrowed from a pattern:
```
error[E0596]: cannot borrow data in a `&` reference as mutable
--> $DIR/mut-pattern-of-immutable-borrow.rs:19:14
|
LL | match &arg.field {
| ---------- this cannot be borrowed as mutable
LL | Some(ref mut s) => s.push('a'),
| ^^^^^^^^^ cannot borrow as mutable
```
Partially address rust-lang#74617.
@ZalatharZalathar mentioned this pull request Nov 11, 2025
bors added a commit that referenced this pull request Nov 11, 2025
Rollup of 16 pull requests
Successful merges:
- #141470 (Add new `function_casts_as_integer` lint)
- #143619 (`c_variadic`: Add future-incompatibility warning for `...` arguments without a pattern outside of `extern` blocks)
- #146495 (rustdoc: Erase `#![doc(document_private_items)]`)
- #147771 (Rename `*exact_{div,shr,shl}` to `*{div,shr,shl}_exact`)
- #147833 (rustdoc-json: move `target` to `json::conversions`)
- #147952 (Add a timeout to the `remote-test-client` connection)
- #147955 (compiletest: Migrate `TestProps` directive handling to a system of named handlers)
- #148480 (Add `Steal::risky_hack_borrow_mut`)
- #148506 (Special case detecting `'static` lifetime requirement coming from `-> Box<dyn Trait>`)
- #148508 (Provide more context when mutably borrowing an imutably borrowed value)
- #148530 (update the bootstrap readme)
- #148608 (Add test for --test-builder success path)
- #148636 (bootstrap: respect `build.python` on macOS)
- #148639 (test(rustdoc): move tests into jump-to-def)
- #148647 (Check unsafety for non-macro attributes in `validate_attr`)
- #148667 (a few small clippy fixes)
r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Nov 11, 2025
Rollup of 16 pull requests
Successful merges:
- #141470 (Add new `function_casts_as_integer` lint)
- #143619 (`c_variadic`: Add future-incompatibility warning for `...` arguments without a pattern outside of `extern` blocks)
- #146495 (rustdoc: Erase `#![doc(document_private_items)]`)
- #147771 (Rename `*exact_{div,shr,shl}` to `*{div,shr,shl}_exact`)
- #147833 (rustdoc-json: move `target` to `json::conversions`)
- #147952 (Add a timeout to the `remote-test-client` connection)
- #147955 (compiletest: Migrate `TestProps` directive handling to a system of named handlers)
- #148480 (Add `Steal::risky_hack_borrow_mut`)
- #148506 (Special case detecting `'static` lifetime requirement coming from `-> Box<dyn Trait>`)
- #148508 (Provide more context when mutably borrowing an imutably borrowed value)
- #148530 (update the bootstrap readme)
- #148608 (Add test for --test-builder success path)
- #148636 (bootstrap: respect `build.python` on macOS)
- #148639 (test(rustdoc): move tests into jump-to-def)
- #148647 (Check unsafety for non-macro attributes in `validate_attr`)
- #148667 (a few small clippy fixes)
r? `@ghost`
`@rustbot` modify labels: rollup
@ZalatharZalathar mentioned this pull request Nov 11, 2025
bors added a commit that referenced this pull request Nov 11, 2025
Rollup of 15 pull requests
Successful merges:
- #141470 (Add new `function_casts_as_integer` lint)
- #143619 (`c_variadic`: Add future-incompatibility warning for `...` arguments without a pattern outside of `extern` blocks)
- #146495 (rustdoc: Erase `#![doc(document_private_items)]`)
- #147771 (Rename `*exact_{div,shr,shl}` to `*{div,shr,shl}_exact`)
- #147833 (rustdoc-json: move `target` to `json::conversions`)
- #147955 (compiletest: Migrate `TestProps` directive handling to a system of named handlers)
- #148480 (Add `Steal::risky_hack_borrow_mut`)
- #148506 (Special case detecting `'static` lifetime requirement coming from `-> Box<dyn Trait>`)
- #148508 (Provide more context when mutably borrowing an imutably borrowed value)
- #148530 (update the bootstrap readme)
- #148608 (Add test for --test-builder success path)
- #148636 (bootstrap: respect `build.python` on macOS)
- #148639 (test(rustdoc): move tests into jump-to-def)
- #148647 (Check unsafety for non-macro attributes in `validate_attr`)
- #148667 (a few small clippy fixes)
r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit 417bea3 into rust-lang:mainNov 11, 2025
11 checks passed
@rustbotrustbot added this to the 1.93.0 milestone Nov 11, 2025
rust-timer added a commit that referenced this pull request Nov 11, 2025
Rollup merge of #148508 - estebank:issue-74617, r=nnethercote
Provide more context when mutably borrowing an imutably borrowed value
Point at statics and consts being mutable borrowed or written to:
```
error[E0594]: cannot assign to immutable static item `NUM`
--> $DIR/E0594.rs:4:5
|
LL | static NUM: i32 = 18;
| --------------- this `static` cannot be written to
...
LL | NUM = 20;
| ^^^^^^^^ cannot assign
```
Point at the expression that couldn't be mutably borrowed from a pattern:
```
error[E0596]: cannot borrow data in a `&` reference as mutable
--> $DIR/mut-pattern-of-immutable-borrow.rs:19:14
|
LL | match &arg.field {
| ---------- this cannot be borrowed as mutable
LL | Some(ref mut s) => s.push('a'),
| ^^^^^^^^^ cannot borrow as mutable
```
Partially address #74617.
github-actionsBot pushed a commit to rust-lang/miri that referenced this pull request Nov 12, 2025
Rollup of 15 pull requests
Successful merges:
- rust-lang/rust#141470 (Add new `function_casts_as_integer` lint)
- rust-lang/rust#143619 (`c_variadic`: Add future-incompatibility warning for `...` arguments without a pattern outside of `extern` blocks)
- rust-lang/rust#146495 (rustdoc: Erase `#![doc(document_private_items)]`)
- rust-lang/rust#147771 (Rename `*exact_{div,shr,shl}` to `*{div,shr,shl}_exact`)
- rust-lang/rust#147833 (rustdoc-json: move `target` to `json::conversions`)
- rust-lang/rust#147955 (compiletest: Migrate `TestProps` directive handling to a system of named handlers)
- rust-lang/rust#148480 (Add `Steal::risky_hack_borrow_mut`)
- rust-lang/rust#148506 (Special case detecting `'static` lifetime requirement coming from `-> Box<dyn Trait>`)
- rust-lang/rust#148508 (Provide more context when mutably borrowing an imutably borrowed value)
- rust-lang/rust#148530 (update the bootstrap readme)
- rust-lang/rust#148608 (Add test for --test-builder success path)
- rust-lang/rust#148636 (bootstrap: respect `build.python` on macOS)
- rust-lang/rust#148639 (test(rustdoc): move tests into jump-to-def)
- rust-lang/rust#148647 (Check unsafety for non-macro attributes in `validate_attr`)
- rust-lang/rust#148667 (a few small clippy fixes)
r? `@ghost`
`@rustbot` modify labels: rollup
tautschnig added a commit to tautschnig/kani that referenced this pull request Nov 12, 2025
Relevant upstream PR:
- rust-lang/rust#148508 (Provide more context when
mutably borrowing an imutably borrowed value)
Resolves: model-checking#4468
github-merge-queueBot pushed a commit to model-checking/kani that referenced this pull request Nov 12, 2025
Relevant upstream PR:
- rust-lang/rust#148508 (Provide more context
when mutably borrowing an imutably borrowed value)
Resolves: #4468
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Aug 17, 2026
Rollup of 15 pull requests
Successful merges:
- rust-lang/rust#141470 (Add new `function_casts_as_integer` lint)
- rust-lang/rust#143619 (`c_variadic`: Add future-incompatibility warning for `...` arguments without a pattern outside of `extern` blocks)
- rust-lang/rust#146495 (rustdoc: Erase `#![doc(document_private_items)]`)
- rust-lang/rust#147771 (Rename `*exact_{div,shr,shl}` to `*{div,shr,shl}_exact`)
- rust-lang/rust#147833 (rustdoc-json: move `target` to `json::conversions`)
- rust-lang/rust#147955 (compiletest: Migrate `TestProps` directive handling to a system of named handlers)
- rust-lang/rust#148480 (Add `Steal::risky_hack_borrow_mut`)
- rust-lang/rust#148506 (Special case detecting `'static` lifetime requirement coming from `-> Box<dyn Trait>`)
- rust-lang/rust#148508 (Provide more context when mutably borrowing an imutably borrowed value)
- rust-lang/rust#148530 (update the bootstrap readme)
- rust-lang/rust#148608 (Add test for --test-builder success path)
- rust-lang/rust#148636 (bootstrap: respect `build.python` on macOS)
- rust-lang/rust#148639 (test(rustdoc): move tests into jump-to-def)
- rust-lang/rust#148647 (Check unsafety for non-macro attributes in `validate_attr`)
- rust-lang/rust#148667 (a few small clippy fixes)
r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@estebank@rustbot@nnethercote@bors@hkBst