Skip to content

Commit f3622ea

Browse files
authored
Rollup merge of #141891 - jdonszelmann:fix-141764, r=jieyouxu
Fix borrowck mentioning a name from an external macro we (deliberately) don't save Most of the info is already in the title 🤷 Closes #141764
2 parents d7fcb09 + 8b5b6d0 commit f3622ea

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -840,14 +840,22 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
840840
} else {
841841
bug!("not an upvar")
842842
};
843-
err.span_label(
844-
*span,
845-
format!(
846-
"calling `{}` requires mutable binding due to {}",
847-
self.describe_place(the_place_err).unwrap(),
848-
reason
849-
),
850-
);
843+
// sometimes we deliberately don't store the name of a place when coming from a macro in
844+
// another crate. We generally want to limit those diagnostics a little, to hide
845+
// implementation details (such as those from pin!() or format!()). In that case show a
846+
// slightly different error message, or none at all if something else happened. In other
847+
// cases the message is likely not useful.
848+
if let Some(place_name) = self.describe_place(the_place_err) {
849+
err.span_label(
850+
*span,
851+
format!("calling `{place_name}` requires mutable binding due to {reason}"),
852+
);
853+
} else if span.from_expansion() {
854+
err.span_label(
855+
*span,
856+
format!("a call in this macro requires a mutable binding due to {reason}",),
857+
);
858+
}
851859
}
852860
}
853861

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[macro_export]
2+
macro_rules! ice {
3+
() => {
4+
fn main() {
5+
let d = &mut 0;
6+
let c = || *d += 1;
7+
c();
8+
}
9+
};
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ aux-build: borrowck-error-in-macro.rs
2+
//@ error-pattern: a call in this macro requires a mutable binding due to mutable borrow of `d`
3+
//FIXME: remove error-pattern (see #141896)
4+
5+
extern crate borrowck_error_in_macro as a;
6+
7+
a::ice! {}
8+
//~^ ERROR cannot borrow value as mutable, as it is not declared as mutable
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0596]: cannot borrow value as mutable, as it is not declared as mutable
2+
--> $DIR/borrowck-error-in-macro.rs:7:1
3+
|
4+
LL | a::ice! {}
5+
| ^^^^^^^^^^
6+
| |
7+
| cannot borrow as mutable
8+
| a call in this macro requires a mutable binding due to mutable borrow of `d`
9+
|
10+
= note: this error originates in the macro `a::ice` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider changing this to be mutable
12+
--> $DIR/auxiliary/borrowck-error-in-macro.rs:6:17
13+
|
14+
LL | let mut c = || *d += 1;
15+
| +++
16+
17+
error: aborting due to 1 previous error
18+
19+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)