Skip to content

Batch of improvements to errors for new error format #33619

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

Merged
merged 6 commits into from
May 16, 2016
Merged
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
4 changes: 2 additions & 2 deletions src/librustc/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
trace.origin);

if !is_simple_error {
err = err.note_expected_found(&"type", &expected, &found);
err.note_expected_found(&"type", &expected, &found);
}

err = err.span_label(trace.origin.span(), &terr);
err.span_label(trace.origin.span(), &terr);

self.check_and_note_conflicting_crates(&mut err, terr, trace.origin.span());

Expand Down
160 changes: 93 additions & 67 deletions src/librustc_borrowck/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,99 +475,104 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {

let mut err = match (new_loan.kind, old_loan.kind) {
(ty::MutBorrow, ty::MutBorrow) => {
struct_span_err!(self.bccx, new_loan.span, E0499,
"cannot borrow `{}`{} as mutable \
more than once at a time",
nl, new_loan_msg)
.span_label(
let mut err = struct_span_err!(self.bccx, new_loan.span, E0499,
"cannot borrow `{}`{} as mutable \
more than once at a time",
nl, new_loan_msg);
err.span_label(
old_loan.span,
&format!("first mutable borrow occurs here{}", old_loan_msg))
.span_label(
&format!("first mutable borrow occurs here{}", old_loan_msg));
err.span_label(
new_loan.span,
&format!("second mutable borrow occurs here{}", new_loan_msg))
.span_label(
&format!("second mutable borrow occurs here{}", new_loan_msg));
err.span_label(
previous_end_span,
&format!("first borrow ends here"))
&format!("first borrow ends here"));
err
}

(ty::UniqueImmBorrow, ty::UniqueImmBorrow) => {
struct_span_err!(self.bccx, new_loan.span, E0524,
let mut err = struct_span_err!(self.bccx, new_loan.span, E0524,
"two closures require unique access to `{}` \
at the same time",
nl)
.span_label(
nl);
err.span_label(
old_loan.span,
&format!("first closure is constructed here"))
.span_label(
&format!("first closure is constructed here"));
err.span_label(
new_loan.span,
&format!("second closure is constructed here"))
.span_label(
&format!("second closure is constructed here"));
err.span_label(
previous_end_span,
&format!("borrow from first closure ends here"))
&format!("borrow from first closure ends here"));
err
}

(ty::UniqueImmBorrow, _) => {
struct_span_err!(self.bccx, new_loan.span, E0500,
"closure requires unique access to `{}` \
but {} is already borrowed{}",
nl, ol_pronoun, old_loan_msg)
.span_label(
let mut err = struct_span_err!(self.bccx, new_loan.span, E0500,
"closure requires unique access to `{}` \
but {} is already borrowed{}",
nl, ol_pronoun, old_loan_msg);
err.span_label(
new_loan.span,
&format!("closure construction occurs here{}", new_loan_msg))
.span_label(
&format!("closure construction occurs here{}", new_loan_msg));
err.span_label(
old_loan.span,
&format!("borrow occurs here{}", old_loan_msg))
.span_label(
&format!("borrow occurs here{}", old_loan_msg));
err.span_label(
previous_end_span,
&format!("borrow ends here"))
&format!("borrow ends here"));
err
}

(_, ty::UniqueImmBorrow) => {
struct_span_err!(self.bccx, new_loan.span, E0501,
"cannot borrow `{}`{} as {} because \
previous closure requires unique access",
nl, new_loan_msg, new_loan.kind.to_user_str())
.span_label(
let mut err = struct_span_err!(self.bccx, new_loan.span, E0501,
"cannot borrow `{}`{} as {} because \
previous closure requires unique access",
nl, new_loan_msg, new_loan.kind.to_user_str());
err.span_label(
new_loan.span,
&format!("borrow occurs here{}", new_loan_msg))
.span_label(
&format!("borrow occurs here{}", new_loan_msg));
err.span_label(
old_loan.span,
&format!("closure construction occurs here{}", old_loan_msg))
.span_label(
&format!("closure construction occurs here{}", old_loan_msg));
err.span_label(
previous_end_span,
&format!("borrow from closure ends here"))
&format!("borrow from closure ends here"));
err
}

(_, _) => {
struct_span_err!(self.bccx, new_loan.span, E0502,
"cannot borrow `{}`{} as {} because \
{} is also borrowed as {}{}",
nl,
new_loan_msg,
new_loan.kind.to_user_str(),
ol_pronoun,
old_loan.kind.to_user_str(),
old_loan_msg)
.span_label(
let mut err = struct_span_err!(self.bccx, new_loan.span, E0502,
"cannot borrow `{}`{} as {} because \
{} is also borrowed as {}{}",
nl,
new_loan_msg,
new_loan.kind.to_user_str(),
ol_pronoun,
old_loan.kind.to_user_str(),
old_loan_msg);
err.span_label(
new_loan.span,
&format!("{} borrow occurs here{}",
new_loan.kind.to_user_str(),
new_loan_msg))
.span_label(
new_loan_msg));
err.span_label(
old_loan.span,
&format!("{} borrow occurs here{}",
old_loan.kind.to_user_str(),
old_loan_msg))
.span_label(
old_loan_msg));
err.span_label(
previous_end_span,
&format!("{} borrow ends here",
old_loan.kind.to_user_str()))
old_loan.kind.to_user_str()));
err
}
};

