1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use rustc::ty::{self, TyCtxt};
use rustc_errors::DiagnosticBuilder;
use syntax_pos::{MultiSpan, Span};
use std::fmt;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Origin { Ast, Mir }
impl fmt::Display for Origin {
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
match *self {
Origin::Mir => write!(w, " (Mir)"),
Origin::Ast => ty::tls::with_opt(|opt_tcx| {
// If user passed `-Z borrowck-mir`, then include an
// AST origin as part of the error report
if let Some(tcx) = opt_tcx {
if tcx.sess.opts.debugging_opts.borrowck_mir {
return write!(w, " (Ast)");
}
}
// otherwise, do not include the origin (i.e., print
// nothing at all)
Ok(())
}),
}
}
}
pub trait BorrowckErrors {
fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
-> DiagnosticBuilder<'a>;
fn struct_span_err<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str)
-> DiagnosticBuilder<'a>;
fn cannot_move_when_borrowed(&self, span: Span, desc: &str, o: Origin)
-> DiagnosticBuilder
{
struct_span_err!(self, span, E0505,
"cannot move out of `{}` because it is borrowed{OGN}",
desc, OGN=o)
}
fn cannot_use_when_mutably_borrowed(&self,
span: Span,
desc: &str,
borrow_span: Span,
borrow_desc: &str,
o: Origin)
-> DiagnosticBuilder
{
let mut err = struct_span_err!(self, span, E0503,
"cannot use `{}` because it was mutably borrowed{OGN}",
desc, OGN=o);
err.span_label(borrow_span, format!("borrow of `{}` occurs here", borrow_desc));
err.span_label(span, format!("use of borrowed `{}`", borrow_desc));
err
}
fn cannot_act_on_uninitialized_variable(&self,
span: Span,
verb: &str,
desc: &str,
o: Origin)
-> DiagnosticBuilder
{
struct_span_err!(self, span, E0381,
"{} of possibly uninitialized variable: `{}`{OGN}",
verb, desc, OGN=o)
}
fn cannot_mutably_borrow_multiply(&self,
new_loan_span: Span,
desc: &str,
opt_via: &str,
old_loan_span: Span,
old_opt_via: &str,
old_load_end_span:Span,
o: Origin)
-> DiagnosticBuilder
{
let mut err = struct_span_err!(self, new_loan_span, E0499,
"cannot borrow `{}`{} as mutable more than once at a time{OGN}",
desc, opt_via, OGN=o);
if old_loan_span == new_loan_span {
// Both borrows are happening in the same place
// Meaning the borrow is occurring in a loop
err.span_label(new_loan_span,
format!("mutable borrow starts here in previous \
iteration of loop{}", opt_via));
err.span_label(old_load_end_span, "mutable borrow ends here");
} else {
err.span_label(old_loan_span,
format!("first mutable borrow occurs here{}", old_opt_via));
err.span_label(new_loan_span,
format!("second mutable borrow occurs here{}", opt_via));
err.span_label(old_load_end_span, "first borrow ends here");
}
err
}
fn cannot_uniquely_borrow_by_two_closures(&self,
new_loan_span: Span,
desc: &str,
old_loan_span: Span,
old_load_end_span: Span,
o: Origin)
-> DiagnosticBuilder
{
let mut err = struct_span_err!(self, new_loan_span, E0524,
"two closures require unique access to `{}` at the same time{OGN}",
desc, OGN=o);
err.span_label(
old_loan_span,
"first closure is constructed here");
err.span_label(
new_loan_span,
"second closure is constructed here");
err.span_label(
old_load_end_span,
"borrow from first closure ends here");
err
}
fn cannot_uniquely_borrow_by_one_closure(&self,
new_loan_span: Span,
desc_new: &str,
opt_via: &str,
old_loan_span: Span,
noun_old: &str,
old_opt_via: &str,
previous_end_span: Span,
o: Origin)
-> DiagnosticBuilder
{
let mut err = struct_span_err!(self, new_loan_span, E0500,
"closure requires unique access to `{}` but {} is already borrowed{}{OGN}",
desc_new, noun_old, old_opt_via, OGN=o);
err.span_label(new_loan_span,
format!("closure construction occurs here{}", opt_via));
err.span_label(old_loan_span,
format!("borrow occurs here{}", old_opt_via));
err.span_label(previous_end_span, "borrow ends here");
err
}
fn cannot_reborrow_already_uniquely_borrowed(&self,
new_loan_span: Span,
desc_new: &str,
opt_via: &str,
kind_new: &str,
old_loan_span: Span,
old_opt_via: &str,
previous_end_span: Span,
o: Origin)
-> DiagnosticBuilder
{
let mut err = struct_span_err!(self, new_loan_span, E0501,
"cannot borrow `{}`{} as {} because previous closure \
requires unique access{OGN}",
desc_new, opt_via, kind_new, OGN=o);
err.span_label(new_loan_span,
format!("borrow occurs here{}", opt_via));
err.span_label(old_loan_span,
format!("closure construction occurs here{}", old_opt_via));
err.span_label(previous_end_span, "borrow from closure ends here");
err
}
fn cannot_reborrow_already_borrowed(&self,
span: Span,
desc_new: &str,
msg_new: &str,
kind_new: &str,
old_span: Span,
noun_old: &str,
kind_old: &str,
msg_old: &str,
old_load_end_span: Span,
o: Origin)
-> DiagnosticBuilder
{
let mut err = struct_span_err!(self, span, E0502,
"cannot borrow `{}`{} as {} because {} is also borrowed as {}{}{OGN}",
desc_new, msg_new, kind_new, noun_old, kind_old, msg_old, OGN=o);
err.span_label(span, format!("{} borrow occurs here{}", kind_new, msg_new));
err.span_label(old_span, format!("{} borrow occurs here{}", kind_old, msg_old));
err.span_label(old_load_end_span, format!("{} borrow ends here", kind_old));
err
}
fn cannot_assign_to_borrowed(&self, span: Span, borrow_span: Span, desc: &str, o: Origin)
-> DiagnosticBuilder
{
let mut err = struct_span_err!(self, span, E0506,
"cannot assign to `{}` because it is borrowed{OGN}",
desc, OGN=o);
err.span_label(borrow_span, format!("borrow of `{}` occurs here", desc));
err.span_label(span, format!("assignment to borrowed `{}` occurs here", desc));
err
}
fn cannot_move_into_closure(&self, span: Span, desc: &str, o: Origin)
-> DiagnosticBuilder
{
struct_span_err!(self, span, E0504,
"cannot move `{}` into closure because it is borrowed{OGN}",
desc, OGN=o)
}
fn cannot_reassign_immutable(&self, span: Span, desc: &str, o: Origin)
-> DiagnosticBuilder
{
struct_span_err!(self, span, E0384,
"re-assignment of immutable variable `{}`{OGN}",
desc, OGN=o)
}
fn cannot_assign(&self, span: Span, desc: &str, o: Origin) -> DiagnosticBuilder
{
struct_span_err!(self, span, E0594,
"cannot assign to {}{OGN}",
desc, OGN=o)
}
fn cannot_assign_static(&self, span: Span, desc: &str, o: Origin)
-> DiagnosticBuilder
{
self.cannot_assign(span, &format!("immutable static item `{}`", desc), o)
}
fn cannot_move_out_of(&self, move_from_span: Span, move_from_desc: &str, o: Origin)
-> DiagnosticBuilder
{
let mut err = struct_span_err!(self, move_from_span, E0507,
"cannot move out of {}{OGN}",
move_from_desc, OGN=o);
err.span_label(
move_from_span,
format!("cannot move out of {}", move_from_desc));
err
}
fn cannot_move_out_of_interior_noncopy(&self,
move_from_span: Span,
ty: ty::Ty,
is_index: bool,
o: Origin)
-> DiagnosticBuilder
{
let type_name = match (&ty.sty, is_index) {
(&ty::TyArray(_, _), true) => "array",
(&ty::TySlice(_), _) => "slice",
_ => span_bug!(move_from_span, "this path should not cause illegal move"),
};
let mut err = struct_span_err!(self, move_from_span, E0508,
"cannot move out of type `{}`, \
a non-copy {}{OGN}",
ty, type_name, OGN=o);
err.span_label(move_from_span, "cannot move out of here");
err
}
fn cannot_move_out_of_interior_of_drop(&self,
move_from_span: Span,
container_ty: ty::Ty,
o: Origin)
-> DiagnosticBuilder
{
let mut err = struct_span_err!(self, move_from_span, E0509,
"cannot move out of type `{}`, \
which implements the `Drop` trait{OGN}",
container_ty, OGN=o);
err.span_label(move_from_span, "cannot move out of here");
err
}
}
impl<'b, 'tcx, 'gcx> BorrowckErrors for TyCtxt<'b, 'tcx, 'gcx> {
fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
sp: S,
msg: &str,
code: &str)
-> DiagnosticBuilder<'a>
{
self.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.sess.struct_span_err(sp, msg)
}
}
|