Skip to content

Tweak wording for borrows within macro invocations #47768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use std::fmt;
use std::rc::Rc;
use std::hash::{Hash, Hasher};
use syntax::ast;
use syntax_pos::{MultiSpan, Span};
use syntax_pos::{MultiSpan, Span, SyntaxContext};
use errors::{DiagnosticBuilder, DiagnosticId};

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

// Annotate the use and the move in the span. Watch out for
// the case where the use and the move are the same. This
// means the use is in a loop.
err = if use_span == move_span {
err.span_label(
use_span,
format!("value moved{} here in previous iteration of loop",
move_note));
err
// means the use is in a loop or within the same macro invocation.
if use_span == move_span && use_span.ctxt() != SyntaxContext::empty() {
err.span_label(use_span, format!("value moved{} here in previous iteration of loop",
move_note));
} else if use_span == move_span {
err.span_label(use_span, format!("value moved{} inside this macro invocation",
move_note));
} else {
err.span_label(use_span, format!("value {} here after move", verb_participle));
err.span_label(move_span, format!("value moved{} here", move_note));
err
};
}

if need_note {
err.note(&format!(
Expand Down
15 changes: 9 additions & 6 deletions src/librustc_mir/borrow_check/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use syntax_pos::Span;
use syntax_pos::{Span, SyntaxContext};
use rustc::middle::region::ScopeTree;
use rustc::mir::{BorrowKind, Field, Local, LocalKind, Location, Operand};
use rustc::mir::{Place, ProjectionElem, Rvalue, Statement, StatementKind};
Expand Down Expand Up @@ -66,11 +66,14 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
for moi in mois {
let move_msg = ""; //FIXME: add " (into closure)"
let move_span = self.mir.source_info(self.move_data.moves[*moi].source).span;
if span == move_span {
err.span_label(
span,
format!("value moved{} here in previous iteration of loop", move_msg),
);
if span == move_span && span.ctxt() != SyntaxContext::empty() {
err.span_label(span, format!("value moved{} inside this macro invocation",
move_msg));
is_loop_move = true;
} else if span == move_span {
err.span_label(span,
format!("value moved{} here in previous iteration of loop",
move_msg));
is_loop_move = true;
} else {
err.span_label(move_span, format!("value moved{} here", move_msg));
Expand Down
25 changes: 25 additions & 0 deletions src/test/ui/borrowck/moves-inside-of-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -Z borrowck=compare

macro_rules! test {
($v:expr) => {{
drop(&$v);
$v
}}
}

fn main() {
let b = Box::new(true);
test!({b});
//~^ ERROR use of moved value: `b` (Ast)
//~^^ ERROR use of moved value: `b` (Mir)
}
10 changes: 10 additions & 0 deletions src/test/ui/borrowck/moves-inside-of-macro.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error[E0382]: use of moved value: `b`
--> $DIR/moves-inside-of-macro.rs:20:12
|
20 | test!({b});
| ^ value moved inside this macro invocation
|
= note: move occurs because `b` has type `std::boxed::Box<bool>`, which does not implement the `Copy` trait

error: aborting due to previous error