Auto merge of #101558 - JhonnyBillM:session-diagnostic-to-diagnostic-handler-refactor, r=davidtwco

Move and rename `SessionDiagnostic` & `SessionSubdiagnostic` traits and macros

After PR #101434, we want to:
- [x] Move `SessionDiagnostic` to `rustc_errors`.
- [x] Add `emit_` methods that accept `impl SessionDiagnostic` to `Handler`.
- [x] _(optional)_ Rename trait `SessionDiagnostic` to `DiagnosticHandler`.
- [x] _(optional)_ Rename macro `SessionDiagnostic` to `DiagnosticHandler`.
- [x] Update Rustc Dev Guide and Docs to reflect these changes. https://github.com/rust-lang/rustc-dev-guide/pull/1460

Now I am having build issues getting the compiler to build when trying to rename the macro.

<details>
  <summary>See diagnostics errors and context when building.</summary>

```
error: diagnostics should only be created in `SessionDiagnostic`/`AddSubdiagnostic` impls
  --> compiler/rustc_attr/src/session_diagnostics.rs:13:10
   |
13 |   #[derive(DiagnosticHandler)]
   |            ^^^^^^^^^^^^^^^^^ in this derive macro expansion
   |
  ::: /Users/jhonny/.cargo/registry/src/github.com-1ecc6299db9ec823/synstructure-0.12.6/src/macros.rs:94:9
   |
94 | /         pub fn $derives(
95 | |             i: $crate::macros::TokenStream
96 | |         ) -> $crate::macros::TokenStream {
   | |________________________________________- in this expansion of `#[derive(DiagnosticHandler)]`
   |
note: the lint level is defined here
  --> compiler/rustc_attr/src/lib.rs:10:9
   |
10 | #![deny(rustc::diagnostic_outside_of_impl)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

```

And also this one:

```
error: diagnostics should only be created in `SessionDiagnostic`/`AddSubdiagnostic` impls
   --> compiler/rustc_attr/src/session_diagnostics.rs:213:32
    |
