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,
19 impl AddToDiagnostic for ForbiddenLetReason {
20 fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
22 F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
25 Self::GenericForbidden => {}
26 Self::NotSupportedOr(span) => {
27 diag.span_note(span, fluent::ast_passes::not_supported_or);
29 Self::NotSupportedParentheses(span) => {
30 diag.span_note(span, fluent::ast_passes::not_supported_parentheses);
37 #[diag(ast_passes::forbidden_let_stable)]
39 pub struct ForbiddenLetStable {
45 #[diag(ast_passes::forbidden_assoc_constraint)]
46 pub struct ForbiddenAssocConstraint {
52 #[diag(ast_passes::keyword_lifetime)]
53 pub struct KeywordLifetime {
59 #[diag(ast_passes::invalid_label)]
60 pub struct InvalidLabel {
67 #[diag(ast_passes::invalid_visibility, code = "E0449")]
68 pub struct InvalidVisibility {
71 #[label(ast_passes::implied)]
72 pub implied: Option<Span>,
74 pub note: Option<InvalidVisibilityNote>,
77 #[derive(Subdiagnostic)]
78 pub enum InvalidVisibilityNote {
79 #[note(ast_passes::individual_impl_items)]
81 #[note(ast_passes::individual_foreign_items)]
82 IndividualForeignItems,
86 #[diag(ast_passes::trait_fn_const, code = "E0379")]
87 pub struct TraitFnConst {
94 #[diag(ast_passes::forbidden_lifetime_bound)]
95 pub struct ForbiddenLifetimeBound {
100 #[derive(Diagnostic)]
101 #[diag(ast_passes::forbidden_non_lifetime_param)]
102 pub struct ForbiddenNonLifetimeParam {
104 pub spans: Vec<Span>,
107 #[derive(Diagnostic)]
108 #[diag(ast_passes::fn_param_too_many)]
109 pub struct FnParamTooMany {
112 pub max_num_args: usize,
115 #[derive(Diagnostic)]
116 #[diag(ast_passes::fn_param_c_var_args_only)]
117 pub struct FnParamCVarArgsOnly {
122 #[derive(Diagnostic)]
123 #[diag(ast_passes::fn_param_c_var_args_not_last)]
124 pub struct FnParamCVarArgsNotLast {
129 #[derive(Diagnostic)]
130 #[diag(ast_passes::fn_param_doc_comment)]
131 pub struct FnParamDocComment {
137 #[derive(Diagnostic)]
138 #[diag(ast_passes::fn_param_forbidden_attr)]
139 pub struct FnParamForbiddenAttr {
144 #[derive(Diagnostic)]
145 #[diag(ast_passes::fn_param_forbidden_self)]
147 pub struct FnParamForbiddenSelf {
153 #[derive(Diagnostic)]
154 #[diag(ast_passes::forbidden_default)]
155 pub struct ForbiddenDefault {
162 #[derive(Diagnostic)]
163 #[diag(ast_passes::assoc_const_without_body)]
164 pub struct AssocConstWithoutBody {
167 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
168 pub replace_span: Span,
171 #[derive(Diagnostic)]
172 #[diag(ast_passes::assoc_fn_without_body)]
173 pub struct AssocFnWithoutBody {
176 #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
177 pub replace_span: Span,
180 #[derive(Diagnostic)]
181 #[diag(ast_passes::assoc_type_without_body)]
182 pub struct AssocTypeWithoutBody {
185 #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
186 pub replace_span: Span,
189 #[derive(Diagnostic)]
190 #[diag(ast_passes::const_without_body)]
191 pub struct ConstWithoutBody {
194 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
195 pub replace_span: Span,
198 #[derive(Diagnostic)]
199 #[diag(ast_passes::static_without_body)]
200 pub struct StaticWithoutBody {
203 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
204 pub replace_span: Span,
207 #[derive(Diagnostic)]
208 #[diag(ast_passes::ty_alias_without_body)]
209 pub struct TyAliasWithoutBody {
212 #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
213 pub replace_span: Span,
216 #[derive(Diagnostic)]
217 #[diag(ast_passes::fn_without_body)]
218 pub struct FnWithoutBody {
221 #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
222 pub replace_span: Span,
224 pub extern_block_suggestion: Option<ExternBlockSuggestion>,
227 pub struct ExternBlockSuggestion {
228 pub start_span: Span,
230 pub abi: Option<Symbol>,
233 impl AddToDiagnostic for ExternBlockSuggestion {
234 fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
236 F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
238 let start_suggestion = if let Some(abi) = self.abi {
239 format!("extern \"{}\" {{", abi)
241 "extern {".to_owned()
243 let end_suggestion = " }".to_owned();
245 diag.multipart_suggestion(
246 fluent::ast_passes::extern_block_suggestion,
247 vec![(self.start_span, start_suggestion), (self.end_span, end_suggestion)],
248 Applicability::MaybeIncorrect,