1 //! Errors emitted by ast_passes. 2 3 use rustc_ast::ParamKindOrd; 4 use rustc_errors::AddToDiagnostic; 5 use rustc_macros::{Diagnostic, Subdiagnostic}; 6 use rustc_span::{symbol::Ident, Span, Symbol}; 7 8 use crate::ast_validation::ForbiddenLetReason; 9 use crate::fluent_generated as fluent; 10 11 #[derive(Diagnostic)] 12 #[diag(ast_passes_forbidden_let)] 13 #[note] 14 pub struct ForbiddenLet { 15 #[primary_span] 16 pub span: Span, 17 #[subdiagnostic] 18 pub(crate) reason: ForbiddenLetReason, 19 } 20 21 #[derive(Diagnostic)] 22 #[diag(ast_passes_forbidden_let_stable)] 23 #[note] 24 pub struct ForbiddenLetStable { 25 #[primary_span] 26 pub span: Span, 27 } 28 29 #[derive(Diagnostic)] 30 #[diag(ast_passes_keyword_lifetime)] 31 pub struct KeywordLifetime { 32 #[primary_span] 33 pub span: Span, 34 } 35 36 #[derive(Diagnostic)] 37 #[diag(ast_passes_invalid_label)] 38 pub struct InvalidLabel { 39 #[primary_span] 40 pub span: Span, 41 pub name: Symbol, 42 } 43 44 #[derive(Diagnostic)] 45 #[diag(ast_passes_visibility_not_permitted, code = "E0449")] 46 pub struct VisibilityNotPermitted { 47 #[primary_span] 48 pub span: Span, 49 #[subdiagnostic] 50 pub note: VisibilityNotPermittedNote, 51 } 52 53 #[derive(Subdiagnostic)] 54 pub enum VisibilityNotPermittedNote { 55 #[note(ast_passes_enum_variant)] 56 EnumVariant, 57 #[note(ast_passes_trait_impl)] 58 TraitImpl, 59 #[note(ast_passes_individual_impl_items)] 60 IndividualImplItems, 61 #[note(ast_passes_individual_foreign_items)] 62 IndividualForeignItems, 63 } 64 65 #[derive(Diagnostic)] 66 #[diag(ast_passes_trait_fn_const, code = "E0379")] 67 pub struct TraitFnConst { 68 #[primary_span] 69 #[label] 70 pub span: Span, 71 } 72 73 #[derive(Diagnostic)] 74 #[diag(ast_passes_forbidden_lifetime_bound)] 75 pub struct ForbiddenLifetimeBound { 76 #[primary_span] 77 pub spans: Vec<Span>, 78 } 79 80 #[derive(Diagnostic)] 81 #[diag(ast_passes_fn_param_too_many)] 82 pub struct FnParamTooMany { 83 #[primary_span] 84 pub span: Span, 85 pub max_num_args: usize, 86 } 87 88 #[derive(Diagnostic)] 89 #[diag(ast_passes_fn_param_c_var_args_only)] 90 pub struct FnParamCVarArgsOnly { 91 #[primary_span] 92 pub span: Span, 93 } 94 95 #[derive(Diagnostic)] 96 #[diag(ast_passes_fn_param_c_var_args_not_last)] 97 pub struct FnParamCVarArgsNotLast { 98 #[primary_span] 99 pub span: Span, 100 } 101 102 #[derive(Diagnostic)] 103 #[diag(ast_passes_fn_param_doc_comment)] 104 pub struct FnParamDocComment { 105 #[primary_span] 106 #[label] 107 pub span: Span, 108 } 109 110 #[derive(Diagnostic)] 111 #[diag(ast_passes_fn_param_forbidden_attr)] 112 pub struct FnParamForbiddenAttr { 113 #[primary_span] 114 pub span: Span, 115 } 116 117 #[derive(Diagnostic)] 118 #[diag(ast_passes_fn_param_forbidden_self)] 119 #[note] 120 pub struct FnParamForbiddenSelf { 121 #[primary_span] 122 #[label] 123 pub span: Span, 124 } 125 126 #[derive(Diagnostic)] 127 #[diag(ast_passes_forbidden_default)] 128 pub struct ForbiddenDefault { 129 #[primary_span] 130 pub span: Span, 131 #[label] 132 pub def_span: Span, 133 } 134 135 #[derive(Diagnostic)] 136 #[diag(ast_passes_assoc_const_without_body)] 137 pub struct AssocConstWithoutBody { 138 #[primary_span] 139 pub span: Span, 140 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")] 141 pub replace_span: Span, 142 } 143 144 #[derive(Diagnostic)] 145 #[diag(ast_passes_assoc_fn_without_body)] 146 pub struct AssocFnWithoutBody { 147 #[primary_span] 148 pub span: Span, 149 #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")] 150 pub replace_span: Span, 151 } 152 153 #[derive(Diagnostic)] 154 #[diag(ast_passes_assoc_type_without_body)] 155 pub struct AssocTypeWithoutBody { 156 #[primary_span] 157 pub span: Span, 158 #[suggestion(code = " = <type>;", applicability = "has-placeholders")] 159 pub replace_span: Span, 160 } 161 162 #[derive(Diagnostic)] 163 #[diag(ast_passes_const_without_body)] 164 pub struct ConstWithoutBody { 165 #[primary_span] 166 pub span: Span, 167 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")] 168 pub replace_span: Span, 169 } 170 171 #[derive(Diagnostic)] 172 #[diag(ast_passes_static_without_body)] 173 pub struct StaticWithoutBody { 174 #[primary_span] 175 pub span: Span, 176 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")] 177 pub replace_span: Span, 178 } 179 180 #[derive(Diagnostic)] 181 #[diag(ast_passes_ty_alias_without_body)] 182 pub struct TyAliasWithoutBody { 183 #[primary_span] 184 pub span: Span, 185 #[suggestion(code = " = <type>;", applicability = "has-placeholders")] 186 pub replace_span: Span, 187 } 188 189 #[derive(Diagnostic)] 190 #[diag(ast_passes_fn_without_body)] 191 pub struct FnWithoutBody { 192 #[primary_span] 193 pub span: Span, 194 #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")] 195 pub replace_span: Span, 196 #[subdiagnostic] 197 pub extern_block_suggestion: Option<ExternBlockSuggestion>, 198 } 199 200 #[derive(Subdiagnostic)] 201 pub enum ExternBlockSuggestion { 202 #[multipart_suggestion(ast_passes_extern_block_suggestion, applicability = "maybe-incorrect")] 203 Implicit { 204 #[suggestion_part(code = "extern {{")] 205 start_span: Span, 206 #[suggestion_part(code = " }}")] 207 end_span: Span, 208 }, 209 #[multipart_suggestion(ast_passes_extern_block_suggestion, applicability = "maybe-incorrect")] 210 Explicit { 211 #[suggestion_part(code = "extern \"{abi}\" {{")] 212 start_span: Span, 213 #[suggestion_part(code = " }}")] 214 end_span: Span, 215 abi: Symbol, 216 }, 217 } 218 219 #[derive(Diagnostic)] 220 #[diag(ast_passes_bound_in_context)] 221 pub struct BoundInContext<'a> { 222 #[primary_span] 223 pub span: Span, 224 pub ctx: &'a str, 225 } 226 227 #[derive(Diagnostic)] 228 #[diag(ast_passes_extern_types_cannot)] 229 #[note(ast_passes_extern_keyword_link)] 230 pub struct ExternTypesCannotHave<'a> { 231 #[primary_span] 232 #[suggestion(code = "", applicability = "maybe-incorrect")] 233 pub span: Span, 234 pub descr: &'a str, 235 pub remove_descr: &'a str, 236 #[label] 237 pub block_span: Span, 238 } 239 240 #[derive(Diagnostic)] 241 #[diag(ast_passes_body_in_extern)] 242 #[note(ast_passes_extern_keyword_link)] 243 pub struct BodyInExtern<'a> { 244 #[primary_span] 245 #[label(ast_passes_cannot_have)] 246 pub span: Span, 247 #[label(ast_passes_invalid)] 248 pub body: Span, 249 #[label(ast_passes_existing)] 250 pub block: Span, 251 pub kind: &'a str, 252 } 253 254 #[derive(Diagnostic)] 255 #[diag(ast_passes_fn_body_extern)] 256 #[help] 257 #[note(ast_passes_extern_keyword_link)] 258 pub struct FnBodyInExtern { 259 #[primary_span] 260 #[label(ast_passes_cannot_have)] 261 pub span: Span, 262 #[suggestion(code = ";", applicability = "maybe-incorrect")] 263 pub body: Span, 264 #[label] 265 pub block: Span, 266 } 267 268 #[derive(Diagnostic)] 269 #[diag(ast_passes_extern_fn_qualifiers)] 270 pub struct FnQualifierInExtern { 271 #[primary_span] 272 pub span: Span, 273 #[label] 274 pub block: Span, 275 #[suggestion(code = "fn ", applicability = "maybe-incorrect", style = "verbose")] 276 pub sugg_span: Span, 277 } 278 279 #[derive(Diagnostic)] 280 #[diag(ast_passes_extern_item_ascii)] 281 #[note] 282 pub struct ExternItemAscii { 283 #[primary_span] 284 pub span: Span, 285 #[label] 286 pub block: Span, 287 } 288 289 #[derive(Diagnostic)] 290 #[diag(ast_passes_bad_c_variadic)] 291 pub struct BadCVariadic { 292 #[primary_span] 293 pub span: Span, 294 } 295 296 #[derive(Diagnostic)] 297 #[diag(ast_passes_item_underscore)] 298 pub struct ItemUnderscore<'a> { 299 #[primary_span] 300 #[label] 301 pub span: Span, 302 pub kind: &'a str, 303 } 304 305 #[derive(Diagnostic)] 306 #[diag(ast_passes_nomangle_ascii, code = "E0754")] 307 pub struct NoMangleAscii { 308 #[primary_span] 309 pub span: Span, 310 } 311 312 #[derive(Diagnostic)] 313 #[diag(ast_passes_module_nonascii, code = "E0754")] 314 #[help] 315 pub struct ModuleNonAscii { 316 #[primary_span] 317 pub span: Span, 318 pub name: Symbol, 319 } 320 321 #[derive(Diagnostic)] 322 #[diag(ast_passes_auto_generic, code = "E0567")] 323 pub struct AutoTraitGeneric { 324 #[primary_span] 325 #[suggestion(code = "", applicability = "machine-applicable")] 326 pub span: Span, 327 #[label] 328 pub ident: Span, 329 } 330 331 #[derive(Diagnostic)] 332 #[diag(ast_passes_auto_super_lifetime, code = "E0568")] 333 pub struct AutoTraitBounds { 334 #[primary_span] 335 #[suggestion(code = "", applicability = "machine-applicable")] 336 pub span: Span, 337 #[label] 338 pub ident: Span, 339 } 340 341 #[derive(Diagnostic)] 342 #[diag(ast_passes_auto_items, code = "E0380")] 343 pub struct AutoTraitItems { 344 #[primary_span] 345 pub spans: Vec<Span>, 346 #[suggestion(code = "", applicability = "machine-applicable")] 347 pub total: Span, 348 #[label] 349 pub ident: Span, 350 } 351 352 #[derive(Diagnostic)] 353 #[diag(ast_passes_generic_before_constraints)] 354 pub struct ArgsBeforeConstraint { 355 #[primary_span] 356 pub arg_spans: Vec<Span>, 357 #[label(ast_passes_constraints)] 358 pub constraints: Span, 359 #[label(ast_passes_args)] 360 pub args: Span, 361 #[suggestion(code = "{suggestion}", applicability = "machine-applicable", style = "verbose")] 362 pub data: Span, 363 pub suggestion: String, 364 pub constraint_len: usize, 365 pub args_len: usize, 366 #[subdiagnostic] 367 pub constraint_spans: EmptyLabelManySpans, 368 #[subdiagnostic] 369 pub arg_spans2: EmptyLabelManySpans, 370 } 371 372 pub struct EmptyLabelManySpans(pub Vec<Span>); 373 374 // The derive for `Vec<Span>` does multiple calls to `span_label`, adding commas between each 375 impl AddToDiagnostic for EmptyLabelManySpans { add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, _: F) where F: Fn( &mut rustc_errors::Diagnostic, rustc_errors::SubdiagnosticMessage, ) -> rustc_errors::SubdiagnosticMessage,376 fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, _: F) 377 where 378 F: Fn( 379 &mut rustc_errors::Diagnostic, 380 rustc_errors::SubdiagnosticMessage, 381 ) -> rustc_errors::SubdiagnosticMessage, 382 { 383 diag.span_labels(self.0, ""); 384 } 385 } 386 387 #[derive(Diagnostic)] 388 #[diag(ast_passes_pattern_in_fn_pointer, code = "E0561")] 389 pub struct PatternFnPointer { 390 #[primary_span] 391 pub span: Span, 392 } 393 394 #[derive(Diagnostic)] 395 #[diag(ast_passes_trait_object_single_bound, code = "E0226")] 396 pub struct TraitObjectBound { 397 #[primary_span] 398 pub span: Span, 399 } 400 401 #[derive(Diagnostic)] 402 #[diag(ast_passes_impl_trait_path, code = "E0667")] 403 pub struct ImplTraitPath { 404 #[primary_span] 405 pub span: Span, 406 } 407 408 #[derive(Diagnostic)] 409 #[diag(ast_passes_nested_impl_trait, code = "E0666")] 410 pub struct NestedImplTrait { 411 #[primary_span] 412 pub span: Span, 413 #[label(ast_passes_outer)] 414 pub outer: Span, 415 #[label(ast_passes_inner)] 416 pub inner: Span, 417 } 418 419 #[derive(Diagnostic)] 420 #[diag(ast_passes_at_least_one_trait)] 421 pub struct AtLeastOneTrait { 422 #[primary_span] 423 pub span: Span, 424 } 425 426 #[derive(Diagnostic)] 427 #[diag(ast_passes_out_of_order_params)] 428 pub struct OutOfOrderParams<'a> { 429 #[primary_span] 430 pub spans: Vec<Span>, 431 #[suggestion(code = "{ordered_params}", applicability = "machine-applicable")] 432 pub sugg_span: Span, 433 pub param_ord: &'a ParamKindOrd, 434 pub max_param: &'a ParamKindOrd, 435 pub ordered_params: &'a str, 436 } 437 438 #[derive(Diagnostic)] 439 #[diag(ast_passes_obsolete_auto)] 440 #[help] 441 pub struct ObsoleteAuto { 442 #[primary_span] 443 pub span: Span, 444 } 445 446 #[derive(Diagnostic)] 447 #[diag(ast_passes_unsafe_negative_impl, code = "E0198")] 448 pub struct UnsafeNegativeImpl { 449 #[primary_span] 450 pub span: Span, 451 #[label(ast_passes_negative)] 452 pub negative: Span, 453 #[label(ast_passes_unsafe)] 454 pub r#unsafe: Span, 455 } 456 457 #[derive(Diagnostic)] 458 #[diag(ast_passes_inherent_cannot_be)] 459 pub struct InherentImplCannot<'a> { 460 #[primary_span] 461 pub span: Span, 462 #[label(ast_passes_because)] 463 pub annotation_span: Span, 464 pub annotation: &'a str, 465 #[label(ast_passes_type)] 466 pub self_ty: Span, 467 #[note(ast_passes_only_trait)] 468 pub only_trait: Option<()>, 469 } 470 471 #[derive(Diagnostic)] 472 #[diag(ast_passes_inherent_cannot_be, code = "E0197")] 473 pub struct InherentImplCannotUnsafe<'a> { 474 #[primary_span] 475 pub span: Span, 476 #[label(ast_passes_because)] 477 pub annotation_span: Span, 478 pub annotation: &'a str, 479 #[label(ast_passes_type)] 480 pub self_ty: Span, 481 } 482 483 #[derive(Diagnostic)] 484 #[diag(ast_passes_unsafe_item)] 485 pub struct UnsafeItem { 486 #[primary_span] 487 pub span: Span, 488 pub kind: &'static str, 489 } 490 491 #[derive(Diagnostic)] 492 #[diag(ast_passes_fieldless_union)] 493 pub struct FieldlessUnion { 494 #[primary_span] 495 pub span: Span, 496 } 497 498 #[derive(Diagnostic)] 499 #[diag(ast_passes_where_after_type_alias)] 500 #[note] 501 pub struct WhereAfterTypeAlias { 502 #[primary_span] 503 pub span: Span, 504 } 505 506 #[derive(Diagnostic)] 507 #[diag(ast_passes_generic_default_trailing)] 508 pub struct GenericDefaultTrailing { 509 #[primary_span] 510 pub span: Span, 511 } 512 513 #[derive(Diagnostic)] 514 #[diag(ast_passes_nested_lifetimes, code = "E0316")] 515 pub struct NestedLifetimes { 516 #[primary_span] 517 pub span: Span, 518 } 519 520 #[derive(Diagnostic)] 521 #[diag(ast_passes_optional_trait_supertrait)] 522 #[note] 523 pub struct OptionalTraitSupertrait { 524 #[primary_span] 525 pub span: Span, 526 pub path_str: String, 527 } 528 529 #[derive(Diagnostic)] 530 #[diag(ast_passes_optional_trait_object)] 531 pub struct OptionalTraitObject { 532 #[primary_span] 533 pub span: Span, 534 } 535 536 #[derive(Diagnostic)] 537 #[diag(ast_passes_tilde_const_disallowed)] 538 pub struct TildeConstDisallowed { 539 #[primary_span] 540 pub span: Span, 541 #[subdiagnostic] 542 pub reason: TildeConstReason, 543 } 544 545 #[derive(Subdiagnostic)] 546 pub enum TildeConstReason { 547 #[note(ast_passes_trait)] 548 TraitObject, 549 #[note(ast_passes_closure)] 550 Closure, 551 #[note(ast_passes_function)] 552 Function { 553 #[primary_span] 554 ident: Span, 555 }, 556 } 557 558 #[derive(Diagnostic)] 559 #[diag(ast_passes_optional_const_exclusive)] 560 pub struct OptionalConstExclusive { 561 #[primary_span] 562 pub span: Span, 563 pub modifier: &'static str, 564 } 565 566 #[derive(Diagnostic)] 567 #[diag(ast_passes_const_and_async)] 568 pub struct ConstAndAsync { 569 #[primary_span] 570 pub spans: Vec<Span>, 571 #[label(ast_passes_const)] 572 pub cspan: Span, 573 #[label(ast_passes_async)] 574 pub aspan: Span, 575 #[label] 576 pub span: Span, 577 } 578 579 #[derive(Diagnostic)] 580 #[diag(ast_passes_pattern_in_foreign, code = "E0130")] 581 pub struct PatternInForeign { 582 #[primary_span] 583 #[label] 584 pub span: Span, 585 } 586 587 #[derive(Diagnostic)] 588 #[diag(ast_passes_pattern_in_bodiless, code = "E0642")] 589 pub struct PatternInBodiless { 590 #[primary_span] 591 #[label] 592 pub span: Span, 593 } 594 595 #[derive(Diagnostic)] 596 #[diag(ast_passes_equality_in_where)] 597 #[note] 598 pub struct EqualityInWhere { 599 #[primary_span] 600 #[label] 601 pub span: Span, 602 #[subdiagnostic] 603 pub assoc: Option<AssociatedSuggestion>, 604 #[subdiagnostic] 605 pub assoc2: Option<AssociatedSuggestion2>, 606 } 607 608 #[derive(Subdiagnostic)] 609 #[suggestion( 610 ast_passes_suggestion, 611 code = "{param}: {path}", 612 style = "verbose", 613 applicability = "maybe-incorrect" 614 )] 615 pub struct AssociatedSuggestion { 616 #[primary_span] 617 pub span: Span, 618 pub ident: Ident, 619 pub param: Ident, 620 pub path: String, 621 } 622 623 #[derive(Subdiagnostic)] 624 #[multipart_suggestion(ast_passes_suggestion_path, applicability = "maybe-incorrect")] 625 pub struct AssociatedSuggestion2 { 626 #[suggestion_part(code = "{args}")] 627 pub span: Span, 628 pub args: String, 629 #[suggestion_part(code = "")] 630 pub predicate: Span, 631 pub trait_segment: Ident, 632 pub potential_assoc: Ident, 633 } 634 635 #[derive(Diagnostic)] 636 #[diag(ast_passes_stability_outside_std, code = "E0734")] 637 pub struct StabilityOutsideStd { 638 #[primary_span] 639 pub span: Span, 640 } 641 642 #[derive(Diagnostic)] 643 #[diag(ast_passes_feature_on_non_nightly, code = "E0554")] 644 pub struct FeatureOnNonNightly { 645 #[primary_span] 646 pub span: Span, 647 pub channel: &'static str, 648 #[subdiagnostic] 649 pub stable_features: Vec<StableFeature>, 650 #[suggestion(code = "", applicability = "machine-applicable")] 651 pub sugg: Option<Span>, 652 } 653 654 pub struct StableFeature { 655 pub name: Symbol, 656 pub since: Symbol, 657 } 658 659 impl AddToDiagnostic for StableFeature { add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, _: F) where F: Fn( &mut rustc_errors::Diagnostic, rustc_errors::SubdiagnosticMessage, ) -> rustc_errors::SubdiagnosticMessage,660 fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, _: F) 661 where 662 F: Fn( 663 &mut rustc_errors::Diagnostic, 664 rustc_errors::SubdiagnosticMessage, 665 ) -> rustc_errors::SubdiagnosticMessage, 666 { 667 diag.set_arg("name", self.name); 668 diag.set_arg("since", self.since); 669 diag.help(fluent::ast_passes_stable_since); 670 } 671 } 672 673 #[derive(Diagnostic)] 674 #[diag(ast_passes_incompatible_features)] 675 #[help] 676 pub struct IncompatibleFeatures { 677 #[primary_span] 678 pub spans: Vec<Span>, 679 pub f1: Symbol, 680 pub f2: Symbol, 681 } 682 683 #[derive(Diagnostic)] 684 #[diag(ast_passes_show_span)] 685 pub struct ShowSpan { 686 #[primary_span] 687 pub span: Span, 688 pub msg: &'static str, 689 } 690 691 #[derive(Diagnostic)] 692 #[diag(ast_passes_negative_bound_not_supported)] 693 pub struct NegativeBoundUnsupported { 694 #[primary_span] 695 pub span: Span, 696 } 697 698 #[derive(Diagnostic)] 699 #[diag(ast_passes_constraint_on_negative_bound)] 700 pub struct ConstraintOnNegativeBound { 701 #[primary_span] 702 pub span: Span, 703 } 704