Skip to content

Commit d0579e2

Browse files
wip
1 parent d935f8f commit d0579e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+246
-271
lines changed

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ attr_parsing_empty_attribute =
1010
unused attribute
1111
.suggestion = remove this attribute
1212
13+
attr_parsing_invalid_target = this attribute is not allowed on this target
14+
1315
attr_parsing_empty_confusables =
1416
expected at least one confusable name
1517
attr_parsing_expected_one_cfg_pattern =

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,13 @@ pub(crate) struct ColdParser;
5757
impl<S: Stage> NoArgsAttributeParser<S> for ColdParser {
5858
const PATH: &[Symbol] = &[sym::cold];
5959
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
60-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
60+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
6161
Allow(Target::Fn),
6262
Allow(Target::Method(MethodKind::Trait { body: true })),
6363
Allow(Target::Method(MethodKind::Trait { body: false })),
6464
Allow(Target::Method(MethodKind::Inherent)),
6565
Allow(Target::ForeignFn),
6666
Allow(Target::Closure),
67-
Warn(Target::Crate),
68-
Warn(Target::Mod),
69-
Warn(Target::Struct),
70-
Warn(Target::Enum),
71-
Warn(Target::Union),
72-
Warn(Target::TyAlias),
73-
Warn(Target::Impl { of_trait: false }),
74-
Warn(Target::Impl { of_trait: true }),
75-
Warn(Target::Field),
76-
Warn(Target::Arm),
77-
Warn(Target::MacroDef),
7867
]);
7968
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Cold;
8069
}

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_span::{Symbol, sym};
1010

1111
use super::{AcceptContext, AttributeOrder, OnDuplicate};
1212
use crate::attributes::SingleAttributeParser;
13-
use crate::context::MaybeWarn::Allow;
13+
use crate::context::MaybeWarn::{Allow, Warn};
1414
use crate::context::{AllowedTargets, Stage};
1515
use crate::parser::ArgParser;
1616
pub(crate) struct InlineParser;
@@ -22,9 +22,14 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
2222
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
2323
Allow(Target::Fn),
2424
Allow(Target::Method(MethodKind::Inherent)),
25-
Allow(Target::Method(MethodKind::Trait { body: false })),
2625
Allow(Target::Method(MethodKind::Trait { body: true })),
2726
Allow(Target::Closure),
27+
Warn(Target::Method(MethodKind::Trait { body: false })),
28+
Warn(Target::ForeignFn),
29+
Warn(Target::Field),
30+
Warn(Target::MacroDef),
31+
Warn(Target::Arm),
32+
Warn(Target::AssocConst),
2833
]);
2934
const TEMPLATE: AttributeTemplate = template!(Word, List: "always|never");
3035

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub(crate) struct ExportStableParser;
7171
impl<S: Stage> NoArgsAttributeParser<S> for ExportStableParser {
7272
const PATH: &[Symbol] = &[sym::export_stable];
7373
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
74-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
74+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowAll; //TODO
7575
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::ExportStable;
7676
}
7777

compiler/rustc_attr_parsing/src/attributes/repr.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,8 @@ impl<S: Stage> CombineAttributeParser<S> for ReprParser {
6060
reprs
6161
}
6262

63-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
64-
Allow(Target::Struct),
65-
Allow(Target::Enum),
66-
Allow(Target::Union),
67-
]);
63+
//TODO should be done here in the future, argument-dependent
64+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowAll;
6865
}
6966

