Skip to content

Commit f1473b6

Browse files
committed
Rollup merge of rust-lang#28443 - GuillaumeGomez:error_codes, r=Manishearth
r? @Manishearth
2 parents d407903 + 1adcfb8 commit f1473b6

File tree

7 files changed

+133
-65
lines changed

7 files changed

+133
-65
lines changed

src/librustc_borrowck/borrowck/check_loans.rs

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -464,40 +464,36 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
464464

465465
match (new_loan.kind, old_loan.kind) {
466466
(ty::MutBorrow, ty::MutBorrow) => {
467-
self.bccx.span_err(
468-
new_loan.span,
469-
&format!("cannot borrow `{}`{} as mutable \
470-
more than once at a time",
471-
nl, new_loan_msg))
467+
span_err!(self.bccx, new_loan.span, E0499,
468+
"cannot borrow `{}`{} as mutable \
469+
more than once at a time",
470+
nl, new_loan_msg);
472471
}
473472

474473
(ty::UniqueImmBorrow, _) => {
475-
self.bccx.span_err(
476-
new_loan.span,
477-
&format!("closure requires unique access to `{}` \
478-
but {} is already borrowed{}",
479-
nl, ol_pronoun, old_loan_msg));
474+
span_err!(self.bccx, new_loan.span, E0500,
475+
"closure requires unique access to `{}` \
476+
but {} is already borrowed{}",
477+
nl, ol_pronoun, old_loan_msg);
480478
}
481479

482480
(_, ty::UniqueImmBorrow) => {
483-
self.bccx.span_err(
484-
new_loan.span,
485-
&format!("cannot borrow `{}`{} as {} because \
486-
previous closure requires unique access",
487-
nl, new_loan_msg, new_loan.kind.to_user_str()));
481+
span_err!(self.bccx, new_loan.span, E0501,
482+
"cannot borrow `{}`{} as {} because \
483+
previous closure requires unique access",
484+
nl, new_loan_msg, new_loan.kind.to_user_str());
488485
}
489486

490487
(_, _) => {
491-
self.bccx.span_err(
492-
new_loan.span,
493-
&format!("cannot borrow `{}`{} as {} because \
494-
{} is also borrowed as {}{}",
495-
nl,
496-
new_loan_msg,
497-
new_loan.kind.to_user_str(),
498-
ol_pronoun,
499-
old_loan.kind.to_user_str(),
500-
old_loan_msg));
488+
span_err!(self.bccx, new_loan.span, E0502,
489+
"cannot borrow `{}`{} as {} because \
490+
{} is also borrowed as {}{}",
491+
nl,
492+
new_loan_msg,
493+
new_loan.kind.to_user_str(),
494+
ol_pronoun,
495+
old_loan.kind.to_user_str(),
496+
old_loan_msg);
501497
}
502498
}
503499

