Migrate ast_lowering::lib and ast_lowering::item to SessionDiagnostic

This commit is contained in:
Jean CASPAR 2022-08-17 16:58:57 +02:00
parent 73ae38bac1
commit 0043d10c71
4 changed files with 83 additions and 32 deletions

View file

@ -1,6 +1,6 @@
use rustc_errors::{fluent, AddSubdiagnostic, Applicability, Diagnostic};
use rustc_macros::SessionDiagnostic;
use rustc_span::Span;
use rustc_span::{Span, Symbol};
#[derive(SessionDiagnostic, Clone, Copy)]
#[error(ast_lowering::generic_type_with_parentheses, code = "E0214")]
@ -27,3 +27,54 @@ impl AddSubdiagnostic for UseAngleBrackets {
);
}
}
#[derive(SessionDiagnostic)]
#[help]
#[error(ast_lowering::invalid_abi, code = "E0703")]
pub struct InvalidAbi {
#[primary_span]
#[label]
pub span: Span,
pub abi: Symbol,
pub valid_abis: String,
}
#[derive(SessionDiagnostic, Clone, Copy)]
#[error(ast_lowering::assoc_ty_parentheses)]
pub struct AssocTyParentheses {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub sub: AssocTyParenthesesSub,
}
#[derive(Clone, Copy)]
pub enum AssocTyParenthesesSub {
Empty { parentheses_span: Span },
NotEmpty { open_param: Span, close_param: Span },
}
impl AddSubdiagnostic for AssocTyParenthesesSub {
fn add_to_diagnostic(self, diag: &mut Diagnostic) {
match self {
Self::Empty { parentheses_span } => diag.multipart_suggestion(
fluent::ast_lowering::remove_parentheses,
vec![(parentheses_span, String::new())],
Applicability::MaybeIncorrect,
),
Self::NotEmpty { open_param, close_param } => diag.multipart_suggestion(
fluent::ast_lowering::use_angle_brackets,
vec![(open_param, String::from("<")), (close_param, String::from(">"))],
Applicability::MaybeIncorrect,
),
};
}
}
#[derive(SessionDiagnostic)]
#[error(ast_lowering::misplaced_impl_trait, code = "E0562")]
pub struct MisplacedImplTrait {
#[primary_span]
pub span: Span,
pub position: String,
}

View file

@ -1,3 +1,4 @@
use super::errors::InvalidAbi;
use super::ResolverAstLoweringExt;
use super::{AstOwner, ImplTraitContext, ImplTraitPosition};
use super::{FnDeclKind, LoweringContext, ParamMode};
@ -7,7 +8,6 @@ use rustc_ast::visit::AssocCtxt;
use rustc_ast::*;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sorted_map::SortedMap;
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
@ -1260,10 +1260,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
fn error_on_invalid_abi(&self, abi: StrLit) {
struct_span_err!(self.tcx.sess, abi.span, E0703, "invalid ABI: found `{}`", abi.symbol)
.span_label(abi.span, "invalid ABI")
.help(&format!("valid ABIs: {}", abi::all_names().join(", ")))
.emit();
self.tcx.sess.emit_err(InvalidAbi {
span: abi.span,
abi: abi.symbol,
valid_abis: abi::all_names().join(", "),
});
}
fn lower_asyncness(&mut self, a: Async) -> hir::IsAsync {

View file

@ -39,6 +39,8 @@
#[macro_use]
extern crate tracing;
use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait};
use rustc_ast::ptr::P;
use rustc_ast::visit;
use rustc_ast::{self as ast, *};
@ -49,7 +51,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sorted_map::SortedMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::Lrc;
use rustc_errors::{struct_span_err, Applicability, Handler, StashKey};
use rustc_errors::{Handler, StashKey};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
@ -1071,19 +1073,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
fn emit_bad_parenthesized_trait_in_assoc_ty(&self, data: &ParenthesizedArgs) {
let mut err = self.tcx.sess.struct_span_err(
data.span,
"parenthesized generic arguments cannot be used in associated type constraints",
);
// Suggest removing empty parentheses: "Trait()" -> "Trait"
if data.inputs.is_empty() {
let sub = if data.inputs.is_empty() {
let parentheses_span =
data.inputs_span.shrink_to_lo().to(data.inputs_span.shrink_to_hi());
err.multipart_suggestion(
"remove these parentheses",
vec![(parentheses_span, String::new())],
Applicability::MaybeIncorrect,
);
AssocTyParenthesesSub::Empty { parentheses_span }
}
// Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
else {
@ -1097,13 +1091,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// End of last argument to end of parameters
let close_param =
data.inputs.last().unwrap().span.shrink_to_hi().to(data.inputs_span.shrink_to_hi());
err.multipart_suggestion(
&format!("use angle brackets instead",),
vec![(open_param, String::from("<")), (close_param, String::from(">"))],
Applicability::MaybeIncorrect,
);
}
err.emit();
AssocTyParenthesesSub::NotEmpty { open_param, close_param }
};
self.tcx.sess.emit_err(AssocTyParentheses { span: data.span, sub });
}
#[instrument(level = "debug", skip(self))]
@ -1342,14 +1332,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
path
}
ImplTraitContext::Disallowed(position) => {
let mut err = struct_span_err!(
self.tcx.sess,
t.span,
E0562,
"`impl Trait` only allowed in function and inherent method return types, not in {}",
position
);
err.emit();
self.tcx.sess.emit_err(MisplacedImplTrait {
span: t.span,
position: position.to_string(),
});
hir::TyKind::Err
}
}

View file

@ -3,3 +3,16 @@ ast_lowering_generic_type_with_parentheses =
.label = only `Fn` traits may use parentheses
ast_lowering_use_angle_brackets = use angle brackets instead
ast_lowering_invalid_abi =
invalid ABI: found `{$abi}`
.label = invalid ABI
.help = valid ABIs: {$valid_abis}
ast_lowering_assoc_ty_parentheses =
parenthesized generic arguments cannot be used in associated type constraints
ast_lowering_remove_parentheses = remove these parentheses
ast_lowering_misplaced_impl_trait =
`impl Trait` only allowed in function and inherent method return types, not in {$position}