1 //! Errors emitted by ast_passes.
3 use rustc_errors::{fluent, AddToDiagnostic, Applicability, Diagnostic};
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(self, diag: &mut Diagnostic) {
22 Self::GenericForbidden => {}
23 Self::NotSupportedOr(span) => {
24 diag.span_note(span, fluent::ast_passes::not_supported_or);
26 Self::NotSupportedParentheses(span) => {
27 diag.span_note(span, fluent::ast_passes::not_supported_parentheses);
34 #[diag(ast_passes::forbidden_let_stable)]
36 pub struct ForbiddenLetStable {
42 #[diag(ast_passes::forbidden_assoc_constraint)]
43 pub struct ForbiddenAssocConstraint {
49 #[diag(ast_passes::keyword_lifetime)]
50 pub struct KeywordLifetime {
56 #[diag(ast_passes::invalid_label)]
57 pub struct InvalidLabel {
64 #[diag(ast_passes::invalid_visibility, code = "E0449")]
65 pub struct InvalidVisibility {
68 #[label(ast_passes::implied)]
69 pub implied: Option<Span>,
71 pub note: Option<InvalidVisibilityNote>,
74 #[derive(Subdiagnostic)]
75 pub enum InvalidVisibilityNote {
76 #[note(ast_passes::individual_impl_items)]
78 #[note(ast_passes::individual_foreign_items)]
79 IndividualForeignItems,
83 #[diag(ast_passes::trait_fn_const, code = "E0379")]
84 pub struct TraitFnConst {
91 #[diag(ast_passes::forbidden_lifetime_bound)]
92 pub struct ForbiddenLifetimeBound {
98 #[diag(ast_passes::forbidden_non_lifetime_param)]
99 pub struct ForbiddenNonLifetimeParam {
101 pub spans: Vec<Span>,
104 #[derive(Diagnostic)]
105 #[diag(ast_passes::fn_param_too_many)]
106 pub struct FnParamTooMany {
109 pub max_num_args: usize,
112 #[derive(Diagnostic)]
113 #[diag(ast_passes::fn_param_c_var_args_only)]
114 pub struct FnParamCVarArgsOnly {
119 #[derive(Diagnostic)]
120 #[diag(ast_passes::fn_param_c_var_args_not_last)]
121 pub struct FnParamCVarArgsNotLast {
126 #[derive(Diagnostic)]
127 #[diag(ast_passes::fn_param_doc_comment)]
128 pub struct FnParamDocComment {
134 #[derive(Diagnostic)]
135 #[diag(ast_passes::fn_param_forbidden_attr)]
136 pub struct FnParamForbiddenAttr {
141 #[derive(Diagnostic)]
142 #[diag(ast_passes::fn_param_forbidden_self)]
144 pub struct FnParamForbiddenSelf {
150 #[derive(Diagnostic)]
151 #[diag(ast_passes::forbidden_default)]
152 pub struct ForbiddenDefault {
159 #[derive(Diagnostic)]
160 #[diag(ast_passes::assoc_const_without_body)]
161 pub struct AssocConstWithoutBody {
164 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
165 pub replace_span: Span,
168 #[derive(Diagnostic)]
169 #[diag(ast_passes::assoc_fn_without_body)]
170 pub struct AssocFnWithoutBody {
173 #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
174 pub replace_span: Span,
177 #[derive(Diagnostic)]
178 #[diag(ast_passes::assoc_type_without_body)]
179 pub struct AssocTypeWithoutBody {
182 #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
183 pub replace_span: Span,
186 #[derive(Diagnostic)]
187 #[diag(ast_passes::const_without_body)]
188 pub struct ConstWithoutBody {
191 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
192 pub replace_span: Span,
195 #[derive(Diagnostic)]
196 #[diag(ast_passes::static_without_body)]
197 pub struct StaticWithoutBody {
200 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
201 pub replace_span: Span,
204 #[derive(Diagnostic)]
205 #[diag(ast_passes::ty_alias_without_body)]
206 pub struct TyAliasWithoutBody {
209 #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
210 pub replace_span: Span,
213 #[derive(Diagnostic)]
214 #[diag(ast_passes::fn_without_body)]
215 pub struct FnWithoutBody {
218 #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
219 pub replace_span: Span,
221 pub extern_block_suggestion: Option<ExternBlockSuggestion>,
224 pub struct ExternBlockSuggestion {
225 pub start_span: Span,
227 pub abi: Option<Symbol>,
230 impl AddToDiagnostic for ExternBlockSuggestion {
231 fn add_to_diagnostic(self, diag: &mut Diagnostic) {
232 let start_suggestion = if let Some(abi) = self.abi {
233 format!("extern \"{}\" {{", abi)
235 "extern {".to_owned()
237 let end_suggestion = " }".to_owned();
239 diag.multipart_suggestion(
240 fluent::ast_passes::extern_block_suggestion,
241 vec![(self.start_span, start_suggestion), (self.end_span, end_suggestion)],
242 Applicability::MaybeIncorrect,