1 //! Errors emitted by ast_passes.
3 use rustc_errors::{fluent, AddToDiagnostic, Applicability, Diagnostic, SubdiagnosticMessage};
4 use rustc_macros::{Diagnostic, Subdiagnostic};
5 use rustc_span::{Span, Symbol};
7 use crate::ast_validation::ForbiddenLetReason;
10 #[diag(ast_passes_forbidden_let)]
12 pub struct ForbiddenLet {
16 pub(crate) reason: ForbiddenLetReason,
20 #[diag(ast_passes_forbidden_let_stable)]
22 pub struct ForbiddenLetStable {
28 #[diag(ast_passes_forbidden_assoc_constraint)]
29 pub struct ForbiddenAssocConstraint {
35 #[diag(ast_passes_keyword_lifetime)]
36 pub struct KeywordLifetime {
42 #[diag(ast_passes_invalid_label)]
43 pub struct InvalidLabel {
50 #[diag(ast_passes_invalid_visibility, code = "E0449")]
51 pub struct InvalidVisibility {
55 pub implied: Option<Span>,
57 pub note: Option<InvalidVisibilityNote>,
60 #[derive(Subdiagnostic)]
61 pub enum InvalidVisibilityNote {
62 #[note(individual_impl_items)]
64 #[note(individual_foreign_items)]
65 IndividualForeignItems,
69 #[diag(ast_passes_trait_fn_const, code = "E0379")]
70 pub struct TraitFnConst {
77 #[diag(ast_passes_forbidden_lifetime_bound)]
78 pub struct ForbiddenLifetimeBound {
84 #[diag(ast_passes_forbidden_non_lifetime_param)]
85 pub struct ForbiddenNonLifetimeParam {
91 #[diag(ast_passes_fn_param_too_many)]
92 pub struct FnParamTooMany {
95 pub max_num_args: usize,
99 #[diag(ast_passes_fn_param_c_var_args_only)]
100 pub struct FnParamCVarArgsOnly {
105 #[derive(Diagnostic)]
106 #[diag(ast_passes_fn_param_c_var_args_not_last)]
107 pub struct FnParamCVarArgsNotLast {
112 #[derive(Diagnostic)]
113 #[diag(ast_passes_fn_param_doc_comment)]
114 pub struct FnParamDocComment {
120 #[derive(Diagnostic)]
121 #[diag(ast_passes_fn_param_forbidden_attr)]
122 pub struct FnParamForbiddenAttr {
127 #[derive(Diagnostic)]
128 #[diag(ast_passes_fn_param_forbidden_self)]
130 pub struct FnParamForbiddenSelf {
136 #[derive(Diagnostic)]
137 #[diag(ast_passes_forbidden_default)]
138 pub struct ForbiddenDefault {
145 #[derive(Diagnostic)]
146 #[diag(ast_passes_assoc_const_without_body)]
147 pub struct AssocConstWithoutBody {
150 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
151 pub replace_span: Span,
154 #[derive(Diagnostic)]
155 #[diag(ast_passes_assoc_fn_without_body)]
156 pub struct AssocFnWithoutBody {
159 #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
160 pub replace_span: Span,
163 #[derive(Diagnostic)]
164 #[diag(ast_passes_assoc_type_without_body)]
165 pub struct AssocTypeWithoutBody {
168 #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
169 pub replace_span: Span,
172 #[derive(Diagnostic)]
173 #[diag(ast_passes_const_without_body)]
174 pub struct ConstWithoutBody {
177 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
178 pub replace_span: Span,
181 #[derive(Diagnostic)]
182 #[diag(ast_passes_static_without_body)]
183 pub struct StaticWithoutBody {
186 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
187 pub replace_span: Span,
190 #[derive(Diagnostic)]
191 #[diag(ast_passes_ty_alias_without_body)]
192 pub struct TyAliasWithoutBody {
195 #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
196 pub replace_span: Span,
199 #[derive(Diagnostic)]
200 #[diag(ast_passes_fn_without_body)]
201 pub struct FnWithoutBody {
204 #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
205 pub replace_span: Span,
207 pub extern_block_suggestion: Option<ExternBlockSuggestion>,
210 pub struct ExternBlockSuggestion {
211 pub start_span: Span,
213 pub abi: Option<Symbol>,
216 impl AddToDiagnostic for ExternBlockSuggestion {
217 fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
219 F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
221 let start_suggestion = if let Some(abi) = self.abi {
222 format!("extern \"{}\" {{", abi)
224 "extern {".to_owned()
226 let end_suggestion = " }".to_owned();
228 diag.multipart_suggestion(
229 fluent::extern_block_suggestion,
230 vec![(self.start_span, start_suggestion), (self.end_span, end_suggestion)],
231 Applicability::MaybeIncorrect,