]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/util/borrowck_errors.rs
New upstream version 1.21.0+dfsg1
[rustc.git] / src / librustc_mir / util / borrowck_errors.rs
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.
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 use rustc::ty::{self, TyCtxt};
12 use rustc_errors::DiagnosticBuilder;
13 use syntax_pos::{MultiSpan, Span};
14
15 use std::fmt;
16
17 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
18 pub enum Origin { Ast, Mir }
19
20 impl fmt::Display for Origin {
21 fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
22 match *self {
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)");
30 }
31 }
32 // otherwise, do not include the origin (i.e., print
33 // nothing at all)
34 Ok(())
35 }),
36 }
37 }
38 }
39
40 pub trait BorrowckErrors {
41 fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
42 sp: S,
43 msg: &str,
44 code: &str)
45 -> DiagnosticBuilder<'a>;
46
47 fn struct_span_err<'a, S: Into<MultiSpan>>(&'a self,
48 sp: S,
49 msg: &str)
50 -> DiagnosticBuilder<'a>;
51
52 fn cannot_move_when_borrowed(&self, span: Span, desc: &str, o: Origin)
53 -> DiagnosticBuilder
54 {
55 struct_span_err!(self, span, E0505,
56 "cannot move out of `{}` because it is borrowed{OGN}",
57 desc, OGN=o)
58 }
59
60 fn cannot_use_when_mutably_borrowed(&self, span: Span, desc: &str, o: Origin)
61 -> DiagnosticBuilder
62 {
63 struct_span_err!(self, span, E0503,
64 "cannot use `{}` because it was mutably borrowed{OGN}",
65 desc, OGN=o)
66 }
67
68 fn cannot_act_on_uninitialized_variable(&self,
69 span: Span,
70 verb: &str,
71 desc: &str,
72 o: Origin)
73 -> DiagnosticBuilder
74 {
75 struct_span_err!(self, span, E0381,
76 "{} of possibly uninitialized variable: `{}`{OGN}",
77 verb, desc, OGN=o)
78 }
79
80 fn cannot_mutably_borrow_multiply(&self,
81 span: Span,
82 desc: &str,
83 opt_via: &str,
84 o: Origin)
85 -> DiagnosticBuilder
86 {
87 struct_span_err!(self, span, E0499,
88 "cannot borrow `{}`{} as mutable more than once at a time{OGN}",
89 desc, opt_via, OGN=o)
90 }
91
92 fn cannot_uniquely_borrow_by_two_closures(&self, span: Span, desc: &str, o: Origin)
93 -> DiagnosticBuilder
94 {
95 struct_span_err!(self, span, E0524,
96 "two closures require unique access to `{}` at the same time{OGN}",
97 desc, OGN=o)
98 }
99
100 fn cannot_uniquely_borrow_by_one_closure(&self,
101 span: Span,
102 desc_new: &str,
103 noun_old: &str,
104 msg_old: &str,
105 o: Origin)
106 -> DiagnosticBuilder
107 {
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)
111 }
112
113 fn cannot_reborrow_already_uniquely_borrowed(&self,
114 span: Span,
115 desc_new: &str,
116 msg_new: &str,
117 kind_new: &str,
118 o: Origin)
119 -> DiagnosticBuilder
120 {
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)
125 }
126
127 fn cannot_reborrow_already_borrowed(&self,
128 span: Span,
129 desc_new: &str,
130 msg_new: &str,
131 kind_new: &str,
132 noun_old: &str,
133 kind_old: &str,
134 msg_old: &str,
135 o: Origin)
136 -> DiagnosticBuilder
137 {
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)
141 }
142
143 fn cannot_assign_to_borrowed(&self, span: Span, desc: &str, o: Origin)
144 -> DiagnosticBuilder
145 {
146 struct_span_err!(self, span, E0506,
147 "cannot assign to `{}` because it is borrowed{OGN}",
148 desc, OGN=o)
149 }
150
151 fn cannot_move_into_closure(&self, span: Span, desc: &str, o: Origin)
152 -> DiagnosticBuilder
153 {
154 struct_span_err!(self, span, E0504,
155 "cannot move `{}` into closure because it is borrowed{OGN}",
156 desc, OGN=o)
157 }
158
159 fn cannot_reassign_immutable(&self, span: Span, desc: &str, o: Origin)
160 -> DiagnosticBuilder
161 {
162 struct_span_err!(self, span, E0384,
163 "re-assignment of immutable variable `{}`{OGN}",
164 desc, OGN=o)
165 }
166
167 fn cannot_assign_static(&self, span: Span, desc: &str, o: Origin)
168 -> DiagnosticBuilder
169 {
170 self.struct_span_err(span, &format!("cannot assign to immutable static item {}{OGN}",
171 desc, OGN=o))
172 }
173 }
174
175 impl<'b, 'tcx, 'gcx> BorrowckErrors for TyCtxt<'b, 'tcx, 'gcx> {
176 fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
177 sp: S,
178 msg: &str,
179 code: &str)
180 -> DiagnosticBuilder<'a>
181 {
182 self.sess.struct_span_err_with_code(sp, msg, code)
183 }
184
185 fn struct_span_err<'a, S: Into<MultiSpan>>(&'a self,
186 sp: S,
187 msg: &str)
188 -> DiagnosticBuilder<'a>
189 {
190 self.sess.struct_span_err(sp, msg)
191 }
192 }