Skip to content

Fix borrowck mentioning a name from an external macro we (deliberately) don't save #141891

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

Merged
merged 3 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
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
24 changes: 16 additions & 8 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,14 +840,22 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
} else {
bug!("not an upvar")
};
err.span_label(
*span,
format!(
"calling `{}` requires mutable binding due to {}",
self.describe_place(the_place_err).unwrap(),
reason
),
);
// sometimes we deliberately don't store the name of a place when coming from a macro in
// another crate. We generally want to limit those diagnostics a little, to hide
// implementation details (such as those from pin!() or format!()). In that case show a
// slightly different error message, or none at all if something else happened. In other
// cases the message is likely not useful.
if let Some(place_name) = self.describe_place(the_place_err) {
err.span_label(
*span,
format!("calling `{place_name}` requires mutable binding due to {reason}"),
);
} else if span.from_expansion() {
err.span_label(
*span,
format!("a call in this macro requires a mutable binding due to {reason}",),
);
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/macros/auxiliary/borrowck-error-in-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[macro_export]
macro_rules! ice {
() => {
fn main() {
let d = &mut 0;
let c = || *d += 1;
c();
}
};
}
8 changes: 8 additions & 0 deletions tests/ui/macros/borrowck-error-in-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@ aux-build: borrowck-error-in-macro.rs
//@ error-pattern: a call in this macro requires a mutable binding due to mutable borrow of `d`
//FIXME: remove error-pattern (see #141896)

extern crate borrowck_error_in_macro as a;

a::ice! {}
//~^ ERROR cannot borrow value as mutable, as it is not declared as mutable
19 changes: 19 additions & 0 deletions tests/ui/macros/borrowck-error-in-macro.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0596]: cannot borrow value as mutable, as it is not declared as mutable
--> $DIR/borrowck-error-in-macro.rs:7:1
|
LL | a::ice! {}
| ^^^^^^^^^^
| |
| cannot borrow as mutable
| a call in this macro requires a mutable binding due to mutable borrow of `d`
|
= note: this error originates in the macro `a::ice` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider changing this to be mutable
--> $DIR/auxiliary/borrowck-error-in-macro.rs:6:17
|
LL | let mut c = || *d += 1;
| +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0596`.
Loading