forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_diagnostics.rs
More file actions
624 lines (598 loc) · 16.1 KB
/
session_diagnostics.rs
File metadata and controls
624 lines (598 loc) · 16.1 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use rustc_errors::codes::*;
use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level, MultiSpan};
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_middle::ty::{GenericArg, Ty};
use rustc_span::Span;
use crate::diagnostics::RegionName;
#[derive(Diagnostic)]
#[diag("cannot move a value of type `{$ty}`", code = E0161)]
pub(crate) struct MoveUnsized<'tcx> {
pub ty: Ty<'tcx>,
#[primary_span]
#[label("the size of `{$ty}` cannot be statically determined")]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("higher-ranked lifetime error")]
pub(crate) struct HigherRankedLifetimeError {
#[subdiagnostic]
pub cause: Option<HigherRankedErrorCause>,
#[primary_span]
pub span: Span,
}
#[derive(Subdiagnostic)]
pub(crate) enum HigherRankedErrorCause {
#[note("could not prove `{$predicate}`")]
CouldNotProve { predicate: String },
#[note("could not normalize `{$value}`")]
CouldNotNormalize { value: String },
}
#[derive(Diagnostic)]
#[diag("higher-ranked subtype error")]
pub(crate) struct HigherRankedSubtypeError {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("`{$kind}` does not live long enough")]
pub(crate) struct GenericDoesNotLiveLongEnough {
pub kind: String,
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("variable does not need to be mutable")]
pub(crate) struct VarNeedNotMut {
#[suggestion(
"remove this `mut`",
style = "short",
applicability = "machine-applicable",
code = ""
)]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("captured variable cannot escape `FnMut` closure body")]
#[note("`FnMut` closures only have access to their captured variables while they are executing...")]
#[note("...therefore, they cannot allow references to captured variables to escape")]
pub(crate) struct FnMutError {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub ty_err: FnMutReturnTypeErr,
}
#[derive(Subdiagnostic)]
pub(crate) enum VarHereDenote {
#[label("variable captured here")]
Captured {
#[primary_span]
span: Span,
},
#[label("variable defined here")]
Defined {
#[primary_span]
span: Span,
},
#[label("inferred to be a `FnMut` closure")]
FnMutInferred {
#[primary_span]
span: Span,
},
}
#[derive(Subdiagnostic)]
pub(crate) enum FnMutReturnTypeErr {
#[label(
"returns a closure that contains a reference to a captured variable, which then escapes the closure body"
)]
ReturnClosure {
#[primary_span]
span: Span,
},
#[label(
"returns an `async` block that contains a reference to a captured variable, which then escapes the closure body"
)]
ReturnAsyncBlock {
#[primary_span]
span: Span,
},
#[label("returns a reference to a captured variable which escapes the closure body")]
ReturnRef {
#[primary_span]
span: Span,
},
}
#[derive(Diagnostic)]
#[diag("lifetime may not live long enough")]
pub(crate) struct LifetimeOutliveErr {
#[primary_span]
pub span: Span,
}
#[derive(Subdiagnostic)]
pub(crate) enum LifetimeReturnCategoryErr<'a> {
#[label(
"{$mir_def_name} was supposed to return data with lifetime `{$outlived_fr_name}` but it is returning data with lifetime `{$fr_name}`"
)]
WrongReturn {
#[primary_span]
span: Span,
mir_def_name: &'a str,
outlived_fr_name: RegionName,
fr_name: &'a RegionName,
},
#[label(
"{$category_desc}requires that `{$free_region_name}` must outlive `{$outlived_fr_name}`"
)]
ShortReturn {
#[primary_span]
span: Span,
category_desc: &'static str,
free_region_name: &'a RegionName,
outlived_fr_name: RegionName,
},
}
#[derive(Subdiagnostic)]
pub(crate) enum RequireStaticErr {
#[note("the used `impl` has a `'static` requirement")]
UsedImpl {
#[primary_span]
multi_span: MultiSpan,
},
}
#[derive(Subdiagnostic)]
pub(crate) enum CaptureVarPathUseCause {
#[label("borrow occurs due to use in coroutine")]
BorrowInCoroutine {
#[primary_span]
path_span: Span,
},
#[label("use occurs due to use in coroutine")]
UseInCoroutine {
#[primary_span]
path_span: Span,
},
#[label("assign occurs due to use in coroutine")]
AssignInCoroutine {
#[primary_span]
path_span: Span,
},
#[label("assign to part occurs due to use in coroutine")]
AssignPartInCoroutine {
#[primary_span]
path_span: Span,
},
#[label("borrow occurs due to use in closure")]
BorrowInClosure {
#[primary_span]
path_span: Span,
},
#[label("use occurs due to use in closure")]
UseInClosure {
#[primary_span]
path_span: Span,
},
#[label("assignment occurs due to use in closure")]
AssignInClosure {
#[primary_span]
path_span: Span,
},
#[label("assignment to part occurs due to use in closure")]
AssignPartInClosure {
#[primary_span]
path_span: Span,
},
}
#[derive(Subdiagnostic)]
pub(crate) enum CaptureVarKind {
#[label("capture is immutable because of use here")]
Immut {
#[primary_span]
kind_span: Span,
},
#[label("capture is mutable because of use here")]
Mut {
#[primary_span]
kind_span: Span,
},
#[label("capture is moved because of use here")]
Move {
#[primary_span]
kind_span: Span,
},
}
#[derive(Subdiagnostic)]
pub(crate) enum CaptureVarCause {
#[label(
"{$is_single_var ->
*[true] borrow occurs
[false] borrows occur
} due to use of {$place} in coroutine"
)]
BorrowUsePlaceCoroutine {
is_single_var: bool,
place: String,
#[primary_span]
var_span: Span,
},
#[label(
"{$is_single_var ->
*[true] borrow occurs
[false] borrows occur
} due to use of {$place} in closure"
)]
BorrowUsePlaceClosure {
is_single_var: bool,
place: String,
#[primary_span]
var_span: Span,
},
#[label("borrow occurs due to use in coroutine")]
BorrowUseInCoroutine {
#[primary_span]
var_span: Span,
},
#[label("borrow occurs due to use in closure")]
BorrowUseInClosure {
#[primary_span]
var_span: Span,
},
#[label("move occurs due to use in coroutine")]
MoveUseInCoroutine {
#[primary_span]
var_span: Span,
},
#[label("move occurs due to use in closure")]
MoveUseInClosure {
#[primary_span]
var_span: Span,
},
#[label("first borrow occurs due to use of {$place} in coroutine")]
FirstBorrowUsePlaceCoroutine {
place: String,
#[primary_span]
var_span: Span,
},
#[label("first borrow occurs due to use of {$place} in closure")]
FirstBorrowUsePlaceClosure {
place: String,
#[primary_span]
var_span: Span,
},
#[label("second borrow occurs due to use of {$place} in coroutine")]
SecondBorrowUsePlaceCoroutine {
place: String,
#[primary_span]
var_span: Span,
},
#[label("second borrow occurs due to use of {$place} in closure")]
SecondBorrowUsePlaceClosure {
place: String,
#[primary_span]
var_span: Span,
},
#[label("mutable borrow occurs due to use of {$place} in closure")]
MutableBorrowUsePlaceClosure {
place: String,
#[primary_span]
var_span: Span,
},
#[label(
"variable {$is_partial ->
[true] partially moved
*[false] moved
} due to use in coroutine"
)]
PartialMoveUseInCoroutine {
#[primary_span]
var_span: Span,
is_partial: bool,
},
#[label(
"variable {$is_partial ->
[true] partially moved
*[false] moved
} due to use in closure"
)]
PartialMoveUseInClosure {
#[primary_span]
var_span: Span,
is_partial: bool,
},
}
#[derive(Diagnostic)]
#[diag("cannot move out of {$place ->
[value] value
*[other] {$place}
} because it is borrowed", code = E0505)]
pub(crate) struct MoveBorrow<'a> {
pub place: &'a str,
pub borrow_place: &'a str,
pub value_place: &'a str,
#[primary_span]
#[label(
"move out of {$value_place ->
[value] value
*[other] {$value_place}
} occurs here"
)]
pub span: Span,
#[label(
"borrow of {$borrow_place ->
[value] value
*[other] {$borrow_place}
} occurs here"
)]
pub borrow_span: Span,
}
#[derive(Diagnostic)]
#[diag("opaque type used twice with different lifetimes")]
pub(crate) struct LifetimeMismatchOpaqueParam<'tcx> {
pub arg: GenericArg<'tcx>,
pub prev: GenericArg<'tcx>,
#[primary_span]
#[label("lifetime `{$arg}` used here")]
#[note(
"if all non-lifetime generic parameters are the same, but the lifetime parameters differ, it is not possible to differentiate the opaque types"
)]
pub span: Span,
#[label("lifetime `{$prev}` previously used here")]
pub prev_span: Span,
}
#[derive(Subdiagnostic)]
pub(crate) enum CaptureReasonLabel<'a> {
#[label(
"{$place_name} {$is_partial ->
[true] partially moved
*[false] moved
} due to this {$is_loop_message ->
[true] call, in previous iteration of loop
*[false] call
}"
)]
Call {
#[primary_span]
fn_call_span: Span,
place_name: &'a str,
is_partial: bool,
is_loop_message: bool,
},
#[label(
"{$place_name} {$is_partial ->
[true] partially moved
*[false] moved
} due to usage in {$is_loop_message ->
[true] operator, in previous iteration of loop
*[false] operator
}"
)]
OperatorUse {
#[primary_span]
fn_call_span: Span,
place_name: &'a str,
is_partial: bool,
is_loop_message: bool,
},
#[label(
"{$place_name} {$is_partial ->
[true] partially moved
*[false] moved
} due to this implicit call to {$is_loop_message ->
[true] `.into_iter()`, in previous iteration of loop
*[false] `.into_iter()`
}"
)]
ImplicitCall {
#[primary_span]
fn_call_span: Span,
place_name: &'a str,
is_partial: bool,
is_loop_message: bool,
},
#[label(
"{$place_name} {$is_partial ->
[true] partially moved
*[false] moved
} due to this method {$is_loop_message ->
[true] call, in previous iteration of loop
*[false] call
}"
)]
MethodCall {
#[primary_span]
fn_call_span: Span,
place_name: &'a str,
is_partial: bool,
is_loop_message: bool,
},
#[label(
"{$place_name} {$is_partial ->
[true] partially moved
*[false] moved
} due to this {$is_loop_message ->
[true] await, in previous iteration of loop
*[false] await
}"
)]
Await {
#[primary_span]
fn_call_span: Span,
place_name: &'a str,
is_partial: bool,
is_loop_message: bool,
},
#[label(
"value {$is_partial ->
[true] partially moved
*[false] moved
} {$is_move_msg ->
[true] into closure here
*[false] here
}{$is_loop_message ->
[true] , in previous iteration of loop
*[false] {\"\"}
}"
)]
MovedHere {
#[primary_span]
move_span: Span,
is_partial: bool,
is_move_msg: bool,
is_loop_message: bool,
},
#[label("help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents")]
BorrowContent {
#[primary_span]
var_span: Span,
},
}
#[derive(Subdiagnostic)]
pub(crate) enum CaptureReasonNote {
#[note("this value implements `FnOnce`, which causes it to be moved when called")]
FnOnceMoveInCall {
#[primary_span]
var_span: Span,
},
#[note("calling this operator moves the value")]
UnOpMoveByOperator {
#[primary_span]
span: Span,
},
#[note("calling this operator moves the left-hand side")]
LhsMoveByOperator {
#[primary_span]
span: Span,
},
#[note("`{$func}` takes ownership of the receiver `self`, which moves {$place_name}")]
FuncTakeSelf {
func: String,
place_name: String,
#[primary_span]
span: Span,
},
}
#[derive(Subdiagnostic)]
pub(crate) enum CaptureReasonSuggest<'tcx> {
#[suggestion(
"consider iterating over a slice of the `{$ty}`'s content to avoid moving into the `for` loop",
applicability = "maybe-incorrect",
code = "&",
style = "verbose"
)]
IterateSlice {
ty: Ty<'tcx>,
#[primary_span]
span: Span,
},
#[suggestion(
"consider reborrowing the `Pin` instead of moving it",
applicability = "maybe-incorrect",
code = ".as_mut()",
style = "verbose"
)]
FreshReborrow {
#[primary_span]
span: Span,
},
}
#[derive(Subdiagnostic)]
pub(crate) enum CaptureArgLabel {
#[label(
"value captured {$is_within ->
[true] here by coroutine
*[false] here
}"
)]
Capture {
is_within: bool,
#[primary_span]
args_span: Span,
},
#[label("{$place} is moved here")]
MoveOutPlace {
place: String,
#[primary_span]
args_span: Span,
},
}
#[derive(Subdiagnostic)]
pub(crate) enum OnClosureNote<'a> {
#[note(
"closure cannot be invoked more than once because it moves the variable `{$place_name}` out of its environment"
)]
InvokedTwice {
place_name: &'a str,
#[primary_span]
span: Span,
},
#[note(
"closure cannot be moved more than once as it is not `Copy` due to moving the variable `{$place_name}` out of its environment"
)]
MovedTwice {
place_name: &'a str,
#[primary_span]
span: Span,
},
}
#[derive(Subdiagnostic)]
pub(crate) enum TypeNoCopy<'a, 'tcx> {
#[label(
"{$is_partial_move ->
[true] partial move
*[false] move
} occurs because {$place} has type `{$ty}`, which does not implement the `Copy` trait"
)]
Label {
is_partial_move: bool,
ty: Ty<'tcx>,
place: &'a str,
#[primary_span]
span: Span,
},
#[label(
"data moved here because {$place} has type `{$ty}`, which does not implement the `Copy` \
trait"
)]
LabelMovedHere {
ty: Ty<'tcx>,
place: &'a str,
#[primary_span]
span: Span,
},
#[note(
"{$is_partial_move ->
[true] partial move
*[false] move
} occurs because {$place} has type `{$ty}`, which does not implement the `Copy` trait"
)]
Note { is_partial_move: bool, ty: Ty<'tcx>, place: &'a str },
}
#[derive(Diagnostic)]
#[diag(
"{$arg ->
[1] 1st
[2] 2nd
[3] 3rd
*[other] {$arg}th
} argument of `{$intrinsic}` is required to be a `const` item"
)]
pub(crate) struct SimdIntrinsicArgConst {
#[primary_span]
pub span: Span,
pub arg: usize,
pub intrinsic: String,
}
pub(crate) struct TailExprDropOrder<F: FnOnce(&mut Diag<'_, ()>)> {
pub borrowed: Span,
pub callback: F,
}
impl<'a, F: FnOnce(&mut Diag<'_, ()>)> Diagnostic<'a, ()> for TailExprDropOrder<F> {
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
let Self { borrowed, callback } = self;
let mut diag = Diag::new(dcx, level, "relative drop order changing in Rust 2024")
.with_span_label(
borrowed,
"this temporary value will be dropped at the end of the block",
);
callback(&mut diag);
diag
}
}