Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,34 +519,13 @@ impl<'a> AstValidator<'a> {
}

fn check_decl_attrs(&self, fn_inputs: &[Param]) {
use SyntheticAttr::*;
fn_inputs
.iter()
.flat_map(|i| i.attrs.as_ref())
.filter(|attr| match &attr.kind {
AttrKind::Normal(normal) => {
let arr = [
sym::allow,
sym::deny,
sym::expect,
sym::forbid,
sym::rustc_splat,
sym::warn,
];
!attr.has_any_name(&arr) && rustc_attr_parsing::is_builtin_attr(&normal.item)
}
AttrKind::Synthetic(CfgTrace(_) | CfgAttrTrace(_)) | AttrKind::Generated(..) => {
false
}
AttrKind::DocComment(..) => true,
})
.for_each(|attr| {
for param in fn_inputs {
for attr in &param.attrs {
if attr.is_doc_comment() {
self.dcx().emit_err(diagnostics::FnParamDocComment { span: attr.span });
} else {
self.dcx().emit_err(diagnostics::FnParamForbiddenAttr { span: attr.span });
}
});
}
}
}

fn check_decl_self_param(&self, fn_inputs: &[Param], self_semantic: SelfSemantic) {
Expand Down
10 changes: 0 additions & 10 deletions compiler/rustc_ast_passes/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,6 @@ pub(crate) struct FnParamDocComment {
pub span: Span,
}

// FIXME(splat): add splat to the allowed built-in attributes when it is complete/stabilized
#[derive(Diagnostic)]
#[diag(
"allow, cfg, cfg_attr, deny, expect, forbid, and warn are the only allowed built-in attributes in function parameters"
)]
pub(crate) struct FnParamForbiddenAttr {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag("`#[{$eii_name}]` is not allowed to have `#[{$attr_name}]`")]
pub(crate) struct EiiImplAttributeNotSupported<'a> {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl NoArgsAttributeParser for ColdParser {
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::ForeignFn),
Allow(Target::Closure),
Error(Target::Param),
]);
const STABILITY: AttributeStability = AttributeStability::Stable;
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::Cold;
Expand Down Expand Up @@ -428,6 +429,7 @@ impl NoArgsAttributeParser for NoMangleParser {
Allow(Target::Method(MethodKind::TraitImpl)),
AllowSilent(Target::Const), // Handled in the `InvalidNoMangleItems` pass
Error(Target::Closure),
Error(Target::Param),
]);
const STABILITY: AttributeStability = AttributeStability::Stable;
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoMangle;
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_attr_parsing/src/attributes/crate_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl SingleAttributeParser for CrateNameParser {
const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError;
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate), Error(Target::Param)]);
const STABILITY: AttributeStability = AttributeStability::Stable;

fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
Expand All @@ -37,7 +37,7 @@ impl CombineAttributeParser for CrateTypeParser {
type Item = CrateType;
const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::CrateType(items);
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate), Error(Target::Param)]);
const TEMPLATE: AttributeTemplate =
template!(NameValueStr: "crate type", "https://doc.rust-lang.org/reference/linkage.html");
const STABILITY: AttributeStability = AttributeStability::Stable;
Expand Down Expand Up @@ -81,7 +81,7 @@ impl SingleAttributeParser for RecursionLimitParser {
const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError;
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N", "https://doc.rust-lang.org/reference/attributes/limits.html#the-recursion_limit-attribute");
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate), Error(Target::Param)]);
const STABILITY: AttributeStability = AttributeStability::Stable;

fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
Expand Down Expand Up @@ -113,7 +113,7 @@ impl SingleAttributeParser for TypeLengthLimitParser {
const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError;
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N");
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate), Error(Target::Param)]);
const STABILITY: AttributeStability = AttributeStability::Stable;

fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
Expand Down Expand Up @@ -156,7 +156,7 @@ impl NoArgsAttributeParser for NoStdParser {
const PATH: &[Symbol] = &[sym::no_std];
const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate), Error(Target::Param)]);
const STABILITY: AttributeStability = AttributeStability::Stable;
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoStd;
}
Expand All @@ -167,7 +167,7 @@ impl NoArgsAttributeParser for NoMainParser {
const PATH: &[Symbol] = &[sym::no_main];
const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate), Error(Target::Param)]);
const STABILITY: AttributeStability = AttributeStability::Stable;
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoMain;
}
Expand All @@ -187,7 +187,7 @@ impl SingleAttributeParser for WindowsSubsystemParser {
const PATH: &[Symbol] = &[sym::windows_subsystem];
const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError;
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate), Error(Target::Param)]);
const TEMPLATE: AttributeTemplate = template!(NameValueStr: ["windows", "console"], "https://doc.rust-lang.org/reference/runtime.html#the-windows_subsystem-attribute");
const STABILITY: AttributeStability = AttributeStability::Stable;

