Skip to content

Commit cf11ef4

Browse files
committed
Fixed client code for diagnostics migration, adding new methods to trait BorrowckErrors as necessary.
1 parent 52cb6fc commit cf11ef4

File tree

4 files changed

+138
-54
lines changed

4 files changed

+138
-54
lines changed

src/librustc_borrowck/borrowck/mod.rs

+19-52
Original file line numberDiff line numberDiff line change
@@ -614,11 +614,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
614614
let partial = moved_lp.depth() > lp.depth();
615615
let msg = if !has_fork && partial { "partially " }
616616
else if has_fork && !has_common { "collaterally "}
617-
else { "" };
618-
let mut err = struct_span_err!(
619-
self.tcx.sess, use_span, E0382,
620-
"{} of {}moved value: `{}`",
621-
verb, msg, nl);
617+
else { "" };
618+
let mut err = self.cannot_act_on_moved_value(use_span,
619+
verb,
620+
msg,
621+
&format!("{}", nl));
622622
let need_note = match lp.ty.sty {
623623
ty::TypeVariants::TyClosure(id, _) => {
624624
let node_id = self.tcx.hir.as_local_node_id(id).unwrap();
@@ -698,10 +698,8 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
698698
&self,
699699
span: Span,
700700
lp: &LoanPath<'tcx>) {
701-
span_err!(
702-
self.tcx.sess, span, E0383,
703-
"partial reinitialization of uninitialized structure `{}`",
704-
self.loan_path_to_string(lp));
701+
self.cannot_partially_reinit_an_uninit_struct(span, &self.loan_path_to_string(lp))
702+
.emit();
705703
}
706704

707705
pub fn report_reassigned_immutable_variable(&self,
@@ -762,8 +760,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
762760
self.cannot_assign(error_span, &descr, Origin::Ast)
763761
}
764762
BorrowViolation(euv::ClosureCapture(_)) => {
765-
struct_span_err!(self.tcx.sess, error_span, E0595,
766-
"closure cannot assign to {}", descr)
763+
self.closure_cannot_assign_to_borrowed(error_span, &descr)
767764
}
768765
BorrowViolation(euv::OverloadedOperator) |
769766
BorrowViolation(euv::AddrOf) |
@@ -772,8 +769,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
772769
BorrowViolation(euv::AutoUnsafe) |
773770
BorrowViolation(euv::ForLoop) |
774771
BorrowViolation(euv::MatchDiscriminant) => {
775-
struct_span_err!(self.tcx.sess, error_span, E0596,
776-
"cannot borrow {} as mutable", descr)
772+
self.cannot_borrow_path_as_mutable(error_span, &descr)
777773
}
778774
BorrowViolation(euv::ClosureInvocation) => {
779775
span_bug!(err.span,
@@ -855,21 +851,12 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
855851

856852
if let Some((yield_span, _)) = maybe_borrow_across_yield {
857853
debug!("err_out_of_scope: opt_yield_span = {:?}", yield_span);
858-
struct_span_err!(self.tcx.sess,
859-
error_span,
860-
E0626,
861-
"borrow may still be in use when generator yields")
862-
.span_label(yield_span, "possible yield occurs here")
854+
self.cannot_borrow_across_generator_yield(error_span, yield_span)
863855
.emit();
864856
return;
865857
}
866858

867-
let mut db = struct_span_err!(self.tcx.sess,
868-
error_span,
869-
E0597,
870-
"{} does not live long enough",
871-
msg);
872-
859+
let mut db = self.path_does_not_live_long_enough(error_span, &msg);
873860
let (value_kind, value_msg) = match err.cmt.cat {
874861
mc::Categorization::Rvalue(..) =>
875862
("temporary value", "temporary value created here"),
@@ -978,11 +965,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
978965
}
979966
err_borrowed_pointer_too_short(loan_scope, ptr_scope) => {
980967
let descr = self.cmt_to_path_or_string(&err.cmt);
981-
let mut db = struct_span_err!(self.tcx.sess, error_span, E0598,
982-
"lifetime of {} is too short to guarantee \
983-
its contents can be safely reborrowed",
984-
descr);
985-
968+
let mut db = self.lifetime_too_short_for_reborrow(error_span, &descr);
986969
let descr = match opt_loan_path(&err.cmt) {
987970
Some(lp) => {
988971
format!("`{}`", self.loan_path_to_string(&lp))
@@ -1054,12 +1037,8 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10541037
let blame = cmt.immutability_blame();
10551038
let mut err = match blame {
10561039
Some(ImmutabilityBlame::ClosureEnv(id)) => {
1057-
let mut err = struct_span_err!(
1058-
self.tcx.sess, span, E0387,
1059-
"{} in a captured outer variable in an `Fn` closure", prefix);
1060-
10611040
// FIXME: the distinction between these 2 messages looks wrong.
1062-
let help = if let BorrowViolation(euv::ClosureCapture(_)) = kind {
1041+
let help_msg = if let BorrowViolation(euv::ClosureCapture(_)) = kind {
10631042
// The aliasability violation with closure captures can
10641043
// happen for nested closures, so we know the enclosing
10651044
// closure incorrectly accepts an `Fn` while it needs to
@@ -1070,15 +1049,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10701049
"consider changing this closure to take self by mutable reference"
10711050
};
10721051
let node_id = self.tcx.hir.def_index_to_node_id(id);
1073-
err.span_help(self.tcx.hir.span(node_id), help);
1074-
err
1052+
let help_span = self.tcx.hir.span(node_id);
1053+
self.cannot_act_on_capture_in_sharable_fn(span, prefix, (help_span, help_msg))
10751054
}
10761055
_ => {
1077-
let mut err = struct_span_err!(
1078-
self.tcx.sess, span, E0389,
1079-
"{} in a `&` reference", prefix);
1080-
err.span_label(span, "assignment into an immutable reference");
1081-
err
1056+
self.cannot_assign_into_immutable_reference(span, prefix)
10821057
}
10831058
};
10841059
self.note_immutability_blame(&mut err, blame);
@@ -1230,17 +1205,9 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
12301205
Err(_) => format!("move |<args>| <body>")
12311206
};
12321207

1233-
struct_span_err!(self.tcx.sess, err.span, E0373,
1234-
"closure may outlive the current function, \
1235-
but it borrows {}, \
1236-
which is owned by the current function",
1237-
cmt_path_or_string)
1238-
.span_label(capture_span,
1239-
format!("{} is borrowed here",
1240-
cmt_path_or_string))
1241-
.span_label(err.span,
1242-
format!("may outlive borrowed value {}",
1243-
cmt_path_or_string))
1208+
self.cannot_capture_in_long_lived_closure(err.span,
1209+
&cmt_path_or_string,
1210+
capture_span)
12441211
.span_suggestion(err.span,
12451212
&format!("to force the closure to take ownership of {} \
12461213
(and any other referenced variables), \

src/librustc_borrowck/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#![feature(quote)]
1919

2020
#[macro_use] extern crate log;
21-
#[macro_use] extern crate syntax;
21+
extern crate syntax;
2222
extern crate syntax_pos;
2323
extern crate rustc_errors as errors;
2424

src/librustc_driver/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,6 @@ pub fn diagnostics_registry() -> errors::registry::Registry {
12591259
let mut all_errors = Vec::new();
12601260
all_errors.extend_from_slice(&rustc::DIAGNOSTICS);
12611261
all_errors.extend_from_slice(&rustc_typeck::DIAGNOSTICS);
1262-
all_errors.extend_from_slice(&rustc_borrowck::DIAGNOSTICS);
12631262
all_errors.extend_from_slice(&rustc_resolve::DIAGNOSTICS);
12641263
all_errors.extend_from_slice(&rustc_privacy::DIAGNOSTICS);
12651264
#[cfg(feature="llvm")]

src/librustc_mir/util/borrowck_errors.rs

+118
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,124 @@ pub trait BorrowckErrors {
294294
err.span_label(move_from_span, "cannot move out of here");
295295
err
296296
}
297+
298+
fn cannot_act_on_moved_value(&self,
299+
use_span: Span,
300+
verb: &str,
301+
optional_adverb_for_moved: &str,
302+
moved_path: &str)
303+
-> DiagnosticBuilder
304+
{
305+
let err = struct_span_err!(self, use_span, E0382,
306+
"{} of {}moved value: `{}`",
307+
verb, optional_adverb_for_moved, moved_path);
308+
err
309+
}
310+
311+
fn cannot_partially_reinit_an_uninit_struct(&self,
312+
span: Span,
313+
uninit_path: &str)
314+
-> DiagnosticBuilder
315+
{
316+
let err = struct_span_err!(self,
317+
span,
318+
E0383,
319+
"partial reinitialization of uninitialized structure `{}`",
320+
uninit_path);
321+
err
322+
}
323+
324+
fn closure_cannot_assign_to_borrowed(&self,
325+
span: Span,
326+
descr: &str)
327+
-> DiagnosticBuilder
328+
{
329+
let err = struct_span_err!(self, span, E0595, "closure cannot assign to {}", descr);
330+
err
331+
}
332+
333+
fn cannot_borrow_path_as_mutable(&self,
334+
span: Span,
335+
path: &str)
336+
-> DiagnosticBuilder
337+
{
338+
let err = struct_span_err!(self, span, E0596, "cannot borrow {} as mutable", path);
339+
err
340+
}
341+
342+
fn cannot_borrow_across_generator_yield(&self,
343+
span: Span,
344+
yield_span: Span)
345+
-> DiagnosticBuilder
346+
{
347+
let mut err = struct_span_err!(self,
348+
span,
349+
E0626,
350+
"borrow may still be in use when generator yields");
351+
err.span_label(yield_span, "possible yield occurs here");
352+
err
353+
}
354+
355+
fn path_does_not_live_long_enough(&self,
356+
span: Span,
357+
path: &str)
358+
-> DiagnosticBuilder
359+
{
360+
let err = struct_span_err!(self, span, E0597, "{} does not live long enough", path);
361+
err
362+
}
363+
364+
fn lifetime_too_short_for_reborrow(&self,
365+
span: Span,
366+
path: &str)
367+
-> DiagnosticBuilder
368+
{
369+
let err = struct_span_err!(self, span, E0598,
370+
"lifetime of {} is too short to guarantee \
371+
its contents can be safely reborrowed",
372+
path);
373+
err
374+
}
375+
376+
fn cannot_act_on_capture_in_sharable_fn(&self,
377+
span: Span,
378+
bad_thing: &str,
379+
help: (Span, &str))
380+
-> DiagnosticBuilder
381+
{
382+
let (help_span, help_msg) = help;
383+
let mut err = struct_span_err!(self, span, E0387,
384+
"{} in a captured outer variable in an `Fn` closure",
385+
bad_thing);
386+
err.span_help(help_span, help_msg);
387+
err
388+
}
389+
390+
fn cannot_assign_into_immutable_reference(&self,
391+
span: Span,
392+
bad_thing: &str)
393+
-> DiagnosticBuilder
394+
{
395+
let mut err = struct_span_err!(self, span, E0389, "{} in a `&` reference", bad_thing);
396+
err.span_label(span, "assignment into an immutable reference");
397+
err
398+
}
399+
400+
fn cannot_capture_in_long_lived_closure(&self,
401+
closure_span: Span,
402+
borrowed_path: &str,
403+
capture_span: Span)
404+
-> DiagnosticBuilder
405+
{
406+
let mut err = struct_span_err!(self, closure_span, E0373,
407+
"closure may outlive the current function, \
408+
but it borrows {}, \
409+
which is owned by the current function",
410+
borrowed_path);
411+
err.span_label(capture_span, format!("{} is borrowed here", borrowed_path))
412+
.span_label(closure_span, format!("may outlive borrowed value {}", borrowed_path));
413+
err
414+
}
297415
}
298416

299417
impl<'b, 'tcx, 'gcx> BorrowckErrors for TyCtxt<'b, 'tcx, 'gcx> {

0 commit comments

Comments
 (0)