7067
macro_rules! int_pat {
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
use rustc_hir::Target;
21
use rustc_hir::attrs::AttributeKind;
32
use rustc_span::{Span, Symbol, sym};
43

54
use crate::attributes::{NoArgsAttributeParser, OnDuplicate};
6-
use crate::context::MaybeWarn::Allow;
75
use crate::context::{AllowedTargets, Stage};
86
pub(crate) struct MayDangleParser;
97
impl<S: Stage> NoArgsAttributeParser<S> for MayDangleParser {
108
const PATH: &[Symbol] = &[sym::may_dangle];
119
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
12-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Param)]);
10+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowAll; //TODO
1311
const CREATE: fn(span: Span) -> AttributeKind = AttributeKind::MayDangle;
1412
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -651,21 +651,32 @@ pub(crate) enum AllowedTargets {
651651
AllowListWarnRest(&'static [MaybeWarn]),
652652
}
653653

654+
pub(crate) enum AllowedResult {
655+
Allowed,
656+
Warn,
657+
Error,
658+
}
659+
654660
impl AllowedTargets {
655-
pub(crate) fn is_allowed(&self, target: Target) -> bool {
661+
pub(crate) fn is_allowed(&self, target: Target) -> AllowedResult {
656662
match self {
657-
AllowedTargets::AllowAll => true,
658-
AllowedTargets::AllowList(list) | AllowedTargets::AllowListWarnRest(list) => {
659-
list.contains(&Allow(target))
663+
AllowedTargets::AllowAll => AllowedResult::Allowed,
664+
AllowedTargets::AllowList(list) => {
665+
if list.contains(&Allow(target)) {
666+
AllowedResult::Allowed
667+
} else if list.contains(&Warn(target)) {
668+
AllowedResult::Warn
669+
} else {
670+
AllowedResult::Error
671+
}
672+
}
673+
AllowedTargets::AllowListWarnRest(list) => {
674+
if list.contains(&Allow(target)) {
675+
AllowedResult::Allowed
676+
} else {
677+
AllowedResult::Warn
678+
}
660679
}
661-
}
662-
}
663-
664-
pub(crate) fn is_warn(&self, target: Target) -> bool {
665-
match self {
666-
AllowedTargets::AllowAll => false,
667-
AllowedTargets::AllowList(list) => list.contains(&Warn(target)),
668-
AllowedTargets::AllowListWarnRest(list) => !list.contains(&Allow(target)),
669680
}
670681
}
671682
}
@@ -884,17 +895,20 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
884895

885896
(accept.accept_fn)(&mut cx, args);
886897

887-
if self.stage.should_emit().should_emit()
888-
&& !accept.allowed_targets.is_allowed(target)
889-
&& !accept.allowed_targets.is_warn(target)
890-
{
891-
self.dcx().span_delayed_bug(
892-
n.item.span(),
893-
format!(
894-
"{:?} target {target:?} not allowed {:?}",
895-
parts, accept.allowed_targets
896-
),
897-
);
898+
if self.stage.should_emit().should_emit() {
899+
match accept.allowed_targets.is_allowed(target) {
900+
AllowedResult::Allowed => {}
901+
AllowedResult::Warn => {
902+
emit_lint(AttributeLint {
903+
id: target_id,
904+
span: n.item.span(),
905+
kind: AttributeLintKind::InvalidTarget { },
906+
});
907+
}
908+
AllowedResult::Error => {
909+
self.dcx().struct_span_err(n.item.span(), "this attribute is not allowed on this target").emit();
910+
}
911+
}
898912
}
899913
} else {
900914
// If we're here, we must be compiling a tool attribute... Or someone

compiler/rustc_attr_parsing/src/lints.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,11 @@ pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<HirId>, lint_emi
3434
*first_span,
3535
session_diagnostics::EmptyAttributeList { attr_span: *first_span },
3636
),
37+
AttributeLintKind::InvalidTarget { } => lint_emitter.emit_node_span_lint(
38+
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
39+
*id,
40+
*span,
41+
session_diagnostics::InvalidTarget { },
42+
)
3743
}
3844
}

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,11 @@ pub(crate) struct EmptyAttributeList {
480480
pub attr_span: Span,
481481
}
482482

483+
#[derive(LintDiagnostic)]
484+
#[diag(attr_parsing_invalid_target)]
485+
pub(crate) struct InvalidTarget {
486+
}
487+
483488
#[derive(Diagnostic)]
484489
#[diag(attr_parsing_invalid_alignment_value, code = E0589)]
485490
pub(crate) struct InvalidAlignmentValue {

compiler/rustc_hir/src/lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ pub enum AttributeLintKind {
3434
UnusedDuplicate { this: Span, other: Span, warning: bool },
3535
IllFormedAttributeInput { suggestions: Vec<String> },
3636
EmptyAttribute { first_span: Span },
37+
InvalidTarget { },
3738
}

0 commit comments

Comments
 (0)