Expand Down Expand Up @@ -243,7 +243,7 @@ impl NoArgsAttributeParser for NoBuiltinsParser {
const PATH: &[Symbol] = &[sym::no_builtins];
const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate), Error(Target::Param)]);
const STABILITY: AttributeStability = AttributeStability::Stable;
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoBuiltins;
}
Expand Down Expand Up @@ -282,7 +282,7 @@ impl CombineAttributeParser for FeatureParser {
type Item = Ident;
const CONVERT: ConvertFn<Self::Item> = AttributeKind::Feature;
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate), Error(Target::Param)]);
const TEMPLATE: AttributeTemplate = template!(List: &["feature1, feature2, ..."]);
const STABILITY: AttributeStability = AttributeStability::Stable;

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_attr_parsing/src/attributes/deprecation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl SingleAttributeParser for DeprecatedParser {
Allow(Target::Impl { of_trait: false }),
Allow(Target::Crate),
Error(Target::WherePredicate),
Error(Target::Param),
]);
const TEMPLATE: AttributeTemplate = template!(
Word,
Expand Down
24 changes: 18 additions & 6 deletions compiler/rustc_attr_parsing/src/attributes/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::{AcceptMapping, AttributeParser, template};
use crate::context::{AcceptContext, FinalizeContext};
use crate::diagnostics::{
DocAliasBadChar, DocAliasDuplicated, DocAliasEmpty, DocAliasMalformed, DocAliasStartEnd,
DocAttrNotCrateLevel, DocAttrNotTraitLevel, DocAttributeNotAttribute,
DocAttrNotCrateLevel, DocAttrNotParameters, DocAttrNotTraitLevel, DocAttributeNotAttribute,
DocAutoCfgExpectsHideOrShow, DocAutoCfgHideShowExpectsList,
DocAutoCfgHideShowNoIdentBeforeValues, DocAutoCfgHideShowUnexpectedItem,
DocAutoCfgHideShowUnexpectedItemAfterValues, DocAutoCfgHideShowValuesMix,
Expand All @@ -28,7 +28,7 @@ use crate::diagnostics::{
use crate::parser::{
ArgParser, MetaItemListParser, MetaItemOrLitParser, MetaItemParser, OwnedPathParser,
};
use crate::target_checking::Policy::Allow;
use crate::target_checking::Policy::{Allow, AllowSilent};

fn check_keyword(cx: &mut AcceptContext<'_, '_>, keyword: Symbol, span: Span) -> bool {
// FIXME: Once rustdoc can handle URL conflicts on case insensitive file systems, we
Expand Down Expand Up @@ -244,7 +244,10 @@ impl DocParser {

cx.check_target(
sym::no_crate_inject.as_str(),
&AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]),
&AllowedTargets::AllowListWarnRest(&[
Allow(Target::Crate),
AllowSilent(Target::Param),
]),
);

self.attribute.no_crate_inject = Some(path.span())
Expand Down Expand Up @@ -356,7 +359,7 @@ impl DocParser {
}

match cx.target {
Target::Use | Target::ExternCrate => {}
Target::Use | Target::ExternCrate | Target::Param => {} // Param has a custom error
_ => {
let item_span = cx.target_span;
cx.emit_lint(
Expand Down Expand Up @@ -634,7 +637,10 @@ impl DocParser {
let span = path.span();
cx.check_target(
concat!("(", stringify!($ident), ")"),
&AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]),
&AllowedTargets::AllowListWarnRest(&[
Allow(Target::Crate),
AllowSilent(Target::Param), // Param has a custom error
]),
);
self.attribute.$ident = Some(span);
}};
Expand All @@ -653,7 +659,10 @@ impl DocParser {

cx.check_target(
s.as_str(),
&AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]),
&AllowedTargets::AllowListWarnRest(&[
Allow(Target::Crate),
AllowSilent(Target::Param), // Param has a custom error
]),
);

// FIXME: It's errorring when the attribute is passed multiple times on the command
Expand Down Expand Up @@ -814,6 +823,9 @@ impl DocParser {
}

fn accept_single_doc_attr(&mut self, cx: &mut AcceptContext<'_, '_>, args: &ArgParser) {
if cx.shared.target == Target::Param {
cx.emit_err(DocAttrNotParameters { span: cx.attr_span });
}
match args {
ArgParser::List(items) if !items.is_empty() => {
for i in items.mixed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl SingleAttributeParser for InstructionSetParser {
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::TraitImpl)),
Allow(Target::Method(MethodKind::Trait { body: true })),
Error(Target::Param),
]);
const TEMPLATE: AttributeTemplate = template!(List: &["set"], "https://doc.rust-lang.org/reference/attributes/codegen.html#the-instruction_set-attribute");
const STABILITY: AttributeStability = AttributeStability::Stable;
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_attr_parsing/src/attributes/link_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl SingleAttributeParser for LinkNameParser {
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
Allow(Target::ForeignFn),
Allow(Target::ForeignStatic),
Error(Target::Param),
]);
const TEMPLATE: AttributeTemplate = template!(
NameValueStr: "name",
Expand Down Expand Up @@ -74,7 +75,7 @@ impl CombineAttributeParser for LinkParser {
r#"name = "...", kind = "dylib|static|...", wasm_import_module = "...", import_name_type = "decorated|noprefix|undecorated""#,
], "https://doc.rust-lang.org/reference/items/external-blocks.html#the-link-attribute");
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::ForeignMod)]);
AllowedTargets::AllowListWarnRest(&[Allow(Target::ForeignMod), Error(Target::Param)]);
const STABILITY: AttributeStability = AttributeStability::Stable;

