forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.rs
More file actions
1478 lines (1337 loc) · 42.1 KB
/
errors.rs
File metadata and controls
1478 lines (1337 loc) · 42.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::io::Error;
use std::path::{Path, PathBuf};
use rustc_errors::codes::*;
use rustc_errors::{
Applicability, Diag, DiagCtxtHandle, DiagSymbolList, Diagnostic, EmissionGuarantee, Level,
MultiSpan, inline_fluent,
};
use rustc_hir::Target;
use rustc_hir::attrs::{MirDialect, MirPhase};
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
use rustc_middle::ty::{MainDefinition, Ty};
use rustc_span::{DUMMY_SP, Span, Symbol};
use crate::check_attr::ProcMacroKind;
use crate::lang_items::Duplicate;
#[derive(LintDiagnostic)]
#[diag("`#[diagnostic::do_not_recommend]` can only be placed on trait implementations")]
pub(crate) struct IncorrectDoNotRecommendLocation;
#[derive(Diagnostic)]
#[diag("`#[autodiff]` should be applied to a function")]
pub(crate) struct AutoDiffAttr {
#[primary_span]
#[label("not a function")]
pub attr_span: Span,
}
#[derive(Diagnostic)]
#[diag("`#[loop_match]` should be applied to a loop")]
pub(crate) struct LoopMatchAttr {
#[primary_span]
pub attr_span: Span,
#[label("not a loop")]
pub node_span: Span,
}
#[derive(Diagnostic)]
#[diag("`#[const_continue]` should be applied to a break expression")]
pub(crate) struct ConstContinueAttr {
#[primary_span]
pub attr_span: Span,
#[label("not a break expression")]
pub node_span: Span,
}
#[derive(LintDiagnostic)]
#[diag("`{$no_mangle_attr}` attribute may not be used in combination with `{$export_name_attr}`")]
pub(crate) struct MixedExportNameAndNoMangle {
#[label("`{$no_mangle_attr}` is ignored")]
#[suggestion(
"remove the `{$no_mangle_attr}` attribute",
style = "verbose",
code = "",
applicability = "machine-applicable"
)]
pub no_mangle_span: Span,
#[note("`{$export_name_attr}` takes precedence")]
pub export_name_span: Span,
pub no_mangle_attr: &'static str,
pub export_name_attr: &'static str,
}
#[derive(LintDiagnostic)]
#[diag("crate-level attribute should be an inner attribute")]
pub(crate) struct OuterCrateLevelAttr {
#[subdiagnostic]
pub suggestion: OuterCrateLevelAttrSuggestion,
}
#[derive(Subdiagnostic)]
#[multipart_suggestion("add a `!`", style = "verbose")]
pub(crate) struct OuterCrateLevelAttrSuggestion {
#[suggestion_part(code = "!")]
pub bang_position: Span,
}
#[derive(LintDiagnostic)]
#[diag("crate-level attribute should be in the root module")]
pub(crate) struct InnerCrateLevelAttr;
#[derive(Diagnostic)]
#[diag("`#[non_exhaustive]` can't be used to annotate items with default field values")]
pub(crate) struct NonExhaustiveWithDefaultFieldValues {
#[primary_span]
pub attr_span: Span,
#[label("this struct has default field values")]
pub defn_span: Span,
}
#[derive(Diagnostic)]
#[diag("`#[doc(alias = \"...\")]` isn't allowed on {$location}")]
pub(crate) struct DocAliasBadLocation<'a> {
#[primary_span]
pub span: Span,
pub location: &'a str,
}
#[derive(Diagnostic)]
#[diag("`#[doc(alias = \"{$attr_str}\"]` is the same as the item's name")]
pub(crate) struct DocAliasNotAnAlias {
#[primary_span]
pub span: Span,
pub attr_str: Symbol,
}
#[derive(Diagnostic)]
#[diag("`#[doc({$attr_name} = \"...\")]` should be used on empty modules")]
pub(crate) struct DocKeywordAttributeEmptyMod {
#[primary_span]
pub span: Span,
pub attr_name: &'static str,
}
#[derive(Diagnostic)]
#[diag("`#[doc({$attr_name} = \"...\")]` should be used on modules")]
pub(crate) struct DocKeywordAttributeNotMod {
#[primary_span]
pub span: Span,
pub attr_name: &'static str,
}
#[derive(Diagnostic)]
#[diag(
"`#[doc(fake_variadic)]` must be used on the first of a set of tuple or fn pointer trait impls with varying arity"
)]
pub(crate) struct DocFakeVariadicNotValid {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("`#[doc(keyword = \"...\")]` should be used on impl blocks")]
pub(crate) struct DocKeywordOnlyImpl {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("`#[doc(search_unbox)]` should be used on generic structs and enums")]
pub(crate) struct DocSearchUnboxInvalid {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("conflicting doc inlining attributes")]
#[help("remove one of the conflicting attributes")]
pub(crate) struct DocInlineConflict {
#[primary_span]
pub spans: MultiSpan,
}
#[derive(LintDiagnostic)]
#[diag("this attribute can only be applied to a `use` item")]
#[note(
"read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#inline-and-no_inline> for more information"
)]
pub(crate) struct DocInlineOnlyUse {
#[label("only applicable on `use` items")]
pub attr_span: Span,
#[label("not a `use` item")]
pub item_span: Span,
}
#[derive(LintDiagnostic)]
#[diag("this attribute can only be applied to an `extern crate` item")]
#[note(
"read <https://doc.rust-lang.org/unstable-book/language-features/doc-masked.html> for more information"
)]
pub(crate) struct DocMaskedOnlyExternCrate {
#[label("only applicable on `extern crate` items")]
pub attr_span: Span,
#[label("not an `extern crate` item")]
pub item_span: Span,
}
#[derive(LintDiagnostic)]
#[diag("this attribute cannot be applied to an `extern crate self` item")]
pub(crate) struct DocMaskedNotExternCrateSelf {
#[label("not applicable on `extern crate self` items")]
pub attr_span: Span,
#[label("`extern crate self` defined here")]
pub item_span: Span,
}
#[derive(Diagnostic)]
#[diag("`#[ffi_const]` function cannot be `#[ffi_pure]`", code = E0757)]
pub(crate) struct BothFfiConstAndPure {
#[primary_span]
pub attr_span: Span,
}
#[derive(LintDiagnostic)]
#[diag("attribute should be applied to an `extern` block with non-Rust ABI")]
#[warning(
"this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!"
)]
pub(crate) struct Link {
#[label("not an `extern` block")]
pub span: Option<Span>,
}
#[derive(Diagnostic)]
#[diag("#[rustc_legacy_const_generics] functions must only have const generics")]
pub(crate) struct RustcLegacyConstGenericsOnly {
#[primary_span]
pub attr_span: Span,
#[label("non-const generic parameter")]
pub param_span: Span,
}
#[derive(Diagnostic)]
#[diag("#[rustc_legacy_const_generics] must have one index for each generic parameter")]
pub(crate) struct RustcLegacyConstGenericsIndex {
#[primary_span]
pub attr_span: Span,
#[label("generic parameters")]
pub generics_span: Span,
}
#[derive(Diagnostic)]
#[diag("index exceeds number of arguments")]
pub(crate) struct RustcLegacyConstGenericsIndexExceed {
#[primary_span]
#[label(
"there {$arg_count ->
[one] is
*[other] are
} only {$arg_count} {$arg_count ->
[one] argument
*[other] arguments
}"
)]
pub span: Span,
pub arg_count: usize,
}
#[derive(Diagnostic)]
#[diag("conflicting representation hints", code = E0566)]
pub(crate) struct ReprConflicting {
#[primary_span]
pub hint_spans: Vec<Span>,
}
#[derive(Diagnostic)]
#[diag("alignment must not be greater than `isize::MAX` bytes", code = E0589)]
#[note("`isize::MAX` is {$size} for the current target")]
pub(crate) struct InvalidReprAlignForTarget {
#[primary_span]
pub span: Span,
pub size: u64,
}
#[derive(LintDiagnostic)]
#[diag("conflicting representation hints", code = E0566)]
pub(crate) struct ReprConflictingLint;
#[derive(Diagnostic)]
#[diag("attribute should be applied to a macro")]
pub(crate) struct MacroOnlyAttribute {
#[primary_span]
pub attr_span: Span,
#[label("not a macro")]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("couldn't read {$file}: {$error}")]
pub(crate) struct DebugVisualizerUnreadable<'a> {
#[primary_span]
pub span: Span,
pub file: &'a Path,
pub error: Error,
}
#[derive(Diagnostic)]
#[diag("attribute should be applied to `const fn`")]
pub(crate) struct RustcAllowConstFnUnstable {
#[primary_span]
pub attr_span: Span,
#[label("not a `const fn`")]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("attribute should be applied to `#[repr(transparent)]` types")]
pub(crate) struct RustcPubTransparent {
#[primary_span]
pub attr_span: Span,
#[label("not a `#[repr(transparent)]` type")]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("attribute cannot be applied to a `async`, `gen` or `async gen` function")]
pub(crate) struct RustcForceInlineCoro {
#[primary_span]
pub attr_span: Span,
#[label("`async`, `gen` or `async gen` function")]
pub span: Span,
}
#[derive(LintDiagnostic)]
pub(crate) enum MacroExport {
#[diag("`#[macro_export]` has no effect on declarative macro definitions")]
#[note("declarative macros follow the same exporting rules as regular items")]
OnDeclMacro,
}
#[derive(Subdiagnostic)]
pub(crate) enum UnusedNote {
#[note("attribute `{$name}` with an empty list has no effect")]
EmptyList { name: Symbol },
#[note("attribute `{$name}` without any lints has no effect")]
NoLints { name: Symbol },
#[note("`default_method_body_is_const` has been replaced with `const` on traits")]
DefaultMethodBodyConst,
#[note(
"the `linker_messages` lint can only be controlled at the root of a crate that needs to be linked"
)]
LinkerMessagesBinaryCrateOnly,
}
#[derive(LintDiagnostic)]
#[diag("unused attribute")]
pub(crate) struct Unused {
#[suggestion("remove this attribute", code = "", applicability = "machine-applicable")]
pub attr_span: Span,
#[subdiagnostic]
pub note: UnusedNote,
}
#[derive(Diagnostic)]
#[diag("attribute should be applied to function or closure", code = E0518)]
pub(crate) struct NonExportedMacroInvalidAttrs {
#[primary_span]
#[label("not a function or closure")]
pub attr_span: Span,
}
#[derive(Diagnostic)]
#[diag("`#[may_dangle]` must be applied to a lifetime or type generic parameter in `Drop` impl")]
pub(crate) struct InvalidMayDangle {
#[primary_span]
pub attr_span: Span,
}
#[derive(LintDiagnostic)]
#[diag("unused attribute")]
pub(crate) struct UnusedDuplicate {
#[suggestion("remove this attribute", code = "", applicability = "machine-applicable")]
pub this: Span,
#[note("attribute also specified here")]
pub other: Span,
#[warning(
"this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!"
)]
pub warning: bool,
}
#[derive(Diagnostic)]
#[diag("multiple `{$name}` attributes")]
pub(crate) struct UnusedMultiple {
#[primary_span]
#[suggestion("remove this attribute", code = "", applicability = "machine-applicable")]
pub this: Span,
#[note("attribute also specified here")]
pub other: Span,
pub name: Symbol,
}
#[derive(LintDiagnostic)]
#[diag("this `#[deprecated]` annotation has no effect")]
pub(crate) struct DeprecatedAnnotationHasNoEffect {
#[suggestion(
"remove the unnecessary deprecation attribute",
applicability = "machine-applicable",
code = ""
)]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("unknown external lang item: `{$lang_item}`", code = E0264)]
pub(crate) struct UnknownExternLangItem {
#[primary_span]
pub span: Span,
pub lang_item: Symbol,
}
#[derive(Diagnostic)]
#[diag("`#[panic_handler]` function required, but not found")]
pub(crate) struct MissingPanicHandler;
#[derive(Diagnostic)]
#[diag("unwinding panics are not supported without std")]
#[help("using nightly cargo, use -Zbuild-std with panic=\"abort\" to avoid unwinding")]
#[note(
"since the core library is usually precompiled with panic=\"unwind\", rebuilding your crate with panic=\"abort\" may not be enough to fix the problem"
)]
pub(crate) struct PanicUnwindWithoutStd;
#[derive(Diagnostic)]
#[diag("lang item required, but not found: `{$name}`")]
#[note(
"this can occur when a binary crate with `#![no_std]` is compiled for a target where `{$name}` is defined in the standard library"
)]
#[help(
"you may be able to compile for a target that doesn't need `{$name}`, specify a target with `--target` or in `.cargo/config`"
)]
pub(crate) struct MissingLangItem {
pub name: Symbol,
}
#[derive(Diagnostic)]
#[diag(
"{$name ->
[panic_impl] `#[panic_handler]`
*[other] `{$name}` lang item
} function is not allowed to have `#[track_caller]`"
)]
pub(crate) struct LangItemWithTrackCaller {
#[primary_span]
pub attr_span: Span,
pub name: Symbol,
#[label(
"{$name ->
[panic_impl] `#[panic_handler]`
*[other] `{$name}` lang item
} function is not allowed to have `#[track_caller]`"
)]
pub sig_span: Span,
}
#[derive(Diagnostic)]
#[diag(
"{$name ->
[panic_impl] `#[panic_handler]`
*[other] `{$name}` lang item
} function is not allowed to have `#[target_feature]`"
)]
pub(crate) struct LangItemWithTargetFeature {
#[primary_span]
pub attr_span: Span,
pub name: Symbol,
#[label(
"{$name ->
[panic_impl] `#[panic_handler]`
*[other] `{$name}` lang item
} function is not allowed to have `#[target_feature]`"
)]
pub sig_span: Span,
}
#[derive(Diagnostic)]
#[diag("`{$name}` lang item must be applied to a {$expected_target}", code = E0718)]
pub(crate) struct LangItemOnIncorrectTarget {
#[primary_span]
#[label("attribute should be applied to a {$expected_target}, not a {$actual_target}")]
pub span: Span,
pub name: Symbol,
pub expected_target: Target,
pub actual_target: Target,
}
#[derive(Diagnostic)]
#[diag("definition of an unknown lang item: `{$name}`", code = E0522)]
pub(crate) struct UnknownLangItem {
#[primary_span]
#[label("definition of unknown lang item `{$name}`")]
pub span: Span,
pub name: Symbol,
}
pub(crate) struct InvalidAttrAtCrateLevel {
pub span: Span,
pub sugg_span: Option<Span>,
pub name: Symbol,
pub item: Option<ItemFollowingInnerAttr>,
}
#[derive(Clone, Copy)]
pub(crate) struct ItemFollowingInnerAttr {
pub span: Span,
pub kind: &'static str,
}
impl<G: EmissionGuarantee> Diagnostic<'_, G> for InvalidAttrAtCrateLevel {
#[track_caller]
fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(
dcx,
level,
inline_fluent!("`{$name}` attribute cannot be used at crate level"),
);
diag.span(self.span);
diag.arg("name", self.name);
// Only emit an error with a suggestion if we can create a string out
// of the attribute span
if let Some(span) = self.sugg_span {
diag.span_suggestion_verbose(
span,
inline_fluent!("perhaps you meant to use an outer attribute"),
String::new(),
Applicability::MachineApplicable,
);
}
if let Some(item) = self.item {
diag.arg("kind", item.kind);
diag.span_label(
item.span,
inline_fluent!("the inner attribute doesn't annotate this {$kind}"),
);
}
diag
}
}
#[derive(Diagnostic)]
#[diag("duplicate diagnostic item in crate `{$crate_name}`: `{$name}`")]
pub(crate) struct DuplicateDiagnosticItemInCrate {
#[primary_span]
pub duplicate_span: Option<Span>,
#[note("the diagnostic item is first defined here")]
pub orig_span: Option<Span>,
#[note("the diagnostic item is first defined in crate `{$orig_crate_name}`")]
pub different_crates: bool,
pub crate_name: Symbol,
pub orig_crate_name: Symbol,
pub name: Symbol,
}
#[derive(Diagnostic)]
#[diag("abi: {$abi}")]
pub(crate) struct LayoutAbi {
#[primary_span]
pub span: Span,
pub abi: String,
}
#[derive(Diagnostic)]
#[diag("align: {$align}")]
pub(crate) struct LayoutAlign {
#[primary_span]
pub span: Span,
pub align: String,
}
#[derive(Diagnostic)]
#[diag("size: {$size}")]
pub(crate) struct LayoutSize {
#[primary_span]
pub span: Span,
pub size: String,
}
#[derive(Diagnostic)]
#[diag("homogeneous_aggregate: {$homogeneous_aggregate}")]
pub(crate) struct LayoutHomogeneousAggregate {
#[primary_span]
pub span: Span,
pub homogeneous_aggregate: String,
}
#[derive(Diagnostic)]
#[diag("layout_of({$normalized_ty}) = {$ty_layout}")]
pub(crate) struct LayoutOf<'tcx> {
#[primary_span]
pub span: Span,
pub normalized_ty: Ty<'tcx>,
pub ty_layout: String,
}
#[derive(Diagnostic)]
#[diag("fn_abi_of({$fn_name}) = {$fn_abi}")]
pub(crate) struct AbiOf {
#[primary_span]
pub span: Span,
pub fn_name: Symbol,
pub fn_abi: String,
}
#[derive(Diagnostic)]
#[diag(
"ABIs are not compatible
left ABI = {$left}
right ABI = {$right}"
)]
pub(crate) struct AbiNe {
#[primary_span]
pub span: Span,
pub left: String,
pub right: String,
}
#[derive(Diagnostic)]
#[diag(
"`#[rustc_abi]` can only be applied to function items, type aliases, and associated functions"
)]
pub(crate) struct AbiInvalidAttribute {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("unrecognized argument")]
pub(crate) struct UnrecognizedArgument {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("feature `{$feature}` is declared stable since {$since}, but was previously declared stable since {$prev_since}", code = E0711)]
pub(crate) struct FeatureStableTwice {
#[primary_span]
pub span: Span,
pub feature: Symbol,
pub since: Symbol,
pub prev_since: Symbol,
}
#[derive(Diagnostic)]
#[diag("feature `{$feature}` is declared {$declared}, but was previously declared {$prev_declared}", code = E0711)]
pub(crate) struct FeaturePreviouslyDeclared<'a> {
#[primary_span]
pub span: Span,
pub feature: Symbol,
pub declared: &'a str,
pub prev_declared: &'a str,
}
#[derive(Diagnostic)]
#[diag("multiple functions with a `#[rustc_main]` attribute", code = E0137)]
pub(crate) struct MultipleRustcMain {
#[primary_span]
pub span: Span,
#[label("first `#[rustc_main]` function")]
pub first: Span,
#[label("additional `#[rustc_main]` function")]
pub additional: Span,
}
#[derive(Diagnostic)]
#[diag("the `main` function cannot be declared in an `extern` block")]
pub(crate) struct ExternMain {
#[primary_span]
pub span: Span,
}
pub(crate) struct NoMainErr {
pub sp: Span,
pub crate_name: Symbol,
pub has_filename: bool,
pub filename: PathBuf,
pub file_empty: bool,
pub non_main_fns: Vec<Span>,
pub main_def_opt: Option<MainDefinition>,
pub add_teach_note: bool,
}
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for NoMainErr {
#[track_caller]
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
let mut diag = Diag::new(
dcx,
level,
inline_fluent!("`main` function not found in crate `{$crate_name}`"),
);
diag.span(DUMMY_SP);
diag.code(E0601);
diag.arg("crate_name", self.crate_name);
diag.arg("filename", self.filename);
diag.arg("has_filename", self.has_filename);
let note = if !self.non_main_fns.is_empty() {
for &span in &self.non_main_fns {
diag.span_note(span, inline_fluent!("here is a function named `main`"));
}
diag.note(inline_fluent!(
"you have one or more functions named `main` not defined at the crate level"
));
diag.help(inline_fluent!("consider moving the `main` function definitions"));
// There were some functions named `main` though. Try to give the user a hint.
inline_fluent!(
"the main function must be defined at the crate level{$has_filename ->
[true] {\" \"}(in `{$filename}`)
*[false] {\"\"}
}"
)
} else if self.has_filename {
inline_fluent!("consider adding a `main` function to `{$filename}`")
} else {
inline_fluent!("consider adding a `main` function at the crate level")
};
if self.file_empty {
diag.note(note);
} else {
diag.span(self.sp.shrink_to_hi());
diag.span_label(self.sp.shrink_to_hi(), note);
}
if let Some(main_def) = self.main_def_opt
&& main_def.opt_fn_def_id().is_none()
{
// There is something at `crate::main`, but it is not a function definition.
diag.span_label(
main_def.span,
inline_fluent!("non-function item at `crate::main` is found"),
);
}
if self.add_teach_note {
diag.note(inline_fluent!("if you don't know the basics of Rust, you can go look to the Rust Book to get started: https://doc.rust-lang.org/book/"));
}
diag
}
}
pub(crate) struct DuplicateLangItem {
pub local_span: Option<Span>,
pub lang_item_name: Symbol,
pub crate_name: Symbol,
pub dependency_of: Option<Symbol>,
pub is_local: bool,
pub path: String,
pub first_defined_span: Option<Span>,
pub orig_crate_name: Option<Symbol>,
pub orig_dependency_of: Option<Symbol>,
pub orig_is_local: bool,
pub orig_path: String,
pub(crate) duplicate: Duplicate,
}
impl<G: EmissionGuarantee> Diagnostic<'_, G> for DuplicateLangItem {
#[track_caller]
fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
let mut diag = Diag::new(
dcx,
level,
match self.duplicate {
Duplicate::Plain => inline_fluent!("found duplicate lang item `{$lang_item_name}`"),
Duplicate::Crate => inline_fluent!(
"duplicate lang item in crate `{$crate_name}`: `{$lang_item_name}`"
),
Duplicate::CrateDepends => inline_fluent!(
"duplicate lang item in crate `{$crate_name}` (which `{$dependency_of}` depends on): `{$lang_item_name}`"
),
},
);
diag.code(E0152);
diag.arg("lang_item_name", self.lang_item_name);
diag.arg("crate_name", self.crate_name);
if let Some(dependency_of) = self.dependency_of {
diag.arg("dependency_of", dependency_of);
}
diag.arg("path", self.path);
if let Some(orig_crate_name) = self.orig_crate_name {
diag.arg("orig_crate_name", orig_crate_name);
}
if let Some(orig_dependency_of) = self.orig_dependency_of {
diag.arg("orig_dependency_of", orig_dependency_of);
}
diag.arg("orig_path", self.orig_path);
if let Some(span) = self.local_span {
diag.span(span);
}
if let Some(span) = self.first_defined_span {
diag.span_note(span, inline_fluent!("the lang item is first defined here"));
} else {
if self.orig_dependency_of.is_none() {
diag.note(inline_fluent!(
"the lang item is first defined in crate `{$orig_crate_name}`"
));
} else {
diag.note(inline_fluent!("the lang item is first defined in crate `{$orig_crate_name}` (which `{$orig_dependency_of}` depends on)"));
}
if self.orig_is_local {
diag.note(inline_fluent!(
"first definition in the local crate (`{$orig_crate_name}`)"
));
} else {
diag.note(inline_fluent!(
"first definition in `{$orig_crate_name}` loaded from {$orig_path}"
));
}
if self.is_local {
diag.note(inline_fluent!("second definition in the local crate (`{$crate_name}`)"));
} else {
diag.note(inline_fluent!(
"second definition in `{$crate_name}` loaded from {$path}"
));
}
}
diag
}
}
#[derive(Diagnostic)]
#[diag("`{$name}` lang item must be applied to a {$kind} with {$at_least ->
[true] at least {$num}
*[false] {$num}
} generic {$num ->
[one] argument
*[other] arguments
}", code = E0718)]
pub(crate) struct IncorrectTarget<'a> {
#[primary_span]
pub span: Span,
#[label(
"this {$kind} has {$actual_num} generic {$actual_num ->
[one] argument
*[other] arguments
}"
)]
pub generics_span: Span,
pub name: &'a str, // cannot be symbol because it renders e.g. `r#fn` instead of `fn`
pub kind: &'static str,
pub num: usize,
pub actual_num: usize,
pub at_least: bool,
}
#[derive(Diagnostic)]
#[diag("lang items are not allowed in stable dylibs")]
pub(crate) struct IncorrectCrateType {
#[primary_span]
pub span: Span,
}
#[derive(LintDiagnostic)]
#[diag(
"useless assignment of {$is_field_assign ->
[true] field
*[false] variable
} of type `{$ty}` to itself"
)]
pub(crate) struct UselessAssignment<'a> {
pub is_field_assign: bool,
pub ty: Ty<'a>,
}
#[derive(LintDiagnostic)]
#[diag("`#[inline]` is ignored on externally exported functions")]
#[help(
"externally exported functions are functions with `#[no_mangle]`, `#[export_name]`, or `#[linkage]`"
)]
pub(crate) struct InlineIgnoredForExported {}
#[derive(Diagnostic)]
#[diag("{$repr}")]
pub(crate) struct ObjectLifetimeErr {
#[primary_span]
pub span: Span,
pub repr: String,
}
#[derive(Diagnostic)]
pub(crate) enum AttrApplication {
#[diag("attribute should be applied to an enum", code = E0517)]
Enum {
#[primary_span]
hint_span: Span,
#[label("not an enum")]
span: Span,
},
#[diag("attribute should be applied to a struct", code = E0517)]
Struct {
#[primary_span]
hint_span: Span,
#[label("not a struct")]
span: Span,
},
#[diag("attribute should be applied to a struct or union", code = E0517)]
StructUnion {
#[primary_span]
hint_span: Span,
#[label("not a struct or union")]
span: Span,
},
#[diag("attribute should be applied to a struct, enum, or union", code = E0517)]
StructEnumUnion {
#[primary_span]
hint_span: Span,
#[label("not a struct, enum, or union")]
span: Span,
},
}
#[derive(Diagnostic)]
#[diag("transparent {$target} cannot have other repr hints", code = E0692)]
pub(crate) struct TransparentIncompatible {
#[primary_span]
pub hint_spans: Vec<Span>,
pub target: String,
}
#[derive(Diagnostic)]
#[diag("deprecated attribute must be paired with either stable or unstable attribute", code = E0549)]
pub(crate) struct DeprecatedAttribute {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("this stability annotation is useless")]
pub(crate) struct UselessStability {
#[primary_span]
#[label("useless stability annotation")]
pub span: Span,
#[label("the stability attribute annotates this item")]
pub item_sp: Span,
}
#[derive(Diagnostic)]
#[diag("an API can't be stabilized after it is deprecated")]
pub(crate) struct CannotStabilizeDeprecated {
#[primary_span]
#[label("invalid version")]
pub span: Span,
#[label("the stability attribute annotates this item")]
pub item_sp: Span,
}
#[derive(Diagnostic)]
#[diag("can't mark as unstable using an already stable feature")]
pub(crate) struct UnstableAttrForAlreadyStableFeature {
#[primary_span]
#[label("this feature is already stable")]
#[help("consider removing the attribute")]
pub attr_span: Span,
#[label("the stability attribute annotates this item")]
pub item_span: Span,
}
#[derive(Diagnostic)]
#[diag("{$descr} has missing stability attribute")]
pub(crate) struct MissingStabilityAttr<'a> {
#[primary_span]
pub span: Span,
pub descr: &'a str,
}
#[derive(Diagnostic)]
#[diag("{$descr} has missing const stability attribute")]
pub(crate) struct MissingConstStabAttr<'a> {
#[primary_span]
pub span: Span,
pub descr: &'a str,
}
#[derive(Diagnostic)]
#[diag("trait implementations cannot be const stable yet")]
#[note("see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information")]
pub(crate) struct TraitImplConstStable {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("const stability on the impl does not match the const stability on the trait")]
pub(crate) struct TraitImplConstStabilityMismatch {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub impl_stability: ImplConstStability,
#[subdiagnostic]
pub trait_stability: TraitConstStability,
}
#[derive(Subdiagnostic)]
pub(crate) enum TraitConstStability {
#[note("...but the trait is stable")]
Stable {
#[primary_span]
span: Span,
},
#[note("...but the trait is unstable")]
Unstable {
#[primary_span]
span: Span,
},
}
#[derive(Subdiagnostic)]
pub(crate) enum ImplConstStability {
#[note("this impl is (implicitly) stable...")]
Stable {
#[primary_span]
span: Span,
},
#[note("this impl is unstable...")]
Unstable {
#[primary_span]
span: Span,
},
}