2017-07-03 17:25:03 +02:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use rustc::ty::{self, TyCtxt};
|
2017-11-19 23:35:53 +01:00
|
|
|
use rustc::session::config::BorrowckMode;
|
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};
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum Origin { Ast, Mir }
|
|
|
|
|
|
|
|
impl fmt::Display for Origin {
|
|
|
|
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
|
2017-11-19 23:35:53 +01:00
|
|
|
// If the user passed `-Z borrowck=compare`, then include
|
|
|
|
// origin info as part of the error report,
|
|
|
|
// otherwise
|
|
|
|
let display_origin = ty::tls::with_opt(|opt_tcx| {
|
|
|
|
if let Some(tcx) = opt_tcx {
|
|
|
|
tcx.sess.opts.borrowck_mode == BorrowckMode::Compare
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if display_origin {
|
|
|
|
match *self {
|
|
|
|
Origin::Mir => write!(w, " (Mir)"),
|
|
|
|
Origin::Ast => write!(w, " (Ast)"),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Print no origin info
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Origin {
|
|
|
|
/// Whether we should emit errors for the origin in the given mode
|
|
|
|
pub fn should_emit_errors(self, mode: BorrowckMode) -> bool {
|
|
|
|
match self {
|
|
|
|
Origin::Ast => mode.use_ast(),
|
|
|
|
Origin::Mir => mode.use_mir(),
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:39:44 +08:00
|
|
|
pub trait BorrowckErrors<'cx>: Sized + Copy {
|
2018-03-08 16:29:35 +08:00
|
|
|
fn struct_span_err_with_code<S: Into<MultiSpan>>(self,
|
2018-03-11 01:03:51 +00:00
|
|
|
sp: S,
|
|
|
|
msg: &str,
|
|
|
|
code: DiagnosticId)
|
2018-03-15 18:39:44 +08:00
|
|
|
-> DiagnosticBuilder<'cx>;
|
2017-07-03 17:25:03 +02:00
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn struct_span_err<S: Into<MultiSpan>>(self,
|
2018-03-11 01:03:51 +00:00
|
|
|
sp: S,
|
|
|
|
msg: &str)
|
2018-03-15 18:39:44 +08:00
|
|
|
-> DiagnosticBuilder<'cx>;
|
2017-07-03 17:25:03 +02:00
|
|
|
|
2017-11-19 23:35:53 +01:00
|
|
|
/// Cancels the given error if we shouldn't emit errors for a given
|
|
|
|
/// origin in the current mode.
|
|
|
|
///
|
|
|
|
/// Always make sure that the error gets passed through this function
|
|
|
|
/// before you return it.
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cancel_if_wrong_origin(self,
|
2018-03-11 01:03:51 +00:00
|
|
|
diag: DiagnosticBuilder<'cx>,
|
|
|
|
o: Origin)
|
2018-03-15 18:39:44 +08:00
|
|
|
-> DiagnosticBuilder<'cx>;
|
2017-11-19 23:35:53 +01:00
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_move_when_borrowed(self, span: Span, desc: &str, o: Origin)
|
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-07-03 17:25:03 +02:00
|
|
|
{
|
2017-11-19 23:35:53 +01:00
|
|
|
let err = struct_span_err!(self, span, E0505,
|
|
|
|
"cannot move out of `{}` because it is borrowed{OGN}",
|
|
|
|
desc, OGN=o);
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_use_when_mutably_borrowed(self,
|
2017-09-28 16:45:09 +02:00
|
|
|
span: Span,
|
|
|
|
desc: &str,
|
|
|
|
borrow_span: Span,
|
|
|
|
borrow_desc: &str,
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-07-03 17:25:03 +02:00
|
|
|
{
|
2017-09-28 16:45:09 +02:00
|
|
|
let mut err = struct_span_err!(self, span, E0503,
|
2017-07-03 17:25:03 +02:00
|
|
|
"cannot use `{}` because it was mutably borrowed{OGN}",
|
2017-09-28 16:45:09 +02:00
|
|
|
desc, OGN=o);
|
|
|
|
|
|
|
|
err.span_label(borrow_span, format!("borrow of `{}` occurs here", borrow_desc));
|
|
|
|
err.span_label(span, format!("use of borrowed `{}`", borrow_desc));
|
|
|
|
|
2017-11-19 23:35:53 +01:00
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_act_on_uninitialized_variable(self,
|
2017-07-03 17:25:03 +02:00
|
|
|
span: Span,
|
|
|
|
verb: &str,
|
|
|
|
desc: &str,
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-07-03 17:25:03 +02:00
|
|
|
{
|
2017-11-19 23:35:53 +01:00
|
|
|
let err = struct_span_err!(self, span, E0381,
|
|
|
|
"{} of possibly uninitialized variable: `{}`{OGN}",
|
|
|
|
verb, desc, OGN=o);
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_mutably_borrow_multiply(self,
|
2017-09-27 10:01:42 +03:00
|
|
|
new_loan_span: Span,
|
2017-07-03 17:25:03 +02:00
|
|
|
desc: &str,
|
|
|
|
opt_via: &str,
|
2017-09-27 10:01:42 +03:00
|
|
|
old_loan_span: Span,
|
|
|
|
old_opt_via: &str,
|
2017-10-25 14:17:17 -04:00
|
|
|
old_load_end_span: Option<Span>,
|
2017-07-03 17:25:03 +02:00
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-07-03 17:25:03 +02:00
|
|
|
{
|
2017-09-27 10:01:42 +03:00
|
|
|
let mut err = struct_span_err!(self, new_loan_span, E0499,
|
2017-07-03 17:25:03 +02:00
|
|
|
"cannot borrow `{}`{} as mutable more than once at a time{OGN}",
|
2017-09-27 10:01:42 +03:00
|
|
|
desc, opt_via, OGN=o);
|
|
|
|
if old_loan_span == new_loan_span {
|
|
|
|
// Both borrows are happening in the same place
|
|
|
|
// Meaning the borrow is occurring in a loop
|
|
|
|
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 {
|
|
|
|
err.span_label(old_loan_span,
|
|
|
|
format!("first mutable borrow occurs here{}", old_opt_via));
|
|
|
|
err.span_label(new_loan_span,
|
|
|
|
format!("second mutable borrow occurs here{}", 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, "first borrow ends here");
|
|
|
|
}
|
2017-09-27 10:01:42 +03:00
|
|
|
}
|
2017-11-19 23:35:53 +01:00
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_uniquely_borrow_by_two_closures(self,
|
2017-09-27 10:01:42 +03:00
|
|
|
new_loan_span: Span,
|
|
|
|
desc: &str,
|
|
|
|
old_loan_span: Span,
|
2017-10-25 14:17:17 -04:00
|
|
|
old_load_end_span: Option<Span>,
|
2017-09-27 10:01:42 +03:00
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-07-03 17:25:03 +02:00
|
|
|
{
|
2017-09-27 10:01:42 +03:00
|
|
|
let mut err = struct_span_err!(self, new_loan_span, E0524,
|
2017-07-03 17:25:03 +02:00
|
|
|
"two closures require unique access to `{}` at the same time{OGN}",
|
2017-09-27 10:01:42 +03:00
|
|
|
desc, OGN=o);
|
|
|
|
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 {
|
|
|
|
err.span_label(
|
|
|
|
old_load_end_span,
|
|
|
|
"borrow from first closure ends here");
|
|
|
|
}
|
2017-11-19 23:35:53 +01:00
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_uniquely_borrow_by_one_closure(self,
|
2017-09-27 10:01:42 +03:00
|
|
|
new_loan_span: Span,
|
2017-07-03 17:25:03 +02:00
|
|
|
desc_new: &str,
|
2017-09-27 10:01:42 +03:00
|
|
|
opt_via: &str,
|
|
|
|
old_loan_span: Span,
|
2017-07-03 17:25:03 +02:00
|
|
|
noun_old: &str,
|
2017-09-27 10:01:42 +03:00
|
|
|
old_opt_via: &str,
|
2017-10-25 14:17:17 -04:00
|
|
|
previous_end_span: Option<Span>,
|
2017-07-03 17:25:03 +02:00
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-07-03 17:25:03 +02:00
|
|
|
{
|
2017-09-27 10:01:42 +03:00
|
|
|
let mut err = struct_span_err!(self, new_loan_span, E0500,
|
2017-07-03 17:25:03 +02:00
|
|
|
"closure requires unique access to `{}` but {} is already borrowed{}{OGN}",
|
2017-09-27 10:01:42 +03:00
|
|
|
desc_new, noun_old, old_opt_via, OGN=o);
|
|
|
|
err.span_label(new_loan_span,
|
|
|
|
format!("closure construction occurs here{}", opt_via));
|
|
|
|
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");
|
|
|
|
}
|
2017-11-19 23:35:53 +01:00
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_reborrow_already_uniquely_borrowed(self,
|
2017-09-27 10:01:42 +03:00
|
|
|
new_loan_span: Span,
|
2017-07-03 17:25:03 +02:00
|
|
|
desc_new: &str,
|
2017-09-27 10:01:42 +03:00
|
|
|
opt_via: &str,
|
2017-07-03 17:25:03 +02:00
|
|
|
kind_new: &str,
|
2017-09-27 10:01:42 +03:00
|
|
|
old_loan_span: Span,
|
|
|
|
old_opt_via: &str,
|
2017-10-25 14:17:17 -04:00
|
|
|
previous_end_span: Option<Span>,
|
2017-07-03 17:25:03 +02:00
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-07-03 17:25:03 +02:00
|
|
|
{
|
2017-09-27 10:01:42 +03:00
|
|
|
let mut err = struct_span_err!(self, new_loan_span, E0501,
|
2017-07-03 17:25:03 +02:00
|
|
|
"cannot borrow `{}`{} as {} because previous closure \
|
|
|
|
requires unique access{OGN}",
|
2017-09-27 10:01:42 +03:00
|
|
|
desc_new, opt_via, kind_new, OGN=o);
|
|
|
|
err.span_label(new_loan_span,
|
|
|
|
format!("borrow occurs here{}", opt_via));
|
|
|
|
err.span_label(old_loan_span,
|
|
|
|
format!("closure construction 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 from closure ends here");
|
|
|
|
}
|
2017-11-19 23:35:53 +01:00
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_reborrow_already_borrowed(self,
|
2017-07-03 17:25:03 +02:00
|
|
|
span: Span,
|
|
|
|
desc_new: &str,
|
|
|
|
msg_new: &str,
|
|
|
|
kind_new: &str,
|
2017-09-27 10:01:42 +03:00
|
|
|
old_span: Span,
|
2017-07-03 17:25:03 +02:00
|
|
|
noun_old: &str,
|
|
|
|
kind_old: &str,
|
|
|
|
msg_old: &str,
|
2017-10-25 14:17:17 -04:00
|
|
|
old_load_end_span: Option<Span>,
|
2017-07-03 17:25:03 +02:00
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-07-03 17:25:03 +02:00
|
|
|
{
|
2017-09-27 10:01:42 +03:00
|
|
|
let mut err = struct_span_err!(self, span, E0502,
|
2017-07-03 17:25:03 +02:00
|
|
|
"cannot borrow `{}`{} as {} because {} is also borrowed as {}{}{OGN}",
|
2017-09-27 10:01:42 +03:00
|
|
|
desc_new, msg_new, kind_new, noun_old, kind_old, msg_old, OGN=o);
|
|
|
|
err.span_label(span, format!("{} borrow occurs here{}", kind_new, msg_new));
|
|
|
|
err.span_label(old_span, format!("{} borrow occurs here{}", kind_old, 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));
|
|
|
|
}
|
2017-11-19 23:35:53 +01:00
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_assign_to_borrowed(self, span: Span, borrow_span: Span, desc: &str, o: Origin)
|
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-07-03 17:25:03 +02:00
|
|
|
{
|
2017-09-28 16:44:23 +02:00
|
|
|
let mut err = struct_span_err!(self, span, E0506,
|
2017-07-03 17:25:03 +02:00
|
|
|
"cannot assign to `{}` because it is borrowed{OGN}",
|
2017-09-28 16:44:23 +02:00
|
|
|
desc, OGN=o);
|
|
|
|
|
|
|
|
err.span_label(borrow_span, format!("borrow of `{}` occurs here", desc));
|
|
|
|
err.span_label(span, format!("assignment to borrowed `{}` occurs here", desc));
|
|
|
|
|
2017-11-19 23:35:53 +01:00
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_move_into_closure(self, span: Span, desc: &str, o: Origin)
|
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-07-03 17:25:03 +02:00
|
|
|
{
|
2017-11-19 23:35:53 +01:00
|
|
|
let err = struct_span_err!(self, span, E0504,
|
|
|
|
"cannot move `{}` into closure because it is borrowed{OGN}",
|
|
|
|
desc, OGN=o);
|
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_reassign_immutable(self, span: Span, desc: &str, is_arg: bool, o: Origin)
|
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-07-03 17:25:03 +02:00
|
|
|
{
|
2018-01-09 19:10:45 -08:00
|
|
|
let msg = if is_arg {
|
|
|
|
"to immutable argument"
|
|
|
|
} else {
|
|
|
|
"twice to immutable variable"
|
|
|
|
};
|
2017-11-19 23:35:53 +01:00
|
|
|
let err = struct_span_err!(self, span, E0384,
|
2018-01-09 19:10:45 -08:00
|
|
|
"cannot assign {} `{}`{OGN}",
|
|
|
|
msg, desc, OGN=o);
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-04-06 20:00:21 +05:30
|
|
|
fn cannot_assign(self, span: Span, desc: &str, o: Origin, is_reference: bool)
|
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-09-28 19:14:37 +02:00
|
|
|
{
|
2018-03-10 20:24:59 +05:30
|
|
|
let msg = if is_reference {
|
|
|
|
"through"
|
|
|
|
} else {
|
|
|
|
"to"
|
|
|
|
};
|
|
|
|
|
2017-11-19 23:35:53 +01:00
|
|
|
let err = struct_span_err!(self, span, E0594,
|
2018-03-10 20:24:59 +05:30
|
|
|
"cannot assign {} {}{OGN}",
|
|
|
|
msg, desc, OGN=o);
|
2017-11-19 23:35:53 +01:00
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-09-28 19:14:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_assign_static(self, span: Span, desc: &str, o: Origin)
|
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-07-03 17:25:03 +02:00
|
|
|
{
|
2018-03-10 20:24:59 +05:30
|
|
|
self.cannot_assign(span, &format!("immutable static item `{}`", desc), o, false)
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
2017-10-03 15:34:52 +02:00
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_move_out_of(self, move_from_span: Span, move_from_desc: &str, o: Origin)
|
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-10-03 15:34:52 +02:00
|
|
|
{
|
|
|
|
let mut err = struct_span_err!(self, move_from_span, E0507,
|
|
|
|
"cannot move out of {}{OGN}",
|
|
|
|
move_from_desc, OGN=o);
|
|
|
|
err.span_label(
|
|
|
|
move_from_span,
|
|
|
|
format!("cannot move out of {}", move_from_desc));
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-10-03 15:34:52 +02:00
|
|
|
}
|
2017-10-03 15:54:12 +02:00
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_move_out_of_interior_noncopy(self,
|
2017-10-03 15:54:12 +02:00
|
|
|
move_from_span: Span,
|
|
|
|
ty: ty::Ty,
|
|
|
|
is_index: bool,
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-10-03 15:54:12 +02:00
|
|
|
{
|
|
|
|
let type_name = match (&ty.sty, is_index) {
|
|
|
|
(&ty::TyArray(_, _), true) => "array",
|
|
|
|
(&ty::TySlice(_), _) => "slice",
|
|
|
|
_ => span_bug!(move_from_span, "this path should not cause illegal move"),
|
|
|
|
};
|
|
|
|
let mut err = struct_span_err!(self, move_from_span, E0508,
|
|
|
|
"cannot move out of type `{}`, \
|
|
|
|
a non-copy {}{OGN}",
|
|
|
|
ty, type_name, OGN=o);
|
|
|
|
err.span_label(move_from_span, "cannot move out of here");
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-10-03 15:54:12 +02:00
|
|
|
}
|
2017-10-03 16:07:20 +02:00
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_move_out_of_interior_of_drop(self,
|
2017-10-03 16:07:20 +02:00
|
|
|
move_from_span: Span,
|
|
|
|
container_ty: ty::Ty,
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-10-03 16:07:20 +02:00
|
|
|
{
|
|
|
|
let mut err = struct_span_err!(self, move_from_span, E0509,
|
|
|
|
"cannot move out of type `{}`, \
|
|
|
|
which implements the `Drop` trait{OGN}",
|
|
|
|
container_ty, OGN=o);
|
|
|
|
err.span_label(move_from_span, "cannot move out of here");
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-10-03 16:07:20 +02:00
|
|
|
}
|
2017-10-09 17:44:56 +02:00
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_act_on_moved_value(self,
|
2017-10-09 17:44:56 +02:00
|
|
|
use_span: Span,
|
|
|
|
verb: &str,
|
|
|
|
optional_adverb_for_moved: &str,
|
2017-10-10 12:39:48 +02:00
|
|
|
moved_path: &str,
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-10-09 17:44:56 +02:00
|
|
|
{
|
|
|
|
let err = struct_span_err!(self, use_span, E0382,
|
2017-10-10 12:39:48 +02:00
|
|
|
"{} of {}moved value: `{}`{OGN}",
|
|
|
|
verb, optional_adverb_for_moved, moved_path, OGN=o);
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_partially_reinit_an_uninit_struct(self,
|
2017-10-09 17:44:56 +02:00
|
|
|
span: Span,
|
2017-10-10 12:39:48 +02:00
|
|
|
uninit_path: &str,
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-10-09 17:44:56 +02:00
|
|
|
{
|
|
|
|
let err = struct_span_err!(self,
|
|
|
|
span,
|
|
|
|
E0383,
|
2017-10-10 12:39:48 +02:00
|
|
|
"partial reinitialization of uninitialized structure `{}`{OGN}",
|
|
|
|
uninit_path, OGN=o);
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn closure_cannot_assign_to_borrowed(self,
|
2017-10-09 17:44:56 +02:00
|
|
|
span: Span,
|
2017-10-10 12:39:48 +02:00
|
|
|
descr: &str,
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-10-09 17:44:56 +02:00
|
|
|
{
|
2017-10-10 12:39:48 +02:00
|
|
|
let err = struct_span_err!(self, span, E0595, "closure cannot assign to {}{OGN}",
|
|
|
|
descr, OGN=o);
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_borrow_path_as_mutable(self,
|
2017-10-09 17:44:56 +02:00
|
|
|
span: Span,
|
2017-10-10 12:39:48 +02:00
|
|
|
path: &str,
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-10-09 17:44:56 +02:00
|
|
|
{
|
2017-10-10 12:39:48 +02:00
|
|
|
let err = struct_span_err!(self, span, E0596, "cannot borrow {} as mutable{OGN}",
|
|
|
|
path, OGN=o);
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_borrow_across_generator_yield(self,
|
2017-10-09 17:44:56 +02:00
|
|
|
span: Span,
|
2017-10-10 12:39:48 +02:00
|
|
|
yield_span: Span,
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-10-09 17:44:56 +02:00
|
|
|
{
|
|
|
|
let mut err = struct_span_err!(self,
|
|
|
|
span,
|
|
|
|
E0626,
|
2017-10-10 12:39:48 +02:00
|
|
|
"borrow may still be in use when generator yields{OGN}",
|
|
|
|
OGN=o);
|
2017-10-09 17:44:56 +02:00
|
|
|
err.span_label(yield_span, "possible yield occurs here");
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn path_does_not_live_long_enough(self,
|
2017-10-09 17:44:56 +02:00
|
|
|
span: Span,
|
2017-10-10 12:39:48 +02:00
|
|
|
path: &str,
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-10-09 17:44:56 +02:00
|
|
|
{
|
2017-10-10 12:39:48 +02:00
|
|
|
let err = struct_span_err!(self, span, E0597, "{} does not live long enough{OGN}",
|
|
|
|
path, OGN=o);
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn lifetime_too_short_for_reborrow(self,
|
2017-10-09 17:44:56 +02:00
|
|
|
span: Span,
|
2017-10-10 12:39:48 +02:00
|
|
|
path: &str,
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-10-09 17:44:56 +02:00
|
|
|
{
|
|
|
|
let err = struct_span_err!(self, span, E0598,
|
|
|
|
"lifetime of {} is too short to guarantee \
|
2017-10-10 12:39:48 +02:00
|
|
|
its contents can be safely reborrowed{OGN}",
|
|
|
|
path, OGN=o);
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_act_on_capture_in_sharable_fn(self,
|
2017-10-09 17:44:56 +02:00
|
|
|
span: Span,
|
|
|
|
bad_thing: &str,
|
2017-10-10 12:39:48 +02:00
|
|
|
help: (Span, &str),
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-10-09 17:44:56 +02:00
|
|
|
{
|
|
|
|
let (help_span, help_msg) = help;
|
|
|
|
let mut err = struct_span_err!(self, span, E0387,
|
2017-10-10 12:39:48 +02:00
|
|
|
"{} in a captured outer variable in an `Fn` closure{OGN}",
|
|
|
|
bad_thing, OGN=o);
|
2017-10-09 17:44:56 +02:00
|
|
|
err.span_help(help_span, help_msg);
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_assign_into_immutable_reference(self,
|
2017-10-09 17:44:56 +02:00
|
|
|
span: Span,
|
2017-10-10 12:39:48 +02:00
|
|
|
bad_thing: &str,
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-10-09 17:44:56 +02:00
|
|
|
{
|
2017-10-10 12:39:48 +02:00
|
|
|
let mut err = struct_span_err!(self, span, E0389, "{} in a `&` reference{OGN}",
|
|
|
|
bad_thing, OGN=o);
|
2017-10-09 17:44:56 +02:00
|
|
|
err.span_label(span, "assignment into an immutable reference");
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cannot_capture_in_long_lived_closure(self,
|
2017-10-09 17:44:56 +02:00
|
|
|
closure_span: Span,
|
|
|
|
borrowed_path: &str,
|
2017-10-10 12:39:48 +02:00
|
|
|
capture_span: Span,
|
|
|
|
o: Origin)
|
2018-03-08 16:29:35 +08:00
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-10-09 17:44:56 +02:00
|
|
|
{
|
|
|
|
let mut err = struct_span_err!(self, closure_span, E0373,
|
|
|
|
"closure may outlive the current function, \
|
|
|
|
but it borrows {}, \
|
2017-10-10 12:39:48 +02:00
|
|
|
which is owned by the current function{OGN}",
|
|
|
|
borrowed_path, OGN=o);
|
2017-10-09 17:44:56 +02:00
|
|
|
err.span_label(capture_span, format!("{} is borrowed here", borrowed_path))
|
|
|
|
.span_label(closure_span, format!("may outlive borrowed value {}", borrowed_path));
|
2017-11-19 23:35:53 +01:00
|
|
|
|
|
|
|
self.cancel_if_wrong_origin(err, o)
|
2017-10-09 17:44:56 +02:00
|
|
|
}
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
impl<'cx, 'gcx, 'tcx> BorrowckErrors<'cx> for TyCtxt<'cx, 'gcx, 'tcx> {
|
|
|
|
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
|
|
|
{
|
|
|
|
self.sess.struct_span_err_with_code(sp, msg, code)
|
|
|
|
}
|
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn struct_span_err<S: Into<MultiSpan>>(self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str)
|
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-07-03 17:25:03 +02:00
|
|
|
{
|
|
|
|
self.sess.struct_span_err(sp, msg)
|
|
|
|
}
|
2017-11-19 23:35:53 +01:00
|
|
|
|
2018-03-08 16:29:35 +08:00
|
|
|
fn cancel_if_wrong_origin(self,
|
|
|
|
mut diag: DiagnosticBuilder<'cx>,
|
|
|
|
o: Origin)
|
|
|
|
-> DiagnosticBuilder<'cx>
|
2017-11-19 23:35:53 +01:00
|
|
|
{
|
2018-02-14 16:11:02 +01:00
|
|
|
if !o.should_emit_errors(self.borrowck_mode()) {
|
2017-11-19 23:35:53 +01:00
|
|
|
self.sess.diagnostic().cancel(&mut diag);
|
|
|
|
}
|
|
|
|
diag
|
|
|
|
}
|
2017-07-03 17:25:03 +02:00
|
|
|
}
|