Skip to content

Commit

Permalink
Auto merge of #43108 - pnkfelix:mir-borrowck3c, r=arielb1
Browse files Browse the repository at this point in the history
MIR borrow check (under debug flag)

Here is the current state of MIR borrow check.

It consists of (1.) some refactoring, (2.) a dataflow analysis to identify the borrows themselves, and (3.) a mir "transform" that does the borrow check itself based on the aforementioned dataflow results.

(There's also a drive-by fix to dataflow that I can factor into a separate PR if necessary. Interestingly I could not find a way to observe the bug outside of MIR borrowck.)

To be clear, this branch is not ready to be used as the default borrow check. Thus the code is guarded: To get mir-borrowck to run, you need to either supply an attribute `#[rustc_mir_borrowck]` or a debug flag `-Z borrowck-mir`.

Here are the main issues with the current MIR borrowck as it stands in this PR:

 * No Notes emitted yet, just errors. (So the feedback is definitely inferior compared to AST borrowck today)
 * Lvalue rendering differs between Ast and Mir. (Mostly minor, but replacement of field names with indices is very bad; big priority for me to fix ASAP.)
 * Lots of ICEs (presumably because some MIR operations used here have well-formedness assumptions that are violated in borrowck-broken code)
 * Conflates lots of cases that are distinguished by AST-borrowck
 * Conflates "uninitialized" with "moved" (special case of previous bullet, one that I think should be fixed ASAP)

 (I am hoping to fix as many of the above issues as I can in the near term, but I also would like to land this even if they are *not* all fixed, because the rebasing effort is getting to be a real drag.)
  • Loading branch information
bors committed Aug 16, 2017
2 parents c886246 + 8738a08 commit 00a6797
Show file tree
Hide file tree
Showing 22 changed files with 2,940 additions and 1,098 deletions.
1 change: 1 addition & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"when debug-printing compiler state, do not include spans"), // o/w tests have closure@path
identify_regions: bool = (false, parse_bool, [UNTRACKED],
"make unnamed regions display as '# (where # is some non-ident unique id)"),
borrowck_mir: bool = (false, parse_bool, [UNTRACKED],
"implicitly treat functions as if they have `#[rustc_mir_borrowck]` attribute"),
time_passes: bool = (false, parse_bool, [UNTRACKED],
"measure time of each rustc pass"),
count_llvm_insns: bool = (false, parse_bool,
Expand Down
59 changes: 22 additions & 37 deletions src/librustc_borrowck/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use rustc::ty::{self, TyCtxt};
use syntax::ast;
use syntax_pos::Span;
use rustc::hir;
use rustc_mir::util::borrowck_errors::{BorrowckErrors, Origin};

use std::rc::Rc;

Expand Down Expand Up @@ -465,10 +466,8 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {

let mut err = match (new_loan.kind, old_loan.kind) {
(ty::MutBorrow, ty::MutBorrow) => {
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);
let mut err = self.bccx.cannot_mutably_borrow_multiply(
new_loan.span, &nl, &new_loan_msg, Origin::Ast);

if new_loan.span == old_loan.span {
// Both borrows are happening in the same place
Expand Down Expand Up @@ -496,10 +495,8 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
}

(ty::UniqueImmBorrow, ty::UniqueImmBorrow) => {
let mut err = struct_span_err!(self.bccx, new_loan.span, E0524,
"two closures require unique access to `{}` \
at the same time",
nl);
let mut err = self.bccx.cannot_uniquely_borrow_by_two_closures(
new_loan.span, &nl, Origin::Ast);
err.span_label(
old_loan.span,
"first closure is constructed here");
Expand All @@ -513,10 +510,8 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
}

(ty::UniqueImmBorrow, _) => {
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);
let mut err = self.bccx.cannot_uniquely_borrow_by_one_closure(
new_loan.span, &nl, &ol_pronoun, &old_loan_msg, Origin::Ast);
err.span_label(
new_loan.span,
format!("closure construction occurs here{}", new_loan_msg));
Expand All @@ -530,10 +525,9 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
}

(_, ty::UniqueImmBorrow) => {
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());
let new_loan_str = &new_loan.kind.to_user_str();
let mut err = self.bccx.cannot_reborrow_already_uniquely_borrowed(
new_loan.span, &nl, &new_loan_msg, new_loan_str, Origin::Ast);
err.span_label(
new_loan.span,
format!("borrow occurs here{}", new_loan_msg));
Expand All @@ -547,15 +541,10 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
}

(..) => {
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);
let mut err = self.bccx.cannot_reborrow_already_borrowed(
new_loan.span,
&nl, &new_loan_msg, &new_loan.kind.to_user_str(),
&ol_pronoun, &old_loan.kind.to_user_str(), &old_loan_msg, Origin::Ast);
err.span_label(
new_loan.span,
format!("{} borrow occurs here{}",
Expand Down Expand Up @@ -645,9 +634,8 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
match self.analyze_restrictions_on_use(id, copy_path, ty::ImmBorrow) {
UseOk => { }
UseWhileBorrowed(loan_path, loan_span) => {
struct_span_err!(self.bccx, span, E0503,
"cannot use `{}` because it was mutably borrowed",
&self.bccx.loan_path_to_string(copy_path))
let desc = self.bccx.loan_path_to_string(copy_path);
self.bccx.cannot_use_when_mutably_borrowed(span, &desc, Origin::Ast)
.span_label(loan_span,
format!("borrow of `{}` occurs here",
&self.bccx.loan_path_to_string(&loan_path))
Expand All @@ -673,9 +661,8 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
UseWhileBorrowed(loan_path, loan_span) => {
let mut err = match move_kind {
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));
let mut err = self.bccx.cannot_move_into_closure(
span, &self.bccx.loan_path_to_string(move_path), Origin::Ast);
err.span_label(
loan_span,
format!("borrow of `{}` occurs here",
Expand All @@ -690,9 +677,8 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
move_data::Declared |
move_data::MoveExpr |
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));
let desc = self.bccx.loan_path_to_string(move_path);
let mut err = self.bccx.cannot_move_when_borrowed(span, &desc, Origin::Ast);
err.span_label(
loan_span,
format!("borrow of `{}` occurs here",
Expand Down Expand Up @@ -874,9 +860,8 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
span: Span,
loan_path: &LoanPath<'tcx>,
loan: &Loan) {
struct_span_err!(self.bccx, span, E0506,
"cannot assign to `{}` because it is borrowed",
self.bccx.loan_path_to_string(loan_path))
self.bccx.cannot_assign_to_borrowed(
span, &self.bccx.loan_path_to_string(loan_path), Origin::Ast)
.span_label(loan.span,
format!("borrow of `{}` occurs here",
self.bccx.loan_path_to_string(loan_path)))
Expand Down
43 changes: 31 additions & 12 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ use rustc::middle::free_region::RegionRelations;
use rustc::ty::{self, TyCtxt};
use rustc::ty::maps::Providers;

use rustc_mir::util::borrowck_errors::{BorrowckErrors, Origin};

use std::fmt;
use std::rc::Rc;
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -218,6 +220,25 @@ pub struct BorrowckCtxt<'a, 'tcx: 'a> {
owner_def_id: DefId,
}

impl<'b, 'tcx: 'b> BorrowckErrors for BorrowckCtxt<'b, 'tcx> {
fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
-> DiagnosticBuilder<'a>
{
self.tcx.sess.struct_span_err_with_code(sp, msg, code)
}

fn struct_span_err<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str)
-> DiagnosticBuilder<'a>
{
self.tcx.sess.struct_span_err(sp, msg)
}
}

///////////////////////////////////////////////////////////////////////////
// Loans and loan paths

Expand Down Expand Up @@ -549,14 +570,13 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
move_data::Declared => {
// If this is an uninitialized variable, just emit a simple warning
// and return.
struct_span_err!(
self.tcx.sess, use_span, E0381,
"{} of possibly uninitialized variable: `{}`",
verb,
self.loan_path_to_string(lp))
.span_label(use_span, format!("use of possibly uninitialized `{}`",
self.loan_path_to_string(lp)))
.emit();
self.cannot_act_on_uninitialized_variable(use_span,
verb,
&self.loan_path_to_string(lp),
Origin::Ast)
.span_label(use_span, format!("use of possibly uninitialized `{}`",
self.loan_path_to_string(lp)))
.emit();
return;
}
_ => {
Expand Down Expand Up @@ -683,10 +703,9 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
lp: &LoanPath<'tcx>,
assign:
&move_data::Assignment) {
let mut err = struct_span_err!(
self.tcx.sess, span, E0384,
"re-assignment of immutable variable `{}`",
self.loan_path_to_string(lp));
let mut err = self.cannot_reassign_immutable(span,
&self.loan_path_to_string(lp),
Origin::Ast);
err.span_label(span, "re-assignment of immutable variable");
if span != assign.span {
err.span_label(assign.span, format!("first assignment to `{}`",
Expand Down
Loading

0 comments on commit 00a6797

Please sign in to comment.