Skip to content

Commit ebd6c71

Browse files
committed
Dont show variables from desugarings in borrowck errors
1 parent 50a0def commit ebd6c71

File tree

9 files changed

+112
-32
lines changed

9 files changed

+112
-32
lines changed

src/librustc/mir/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,13 @@ impl<'tcx> LocalDecl<'tcx> {
915915
}
916916
}
917917

918+
/// Returns `true` is the local is from a compiler desugaring, e.g.,
919+
/// `__next` from a `for` loop.
920+
#[inline]
921+
pub fn from_compiler_desugaring(&self) -> bool {
922+
self.source_info.span.compiler_desugaring_kind().is_some()
923+
}
924+
918925
/// Creates a new `LocalDecl` for a temporary.
919926
#[inline]
920927
pub fn new_temp(ty: Ty<'tcx>, span: Span) -> Self {

src/librustc_mir/borrow_check/error_reporting.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1760,15 +1760,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17601760
}
17611761

17621762
/// Appends end-user visible description of the `local` place to `buf`. If `local` doesn't have
1763-
/// a name, then `Err` is returned
1763+
/// a name, or its name was generated by the compiler, then `Err` is returned
17641764
fn append_local_to_string(&self, local_index: Local, buf: &mut String) -> Result<(), ()> {
17651765
let local = &self.mir.local_decls[local_index];
17661766
match local.name {
1767-
Some(name) => {
1768-
buf.push_str(&name.to_string());
1767+
Some(name) if !local.from_compiler_desugaring() => {
1768+
buf.push_str(name.as_str().get());
17691769
Ok(())
17701770
}
1771-
None => Err(()),
1771+
_ => Err(()),
17721772
}
17731773
}
17741774

src/librustc_mir/borrow_check/mutability_errors.rs

+25-22
Original file line numberDiff line numberDiff line change
@@ -420,28 +420,31 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
420420
);
421421
}
422422

423-
if let Some(name) = local_decl.name {
424-
err.span_label(
425-
span,
426-
format!(
427-
"`{NAME}` is a `{SIGIL}` {DESC}, \
428-
so the data it refers to cannot be {ACTED_ON}",
429-
NAME = name,
430-
SIGIL = pointer_sigil,
431-
DESC = pointer_desc,
432-
ACTED_ON = acted_on
433-
),
434-
);
435-
} else {
436-
err.span_label(
437-
span,
438-
format!(
439-
"cannot {ACT} through `{SIGIL}` {DESC}",
440-
ACT = act,
441-
SIGIL = pointer_sigil,
442-
DESC = pointer_desc
443-
),
444-
);
423+
match local_decl.name {
424+
Some(name) if !local_decl.from_compiler_desugaring() => {
425+
err.span_label(
426+
span,
427+
format!(
428+
"`{NAME}` is a `{SIGIL}` {DESC}, \
429+
so the data it refers to cannot be {ACTED_ON}",
430+
NAME = name,
431+
SIGIL = pointer_sigil,
432+
DESC = pointer_desc,
433+
ACTED_ON = acted_on
434+
),
435+
);
436+
}
437+
_ => {
438+
err.span_label(
439+
span,
440+
format!(
441+
"cannot {ACT} through `{SIGIL}` {DESC}",
442+
ACT = act,
443+
SIGIL = pointer_sigil,
444+
DESC = pointer_desc
445+
),
446+
);
447+
}
445448
}
446449
}
447450

src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl BorrowExplanation {
112112
};
113113

