Skip to content

Commit 0ec2e8f

Browse files
committed
Tweak wording for borrows within macro invocations
1 parent 4cf26f8 commit 0ec2e8f

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

src/librustc_borrowck/borrowck/mod.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use std::fmt;
4646
use std::rc::Rc;
4747
use std::hash::{Hash, Hasher};
4848
use syntax::ast;
49-
use syntax_pos::{MultiSpan, Span};
49+
use syntax_pos::{MultiSpan, Span, SyntaxContext};
5050
use errors::{DiagnosticBuilder, DiagnosticId};
5151

5252
use rustc::hir;
@@ -723,18 +723,17 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
723723

724724
// Annotate the use and the move in the span. Watch out for
725725
// the case where the use and the move are the same. This
726-
// means the use is in a loop.
727-
err = if use_span == move_span {
728-
err.span_label(
729-
use_span,
730-
format!("value moved{} here in previous iteration of loop",
731-
move_note));
732-
err
726+
// means the use is in a loop or within the same macro invocation.
727+
if use_span == move_span && use_span.ctxt() != SyntaxContext::empty() {
728+
err.span_label(use_span, format!("value moved{} here in previous iteration of loop",
729+
move_note));
730+
} else if use_span == move_span {
731+
err.span_label(use_span, format!("value moved{} inside this macro invocation",
732+
move_note));
733733
} else {
734734
err.span_label(use_span, format!("value {} here after move", verb_participle));
735735
err.span_label(move_span, format!("value moved{} here", move_note));
736-
err
737-
};
736+
}
738737

739738
if need_note {
740739
err.note(&format!(

src/librustc_mir/borrow_check/error_reporting.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use syntax_pos::Span;
11+
use syntax_pos::{Span, SyntaxContext};
1212
use rustc::middle::region::ScopeTree;
1313
use rustc::mir::{BorrowKind, Field, Local, LocalKind, Location, Operand};
1414
use rustc::mir::{Place, ProjectionElem, Rvalue, Statement, StatementKind};
@@ -66,11 +66,14 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
6666
for moi in mois {
6767
let move_msg = ""; //FIXME: add " (into closure)"
6868
let move_span = self.mir.source_info(self.move_data.moves[*moi].source).span;
69-
if span == move_span {
70-
err.span_label(
71-
span,
72-
format!("value moved{} here in previous iteration of loop", move_msg),
73-
);
69+
if span == move_span && span.ctxt() != SyntaxContext::empty() {
70+
err.span_label(span, format!("value moved{} inside this macro invocation",
71+
move_msg));
72+
is_loop_move = true;
73+
} else if span == move_span {
74+
err.span_label(span,
75+
format!("value moved{} here in previous iteration of loop",
76+
move_msg));
7477
is_loop_move = true;
7578
} else {
7679
err.span_label(move_span, format!("value moved{} here", move_msg));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z borrowck=compare
12+
13+
macro_rules! test {
14+
($v:expr) => {{
15+
drop(&$v);
16+
$v
17+
}}
18+
}
19+
20+
fn main() {
21+
let b = Box::new(true);
22+
test!({b});
23+
//~^ ERROR use of moved value: `b` (Ast)
24+
//~^^ ERROR use of moved value: `b` (Mir)
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error[E0382]: use of moved value: `b`
2+
--> $DIR/moves-inside-of-macro.rs:20:12
3+
|
4+
20 | test!({b});
5+
| ^ value moved inside this macro invocation
6+
|
7+
= note: move occurs because `b` has type `std::boxed::Box<bool>`, which does not implement the `Copy` trait
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)