fn extend(
Expand Down Expand Up @@ -527,6 +528,7 @@ impl SingleAttributeParser for LinkSectionParser {
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::Trait { body: true })),
Allow(Target::Method(MethodKind::TraitImpl)),
Error(Target::Param),
]);
const TEMPLATE: AttributeTemplate = template!(
NameValueStr: "name",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl NoArgsAttributeParser for AutomaticallyDerivedParser {
Allow(Target::Impl { of_trait: true }),
Error(Target::Crate),
Error(Target::WherePredicate),
Error(Target::Param),
]);
const STABILITY: AttributeStability = AttributeStability::Stable;
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::AutomaticallyDerived;
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const MACRO_USE_ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListW
Allow(Target::Mod),
Allow(Target::ExternCrate),
Error(Target::WherePredicate),
Error(Target::Param),
]);

impl AttributeParser for MacroUseParser {
Expand Down Expand Up @@ -152,6 +153,7 @@ impl SingleAttributeParser for MacroExportParser {
Allow(Target::MacroDef),
Error(Target::WherePredicate),
Error(Target::Crate),
Error(Target::Param),
]);
const STABILITY: AttributeStability = AttributeStability::Stable;

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_attr_parsing/src/attributes/must_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl SingleAttributeParser for MustUseParser {
// `#[must_use]`
Allow(Target::Trait),
Error(Target::WherePredicate),
Error(Target::Param),
]);
const TEMPLATE: AttributeTemplate = template!(
Word, NameValueStr: "reason",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ pub(crate) struct NoImplicitPreludeParser;
impl NoArgsAttributeParser for NoImplicitPreludeParser {
const PATH: &[rustc_span::Symbol] = &[sym::no_implicit_prelude];
const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::Mod), Allow(Target::Crate)]);
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
Allow(Target::Mod),
Allow(Target::Crate),
Error(Target::Param),
]);
const STABILITY: AttributeStability = AttributeStability::Stable;
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoImplicitPrelude;
}
7 changes: 5 additions & 2 deletions compiler/rustc_attr_parsing/src/attributes/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ pub(crate) struct PathParser;
impl SingleAttributeParser for PathParser {
const PATH: &[Symbol] = &[sym::path];
const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError;
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::Mod), Error(Target::Crate)]);
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
Allow(Target::Mod),
Error(Target::Crate),
Error(Target::Param),
]);
const TEMPLATE: AttributeTemplate = template!(
NameValueStr: "file",
"https://doc.rust-lang.org/reference/items/modules.html#the-path-attribute"
Expand Down
14 changes: 10 additions & 4 deletions compiler/rustc_attr_parsing/src/attributes/test_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ pub(crate) struct IgnoreParser;
impl SingleAttributeParser for IgnoreParser {
const PATH: &[Symbol] = &[sym::ignore];
const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::Fn), Error(Target::WherePredicate)]);
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
Allow(Target::Fn),
Error(Target::WherePredicate),
Error(Target::Param),
]);
const TEMPLATE: AttributeTemplate = template!(
Word, NameValueStr: "reason",
"https://doc.rust-lang.org/reference/attributes/testing.html#the-ignore-attribute"
Expand Down Expand Up @@ -51,8 +54,11 @@ pub(crate) struct ShouldPanicParser;
impl SingleAttributeParser for ShouldPanicParser {
const PATH: &[Symbol] = &[sym::should_panic];
const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError;
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowListWarnRest(&[Allow(Target::Fn), Error(Target::WherePredicate)]);
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
Allow(Target::Fn),
Error(Target::WherePredicate),
Error(Target::Param),
]);
const TEMPLATE: AttributeTemplate = template!(
Word, List: &[r#"expected = "reason""#], NameValueStr: "reason",
"https://doc.rust-lang.org/reference/attributes/testing.html#the-should_panic-attribute"
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_attr_parsing/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,13 @@ pub(crate) struct DocAttrNotCrateLevel {
pub attr_name: Symbol,
}

#[derive(Diagnostic)]
#[diag("`#[doc(...)]` isn't allowed on parameters")]
pub(crate) struct DocAttrNotParameters {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag("nonexistent keyword `{$keyword}` used in `#[doc(keyword = \"...\")]`")]
#[help("only existing keywords are allowed in core/std")]
Expand Down
Loading