1 //! Errors emitted by ast_passes.
3 use rustc_errors::{fluent, AddSubdiagnostic, Applicability, Diagnostic};
4 use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
5 use rustc_span::{Span, Symbol};
7 use crate::ast_validation::ForbiddenLetReason;
9 #[derive(SessionDiagnostic)]
10 #[diag(ast_passes::forbidden_let)]
12 pub struct ForbiddenLet {
16 pub(crate) reason: ForbiddenLetReason,
19 impl AddSubdiagnostic 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);
33 #[derive(SessionDiagnostic)]
34 #[diag(ast_passes::forbidden_let_stable)]
36 pub struct ForbiddenLetStable {
41 #[derive(SessionDiagnostic)]
42 #[diag(ast_passes::forbidden_assoc_constraint)]
43 pub struct ForbiddenAssocConstraint {
48 #[derive(SessionDiagnostic)]
49 #[diag(ast_passes::keyword_lifetime)]
50 pub struct KeywordLifetime {
55 #[derive(SessionDiagnostic)]
56 #[diag(ast_passes::invalid_label)]
57 pub struct InvalidLabel {
63 #[derive(SessionDiagnostic)]
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(SessionSubdiagnostic)]
75 pub enum InvalidVisibilityNote {
76 #[note(ast_passes::individual_impl_items)]
78 #[note(ast_passes::individual_foreign_items)]
79 IndividualForeignItems,
82 #[derive(SessionDiagnostic)]
83 #[diag(ast_passes::trait_fn_async, code = "E0706")]
85 #[note(ast_passes::note2)]
86 pub struct TraitFnAsync {
93 #[derive(SessionDiagnostic)]
94 #[diag(ast_passes::trait_fn_const, code = "E0379")]
95 pub struct TraitFnConst {
101 #[derive(SessionDiagnostic)]
102 #[diag(ast_passes::forbidden_lifetime_bound)]
103 pub struct ForbiddenLifetimeBound {
105 pub spans: Vec<Span>,
108 #[derive(SessionDiagnostic)]
109 #[diag(ast_passes::forbidden_non_lifetime_param)]
110 pub struct ForbiddenNonLifetimeParam {
112 pub spans: Vec<Span>,
115 #[derive(SessionDiagnostic)]
116 #[diag(ast_passes::fn_param_too_many)]
117 pub struct FnParamTooMany {
120 pub max_num_args: usize,
123 #[derive(SessionDiagnostic)]
124 #[diag(ast_passes::fn_param_c_var_args_only)]
125 pub struct FnParamCVarArgsOnly {
130 #[derive(SessionDiagnostic)]
131 #[diag(ast_passes::fn_param_c_var_args_not_last)]
132 pub struct FnParamCVarArgsNotLast {
137 #[derive(SessionDiagnostic)]
138 #[diag(ast_passes::fn_param_doc_comment)]
139 pub struct FnParamDocComment {
145 #[derive(SessionDiagnostic)]
146 #[diag(ast_passes::fn_param_forbidden_attr)]
147 pub struct FnParamForbiddenAttr {
152 #[derive(SessionDiagnostic)]
153 #[diag(ast_passes::fn_param_forbidden_self)]
155 pub struct FnParamForbiddenSelf {
161 #[derive(SessionDiagnostic)]
162 #[diag(ast_passes::forbidden_default)]
163 pub struct ForbiddenDefault {
170 #[derive(SessionDiagnostic)]
171 #[diag(ast_passes::assoc_const_without_body)]
172 pub struct AssocConstWithoutBody {
175 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
176 pub replace_span: Span,
179 #[derive(SessionDiagnostic)]
180 #[diag(ast_passes::assoc_fn_without_body)]
181 pub struct AssocFnWithoutBody {
184 #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
185 pub replace_span: Span,
188 #[derive(SessionDiagnostic)]
189 #[diag(ast_passes::assoc_type_without_body)]
190 pub struct AssocTypeWithoutBody {
193 #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
194 pub replace_span: Span,
197 #[derive(SessionDiagnostic)]
198 #[diag(ast_passes::const_without_body)]
199 pub struct ConstWithoutBody {
202 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
203 pub replace_span: Span,
206 #[derive(SessionDiagnostic)]
207 #[diag(ast_passes::static_without_body)]
208 pub struct StaticWithoutBody {
211 #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
212 pub replace_span: Span,
215 #[derive(SessionDiagnostic)]
216 #[diag(ast_passes::ty_alias_without_body)]
217 pub struct TyAliasWithoutBody {
220 #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
221 pub replace_span: Span,
224 #[derive(SessionDiagnostic)]
225 #[diag(ast_passes::fn_without_body)]
226 pub struct FnWithoutBody {
229 #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
230 pub replace_span: Span,
232 pub extern_block_suggestion: Option<ExternBlockSuggestion>,
235 pub struct ExternBlockSuggestion {
236 pub start_span: Span,
238 pub abi: Option<Symbol>,
241 impl AddSubdiagnostic for ExternBlockSuggestion {
242 fn add_to_diagnostic(self, diag: &mut Diagnostic) {
243 let start_suggestion = if let Some(abi) = self.abi {
244 format!("extern \"{}\" {{", abi)
246 "extern {".to_owned()
248 let end_suggestion = " }".to_owned();
250 diag.multipart_suggestion(
251 fluent::ast_passes::extern_block_suggestion,
252 vec![(self.start_span, start_suggestion), (self.end_span, end_suggestion)],
253 Applicability::MaybeIncorrect,