Skip to content

Commit d3ae065

Browse files
Introduce DEFAULT_TARGETS for all targets except MacroCall
1 parent 1a70f8d commit d3ae065

9 files changed

Lines changed: 86 additions & 14 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ impl SingleAttributeParser for SanitizeParser {
556556
const PATH: &[Symbol] = &[sym::sanitize];
557557

558558
// FIXME: still checked in check_attrs.rs
559-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
559+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(DEFAULT_TARGETS);
560560

561561
const TEMPLATE: AttributeTemplate = template!(List: &[
562562
r#"address = "on|off""#,

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl CombineAttributeParser for RegisterToolParser {
293293
const PATH: &[Symbol] = &[sym::register_tool];
294294
type Item = Ident;
295295
const CONVERT: ConvertFn<Self::Item> = |tools, _span| AttributeKind::RegisterTool(tools);
296-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
296+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(DEFAULT_TARGETS);
297297
const TEMPLATE: AttributeTemplate = template!(List: &["tool1, tool2, ..."]);
298298

299299
fn extend(

compiler/rustc_attr_parsing/src/attributes/doc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_session::errors::feature_err;
99
use rustc_span::{Span, Symbol, edition, sym};
1010
use thin_vec::ThinVec;
1111

12-
use super::prelude::{ALL_TARGETS, AllowedTargets};
12+
use super::prelude::{AllowedTargets, DEFAULT_TARGETS};
1313
use super::{AcceptMapping, AttributeParser};
1414
use crate::context::{AcceptContext, FinalizeContext};
1515
use crate::errors::{
@@ -707,7 +707,7 @@ impl AttributeParser for DocParser {
707707
},
708708
)];
709709
// FIXME: Currently emitted from 2 different places, generating duplicated warnings.
710-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
710+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(DEFAULT_TARGETS);
711711
// const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
712712
// Allow(Target::ExternCrate),
713713
// Allow(Target::Use),

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl CombineAttributeParser for LinkParser {
6969
r#"name = "...", import_name_type = "decorated|noprefix|undecorated""#,
7070
r#"name = "...", kind = "dylib|static|...", wasm_import_module = "...", import_name_type = "decorated|noprefix|undecorated""#,
7171
], "https://doc.rust-lang.org/reference/items/external-blocks.html#the-link-attribute");
72-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); //FIXME Still checked fully in `check_attr.rs`
72+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(DEFAULT_TARGETS); //FIXME Still checked fully in `check_attr.rs`
7373

7474
fn extend(
7575
cx: &mut AcceptContext<'_, '_>,
@@ -528,7 +528,7 @@ impl SingleAttributeParser for LinkSectionParser {
528528
pub(crate) struct ExportStableParser;
529529
impl NoArgsAttributeParser for ExportStableParser {
530530
const PATH: &[Symbol] = &[sym::export_stable];
531-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); //FIXME Still checked fully in `check_attr.rs`
531+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(DEFAULT_TARGETS); //FIXME Still checked fully in `check_attr.rs`
532532
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::ExportStable;
533533
}
534534

compiler/rustc_attr_parsing/src/attributes/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ pub(super) use crate::parser::*;
2424
#[doc(hidden)]
2525
pub(super) use crate::target_checking::Policy::{Allow, Error, Warn};
2626
#[doc(hidden)]
27-
pub(super) use crate::target_checking::{ALL_TARGETS, AllowedTargets};
27+
pub(super) use crate::target_checking::{ALL_TARGETS, AllowedTargets, DEFAULT_TARGETS};

compiler/rustc_attr_parsing/src/attributes/repr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl CombineAttributeParser for ReprParser {
5858

5959
//FIXME Still checked fully in `check_attr.rs`
6060
//This one is slightly more complicated because the allowed targets depend on the arguments
61-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
61+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(DEFAULT_TARGETS);
6262
}
6363

6464
macro_rules! int_pat {

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ pub(crate) struct LangParser;
532532

533533
impl SingleAttributeParser for LangParser {
534534
const PATH: &[Symbol] = &[sym::lang];
535-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); // Targets are checked per lang item in `rustc_passes`
535+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(DEFAULT_TARGETS); // Targets are checked per lang item in `rustc_passes`
536536
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
537537

538538
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
@@ -564,7 +564,7 @@ pub(crate) struct PanicHandlerParser;
564564

565565
impl NoArgsAttributeParser for PanicHandlerParser {
566566
const PATH: &[Symbol] = &[sym::panic_handler];
567-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); // Targets are checked per lang item in `rustc_passes`
567+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(DEFAULT_TARGETS); // Targets are checked per lang item in `rustc_passes`
568568
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::Lang(LangItem::PanicImpl);
569569
}
570570

compiler/rustc_attr_parsing/src/attributes/semantics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ use super::prelude::*;
33
pub(crate) struct MayDangleParser;
44
impl NoArgsAttributeParser for MayDangleParser {
55
const PATH: &[Symbol] = &[sym::may_dangle];
6-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); //FIXME Still checked fully in `check_attr.rs`
6+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(DEFAULT_TARGETS); //FIXME Still checked fully in `check_attr.rs`
77
const CREATE: fn(span: Span) -> AttributeKind = AttributeKind::MayDangle;
88
}

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::errors::{
1414
UnsupportedAttributesInWhere,
1515
};
1616
use crate::session_diagnostics::InvalidTarget;
17-
use crate::target_checking::Policy::Allow;
17+
use crate::target_checking::Policy::{Allow, Warn};
1818

1919
#[derive(Debug)]
2020
pub(crate) enum AllowedTargets {
@@ -394,10 +394,82 @@ fn filter_targets(
394394
added_fake_targets.push(target_group_name);
395395
}
396396

397+
/// This is a list of default targets to which a attribute can be applied
398+
/// This is used for attributes that are not parted to the new target checking system yet can use this list as a placeholder.
399+
/// This excludes `Target::MacroCall`, as attributes on macro calls are otherwise not checked for parsed attributes.
400+
pub(crate) const DEFAULT_TARGETS: &'static [Policy] = {
401+
use Policy::Allow;
402+
&[
403+
Allow(Target::ExternCrate),
404+
Allow(Target::Use),
405+
Allow(Target::Static),
406+
Allow(Target::Const),
407+
Allow(Target::Fn),
408+
Allow(Target::Closure),
409+
Allow(Target::Mod),
410+
Allow(Target::ForeignMod),
411+
Allow(Target::GlobalAsm),
412+
Allow(Target::TyAlias),
413+
Allow(Target::Enum),
414+
Allow(Target::Variant),
415+
Allow(Target::Struct),
416+
Allow(Target::Field),
417+
Allow(Target::Union),
418+
Allow(Target::Trait),
419+
Allow(Target::TraitAlias),
420+
Allow(Target::Impl { of_trait: false }),
421+
Allow(Target::Impl { of_trait: true }),
422+
Allow(Target::Expression),
423+
Allow(Target::Statement),
424+
Allow(Target::Arm),
425+
Allow(Target::AssocConst),
426+
Allow(Target::Method(MethodKind::Inherent)),
427+
Allow(Target::Method(MethodKind::Trait { body: false })),
428+
Allow(Target::Method(MethodKind::Trait { body: true })),
429+
Allow(Target::Method(MethodKind::TraitImpl)),
430+
Allow(Target::AssocTy),
431+
Allow(Target::ForeignFn),
432+
Allow(Target::ForeignStatic),
433+
Allow(Target::ForeignTy),
434+
Allow(Target::MacroDef),
435+
Allow(Target::Param),
436+
Allow(Target::PatField),
437+
Allow(Target::ExprField),
438+
Allow(Target::WherePredicate),
439+
Allow(Target::Crate),
440+
Allow(Target::Delegation { mac: false }),
441+
Allow(Target::Delegation { mac: true }),
442+
Allow(Target::GenericParam {
443+
kind: rustc_hir::target::GenericParamKind::Const,
444+
has_default: false,
445+
}),
446+
Allow(Target::GenericParam {
447+
kind: rustc_hir::target::GenericParamKind::Const,
448+
has_default: true,
449+
}),
450+
Allow(Target::GenericParam {
451+
kind: rustc_hir::target::GenericParamKind::Lifetime,
452+
has_default: false,
453+
}),
454+
Allow(Target::GenericParam {
455+
kind: rustc_hir::target::GenericParamKind::Lifetime,
456+
has_default: true,
457+
}),
458+
Allow(Target::GenericParam {
459+
kind: rustc_hir::target::GenericParamKind::Type,
460+
has_default: false,
461+
}),
462+
Allow(Target::GenericParam {
463+
kind: rustc_hir::target::GenericParamKind::Type,
464+
has_default: true,
465+
}),
466+
Warn(Target::MacroCall),
467+
]
468+
};
469+
397470
/// This is the list of all targets to which a attribute can be applied
398471
/// This is used for:
399-
/// - `rustc_dummy`, which can be applied to all targets
400-
/// - Attributes that are not parted to the new target system yet can use this list as a placeholder
472+
/// - Attributes that are not parted to the new target checking system yet can use this list as a placeholder
401473
pub(crate) const ALL_TARGETS: &'static [Policy] = {
402474
use Policy::Allow;
403475
&[

0 commit comments

Comments
 (0)