Skip to content

Commit

Permalink
Set of fixes to improve borrowcks that weren't updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Turner committed May 12, 2016
1 parent e37f859 commit f3054ce
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
37 changes: 28 additions & 9 deletions src/librustc_borrowck/borrowck/check_loans.rs
Expand Up @@ -663,23 +663,39 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
UseOk => { }
UseWhileBorrowed(loan_path, loan_span) => {
let mut err = match move_kind {
move_data::Captured =>
move_data::Captured => {
struct_span_err!(self.bccx, span, E0504,
"cannot move `{}` into closure because it is borrowed",
&self.bccx.loan_path_to_string(move_path)),
&self.bccx.loan_path_to_string(move_path))
.span_label(
loan_span,
&format!("borrow of `{}` occurs here",
&self.bccx.loan_path_to_string(&loan_path))
)
.span_label(
span,
&format!("move into closure occurs here")
)
}
move_data::Declared |
move_data::MoveExpr |
move_data::MovePat =>
move_data::MovePat => {
struct_span_err!(self.bccx, span, E0505,
"cannot move out of `{}` because it is borrowed",
&self.bccx.loan_path_to_string(move_path))
.span_label(
loan_span,
&format!("borrow of `{}` occurs here",
&self.bccx.loan_path_to_string(&loan_path))
)
.span_label(
span,
&format!("move out of `{}` occurs here",
&self.bccx.loan_path_to_string(move_path))
)
}
};

err.span_note(
loan_span,
&format!("borrow of `{}` occurs here",
&self.bccx.loan_path_to_string(&loan_path))
);
err.emit();
}
}
Expand Down Expand Up @@ -845,9 +861,12 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
struct_span_err!(self.bccx, span, E0506,
"cannot assign to `{}` because it is borrowed",
self.bccx.loan_path_to_string(loan_path))
.span_note(loan.span,
.span_label(loan.span,
&format!("borrow of `{}` occurs here",
self.bccx.loan_path_to_string(loan_path)))
.span_label(span,
&format!("assignment to `{}` occurs here",
self.bccx.loan_path_to_string(loan_path)))
.emit();
}
}
16 changes: 11 additions & 5 deletions src/librustc_borrowck/borrowck/gather_loans/move_error.rs
Expand Up @@ -72,7 +72,7 @@ fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
let mut err = report_cannot_move_out_of(bccx, error.move_from.clone());
let mut is_first_note = true;
for move_to in &error.move_to_places {
note_move_destination(&mut err, move_to.span,
err = note_move_destination(err, move_to.span,
move_to.name, is_first_note);
is_first_note = false;
}
Expand Down Expand Up @@ -124,6 +124,10 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
struct_span_err!(bccx, move_from.span, E0507,
"cannot move out of {}",
move_from.descriptive_string(bccx.tcx))
.span_label(
move_from.span,
&format!("move occurs here")
)
}

Categorization::Interior(ref b, mc::InteriorElement(Kind::Index, _)) => {
Expand Down Expand Up @@ -159,22 +163,24 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
}
}

fn note_move_destination(err: &mut DiagnosticBuilder,
fn note_move_destination(mut err: DiagnosticBuilder,
move_to_span: codemap::Span,
pat_name: ast::Name,
is_first_note: bool) {
is_first_note: bool) -> DiagnosticBuilder {
if is_first_note {
err.span_note(
err = err.span_label(
move_to_span,
"attempting to move value to here");
&format!("attempting to move value to here"));
err.help(
&format!("to prevent the move, \
use `ref {0}` or `ref mut {0}` to capture value by \
reference",
pat_name));
err
} else {
err.span_note(move_to_span,
&format!("and here (use `ref {0}` or `ref mut {0}`)",
pat_name));
err
}
}
9 changes: 7 additions & 2 deletions src/librustc_borrowck/borrowck/mod.rs
Expand Up @@ -620,11 +620,13 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
}

// General fallback.
let span = err.span.clone();
let mut db = self.struct_span_err(
err.span,
&self.bckerr_to_string(&err));
self.note_and_explain_bckerr(&mut db, err);
db.emit();
db.span_label(span, &format!("cannot borrow"))
.emit();
}

pub fn report_use_of_moved_value(&self,
Expand All @@ -647,7 +649,10 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
self.tcx.sess, use_span, E0381,
"{} of possibly uninitialized variable: `{}`",
verb,
self.loan_path_to_string(lp)).emit();
self.loan_path_to_string(lp))
.span_label(use_span, &format!("use of possibly uninitialized `{}`",
self.loan_path_to_string(lp)))
.emit();
return;
}
_ => {
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/errors/snippet/mod.rs
Expand Up @@ -612,6 +612,7 @@ impl FileInfo {
styled_buffer.set_style(0, p, Style::UnderlinePrimary);
} else {
styled_buffer.putc(1, p, '-', Style::UnderlineSecondary);
styled_buffer.set_style(0, p, Style::UnderlineSecondary);
}
}
}
Expand Down

0 comments on commit f3054ce

Please sign in to comment.