Skip to content

Commit 2e527f0

Browse files
committed
fix bug where borrowck tries to describe a name from a macro in another crate
1 parent a5f7d44 commit 2e527f0

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//@ 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`
23

34
extern crate borrowck_error_in_macro as a;
45

56
a::ice! {}
7+
//~^ 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:6: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)