2022-03-24 02:03:04 +00:00
|
|
|
use rustc_errors::{struct_span_err, DiagnosticBuilder, DiagnosticId, ErrorGuaranteed, MultiSpan};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2022-03-24 02:03:04 +00:00
|
|
|
use rustc_span::Span;
|
2019-11-11 22:46:56 +01:00
|
|
|
|
2020-12-30 18:48:40 +01:00
|
|
|
impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
|
2022-01-27 09:44:25 +00:00
|
|
|
crate fn cannot_move_when_borrowed(
|
|
|
|
&self,
|
|
|
|
span: Span,
|
|
|
|
desc: &str,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2020-03-25 11:17:06 +01:00
|
|
|
struct_span_err!(self, span, E0505, "cannot move out of {} because it is borrowed", desc,)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_use_when_mutably_borrowed(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
span: Span,
|
|
|
|
desc: &str,
|
|
|
|
borrow_span: Span,
|
|
|
|
borrow_desc: &str,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2018-06-22 00:10:52 -03:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0503,
|
2020-03-25 11:17:06 +01:00
|
|
|
"cannot use {} because it was mutably borrowed",
|
2018-06-22 00:10:52 -03:00
|
|
|
desc,
|
|
|
|
);
|
|
|
|
|
2020-03-25 11:17:06 +01:00
|
|
|
err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_desc));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_act_on_uninitialized_variable(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
span: Span,
|
|
|
|
verb: &str,
|
|
|
|
desc: &str,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(
|
2018-06-22 00:10:52 -03:00
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0381,
|
2019-09-01 18:09:59 +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
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_mutably_borrow_multiply(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
new_loan_span: Span,
|
|
|
|
desc: &str,
|
|
|
|
opt_via: &str,
|
|
|
|
old_loan_span: Span,
|
|
|
|
old_opt_via: &str,
|
|
|
|
old_load_end_span: Option<Span>,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2019-01-04 22:43:51 +01:00
|
|
|
let via =
|
2020-03-26 01:55:16 +01:00
|
|
|
|msg: &str| if msg.is_empty() { "".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,
|
2020-03-25 11:17:06 +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!(
|
2020-10-08 10:45:34 +05:30
|
|
|
"{}{} was mutably borrowed here in the previous iteration of the loop{}",
|
|
|
|
desc,
|
|
|
|
via(opt_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, "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
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_uniquely_borrow_by_two_closures(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
new_loan_span: Span,
|
|
|
|
desc: &str,
|
|
|
|
old_loan_span: Span,
|
|
|
|
old_load_end_span: Option<Span>,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2018-06-22 00:10:52 -03:00
|
|
|
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,
|
2020-03-25 11:17:06 +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
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_uniquely_borrow_by_one_closure(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
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>,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2018-06-22 00:10:52 -03:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
new_loan_span,
|
|
|
|
E0500,
|
2020-03-25 11:17:06 +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
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_reborrow_already_uniquely_borrowed(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
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,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2018-06-22 00:10:52 -03:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
new_loan_span,
|
|
|
|
E0501,
|
2020-03-25 11:17:06 +01:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_reborrow_already_borrowed(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
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>,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2019-01-04 22:43:51 +01:00
|
|
|
let via =
|
2020-03-26 01:55:16 +01:00
|
|
|
|msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {})", msg) };
|
2018-06-22 00:10:52 -03:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0502,
|
2020-03-26 01:55:16 +01:00
|
|
|
"cannot borrow {}{} as {} because {} is also borrowed 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!(
|
2020-03-25 11:17:06 +01:00
|
|
|
"{} borrow of {} -- which overlaps with {} -- occurs here",
|
2019-01-04 22:43:51 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_assign_to_borrowed(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
span: Span,
|
|
|
|
borrow_span: Span,
|
|
|
|
desc: &str,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2018-06-22 00:10:52 -03:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
span,
|
|
|
|
E0506,
|
2020-03-25 11:17:06 +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
|
|
|
|
2020-03-25 11:17:06 +01:00
|
|
|
err.span_label(borrow_span, format!("borrow of {} occurs here", desc));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_reassign_immutable(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
span: Span,
|
|
|
|
desc: &str,
|
|
|
|
is_arg: bool,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2018-01-09 19:10:45 -08:00
|
|
|
let msg = if is_arg { "to immutable argument" } else { "twice to immutable variable" };
|
2020-03-25 11:17:06 +01:00
|
|
|
struct_span_err!(self, span, E0384, "cannot assign {} {}", msg, desc)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2022-01-23 12:34:26 -06:00
|
|
|
crate fn cannot_assign(
|
|
|
|
&self,
|
|
|
|
span: Span,
|
|
|
|
desc: &str,
|
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
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
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_move_out_of(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
move_from_span: Span,
|
|
|
|
move_from_desc: &str,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(self, move_from_span, E0507, "cannot move out of {}", move_from_desc,)
|
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.
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_move_out_of_interior_noncopy(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
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>,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2020-08-03 00:49:11 +02:00
|
|
|
let type_name = match (&ty.kind(), 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
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_move_out_of_interior_of_drop(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
move_from_span: Span,
|
2019-04-26 14:26:49 +02:00
|
|
|
container_ty: Ty<'_>,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2018-06-22 00:10:52 -03:00
|
|
|
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
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_act_on_moved_value(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
use_span: Span,
|
|
|
|
verb: &str,
|
|
|
|
optional_adverb_for_moved: &str,
|
|
|
|
moved_path: Option<String>,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
2018-06-22 00:10:52 -03:00
|
|
|
let moved_path = moved_path.map(|mp| format!(": `{}`", mp)).unwrap_or_default();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_borrow_path_as_mutable_because(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
span: Span,
|
|
|
|
path: &str,
|
2018-07-15 15:51:35 +01:00
|
|
|
reason: &str,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(self, span, E0596, "cannot borrow {} as mutable{}", path, reason,)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 21:00:21 +01:00
|
|
|
crate fn cannot_mutate_in_immutable_section(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-09-10 22:34:38 +01:00
|
|
|
mutate_span: Span,
|
2019-10-19 21:00:21 +01:00
|
|
|
immutable_span: Span,
|
|
|
|
immutable_place: &str,
|
|
|
|
immutable_section: &str,
|
2018-09-10 22:34:38 +01:00
|
|
|
action: &str,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2018-09-10 22:34:38 +01:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
mutate_span,
|
|
|
|
E0510,
|
2020-03-25 11:17:06 +01:00
|
|
|
"cannot {} {} in {}",
|
2018-09-10 22:34:38 +01:00
|
|
|
action,
|
2019-10-19 21:00:21 +01:00
|
|
|
immutable_place,
|
|
|
|
immutable_section,
|
2018-09-10 22:34:38 +01:00
|
|
|
);
|
|
|
|
err.span_label(mutate_span, format!("cannot {}", action));
|
2019-10-19 21:00:21 +01:00
|
|
|
err.span_label(immutable_span, format!("value is immutable in {}", immutable_section));
|
2019-06-29 17:36:46 +01:00
|
|
|
err
|
2018-09-10 22:34:38 +01:00
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_borrow_across_generator_yield(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
span: Span,
|
|
|
|
yield_span: Span,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2018-06-22 00:10:52 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-27 09:44:25 +00:00
|
|
|
crate fn cannot_borrow_across_destructor(
|
|
|
|
&self,
|
|
|
|
borrow_span: Span,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
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
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn path_does_not_live_long_enough(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
span: Span,
|
|
|
|
path: &str,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(self, span, E0597, "{} does not live long enough", path,)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_return_reference_to_local(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-10-14 15:12:02 +01:00
|
|
|
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,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2018-10-14 15:12:02 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn cannot_capture_in_long_lived_closure(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
closure_span: Span,
|
2020-04-07 23:57:26 +02:00
|
|
|
closure_kind: &str,
|
2018-06-22 00:10:52 -03:00
|
|
|
borrowed_path: &str,
|
|
|
|
capture_span: Span,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2018-06-22 00:10:52 -03:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self,
|
|
|
|
closure_span,
|
|
|
|
E0373,
|
2020-04-07 23:57:26 +02:00
|
|
|
"{} may outlive the current function, \
|
2018-06-22 00:10:52 -03:00
|
|
|
but it borrows {}, \
|
2019-06-29 17:36:46 +01:00
|
|
|
which is owned by the current function",
|
2020-04-07 23:57:26 +02:00
|
|
|
closure_kind,
|
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
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn thread_local_value_does_not_live_long_enough(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-08-06 21:06:00 +02:00
|
|
|
span: Span,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(self, span, E0712, "thread-local variable borrowed past end of function",)
|
2018-08-06 21:06:00 +02:00
|
|
|
}
|
2018-09-23 16:30:46 +01:00
|
|
|
|
2022-01-27 09:44:25 +00:00
|
|
|
crate fn temporary_value_borrowed_for_too_long(
|
|
|
|
&self,
|
|
|
|
span: Span,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
|
2019-06-29 17:36:46 +01:00
|
|
|
struct_span_err!(self, span, E0716, "temporary value dropped while borrowed",)
|
2018-09-23 16:30:46 +01:00
|
|
|
}
|
2017-07-03 17:25:03 +02:00
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
fn struct_span_err_with_code<S: Into<MultiSpan>>(
|
2019-07-12 20:37:06 +01:00
|
|
|
&self,
|
2018-06-22 00:10:52 -03:00
|
|
|
sp: S,
|
|
|
|
msg: &str,
|
|
|
|
code: DiagnosticId,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
2019-07-12 20:37:06 +01:00
|
|
|
self.infcx.tcx.sess.struct_span_err_with_code(sp, msg, code)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
2019-07-12 20:37:06 +01:00
|
|
|
}
|
2017-07-03 17:25:03 +02:00
|
|
|
|
2019-07-13 09:37:19 +01:00
|
|
|
crate fn borrowed_data_escapes_closure<'tcx>(
|
2019-07-12 20:37:06 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
escape_span: Span,
|
|
|
|
escapes_from: &str,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
2019-07-12 20:37:06 +01:00
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
escape_span,
|
|
|
|
E0521,
|
|
|
|
"borrowed data escapes outside of {}",
|
|
|
|
escapes_from,
|
|
|
|
)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|