macros: add diagnostic derive for lints

`SessionDiagnostic` isn't suitable for use on lints as whether or not it
creates an error or a warning is decided at compile-time by the macro,
whereas lints decide this at runtime based on the location of the lint
being reported (as it will depend on the user's `allow`/`deny`
attributes, etc). Re-using most of the machinery for
`SessionDiagnostic`, this macro introduces a `LintDiagnostic` derive
which implements a `DecorateLint` trait, taking a
`LintDiagnosticBuilder` and adding to the lint according to the
diagnostic struct.
This commit is contained in:
David Wood
2022-06-30 08:57:45 +01:00
parent 7f9d8480d6
commit 9d864c8d56
12 changed files with 847 additions and 614 deletions

View File

@@ -1,8 +1,7 @@
#![deny(unused_must_use)]
use crate::diagnostics::error::{
span_err, throw_invalid_attr, throw_invalid_nested_attr, throw_span_err,
SessionDiagnosticDeriveError,
span_err, throw_invalid_attr, throw_invalid_nested_attr, throw_span_err, DiagnosticDeriveError,
};
use crate::diagnostics::utils::{
report_error_if_not_applied_to_applicability, report_error_if_not_applied_to_span,
@@ -214,7 +213,7 @@ impl<'a> HasFieldMap for SessionSubdiagnosticDeriveBuilder<'a> {
}
impl<'a> SessionSubdiagnosticDeriveBuilder<'a> {
fn identify_kind(&mut self) -> Result<(), SessionDiagnosticDeriveError> {
fn identify_kind(&mut self) -> Result<(), DiagnosticDeriveError> {
for attr in self.variant.ast().attrs {
let span = attr.span().unwrap();
@@ -351,7 +350,7 @@ impl<'a> SessionSubdiagnosticDeriveBuilder<'a> {
&mut self,
binding: &BindingInfo<'_>,
is_suggestion: bool,
) -> Result<TokenStream, SessionDiagnosticDeriveError> {
) -> Result<TokenStream, DiagnosticDeriveError> {
let ast = binding.ast();
let inner_ty = FieldInnerTy::from_type(&ast.ty);
@@ -411,7 +410,7 @@ impl<'a> SessionSubdiagnosticDeriveBuilder<'a> {
Ok(inner_ty.with(binding, generated))
}
fn into_tokens(&mut self) -> Result<TokenStream, SessionDiagnosticDeriveError> {
fn into_tokens(&mut self) -> Result<TokenStream, DiagnosticDeriveError> {
self.identify_kind()?;
let Some(kind) = self.kind.map(|(kind, _)| kind) else {
throw_span_err!(