213 |         let mut diag = handler.struct_span_err_with_code(
    |                                ^^^^^^^^^^^^^^^^^^^^^^^^^
```

> **Note**
> Can't find where this message is coming from, because you can see in [this experimental branch](https://github.com/JhonnyBillM/rust/tree/experimental/trying-to-rename-session-diagnostic-macro)  that I updated all errors and diags to say:
> error: diagnostics should only be created in **`DiagnosticHandler`**/`AddSubdiagnostic` impls
> and not:
> error: diagnostics should only be created in **`SessionDiagnostic`**/`AddSubdiagnostic` impls

</details>

I tried building the compiler in different ways (playing with the stages etc), but nothing worked.

## Question

**Do we need to build or do something different when renaming a macro and identifiers?**

For context, see experimental commit f2193a98b4 where the macro and symbols are renamed, but it doesn't compile.
This commit is contained in:
bors 2022-09-21 19:58:39 +00:00
commit 9062b780b3
56 changed files with 833 additions and 792 deletions

View file

@ -1,10 +1,10 @@
use hir::GenericParamKind;
use rustc_errors::{
fluent, AddSubdiagnostic, Applicability, DiagnosticMessage, DiagnosticStyledString, MultiSpan,
fluent, AddToDiagnostic, Applicability, DiagnosticMessage, DiagnosticStyledString, MultiSpan,
};
use rustc_hir as hir;
use rustc_hir::{FnRetTy, Ty};
use rustc_macros::SessionDiagnostic;
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_middle::ty::{Region, TyCtxt};
use rustc_span::symbol::kw;
use rustc_span::{symbol::Ident, BytePos, Span};
@ -16,7 +16,7 @@ use crate::infer::error_reporting::{
pub mod note_and_explain;
#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(infer::opaque_hidden_type)]
pub struct OpaqueHiddenTypeDiag {
#[primary_span]
@ -28,7 +28,7 @@ pub struct OpaqueHiddenTypeDiag {
pub hidden_type: Span,
}
#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(infer::type_annotations_needed, code = "E0282")]
pub struct AnnotationRequired<'a> {
#[primary_span]
@ -46,7 +46,7 @@ pub struct AnnotationRequired<'a> {
}
// Copy of `AnnotationRequired` for E0283
#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(infer::type_annotations_needed, code = "E0283")]
pub struct AmbigousImpl<'a> {
#[primary_span]
@ -64,7 +64,7 @@ pub struct AmbigousImpl<'a> {
}
// Copy of `AnnotationRequired` for E0284
#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(infer::type_annotations_needed, code = "E0284")]
pub struct AmbigousReturn<'a> {
#[primary_span]
@ -81,7 +81,7 @@ pub struct AmbigousReturn<'a> {
pub multi_suggestions: Vec<SourceKindMultiSuggestion<'a>>,
}
#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(infer::need_type_info_in_generator, code = "E0698")]
pub struct NeedTypeInfoInGenerator<'a> {
#[primary_span]
@ -92,7 +92,7 @@ pub struct NeedTypeInfoInGenerator<'a> {
}
// Used when a better one isn't available
#[derive(SessionSubdiagnostic)]
#[derive(Subdiagnostic)]
#[label(infer::label_bad)]
pub struct InferenceBadError<'a> {
#[primary_span]
@ -106,7 +106,7 @@ pub struct InferenceBadError<'a> {
pub name: String,
}
#[derive(SessionSubdiagnostic)]
#[derive(Subdiagnostic)]
pub enum SourceKindSubdiag<'a> {
#[suggestion_verbose(
infer::source_kind_subdiag_let,
@ -147,7 +147,7 @@ pub enum SourceKindSubdiag<'a> {
},
}
#[derive(SessionSubdiagnostic)]
#[derive(Subdiagnostic)]
pub enum SourceKindMultiSuggestion<'a> {
#[multipart_suggestion_verbose(
infer::source_kind_fully_qualified,
@ -228,7 +228,7 @@ pub enum RegionOriginNote<'a> {
},
}
impl AddSubdiagnostic for RegionOriginNote<'_> {
impl AddToDiagnostic for RegionOriginNote<'_> {
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
let mut label_or_note = |span, msg: DiagnosticMessage| {
let sub_count = diag.children.iter().filter(|d| d.span.is_dummy()).count();
@ -289,7 +289,7 @@ pub enum LifetimeMismatchLabels {
},
}
impl AddSubdiagnostic for LifetimeMismatchLabels {
impl AddToDiagnostic for LifetimeMismatchLabels {
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
match self {
LifetimeMismatchLabels::InRet { param_span, ret_span, span, label_var1 } => {
@ -339,7 +339,7 @@ pub struct AddLifetimeParamsSuggestion<'a> {
pub add_note: bool,
}
impl AddSubdiagnostic for AddLifetimeParamsSuggestion<'_> {
impl AddToDiagnostic for AddLifetimeParamsSuggestion<'_> {
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
let mut mk_suggestion = || {
let (
@ -422,7 +422,7 @@ impl AddSubdiagnostic for AddLifetimeParamsSuggestion<'_> {
}
}
#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(infer::lifetime_mismatch, code = "E0623")]
pub struct LifetimeMismatch<'a> {
#[primary_span]
@ -438,7 +438,7 @@ pub struct IntroducesStaticBecauseUnmetLifetimeReq {
pub binding_span: Span,
}
impl AddSubdiagnostic for IntroducesStaticBecauseUnmetLifetimeReq {
impl AddToDiagnostic for IntroducesStaticBecauseUnmetLifetimeReq {
fn add_to_diagnostic(mut self, diag: &mut rustc_errors::Diagnostic) {
self.unmet_requirements
.push_span_label(self.binding_span, fluent::infer::msl_introduces_static);
@ -450,7 +450,7 @@ pub struct ImplNote {
pub impl_span: Option<Span>,
}
impl AddSubdiagnostic for ImplNote {
impl AddToDiagnostic for ImplNote {
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
match self.impl_span {
Some(span) => diag.span_note(span, fluent::infer::msl_impl_note),
@ -465,7 +465,7 @@ pub enum TraitSubdiag {
}
// FIXME(#100717) used in `Vec<TraitSubdiag>` so requires eager translation/list support
impl AddSubdiagnostic for TraitSubdiag {
impl AddToDiagnostic for TraitSubdiag {
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
match self {
TraitSubdiag::Note { span } => {
@ -483,7 +483,7 @@ impl AddSubdiagnostic for TraitSubdiag {
}
}
#[derive(SessionDiagnostic)]
#[derive(Diagnostic)]
#[diag(infer::mismatched_static_lifetime)]
pub struct MismatchedStaticLifetime<'a> {
#[primary_span]

View file

@ -1,5 +1,5 @@
use crate::infer::error_reporting::nice_region_error::find_anon_type;
use rustc_errors::{self, fluent, AddSubdiagnostic, IntoDiagnosticArg};
use rustc_errors::{self, fluent, AddToDiagnostic, IntoDiagnosticArg};
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::{symbol::kw, Span};
@ -158,7 +158,7 @@ impl RegionExplanation<'_> {
}
}
impl AddSubdiagnostic for RegionExplanation<'_> {
impl AddToDiagnostic for RegionExplanation<'_> {
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
if let Some(span) = self.desc.span {
diag.span_note(span, fluent::infer::region_explanation);

View file

@ -4,6 +4,7 @@ use crate::errors::{
};
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::InferCtxt;
use rustc_errors::IntoDiagnostic;
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, IntoDiagnosticArg};
use rustc_hir as hir;
use rustc_hir::def::Res;
@ -18,7 +19,6 @@ use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Print, Printer};
use rustc_middle::ty::{self, DefIdTree, InferConst};
use rustc_middle::ty::{GenericArg, GenericArgKind, SubstsRef};
use rustc_middle::ty::{IsSuggestable, Ty, TyCtxt, TypeckResults};
use rustc_session::SessionDiagnostic;
use rustc_span::symbol::{kw, Ident};
use rustc_span::{BytePos, Span};
use std::borrow::Cow;

View file

@ -11,7 +11,7 @@ use crate::infer::lexical_region_resolve::RegionResolutionError;
use crate::infer::SubregionOrigin;
use crate::infer::TyCtxt;
use rustc_errors::AddSubdiagnostic;
use rustc_errors::AddToDiagnostic;
use rustc_errors::{Diagnostic, ErrorGuaranteed};
use rustc_hir::Ty;
use rustc_middle::ty::Region;

View file

@ -2,7 +2,7 @@ use crate::errors::RegionOriginNote;
use crate::infer::error_reporting::note_and_explain_region;
use crate::infer::{self, InferCtxt, SubregionOrigin};
use rustc_errors::{
fluent, struct_span_err, AddSubdiagnostic, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
fluent, struct_span_err, AddToDiagnostic, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
};
use rustc_middle::traits::ObligationCauseCode;
use rustc_middle::ty::error::TypeError;