Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rustc: Convert borrowck errors to new diagnostic macros
  • Loading branch information
brson committed Jan 12, 2014
commit e92ecf64c4ac19c716163df3d5e2254dab9a784a
18 changes: 18 additions & 0 deletions src/librustc/diag_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@

reg_diag!(A0000)
reg_diag!(A0001)
reg_diag!(A0002)
reg_diag!(A0003)
reg_diag!(A0004)
reg_diag!(A0005)
reg_diag!(A0006)
reg_diag!(A0007)
reg_diag!(A0008)
reg_diag!(A0009)
reg_diag!(A0010)
reg_diag!(A0011)
reg_diag!(A0012)
reg_diag!(A0013)
reg_diag!(A0014)
reg_diag!(A0015)
reg_diag!(A0016)
reg_diag!(A0017)
reg_diag!(A0018)
reg_diag!(A0019)
18 changes: 18 additions & 0 deletions src/librustc/driver/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,27 @@ impl Session_ {
pub fn span_fatal(&self, sp: Span, msg: &str) -> ! {
self.span_diagnostic.span_fatal(sp, msg)
}
pub fn span_fatal_with_diagnostic_code(&self, sp: Span, code: &str, msg: &str) -> ! {
self.span_diagnostic.span_fatal_with_diagnostic_code(sp, code, msg)
}
pub fn fatal(&self, msg: &str) -> ! {
self.span_diagnostic.handler().fatal(msg)
}
pub fn fatal_with_diagnostic_code(&self, code: &str, msg: &str) -> ! {
self.span_diagnostic.handler().fatal_with_diagnostic_code(code, msg)
}
pub fn span_err(&self, sp: Span, msg: &str) {
self.span_diagnostic.span_err(sp, msg)
}
pub fn span_err_with_diagnostic_code(&self, sp: Span, code: &str, msg: &str) {
self.span_diagnostic.span_err_with_diagnostic_code(sp, code, msg)
}
pub fn err(&self, msg: &str) {
self.span_diagnostic.handler().err(msg)
}
pub fn err_with_diagnostic_code(&self, code: &str, msg: &str) {
self.span_diagnostic.handler().err_with_diagnostic_code(code, msg)
}
pub fn err_count(&self) -> uint {
self.span_diagnostic.handler().err_count()
}
Expand All @@ -246,9 +258,15 @@ impl Session_ {
pub fn span_warn(&self, sp: Span, msg: &str) {
self.span_diagnostic.span_warn(sp, msg)
}
pub fn span_warn_with_diagnostic_code(&self, sp: Span, code: &str, msg: &str) {
self.span_diagnostic.span_warn_with_diagnostic_code(sp, code, msg)
}
pub fn warn(&self, msg: &str) {
self.span_diagnostic.handler().warn(msg)
}
pub fn warn_with_diagnostic_code(&self, code: &str, msg: &str) {
self.span_diagnostic.handler().warn_with_diagnostic_code(code, msg)
}
pub fn span_note(&self, sp: Span, msg: &str) {
self.span_diagnostic.span_note(sp, msg)
}
Expand Down
48 changes: 21 additions & 27 deletions src/librustc/middle/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,10 @@ impl<'a> CheckLoanCtxt<'a> {

match (new_loan.mutbl, old_loan.mutbl) {
(MutableMutability, MutableMutability) => {
self.bccx.span_err(
new_loan.span,
format!("cannot borrow `{}` as mutable \
span_err!(self.bccx, new_loan.span, A0012,
"cannot borrow `{}` as mutable \
more than once at a time",
self.bccx.loan_path_to_str(new_loan.loan_path)));
self.bccx.loan_path_to_str(new_loan.loan_path));
self.bccx.span_note(
old_loan.span,
format!("previous borrow of `{}` as mutable occurs here",
Expand All @@ -239,13 +238,12 @@ impl<'a> CheckLoanCtxt<'a> {
}

_ => {
self.bccx.span_err(
new_loan.span,
format!("cannot borrow `{}` as {} because \
span_err!(self.bccx, new_loan.span, A0013,
"cannot borrow `{}` as {} because \
it is also borrowed as {}",
self.bccx.loan_path_to_str(new_loan.loan_path),
self.bccx.mut_to_str(new_loan.mutbl),
self.bccx.mut_to_str(old_loan.mutbl)));
self.bccx.loan_path_to_str(new_loan.loan_path),
self.bccx.mut_to_str(new_loan.mutbl),
self.bccx.mut_to_str(old_loan.mutbl));
self.bccx.span_note(
old_loan.span,
format!("previous borrow of `{}` occurs here",
Expand Down Expand Up @@ -332,11 +330,10 @@ impl<'a> CheckLoanCtxt<'a> {
}

// Otherwise, just a plain error.
self.bccx.span_err(
expr.span,
format!("cannot assign to {} {}",
cmt.mutbl.to_user_str(),
self.bccx.cmt_to_str(cmt)));
span_err!(self.bccx, expr.span, A0014,
"cannot assign to {} {}",
cmt.mutbl.to_user_str(),
self.bccx.cmt_to_str(cmt));
return;

fn mark_variable_as_used_mut(this: &CheckLoanCtxt,
Expand Down Expand Up @@ -602,10 +599,9 @@ impl<'a> CheckLoanCtxt<'a> {
expr: &ast::Expr,
loan_path: &LoanPath,
loan: &Loan) {
self.bccx.span_err(
expr.span,
format!("cannot assign to `{}` because it is borrowed",
self.bccx.loan_path_to_str(loan_path)));
span_err!(self.bccx, expr.span, A0015,
"cannot assign to `{}` because it is borrowed",
self.bccx.loan_path_to_str(loan_path));
self.bccx.span_note(
loan.span,
format!("borrow of `{}` occurs here",
Expand All @@ -630,11 +626,10 @@ impl<'a> CheckLoanCtxt<'a> {
match self.analyze_move_out_from(id, move_path) {
MoveOk => {}
MoveWhileBorrowed(loan_path, loan_span) => {
self.bccx.span_err(
span,
format!("cannot move out of `{}` \
span_err!(self.bccx, span, A0016,
"cannot move out of `{}` \
because it is borrowed",
self.bccx.loan_path_to_str(move_path)));
self.bccx.loan_path_to_str(move_path));
self.bccx.span_note(
loan_span,
format!("borrow of `{}` occurs here",
Expand Down Expand Up @@ -727,11 +722,10 @@ fn check_loans_in_fn<'a>(this: &mut CheckLoanCtxt<'a>,
match move_err {
MoveOk => {}
MoveWhileBorrowed(loan_path, loan_span) => {
this.bccx.span_err(
cap_var.span,
format!("cannot move `{}` into closure \
span_err!(this.bccx, cap_var.span, A0017,
"cannot move `{}` into closure \
because it is borrowed",
this.bccx.loan_path_to_str(move_path)));
this.bccx.loan_path_to_str(move_path));
this.bccx.span_note(
loan_span,
format!("borrow of `{}` occurs here",
Expand Down
14 changes: 6 additions & 8 deletions src/librustc/middle/borrowck/gather_loans/gather_moves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ fn check_is_legal_to_move_from(bccx: &BorrowckCtxt,
mc::cat_deref(_, _, mc::unsafe_ptr(..)) |
mc::cat_stack_upvar(..) |
mc::cat_copied_upvar(mc::CopiedUpvar { onceness: ast::Many, .. }) => {
bccx.span_err(
cmt0.span,
format!("cannot move out of {}",
bccx.cmt_to_str(cmt)));
span_err!(bccx, cmt0.span, A0018,
"cannot move out of {}",
bccx.cmt_to_str(cmt));
false
}

Expand Down Expand Up @@ -143,11 +142,10 @@ fn check_is_legal_to_move_from(bccx: &BorrowckCtxt,
match ty::get(b.ty).sty {
ty::ty_struct(did, _) | ty::ty_enum(did, _) => {
if ty::has_dtor(bccx.tcx, did) {
bccx.span_err(
cmt0.span,
format!("cannot move out of type `{}`, \
span_err!(bccx, cmt0.span, A0019,
"cannot move out of type `{}`, \
which defines the `Drop` trait",
b.ty.user_string(bccx.tcx)));
b.ty.user_string(bccx.tcx));
false
} else {
check_is_legal_to_move_from(bccx, cmt0, b)
Expand Down
81 changes: 39 additions & 42 deletions src/librustc/middle/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,7 @@ impl BorrowckCtxt {
}

pub fn report(&self, err: BckError) {
self.span_err(
err.span,
self.bckerr_to_str(err));
self.raise_bck_err(err);
self.note_and_explain_bckerr(err);
}

Expand All @@ -529,20 +527,18 @@ impl BorrowckCtxt {

match move.kind {
move_data::Declared => {
self.tcx.sess.span_err(
use_span,
format!("{} of possibly uninitialized value: `{}`",
verb,
self.loan_path_to_str(lp)));
span_err!(self.tcx.sess, use_span, A0006,
"{} of possibly uninitialized value: `{}`",
verb,
self.loan_path_to_str(lp));
}
_ => {
let partially = if lp == moved_lp {""} else {"partially "};
self.tcx.sess.span_err(
use_span,
format!("{} of {}moved value: `{}`",
verb,
partially,
self.loan_path_to_str(lp)));
span_err!(self.tcx.sess, use_span, A0007,
"{} of {}moved value: `{}`",
verb,
partially,
self.loan_path_to_str(lp));
}
}

Expand Down Expand Up @@ -616,53 +612,57 @@ impl BorrowckCtxt {
lp: &LoanPath,
assign:
&move_data::Assignment) {
self.tcx.sess.span_err(
span,
format!("re-assignment of immutable variable `{}`",
self.loan_path_to_str(lp)));
span_err!(self.tcx.sess, span, A0008,
"re-assignment of immutable variable `{}`",
self.loan_path_to_str(lp));
self.tcx.sess.span_note(
assign.span,
format!("prior assignment occurs here"));
}

pub fn span_err(&self, s: Span, m: &str) {
self.tcx.sess.span_err(s, m);
pub fn span_err_with_diagnostic_code(&self, s: Span, c: &str, m: &str) {
self.tcx.sess.span_err_with_diagnostic_code(s, c, m);
}

pub fn span_note(&self, s: Span, m: &str) {
self.tcx.sess.span_note(s, m);
}

pub fn bckerr_to_str(&self, err: BckError) -> ~str {
pub fn raise_bck_err(&self, err: BckError) {
match err.code {
err_mutbl(lk) => {
format!("cannot borrow {} {} as {}",
err.cmt.mutbl.to_user_str(),
self.cmt_to_str(err.cmt),
self.mut_to_str(lk))
span_err!(self.tcx.sess, err.span, A0001,
"cannot borrow {} {} as {}",
err.cmt.mutbl.to_user_str(),
self.cmt_to_str(err.cmt),
self.mut_to_str(lk));
}
err_out_of_root_scope(..) => {
format!("cannot root managed value long enough")
span_err!(self.tcx.sess, err.span, A0002,
"cannot root managed value long enough");
}
err_out_of_scope(..) => {
format!("borrowed value does not live long enough")
span_err!(self.tcx.sess, err.span, A0003,
"borrowed value does not live long enough");
}
err_freeze_aliasable_const => {
// Means that the user borrowed a ~T or enum value
// residing in &const or @const pointer. Terrible
// error message, but then &const and @const are
// supposed to be going away.
format!("unsafe borrow of aliasable, const value")
span_err!(self.tcx.sess, err.span, A0004,
"unsafe borrow of aliasable, const value");
}
err_borrowed_pointer_too_short(..) => {
let descr = match opt_loan_path(err.cmt) {
Some(lp) => format!("`{}`", self.loan_path_to_str(lp)),
None => self.cmt_to_str(err.cmt),
};

format!("lifetime of {} is too short to guarantee \
its contents can be safely reborrowed",
descr)
span_err!(self.tcx.sess, err.span, A0005,
"lifetime of {} is too short to guarantee \
its contents can be safely reborrowed",
descr);
}
}
}
Expand All @@ -678,21 +678,18 @@ impl BorrowckCtxt {

match cause {
mc::AliasableOther => {
self.tcx.sess.span_err(
span,
format!("{} in an aliasable location", prefix));
span_err!(self.tcx.sess, span, A0009,
"{} in an aliasable location", prefix);
}
mc::AliasableManaged => {
self.tcx.sess.span_err(span, format!("{} in a `@` pointer",
prefix))
span_err!(self.tcx.sess, span, A0010,
"{} in a `@` pointer", prefix);
}
mc::AliasableBorrowed(m) => {
self.tcx.sess.span_err(
span,
format!("{} in a `&{}` pointer; \
try an `&mut` instead",
prefix,
self.mut_to_keyword(m)));
span_err!(self.tcx.sess, span, A0011,
"{} in a `&{}` pointer; try an `&mut` instead",
prefix,
self.mut_to_keyword(m));
}
}
}
Expand Down