mirror of
https://github.com/allankoechke/rust.git
synced 2026-12-05 01:24:03 +00:00
dont suggest changing the mutability of a borrow inside a macro
the suggestion rewrote the borrow where it was written. for a borrow coming from a macro body that is the macro definition rather than the call site, and for a macro from another crate a file the user cannot edit. it was machine applicable so rustfix would apply it and a macro invoked at more than one call site could stop compiling. gate it on can_be_used_for_suggestions so those cases fall through to the existing note that the trait is implemented for the mutable borrow but not the shared one.
This commit is contained in:
@@ -2530,7 +2530,14 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
|
||||
.sess
|
||||
.source_map()
|
||||
.span_take_while(span, |c| c.is_whitespace() || *c == '&');
|
||||
if points_at_arg && mutability.is_not() && refs_number > 0 {
|
||||
if points_at_arg
|
||||
&& mutability.is_not()
|
||||
&& refs_number > 0
|
||||
// The borrow can sit in a macro body, where rewriting it would edit the
|
||||
// macro definition and so every one of its call sites, or a crate the user
|
||||
// does not own. Fall through to the note in that case.
|
||||
&& span.can_be_used_for_suggestions()
|
||||
{
|
||||
// If we have a call like foo(&mut buf), then don't suggest foo(&mut mut buf)
|
||||
if snippet
|
||||
.trim_start_matches(|c: char| c.is_whitespace() || c == '&')
|
||||
|
||||
6
tests/ui/suggestions/auxiliary/mut_borrow_macro.rs
Normal file
6
tests/ui/suggestions/auxiliary/mut_borrow_macro.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
#[macro_export]
|
||||
macro_rules! call_it {
|
||||
($f:expr) => {
|
||||
(0..100).for_each(&$f)
|
||||
};
|
||||
}
|
||||
26
tests/ui/suggestions/change-mut-borrow-in-macro.rs
Normal file
26
tests/ui/suggestions/change-mut-borrow-in-macro.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
//@ edition: 2021
|
||||
//@ aux-build: mut_borrow_macro.rs
|
||||
|
||||
// The borrow that needs to become `&mut` lives in a macro body, so rewriting it would edit the
|
||||
// macro definition rather than the call site, and for an external macro a file the user does not
|
||||
// own.
|
||||
|
||||
extern crate mut_borrow_macro;
|
||||
|
||||
macro_rules! local_call_it {
|
||||
($f:expr) => {
|
||||
(0..100).for_each(&$f)
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut value = 0;
|
||||
let mut func = |increment: usize| value += increment;
|
||||
//~^ ERROR expected a closure that implements the `Fn` trait
|
||||
local_call_it!(func);
|
||||
|
||||
let mut other = 0;
|
||||
let mut other_func = |increment: usize| other += increment;
|
||||
//~^ ERROR expected a closure that implements the `Fn` trait
|
||||
mut_borrow_macro::call_it!(other_func);
|
||||
}
|
||||
40
tests/ui/suggestions/change-mut-borrow-in-macro.stderr
Normal file
40
tests/ui/suggestions/change-mut-borrow-in-macro.stderr
Normal file
@@ -0,0 +1,40 @@
|
||||
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut`
|
||||
--> $DIR/change-mut-borrow-in-macro.rs:18:20
|
||||
|
|
||||
LL | (0..100).for_each(&$f)
|
||||
| -------- --- the requirement to implement `Fn` derives from here
|
||||
| |
|
||||
| required by a bound introduced by this call
|
||||
...
|
||||
LL | let mut func = |increment: usize| value += increment;
|
||||
| ^^^^^^^^^^^^^^^^^^ ----- closure is `FnMut` because it mutates the variable `value` here
|
||||
| |
|
||||
| this closure implements `FnMut`, not `Fn`
|
||||
|
|
||||
= note: `FnMut(usize)` is implemented for `&mut {closure@$DIR/change-mut-borrow-in-macro.rs:18:20: 18:38}`, but not for `&{closure@$DIR/change-mut-borrow-in-macro.rs:18:20: 18:38}`
|
||||
= note: required for `&{closure@$DIR/change-mut-borrow-in-macro.rs:18:20: 18:38}` to implement `FnMut(usize)`
|
||||
note: required by a bound in `for_each`
|
||||
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
|
||||
|
||||
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut`
|
||||
--> $DIR/change-mut-borrow-in-macro.rs:23:26
|
||||
|
|
||||
LL | let mut other_func = |increment: usize| other += increment;
|
||||
| ^^^^^^^^^^^^^^^^^^ ----- closure is `FnMut` because it mutates the variable `other` here
|
||||
| |
|
||||
| this closure implements `FnMut`, not `Fn`
|
||||
LL |
|
||||
LL | mut_borrow_macro::call_it!(other_func);
|
||||
| --------------------------------------
|
||||
| |
|
||||
| the requirement to implement `Fn` derives from here
|
||||
| required by a bound introduced by this call
|
||||
|
|
||||
= note: `FnMut(usize)` is implemented for `&mut {closure@$DIR/change-mut-borrow-in-macro.rs:23:26: 23:44}`, but not for `&{closure@$DIR/change-mut-borrow-in-macro.rs:23:26: 23:44}`
|
||||
= note: required for `&{closure@$DIR/change-mut-borrow-in-macro.rs:23:26: 23:44}` to implement `FnMut(usize)`
|
||||
note: required by a bound in `for_each`
|
||||
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0525`.
|
||||
Reference in New Issue
Block a user