1 // Copyright 2012-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.
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.
11 use rustc
::ty
::{self, TyCtxt}
;
12 use rustc_errors
::DiagnosticBuilder
;
13 use syntax_pos
::{MultiSpan, Span}
;
17 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
18 pub enum Origin { Ast, Mir }
20 impl fmt
::Display
for Origin
{
21 fn fmt(&self, w
: &mut fmt
::Formatter
) -> fmt
::Result
{
23 Origin
::Mir
=> write
!(w
, " (Mir)"),
24 Origin
::Ast
=> ty
::tls
::with_opt(|opt_tcx
| {
25 // If user passed `-Z borrowck-mir`, then include an
26 // AST origin as part of the error report
27 if let Some(tcx
) = opt_tcx
{
28 if tcx
.sess
.opts
.debugging_opts
.borrowck_mir
{
29 return write
!(w
, " (Ast)");
32 // otherwise, do not include the origin (i.e., print
40 pub trait BorrowckErrors
{
41 fn struct_span_err_with_code
<'a
, S
: Into
<MultiSpan
>>(&'a
self,
45 -> DiagnosticBuilder
<'a
>;
47 fn struct_span_err
<'a
, S
: Into
<MultiSpan
>>(&'a
self,
50 -> DiagnosticBuilder
<'a
>;
52 fn cannot_move_when_borrowed(&self, span
: Span
, desc
: &str, o
: Origin
)
55 struct_span_err
!(self, span
, E0505
,
56 "cannot move out of `{}` because it is borrowed{OGN}",
60 fn cannot_use_when_mutably_borrowed(&self, span
: Span
, desc
: &str, o
: Origin
)
63 struct_span_err
!(self, span
, E0503
,
64 "cannot use `{}` because it was mutably borrowed{OGN}",
68 fn cannot_act_on_uninitialized_variable(&self,
75 struct_span_err
!(self, span
, E0381
,
76 "{} of possibly uninitialized variable: `{}`{OGN}",
80 fn cannot_mutably_borrow_multiply(&self,
87 struct_span_err
!(self, span
, E0499
,
88 "cannot borrow `{}`{} as mutable more than once at a time{OGN}",
92 fn cannot_uniquely_borrow_by_two_closures(&self, span
: Span
, desc
: &str, o
: Origin
)
95 struct_span_err
!(self, span
, E0524
,
96 "two closures require unique access to `{}` at the same time{OGN}",
100 fn cannot_uniquely_borrow_by_one_closure(&self,
108 struct_span_err
!(self, span
, E0500
,
109 "closure requires unique access to `{}` but {} is already borrowed{}{OGN}",
110 desc_new
, noun_old
, msg_old
, OGN
=o
)
113 fn cannot_reborrow_already_uniquely_borrowed(&self,
121 struct_span_err
!(self, span
, E0501
,
122 "cannot borrow `{}`{} as {} because previous closure \
123 requires unique access{OGN}",
124 desc_new
, msg_new
, kind_new
, OGN
=o
)
127 fn cannot_reborrow_already_borrowed(&self,
138 struct_span_err
!(self, span
, E0502
,
139 "cannot borrow `{}`{} as {} because {} is also borrowed as {}{}{OGN}",
140 desc_new
, msg_new
, kind_new
, noun_old
, kind_old
, msg_old
, OGN
=o
)
143 fn cannot_assign_to_borrowed(&self, span
: Span
, desc
: &str, o
: Origin
)
146 struct_span_err
!(self, span
, E0506
,
147 "cannot assign to `{}` because it is borrowed{OGN}",
151 fn cannot_move_into_closure(&self, span
: Span
, desc
: &str, o
: Origin
)
154 struct_span_err
!(self, span
, E0504
,
155 "cannot move `{}` into closure because it is borrowed{OGN}",
159 fn cannot_reassign_immutable(&self, span
: Span
, desc
: &str, o
: Origin
)
162 struct_span_err
!(self, span
, E0384
,
163 "re-assignment of immutable variable `{}`{OGN}",
167 fn cannot_assign_static(&self, span
: Span
, desc
: &str, o
: Origin
)
170 self.struct_span_err(span
, &format
!("cannot assign to immutable static item {}{OGN}",
175 impl<'b
, 'tcx
, 'gcx
> BorrowckErrors
for TyCtxt
<'b
, 'tcx
, 'gcx
> {
176 fn struct_span_err_with_code
<'a
, S
: Into
<MultiSpan
>>(&'a
self,
180 -> DiagnosticBuilder
<'a
>
182 self.sess
.struct_span_err_with_code(sp
, msg
, code
)
185 fn struct_span_err
<'a
, S
: Into
<MultiSpan
>>(&'a
self,
188 -> DiagnosticBuilder
<'a
>
190 self.sess
.struct_span_err(sp
, msg
)