2019-04-26 14:26:49 +02:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
2017-10-27 08:21:22 +02:00
|
|
|
use rustc_errors::{DiagnosticBuilder, DiagnosticId};
|
2017-07-03 17:25:03 +02:00
|
|
|
use syntax_pos::{MultiSpan, Span};
|
|
|
|
|
2019-07-12 20:05:33 +01:00
|
|
|
pub(crate) trait BorrowckErrors<'cx>: Sized + Copy {
|
2018-06-22 00:10:52 -03:00
|
|
|
fn struct_span_err_with_code<S: Into<MultiSpan>>(
|
|
|
|
self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str,
|
|
|
|
code: DiagnosticId,
|
|
|
|
) -> DiagnosticBuilder<'cx>;
|
2017-07-03 17:25:03 +02:00
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn struct_span_err<S: Into<MultiSpan>>(self, sp: S, msg: &str) -> DiagnosticBuilder<'cx>;
|
2017-07-03 17:25:03 +02:00
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_move_when_borrowed(
|
|
|
|
self,
|
|
|
|
span: Span,
|
|
|
|
desc: &str,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(
|
2018-06-22 00:10:52 -03:00
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0505,
|
2019-06-29 17:36:46 +01:00
|
|
|
"cannot move out of `{}` because it is borrowed",
|
2018-06-22 00:10:52 -03:00
|
|
|
desc,
|
2019-06-29 17:36:46 +01:00
|
|
|
)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_use_when_mutably_borrowed(
|
|
|
|
self,
|
|
|
|
span: Span,
|
|
|
|
desc: &str,
|
|
|
|
borrow_span: Span,
|
|
|
|
borrow_desc: &str,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0503,
|
2019-06-29 17:36:46 +01:00
|
|
|
"cannot use `{}` because it was mutably borrowed",
|
2018-06-22 00:10:52 -03:00
|
|
|
desc,
|
|
|
|
);
|
|
|
|
|
|
|
|
err.span_label(
|
|
|
|
borrow_span,
|
|
|
|
format!("borrow of `{}` occurs here", borrow_desc),
|
|
|
|
);
|
2017-09-28 16:45:09 +02:00
|
|
|
err.span_label(span, format!("use of borrowed `{}`", borrow_desc));
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_act_on_uninitialized_variable(
|
|
|
|
self,
|
|
|
|
span: Span,
|
|
|
|
verb: &str,
|
|
|
|
desc: &str,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(
|
2018-06-22 00:10:52 -03:00
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0381,
|
2019-06-29 17:36:46 +01:00
|
|
|
"{} of possibly uninitialized variable: `{}`",
|
2018-06-22 00:10:52 -03:00
|
|
|
verb,
|
|
|
|
desc,
|
2019-06-29 17:36:46 +01:00
|
|
|
)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_mutably_borrow_multiply(
|
|
|
|
self,
|
|
|
|
new_loan_span: Span,
|
|
|
|
desc: &str,
|
|
|
|
opt_via: &str,
|
|
|
|
old_loan_span: Span,
|
|
|
|
old_opt_via: &str,
|
|
|
|
old_load_end_span: Option<Span>,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
2019-01-04 22:43:51 +01:00
|
|
|
let via = |msg: &str|
|
|
|
|
if msg.is_empty() { msg.to_string() } else { format!(" (via `{}`)", msg) };
|
2018-06-22 00:10:52 -03:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
new_loan_span,
|
|
|
|
E0499,
|
2019-06-29 17:36:46 +01:00
|
|
|
"cannot borrow `{}`{} as mutable more than once at a time",
|
2018-06-22 00:10:52 -03:00
|
|
|
desc,
|
2019-01-04 22:43:51 +01:00
|
|
|
via(opt_via),
|
2018-06-22 00:10:52 -03:00
|
|
|
);
|
2017-09-27 10:01:42 +03:00
|
|
|
if old_loan_span == new_loan_span {
|
|
|
|
// Both borrows are happening in the same place
|
|
|
|
// Meaning the borrow is occurring in a loop
|
2018-06-22 00:10:52 -03:00
|
|
|
err.span_label(
|
|
|
|
new_loan_span,
|
|
|
|
format!(
|
|
|
|
"mutable borrow starts here in previous \
|
|
|
|
iteration of loop{}",
|
|
|
|
opt_via
|
|
|
|
),
|
|
|
|
);
|
2017-10-25 14:17:17 -04:00
|
|
|
if let Some(old_load_end_span) = old_load_end_span {
|
|
|
|
err.span_label(old_load_end_span, "mutable borrow ends here");
|
|
|
|
}
|
2017-09-27 10:01:42 +03:00
|
|
|
} else {
|
2018-06-22 00:10:52 -03:00
|
|
|
err.span_label(
|
|
|
|
old_loan_span,
|
2019-01-04 22:43:51 +01:00
|
|
|
format!("first mutable borrow occurs here{}", via(old_opt_via)),
|
2018-06-22 00:10:52 -03:00
|
|
|
);
|
|
|
|
err.span_label(
|
|
|
|
new_loan_span,
|
2019-01-04 22:43:51 +01:00
|
|
|
format!("second mutable borrow occurs here{}", via(opt_via)),
|
2018-06-22 00:10:52 -03:00
|
|
|
);
|
2017-10-25 14:17:17 -04:00
|
|
|
if let Some(old_load_end_span) = old_load_end_span {
|
|
|
|
err.span_label(old_load_end_span, "first borrow ends here");
|
|
|
|
}
|
2017-09-27 10:01:42 +03:00
|
|
|
}
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_uniquely_borrow_by_two_closures(
|
|
|
|
self,
|
|
|
|
new_loan_span: Span,
|
|
|
|
desc: &str,
|
|
|
|
old_loan_span: Span,
|
|
|
|
old_load_end_span: Option<Span>,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
2017-09-27 10:01:42 +03:00
|
|
|
new_loan_span,
|
2018-06-22 00:10:52 -03:00
|
|
|
E0524,
|
2019-06-29 17:36:46 +01:00
|
|
|
"two closures require unique access to `{}` at the same time",
|
2018-06-22 00:10:52 -03:00
|
|
|
desc,
|
|
|
|
);
|
2018-08-01 20:38:02 +01:00
|
|
|
if old_loan_span == new_loan_span {
|
|
|
|
err.span_label(
|
|
|
|
old_loan_span,
|
|
|
|
"closures are constructed here in different iterations of loop"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
err.span_label(old_loan_span, "first closure is constructed here");
|
|
|
|
err.span_label(new_loan_span, "second closure is constructed here");
|
|
|
|
}
|
2017-10-25 14:17:17 -04:00
|
|
|
if let Some(old_load_end_span) = old_load_end_span {
|
2018-06-22 00:10:52 -03:00
|
|
|
err.span_label(old_load_end_span, "borrow from first closure ends here");
|
2017-10-25 14:17:17 -04:00
|
|
|
}
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_uniquely_borrow_by_one_closure(
|
|
|
|
self,
|
|
|
|
new_loan_span: Span,
|
2018-10-10 21:56:17 +02:00
|
|
|
container_name: &str,
|
2018-06-22 00:10:52 -03:00
|
|
|
desc_new: &str,
|
|
|
|
opt_via: &str,
|
|
|
|
old_loan_span: Span,
|
|
|
|
noun_old: &str,
|
|
|
|
old_opt_via: &str,
|
|
|
|
previous_end_span: Option<Span>,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
new_loan_span,
|
|
|
|
E0500,
|
2019-06-29 17:36:46 +01:00
|
|
|
"closure requires unique access to `{}` but {} is already borrowed{}",
|
2018-06-22 00:10:52 -03:00
|
|
|
desc_new,
|
|
|
|
noun_old,
|
|
|
|
old_opt_via,
|
|
|
|
);
|
|
|
|
err.span_label(
|
|
|
|
new_loan_span,
|
2018-10-10 21:56:17 +02:00
|
|
|
format!("{} construction occurs here{}", container_name, opt_via),
|
2018-06-22 00:10:52 -03:00
|
|
|
);
|
|
|
|
err.span_label(old_loan_span, format!("borrow occurs here{}", old_opt_via));
|
2017-10-25 14:17:17 -04:00
|
|
|
if let Some(previous_end_span) = previous_end_span {
|
|
|
|
err.span_label(previous_end_span, "borrow ends here");
|
|
|
|
}
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_reborrow_already_uniquely_borrowed(
|
|
|
|
self,
|
|
|
|
new_loan_span: Span,
|
2018-10-10 21:56:17 +02:00
|
|
|
container_name: &str,
|
2018-06-22 00:10:52 -03:00
|
|
|
desc_new: &str,
|
|
|
|
opt_via: &str,
|
|
|
|
kind_new: &str,
|
|
|
|
old_loan_span: Span,
|
|
|
|
old_opt_via: &str,
|
|
|
|
previous_end_span: Option<Span>,
|
2018-11-30 14:55:51 +01:00
|
|
|
second_borrow_desc: &str,
|
2018-06-22 00:10:52 -03:00
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
new_loan_span,
|
|
|
|
E0501,
|
|
|
|
"cannot borrow `{}`{} as {} because previous closure \
|
2019-06-29 17:36:46 +01:00
|
|
|
requires unique access",
|
2018-06-22 00:10:52 -03:00
|
|
|
desc_new,
|
|
|
|
opt_via,
|
|
|
|
kind_new,
|
|
|
|
);
|
2018-11-30 14:55:51 +01:00
|
|
|
err.span_label(
|
|
|
|
new_loan_span,
|
|
|
|
format!("{}borrow occurs here{}", second_borrow_desc, opt_via),
|
|
|
|
);
|
2018-06-22 00:10:52 -03:00
|
|
|
err.span_label(
|
|
|
|
old_loan_span,
|
2018-10-10 21:56:17 +02:00
|
|
|
format!("{} construction occurs here{}", container_name, old_opt_via),
|
2018-06-22 00:10:52 -03:00
|
|
|
);
|
2017-10-25 14:17:17 -04:00
|
|
|
if let Some(previous_end_span) = previous_end_span {
|
|
|
|
err.span_label(previous_end_span, "borrow from closure ends here");
|
|
|
|
}
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_reborrow_already_borrowed(
|
|
|
|
self,
|
|
|
|
span: Span,
|
|
|
|
desc_new: &str,
|
|
|
|
msg_new: &str,
|
|
|
|
kind_new: &str,
|
|
|
|
old_span: Span,
|
|
|
|
noun_old: &str,
|
|
|
|
kind_old: &str,
|
|
|
|
msg_old: &str,
|
|
|
|
old_load_end_span: Option<Span>,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
2019-01-04 22:43:51 +01:00
|
|
|
let via = |msg: &str|
|
|
|
|
if msg.is_empty() { msg.to_string() } else { format!(" (via `{}`)", msg) };
|
2018-06-22 00:10:52 -03:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0502,
|
2019-01-04 22:43:51 +01:00
|
|
|
"cannot borrow `{}`{} as {} because {} is also borrowed \
|
2019-06-29 17:36:46 +01:00
|
|
|
as {}{}",
|
2018-06-22 00:10:52 -03:00
|
|
|
desc_new,
|
2019-01-04 22:43:51 +01:00
|
|
|
via(msg_new),
|
2018-06-22 00:10:52 -03:00
|
|
|
kind_new,
|
|
|
|
noun_old,
|
|
|
|
kind_old,
|
2019-01-04 22:43:51 +01:00
|
|
|
via(msg_old),
|
2018-06-22 00:10:52 -03:00
|
|
|
);
|
2019-01-04 22:43:51 +01:00
|
|
|
|
|
|
|
if msg_new == "" {
|
|
|
|
// If `msg_new` is empty, then this isn't a borrow of a union field.
|
|
|
|
err.span_label(span, format!("{} borrow occurs here", kind_new));
|
|
|
|
err.span_label(old_span, format!("{} borrow occurs here", kind_old));
|
|
|
|
} else {
|
|
|
|
// If `msg_new` isn't empty, then this a borrow of a union field.
|
|
|
|
err.span_label(
|
|
|
|
span,
|
|
|
|
format!(
|
|
|
|
"{} borrow of `{}` -- which overlaps with `{}` -- occurs here",
|
|
|
|
kind_new, msg_new, msg_old,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
err.span_label(
|
|
|
|
old_span,
|
|
|
|
format!("{} borrow occurs here{}", kind_old, via(msg_old)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-10-25 14:17:17 -04:00
|
|
|
if let Some(old_load_end_span) = old_load_end_span {
|
|
|
|
err.span_label(old_load_end_span, format!("{} borrow ends here", kind_old));
|
|
|
|
}
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_assign_to_borrowed(
|
|
|
|
self,
|
|
|
|
span: Span,
|
|
|
|
borrow_span: Span,
|
|
|
|
desc: &str,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0506,
|
2019-06-29 17:36:46 +01:00
|
|
|
"cannot assign to `{}` because it is borrowed",
|
2018-06-22 00:10:52 -03:00
|
|
|
desc,
|
|
|
|
);
|
2017-09-28 16:44:23 +02:00
|
|
|
|
|
|
|
err.span_label(borrow_span, format!("borrow of `{}` occurs here", desc));
|
2018-06-22 00:10:52 -03:00
|
|
|
err.span_label(
|
|
|
|
span,
|
|
|
|
format!("assignment to borrowed `{}` occurs here", desc),
|
|
|
|
);
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_reassign_immutable(
|
|
|
|
self,
|
|
|
|
span: Span,
|
|
|
|
desc: &str,
|
|
|
|
is_arg: bool,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
2018-01-09 19:10:45 -08:00
|
|
|
let msg = if is_arg {
|
|
|
|
"to immutable argument"
|
|
|
|
} else {
|
|
|
|
"twice to immutable variable"
|
|
|
|
};
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(
|
2018-06-22 00:10:52 -03:00
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0384,
|
2019-06-29 17:36:46 +01:00
|
|
|
"cannot assign {} `{}`",
|
2018-06-22 00:10:52 -03:00
|
|
|
msg,
|
|
|
|
desc,
|
2019-06-29 17:36:46 +01:00
|
|
|
)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 20:05:33 +01:00
|
|
|
fn cannot_assign(self, span: Span, desc: &str) -> DiagnosticBuilder<'cx> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(self, span, E0594, "cannot assign to {}", desc)
|
2017-09-28 19:14:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_move_out_of(
|
|
|
|
self,
|
|
|
|
move_from_span: Span,
|
|
|
|
move_from_desc: &str,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(
|
2018-06-22 00:10:52 -03:00
|
|
|
self,
|
|
|
|
move_from_span,
|
|
|
|
E0507,
|
2019-06-29 17:36:46 +01:00
|
|
|
"cannot move out of {}",
|
2018-06-22 00:10:52 -03:00
|
|
|
move_from_desc,
|
2019-06-29 17:36:46 +01:00
|
|
|
)
|
2017-10-03 15:34:52 +02:00
|
|
|
}
|
2017-10-03 15:54:12 +02:00
|
|
|
|
2018-05-31 12:46:43 +02:00
|
|
|
/// Signal an error due to an attempt to move out of the interior
|
|
|
|
/// of an array or slice. `is_index` is None when error origin
|
|
|
|
/// didn't capture whether there was an indexing operation or not.
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_move_out_of_interior_noncopy(
|
|
|
|
self,
|
|
|
|
move_from_span: Span,
|
2019-04-26 14:26:49 +02:00
|
|
|
ty: Ty<'_>,
|
2018-06-22 00:10:52 -03:00
|
|
|
is_index: Option<bool>,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
2017-10-03 15:54:12 +02:00
|
|
|
let type_name = match (&ty.sty, is_index) {
|
2018-08-22 01:35:02 +01:00
|
|
|
(&ty::Array(_, _), Some(true)) | (&ty::Array(_, _), None) => "array",
|
|
|
|
(&ty::Slice(_), _) => "slice",
|
2017-10-03 15:54:12 +02:00
|
|
|
_ => span_bug!(move_from_span, "this path should not cause illegal move"),
|
|
|
|
};
|
2018-06-22 00:10:52 -03:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
move_from_span,
|
|
|
|
E0508,
|
2019-06-29 17:36:46 +01:00
|
|
|
"cannot move out of type `{}`, a non-copy {}",
|
2018-06-22 00:10:52 -03:00
|
|
|
ty,
|
|
|
|
type_name,
|
|
|
|
);
|
2017-10-03 15:54:12 +02:00
|
|
|
err.span_label(move_from_span, "cannot move out of here");
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2017-10-03 15:54:12 +02:00
|
|
|
}
|
2017-10-03 16:07:20 +02:00
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_move_out_of_interior_of_drop(
|
|
|
|
self,
|
|
|
|
move_from_span: Span,
|
2019-04-26 14:26:49 +02:00
|
|
|
container_ty: Ty<'_>,
|
2018-06-22 00:10:52 -03:00
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
move_from_span,
|
|
|
|
E0509,
|
2019-06-29 17:36:46 +01:00
|
|
|
"cannot move out of type `{}`, which implements the `Drop` trait",
|
2018-06-22 00:10:52 -03:00
|
|
|
container_ty,
|
|
|
|
);
|
2017-10-03 16:07:20 +02:00
|
|
|
err.span_label(move_from_span, "cannot move out of here");
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2017-10-03 16:07:20 +02:00
|
|
|
}
|
2017-10-09 17:44:56 +02:00
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_act_on_moved_value(
|
|
|
|
self,
|
|
|
|
use_span: Span,
|
|
|
|
verb: &str,
|
|
|
|
optional_adverb_for_moved: &str,
|
|
|
|
moved_path: Option<String>,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let moved_path = moved_path
|
|
|
|
.map(|mp| format!(": `{}`", mp))
|
2018-10-12 16:16:00 +02:00
|
|
|
.unwrap_or_default();
|
2018-06-22 00:10:52 -03:00
|
|
|
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(
|
2018-06-22 00:10:52 -03:00
|
|
|
self,
|
|
|
|
use_span,
|
|
|
|
E0382,
|
2019-06-29 17:36:46 +01:00
|
|
|
"{} of {}moved value{}",
|
2018-06-22 00:10:52 -03:00
|
|
|
verb,
|
|
|
|
optional_adverb_for_moved,
|
|
|
|
moved_path,
|
2019-06-29 17:36:46 +01:00
|
|
|
)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2018-07-15 15:51:35 +01:00
|
|
|
fn cannot_borrow_path_as_mutable_because(
|
2018-06-22 00:10:52 -03:00
|
|
|
self,
|
|
|
|
span: Span,
|
|
|
|
path: &str,
|
2018-07-15 15:51:35 +01:00
|
|
|
reason: &str,
|
2018-06-22 00:10:52 -03:00
|
|
|
) -> DiagnosticBuilder<'cx> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(
|
2018-06-22 00:10:52 -03:00
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0596,
|
2019-06-29 17:36:46 +01:00
|
|
|
"cannot borrow {} as mutable{}",
|
2018-06-22 00:10:52 -03:00
|
|
|
path,
|
2018-07-15 15:51:35 +01:00
|
|
|
reason,
|
2019-06-29 17:36:46 +01:00
|
|
|
)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:34:38 +01:00
|
|
|
fn cannot_mutate_in_match_guard(
|
|
|
|
self,
|
|
|
|
mutate_span: Span,
|
|
|
|
match_span: Span,
|
|
|
|
match_place: &str,
|
|
|
|
action: &str,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
mutate_span,
|
|
|
|
E0510,
|
2019-06-29 17:36:46 +01:00
|
|
|
"cannot {} `{}` in match guard",
|
2018-09-10 22:34:38 +01:00
|
|
|
action,
|
|
|
|
match_place,
|
|
|
|
);
|
|
|
|
err.span_label(mutate_span, format!("cannot {}", action));
|
2018-10-31 00:10:10 +01:00
|
|
|
err.span_label(match_span, String::from("value is immutable in match guard"));
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2018-09-10 22:34:38 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_borrow_across_generator_yield(
|
|
|
|
self,
|
|
|
|
span: Span,
|
|
|
|
yield_span: Span,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0626,
|
2019-06-29 17:36:46 +01:00
|
|
|
"borrow may still be in use when generator yields",
|
2018-06-22 00:10:52 -03:00
|
|
|
);
|
2017-10-09 17:44:56 +02:00
|
|
|
err.span_label(yield_span, "possible yield occurs here");
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2018-09-18 01:47:53 +02:00
|
|
|
fn cannot_borrow_across_destructor(
|
|
|
|
self,
|
|
|
|
borrow_span: Span,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(
|
2018-09-18 01:47:53 +02:00
|
|
|
self,
|
|
|
|
borrow_span,
|
|
|
|
E0713,
|
2019-06-29 17:36:46 +01:00
|
|
|
"borrow may still be in use when destructor runs",
|
|
|
|
)
|
2018-09-18 01:47:53 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn path_does_not_live_long_enough(
|
|
|
|
self,
|
|
|
|
span: Span,
|
|
|
|
path: &str,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(
|
2018-06-22 00:10:52 -03:00
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0597,
|
2019-06-29 17:36:46 +01:00
|
|
|
"{} does not live long enough",
|
2018-06-22 00:10:52 -03:00
|
|
|
path,
|
2019-06-29 17:36:46 +01:00
|
|
|
)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-14 15:12:02 +01:00
|
|
|
fn cannot_return_reference_to_local(
|
|
|
|
self,
|
|
|
|
span: Span,
|
2019-04-22 18:20:19 +01:00
|
|
|
return_kind: &str,
|
2018-10-14 15:12:02 +01:00
|
|
|
reference_desc: &str,
|
|
|
|
path_desc: &str,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0515,
|
2019-06-29 17:36:46 +01:00
|
|
|
"cannot {RETURN} {REFERENCE} {LOCAL}",
|
2019-04-22 18:20:19 +01:00
|
|
|
RETURN=return_kind,
|
2018-10-14 15:12:02 +01:00
|
|
|
REFERENCE=reference_desc,
|
|
|
|
LOCAL=path_desc,
|
|
|
|
);
|
|
|
|
|
|
|
|
err.span_label(
|
|
|
|
span,
|
2019-04-22 18:20:19 +01:00
|
|
|
format!("{}s a {} data owned by the current function", return_kind, reference_desc),
|
2018-10-14 15:12:02 +01:00
|
|
|
);
|
|
|
|
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2018-10-14 15:12:02 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn cannot_capture_in_long_lived_closure(
|
|
|
|
self,
|
|
|
|
closure_span: Span,
|
|
|
|
borrowed_path: &str,
|
|
|
|
capture_span: Span,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
closure_span,
|
|
|
|
E0373,
|
|
|
|
"closure may outlive the current function, \
|
|
|
|
but it borrows {}, \
|
2019-06-29 17:36:46 +01:00
|
|
|
which is owned by the current function",
|
2018-06-22 00:10:52 -03:00
|
|
|
borrowed_path,
|
|
|
|
);
|
2017-10-09 17:44:56 +02:00
|
|
|
err.span_label(capture_span, format!("{} is borrowed here", borrowed_path))
|
2018-06-22 00:10:52 -03:00
|
|
|
.span_label(
|
|
|
|
closure_span,
|
|
|
|
format!("may outlive borrowed value {}", borrowed_path),
|
|
|
|
);
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
2018-08-06 21:06:00 +02:00
|
|
|
|
2018-10-14 14:53:10 +01:00
|
|
|
fn borrowed_data_escapes_closure(
|
|
|
|
self,
|
|
|
|
escape_span: Span,
|
|
|
|
escapes_from: &str,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(
|
2018-10-14 14:53:10 +01:00
|
|
|
self,
|
|
|
|
escape_span,
|
|
|
|
E0521,
|
2019-06-29 17:36:46 +01:00
|
|
|
"borrowed data escapes outside of {}",
|
2018-10-14 14:53:10 +01:00
|
|
|
escapes_from,
|
2019-06-29 17:36:46 +01:00
|
|
|
)
|
2018-10-14 14:53:10 +01:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:06:00 +02:00
|
|
|
fn thread_local_value_does_not_live_long_enough(
|
|
|
|
self,
|
|
|
|
span: Span,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(
|
2018-08-06 21:06:00 +02:00
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0712,
|
2019-06-29 17:36:46 +01:00
|
|
|
"thread-local variable borrowed past end of function",
|
|
|
|
)
|
2018-08-06 21:06:00 +02:00
|
|
|
}
|
2018-09-23 16:30:46 +01:00
|
|
|
|
|
|
|
fn temporary_value_borrowed_for_too_long(
|
|
|
|
self,
|
|
|
|
span: Span,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(
|
2018-09-23 16:30:46 +01:00
|
|
|
self,
|
|
|
|
span,
|
2018-09-25 16:06:28 +02:00
|
|
|
E0716,
|
2019-06-29 17:36:46 +01:00
|
|
|
"temporary value dropped while borrowed",
|
|
|
|
)
|
2018-09-23 16:30:46 +01:00
|
|
|
}
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl BorrowckErrors<'tcx> for TyCtxt<'tcx> {
|
2018-06-22 00:10:52 -03:00
|
|
|
fn struct_span_err_with_code<S: Into<MultiSpan>>(
|
|
|
|
self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str,
|
|
|
|
code: DiagnosticId,
|
2019-06-11 22:03:44 +03:00
|
|
|
) -> DiagnosticBuilder<'tcx> {
|
2017-07-03 17:25:03 +02:00
|
|
|
self.sess.struct_span_err_with_code(sp, msg, code)
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
fn struct_span_err<S: Into<MultiSpan>>(self, sp: S, msg: &str) -> DiagnosticBuilder<'tcx> {
|
2017-07-03 17:25:03 +02:00
|
|
|
self.sess.struct_span_err(sp, msg)
|
|
|
|
}
|
|
|
|
}
|