match new_loan.cause {
euv::ClosureCapture(span) => {
err = err.span_label(
err.span_label(
span,
&format!("borrow occurs due to use of `{}` in closure", nl));
}
Expand All @@ -576,7 +581,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {

match old_loan.cause {
euv::ClosureCapture(span) => {
err = err.span_label(
err.span_label(
span,
&format!("previous borrow occurs due to use of `{}` in closure",
ol));
Expand Down Expand Up @@ -663,23 +668,41 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
UseOk => { }
UseWhileBorrowed(loan_path, loan_span) => {
let mut err = match move_kind {
move_data::Captured =>
struct_span_err!(self.bccx, span, E0504,
move_data::Captured => {
let mut err = 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));
err.span_label(
loan_span,
&format!("borrow of `{}` occurs here",
&self.bccx.loan_path_to_string(&loan_path))
);
err.span_label(
span,
&format!("move into closure occurs here")
);
err
}
move_data::Declared |
move_data::MoveExpr |
move_data::MovePat =>
struct_span_err!(self.bccx, span, E0505,
move_data::MovePat => {
let mut err = struct_span_err!(self.bccx, span, E0505,
"cannot move out of `{}` because it is borrowed",
&self.bccx.loan_path_to_string(move_path))
&self.bccx.loan_path_to_string(move_path));
err.span_label(
loan_span,
&format!("borrow of `{}` occurs here",
&self.bccx.loan_path_to_string(&loan_path))
);
err.span_label(
span,
&format!("move out of `{}` occurs here",
&self.bccx.loan_path_to_string(move_path))
);
err
}
};

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 +868,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();
}
}
41 changes: 26 additions & 15 deletions src/librustc_borrowck/borrowck/gather_loans/move_error.rs
Original file line number Diff line number Diff line change
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 @@ -121,18 +121,25 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
Categorization::Deref(_, _, mc::Implicit(..)) |
Categorization::Deref(_, _, mc::UnsafePtr(..)) |
Categorization::StaticItem => {
struct_span_err!(bccx, move_from.span, E0507,
let mut err = struct_span_err!(bccx, move_from.span, E0507,
"cannot move out of {}",
move_from.descriptive_string(bccx.tcx))
move_from.descriptive_string(bccx.tcx));
err.span_label(
move_from.span,
&format!("move occurs here")
);
err
}

Categorization::Interior(ref b, mc::InteriorElement(Kind::Index, _)) => {
let expr = bccx.tcx.map.expect_expr(move_from.id);
if let hir::ExprIndex(..) = expr.node {
struct_span_err!(bccx, move_from.span, E0508,
"cannot move out of type `{}`, \
a non-copy fixed-size array",
b.ty)
let mut err = struct_span_err!(bccx, move_from.span, E0508,
"cannot move out of type `{}`, \
a non-copy fixed-size array",
b.ty);
err.span_label(move_from.span, &format!("can not move out of here"));
err
} else {
span_bug!(move_from.span, "this path should not cause illegal move");
}
Expand All @@ -143,10 +150,12 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
match b.ty.sty {
ty::TyStruct(def, _) |
ty::TyEnum(def, _) if def.has_dtor() => {
struct_span_err!(bccx, move_from.span, E0509,
"cannot move out of type `{}`, \
which defines the `Drop` trait",
b.ty)
let mut err = struct_span_err!(bccx, move_from.span, E0509,
"cannot move out of type `{}`, \
which defines the `Drop` trait",
b.ty);
err.span_label(move_from.span, &format!("can not move out of here"));
err
},
_ => {
span_bug!(move_from.span, "this path should not cause illegal move");
Expand All @@ -159,22 +168,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.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
}
}
Loading