1
Fork 0

Migrate fn_param_forbidden_self and rename others to have prefix fn_param_

This commit is contained in:
finalchild 2022-08-18 18:20:14 +09:00
parent 07e0bc9600
commit bfefefbcfa
3 changed files with 35 additions and 28 deletions

View file

@ -331,7 +331,7 @@ impl<'a> AstValidator<'a> {
let max_num_args: usize = u16::MAX.into(); let max_num_args: usize = u16::MAX.into();
if fn_decl.inputs.len() > max_num_args { if fn_decl.inputs.len() > max_num_args {
let Param { span, .. } = fn_decl.inputs[0]; let Param { span, .. } = fn_decl.inputs[0];
self.session.emit_err(TooManyParams { span, max_num_args }); self.session.emit_err(FnParamTooMany { span, max_num_args });
} }
} }
@ -339,13 +339,13 @@ impl<'a> AstValidator<'a> {
match &*fn_decl.inputs { match &*fn_decl.inputs {
[Param { ty, span, .. }] => { [Param { ty, span, .. }] => {
if let TyKind::CVarArgs = ty.kind { if let TyKind::CVarArgs = ty.kind {
self.session.emit_err(CVarArgsIsSoleParam { span: *span }); self.session.emit_err(FnParamCVarArgsOnly { span: *span });
} }
} }
[ps @ .., _] => { [ps @ .., _] => {
for Param { ty, span, .. } in ps { for Param { ty, span, .. } in ps {
if let TyKind::CVarArgs = ty.kind { if let TyKind::CVarArgs = ty.kind {
self.session.emit_err(CVarArgsNotLast { span: *span }); self.session.emit_err(FnParamCVarArgsNotLast { span: *span });
} }
} }
} }
@ -372,9 +372,9 @@ impl<'a> AstValidator<'a> {
}) })
.for_each(|attr| { .for_each(|attr| {
if attr.is_doc_comment() { if attr.is_doc_comment() {
self.session.emit_err(DocCommentOnFnParam { span: attr.span }); self.session.emit_err(FnParamDocComment { span: attr.span });
} else { } else {
self.session.emit_err(ForbiddenAttrOnFnParam { span: attr.span }); self.session.emit_err(FnParamForbiddenAttr { span: attr.span });
} }
}); });
} }
@ -382,14 +382,7 @@ impl<'a> AstValidator<'a> {
fn check_decl_self_param(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) { fn check_decl_self_param(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) {
if let (SelfSemantic::No, [param, ..]) = (self_semantic, &*fn_decl.inputs) { if let (SelfSemantic::No, [param, ..]) = (self_semantic, &*fn_decl.inputs) {
if param.is_self() { if param.is_self() {
self.err_handler() self.session.emit_err(FnParamForbiddenSelf { span: param.span });
.struct_span_err(
param.span,
"`self` parameter is only allowed in associated functions",
)
.span_label(param.span, "not semantically valid as function parameter")
.note("associated functions are those in `impl` or `trait` definitions")
.emit();
} }
} }
} }

View file

@ -106,38 +106,47 @@ pub struct ForbiddenNonLifetimeParam {
} }
#[derive(SessionDiagnostic)] #[derive(SessionDiagnostic)]
#[error(ast_passes::too_many_params)] #[error(ast_passes::fn_param_too_many)]
pub struct TooManyParams { pub struct FnParamTooMany {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
pub max_num_args: usize, pub max_num_args: usize,
} }
#[derive(SessionDiagnostic)] #[derive(SessionDiagnostic)]
#[error(ast_passes::c_var_args_is_sole_param)] #[error(ast_passes::fn_param_c_var_args_only)]
pub struct CVarArgsIsSoleParam { pub struct FnParamCVarArgsOnly {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
} }
#[derive(SessionDiagnostic)] #[derive(SessionDiagnostic)]
#[error(ast_passes::c_var_args_not_last)] #[error(ast_passes::fn_param_c_var_args_not_last)]
pub struct CVarArgsNotLast { pub struct FnParamCVarArgsNotLast {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
} }
#[derive(SessionDiagnostic)] #[derive(SessionDiagnostic)]
#[error(ast_passes::doc_comment_on_fn_param)] #[error(ast_passes::fn_param_doc_comment)]
pub struct DocCommentOnFnParam { pub struct FnParamDocComment {
#[primary_span] #[primary_span]
#[label] #[label]
pub span: Span, pub span: Span,
} }
#[derive(SessionDiagnostic)] #[derive(SessionDiagnostic)]
#[error(ast_passes::forbidden_attr_on_fn_param)] #[error(ast_passes::fn_param_forbidden_attr)]
pub struct ForbiddenAttrOnFnParam { pub struct FnParamForbiddenAttr {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
} }
#[derive(SessionDiagnostic)]
#[error(ast_passes::fn_param_forbidden_self)]
#[note]
pub struct FnParamForbiddenSelf {
#[primary_span]
#[label]
pub span: Span,
}

View file

@ -38,18 +38,23 @@ ast_passes_forbidden_lifetime_bound =
ast_passes_forbidden_non_lifetime_param = ast_passes_forbidden_non_lifetime_param =
only lifetime parameters can be used in this context only lifetime parameters can be used in this context
ast_passes_too_many_params = ast_passes_fn_param_too_many =
function can not have more than {$max_num_args} arguments function can not have more than {$max_num_args} arguments
ast_passes_c_var_args_is_sole_param = ast_passes_fn_param_c_var_args_only =
C-variadic function must be declared with at least one named argument C-variadic function must be declared with at least one named argument
ast_passes_c_var_args_not_last = ast_passes_fn_param_c_var_args_not_last =
`...` must be the last argument of a C-variadic function `...` must be the last argument of a C-variadic function
ast_passes_doc_comment_on_fn_param = ast_passes_fn_param_doc_comment =
documentation comments cannot be applied to function parameters documentation comments cannot be applied to function parameters
.label = doc comments are not allowed here .label = doc comments are not allowed here
ast_passes_forbidden_attr_on_fn_param = ast_passes_fn_param_forbidden_attr =
allow, cfg, cfg_attr, deny, expect, forbid, and warn are the only allowed built-in attributes in function parameters allow, cfg, cfg_attr, deny, expect, forbid, and warn are the only allowed built-in attributes in function parameters
ast_passes_fn_param_forbidden_self =
`self` parameter is only allowed in associated functions
.label = not semantically valid as function parameter
.note = associated functions are those in `impl` or `trait` definitions