114114
match local_decl.name {
115-
Some(local_name) => {
115+
Some(local_name) if !local_decl.from_compiler_desugaring() => {
116116
let message = format!(
117117
"{B}borrow might be used here, when `{LOC}` is dropped \
118118
and runs the {DTOR} for {TYPE}",
@@ -130,7 +130,7 @@ impl BorrowExplanation {
130130
);
131131
}
132132
}
133-
None => {
133+
_ => {
134134
err.span_label(
135135
local_decl.source_info.span,
136136
format!(

src/libsyntax/parse/parser.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -8423,6 +8423,8 @@ impl<'a> Parser<'a> {
84238423
for (index, input) in decl.inputs.iter_mut().enumerate() {
84248424
let id = ast::DUMMY_NODE_ID;
84258425
let span = input.pat.span;
8426+
let desugared_span = self.sess.source_map()
8427+
.mark_span_with_reason(CompilerDesugaringKind::Async, span, None);
84268428

84278429
// Construct a name for our temporary argument.
84288430
let name = format!("__arg{}", index);
@@ -8439,8 +8441,7 @@ impl<'a> Parser<'a> {
84398441
// this would affect the input to procedural macros, but they can have
84408442
// their span marked as being the result of a compiler desugaring so
84418443
// that they aren't linted against.
8442-
input.pat.span = self.sess.source_map().mark_span_with_reason(
8443-
CompilerDesugaringKind::Async, span, None);
8444+
input.pat.span = desugared_span;
84448445

84458446
(binding_mode, ident, true)
84468447
}
@@ -8460,7 +8461,7 @@ impl<'a> Parser<'a> {
84608461
node: PatKind::Ident(
84618462
BindingMode::ByValue(Mutability::Immutable), ident, None,
84628463
),
8463-
span,
8464+
span: desugared_span,
84648465
}),
84658466
source: ArgSource::AsyncFn(input.pat.clone()),
84668467
})
@@ -8473,7 +8474,7 @@ impl<'a> Parser<'a> {
84738474
pat: P(Pat {
84748475
id,
84758476
node: PatKind::Ident(binding_mode, ident, None),
8476-
span,
8477+
span: desugared_span,
84778478
}),
84788479
// We explicitly do not specify the type for this statement. When the user's
84798480
// argument type is `impl Trait` then this would require the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Test that we don't show variables with from async fn desugaring
2+
3+
// edition:2018
4+
#![feature(async_await)]
5+
6+
async fn async_fn(&ref mut s: &[i32]) {}
7+
//~^ ERROR cannot borrow data in a `&` reference as mutable [E0596]
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0596]: cannot borrow data in a `&` reference as mutable
2+
--> $DIR/dont-print-desugared-async.rs:6:20
3+
|
4+
LL | async fn async_fn(&ref mut s: &[i32]) {}
5+
| -^^^^^^^^^
6+
| ||
7+
| |cannot borrow as mutable through `&` reference
8+
| help: consider changing this to be a mutable reference: `&mut ref mut s`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0596`.
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Test that we don't show variables with from for loop desugaring
2+
3+
fn for_loop(s: &[i32]) {
4+
for &ref mut x in s {}
5+
//~^ ERROR cannot borrow data in a `&` reference as mutable [E0596]
6+
}
7+
8+
struct D<'a>(&'a ());
9+
10+
impl Drop for D<'_> {
11+
fn drop(&mut self) {}
12+
}
13+
14+
fn for_loop_dropck(v: Vec<D<'static>>) {
15+
for ref mut d in v {
16+
let y = ();
17+
*d = D(&y); //~ ERROR `y` does not live long enough
18+
}
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0596]: cannot borrow data in a `&` reference as mutable
2+
--> $DIR/dont-print-desugared.rs:4:10
3+
|
4+
LL | for &ref mut x in s {}
5+
| -^^^^^^^^^
6+
| ||
7+
| |cannot borrow as mutable through `&` reference
8+
| help: consider changing this to be a mutable reference: `&mut ref mut x`
9+
10+
error[E0597]: `y` does not live long enough
11+
--> $DIR/dont-print-desugared.rs:17:16
12+
|
13+
LL | for ref mut d in v {
14+
| - a temporary with access to the borrow is created here ...
15+
LL | let y = ();
16+
LL | *d = D(&y);
17+
| ^^ borrowed value does not live long enough
18+
LL | }
19+
| -
20+
| |
21+
| `y` dropped here while still borrowed
22+
| ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
23+
24+
error: aborting due to 2 previous errors
25+
26+
Some errors have detailed explanations: E0596, E0597.
27+
For more information about an error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)