@@ -617,11 +613,9 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
617613
match self.analyze_restrictions_on_use(id, copy_path, ty::ImmBorrow) {
618614
UseOk => { }
619615
UseWhileBorrowed(loan_path, loan_span) => {
620-
self.bccx.span_err(
621-
span,
622-
&format!("cannot use `{}` because it was mutably borrowed",
623-
&self.bccx.loan_path_to_string(copy_path))
624-
);
616+
span_err!(self.bccx, span, E0503,
617+
"cannot use `{}` because it was mutably borrowed",
618+
&self.bccx.loan_path_to_string(copy_path));
625619
self.bccx.span_note(
626620
loan_span,
627621
&format!("borrow of `{}` occurs here",
@@ -642,18 +636,19 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
642636
match self.analyze_restrictions_on_use(id, move_path, ty::MutBorrow) {
643637
UseOk => { }
644638
UseWhileBorrowed(loan_path, loan_span) => {
645-
let err_message = match move_kind {
639+
match move_kind {
646640
move_data::Captured =>
647-
format!("cannot move `{}` into closure because it is borrowed",
648-
&self.bccx.loan_path_to_string(move_path)),
641+
span_err!(self.bccx, span, E0504,
642+
"cannot move `{}` into closure because it is borrowed",
643+
&self.bccx.loan_path_to_string(move_path)),
649644
move_data::Declared |
650645
move_data::MoveExpr |
651646
move_data::MovePat =>
652-
format!("cannot move out of `{}` because it is borrowed",
653-
&self.bccx.loan_path_to_string(move_path))
647+
span_err!(self.bccx, span, E0505,
648+
"cannot move out of `{}` because it is borrowed",
649+
&self.bccx.loan_path_to_string(move_path))
654650
};
655651

656-
self.bccx.span_err(span, &err_message[..]);
657652
self.bccx.span_note(
658653
loan_span,
659654
&format!("borrow of `{}` occurs here",
@@ -820,10 +815,9 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
820815
span: Span,
821816
loan_path: &LoanPath<'tcx>,
822817
loan: &Loan) {
823-
self.bccx.span_err(
824-
span,
825-
&format!("cannot assign to `{}` because it is borrowed",
826-
self.bccx.loan_path_to_string(loan_path)));
818+
span_err!(self.bccx, span, E0506,
819+
"cannot assign to `{}` because it is borrowed",
820+
self.bccx.loan_path_to_string(loan_path));
827821
self.bccx.span_note(
828822
loan.span,
829823
&format!("borrow of `{}` occurs here",

src/librustc_borrowck/borrowck/gather_loans/move_error.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,18 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
119119
mc::cat_deref(_, _, mc::Implicit(..)) |
120120
mc::cat_deref(_, _, mc::UnsafePtr(..)) |
121121
mc::cat_static_item => {
122-
bccx.span_err(move_from.span,
123-
&format!("cannot move out of {}",
124-
move_from.descriptive_string(bccx.tcx)));
122+
span_err!(bccx, move_from.span, E0507,
123+
"cannot move out of {}",
124+
move_from.descriptive_string(bccx.tcx));
125125
}
126126

127127
mc::cat_interior(ref b, mc::InteriorElement(Kind::Index, _)) => {
128128
let expr = bccx.tcx.map.expect_expr(move_from.id);
129129
if let hir::ExprIndex(..) = expr.node {
130-
bccx.span_err(move_from.span,
131-
&format!("cannot move out of type `{}`, \
132-
a non-copy fixed-size array",
133-
b.ty));
130+
span_err!(bccx, move_from.span, E0508,
131+
"cannot move out of type `{}`, \
132+
a non-copy fixed-size array",
133+
b.ty);
134134
}
135135
}
136136

@@ -139,11 +139,10 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
139139
match b.ty.sty {
140140
ty::TyStruct(def, _) |
141141
ty::TyEnum(def, _) if def.has_dtor() => {
142-
bccx.span_err(
143-
move_from.span,
144-
&format!("cannot move out of type `{}`, \
145-
which defines the `Drop` trait",
146-
b.ty));
142+
span_err!(bccx, move_from.span, E0509,
143+
"cannot move out of type `{}`, \
144+
which defines the `Drop` trait",
145+
b.ty);
147146
},
148147
_ => {
149148
bccx.span_bug(move_from.span, "this path should not cause illegal move")

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,10 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
803803
self.tcx.sess.span_err(s, m);
804804
}
805805

806+
pub fn span_err_with_code(&self, s: Span, msg: &str, code: &str) {
807+
self.tcx.sess.span_err_with_code(s, msg, code);
808+
}
809+
806810
pub fn span_bug(&self, s: Span, m: &str) {
807811
self.tcx.sess.span_bug(s, m);
808812
}

src/librustc_borrowck/diagnostics.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,50 @@ fn mutable() {
263263
You can read more about cell types in the API documentation:
264264
265265
https://doc.rust-lang.org/std/cell/
266-
"##
266+
"##,
267+
268+
E0499: r##"
269+
A variable was borrowed as mutable more than once. Erroneous code example:
270+
271+
```
272+
let mut i = 0;
273+
let mut x = &mut i;
274+
let mut a = &mut i;
275+
// error: cannot borrow `i` as mutable more than once at a time
276+
```
277+
278+
Please note that in rust, you can either have many immutable references, or one
279+
mutable reference. Take a look at
280+
https://doc.rust-lang.org/stable/book/references-and-borrowing.html for more
281+
information. Example:
282+
283+
284+
```
285+
let mut i = 0;
286+
let mut x = &mut i; // ok!
287+
288+
// or:
289+
let mut i = 0;
290+
let a = &i; // ok!
291+
let b = &i; // still ok!
292+
let c = &i; // still ok!
293+
```
294+
"##,
267295

268296
}
269297

270298
register_diagnostics! {
271299
E0385, // {} in an aliasable location
272300
E0388, // {} in a static location
273-
E0389 // {} in a `&` reference
301+
E0389, // {} in a `&` reference
302+
E0500, // closure requires unique access to `..` but .. is already borrowed
303+
E0501, // cannot borrow `..`.. as .. because previous closure requires unique access
304+
E0502, // cannot borrow `..`.. as .. because .. is also borrowed as ...
305+
E0503, // cannot use `..` because it was mutably borrowed
306+
E0504, // cannot move `..` into closure because it is borrowed
307+
E0505, // cannot move out of `..` because it is borrowed
308+
E0506, // cannot assign to `..` because it is borrowed
309+
E0507, // cannot move out of ..
310+
E0508, // cannot move out of type `..`, a non-copy fixed-size array
311+
E0509, // cannot move out of type `..`, which defines the `Drop` trait
274312
}

src/librustc_trans/diagnostics.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(non_snake_case)]
12+
13+
register_long_diagnostics! {
14+
15+
}
16+
17+
register_diagnostics! {
18+
E0510, // invalid use of `return_address` intrinsic: function does not use out pointer
19+
E0511, // invalid monomorphization of `{}` intrinsic
20+
E0512, // transmute called on types with potentially different sizes...
21+
}

src/librustc_trans/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ pub mod back {
8080
pub mod msvc;
8181
}
8282

83+
pub mod diagnostics;
84+
8385
pub mod trans;
8486
pub mod save;
8587

src/librustc_trans/trans/intrinsic.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ use syntax::ast;
4444
use syntax::ptr::P;
4545
use syntax::parse::token;
4646

47+
use rustc::session::Session;
48+
use syntax::codemap::Span;
49+
4750
use std::cmp::Ordering;
4851

4952
pub fn get_simple_intrinsic(ccx: &CrateContext, item: &hir::ForeignItem) -> Option<ValueRef> {
@@ -99,6 +102,10 @@ pub fn get_simple_intrinsic(ccx: &CrateContext, item: &hir::ForeignItem) -> Opti
99102
Some(ccx.get_intrinsic(&name))
100103
}
101104

105+
pub fn span_transmute_size_error(a: &Session, b: Span, msg: &str) {
106+
span_err!(a, b, E0512, "{}", msg);
107+
}
108+
102109
/// Performs late verification that intrinsics are used correctly. At present,
103110
/// the only intrinsic that needs such verification is `transmute`.
104111
pub fn check_intrinsics(ccx: &CrateContext) {
@@ -127,8 +134,7 @@ pub fn check_intrinsics(ccx: &CrateContext) {
127134
last_failing_id = Some(transmute_restriction.id);
128135

129136
if transmute_restriction.original_from != transmute_restriction.substituted_from {
130-
ccx.sess().span_err(
131-
transmute_restriction.span,
137+
span_transmute_size_error(ccx.sess(), transmute_restriction.span,
132138
&format!("transmute called on types with potentially different sizes: \
133139
{} (could be {} bit{}) to {} (could be {} bit{})",
134140
transmute_restriction.original_from,
@@ -138,8 +144,7 @@ pub fn check_intrinsics(ccx: &CrateContext) {
138144
to_type_size as usize,
139145
if to_type_size == 1 {""} else {"s"}));
140146
} else {
141-
ccx.sess().span_err(
142-
transmute_restriction.span,
147+
span_transmute_size_error(ccx.sess(), transmute_restriction.span,
143148
&format!("transmute called on types with different sizes: \
144149
{} ({} bit{}) to {} ({} bit{})",
145150
transmute_restriction.original_from,
@@ -798,9 +803,9 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
798803

799804
(_, "return_address") => {
800805
if !fcx.caller_expects_out_pointer {
801-
tcx.sess.span_err(call_info.span,
802-
"invalid use of `return_address` intrinsic: function \
803-
does not use out pointer");
806+
span_err!(tcx.sess, call_info.span, E0510,
807+
"invalid use of `return_address` intrinsic: function \
808+
does not use out pointer");
804809
C_null(Type::i8p(ccx))
805810
} else {
806811
PointerCast(bcx, llvm::get_param(fcx.llfn, 0), Type::i8p(ccx))
@@ -1439,6 +1444,10 @@ fn get_rust_try_fn<'a, 'tcx>(fcx: &FunctionContext<'a, 'tcx>,
14391444
return rust_try
14401445
}
14411446

1447+
fn span_invalid_monomorphization_error(a: &Session, b: Span, c: &str) {
1448+
span_err!(a, b, E0511, "{}", c);
1449+
}
1450+
14421451
fn generic_simd_intrinsic<'blk, 'tcx, 'a>
14431452
(bcx: Block<'blk, 'tcx>,
14441453
name: &str,
@@ -1457,10 +1466,11 @@ fn generic_simd_intrinsic<'blk, 'tcx, 'a>
14571466
emit_error!($msg, )
14581467
};
14591468
($msg: tt, $($fmt: tt)*) => {
1460-
bcx.sess().span_err(call_info.span,
1461-
&format!(concat!("invalid monomorphization of `{}` intrinsic: ",
1462-
$msg),
1463-
name, $($fmt)*));
1469+
span_invalid_monomorphization_error(
1470+
bcx.sess(), call_info.span,
1471+
&format!(concat!("invalid monomorphization of `{}` intrinsic: ",
1472+
$msg),
1473+
name, $($fmt)*));
14641474
}
14651475
}
14661476
macro_rules! require {

0 commit comments

Comments
 (0)