Skip to content

Commit 84b631e

Browse files
Wip
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent 063fe42 commit 84b631e

File tree

11 files changed

+77
-23
lines changed

11 files changed

+77
-23
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
7575
if !e.attrs.is_empty() {
7676
let old_attrs = self.attrs.get(&ex.hir_id.local_id).copied().unwrap_or(&[]);
7777
let new_attrs = self
78-
.lower_attrs_vec(&e.attrs, e.span, ex.hir_id, Target::Expression)
78+
.lower_attrs_vec(&e.attrs, e.span, ex.hir_id, Target::from_expr(e))
7979
.into_iter()
8080
.chain(old_attrs.iter().cloned());
8181
let new_attrs = &*self.arena.alloc_from_iter(new_attrs);
@@ -98,7 +98,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
9898
}
9999

100100
let expr_hir_id = self.lower_node_id(e.id);
101-
let attrs = self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::Expression);
101+
let attrs = self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e));
102102

103103
let kind = match &e.kind {
104104
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
@@ -1912,7 +1912,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19121912
//
19131913
// Also, add the attributes to the outer returned expr node.
19141914
let expr = self.expr_drop_temps_mut(for_span, match_expr);
1915-
self.lower_attrs(expr.hir_id, &e.attrs, e.span, Target::Expression);
1915+
self.lower_attrs(expr.hir_id, &e.attrs, e.span, Target::from_expr(e));
19161916
expr
19171917
}
19181918

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
822822

823823
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
824824
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
825-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, false));
825+
let attrs = self.lower_attrs(
826+
hir_id,
827+
&i.attrs,
828+
i.span,
829+
Target::from_assoc_item_kind(&i.kind, false),
830+
);
826831
let trait_item_def_id = hir_id.expect_owner();
827832

828833
let (ident, generics, kind, has_default) = match &i.kind {
@@ -993,8 +998,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
993998
let has_value = true;
994999
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
9951000
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
996-
let attrs =
997-
self.lower_attrs(hir_id, &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, is_in_trait_impl));
1001+
let attrs = self.lower_attrs(
1002+
hir_id,
1003+
&i.attrs,
1004+
i.span,
1005+
Target::from_assoc_item_kind(&i.kind, is_in_trait_impl),
1006+
);
9981007

9991008
let (ident, (generics, kind)) = match &i.kind {
10001009
AssocItemKind::Const(box ConstItem {

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ pub(crate) struct ColdParser;
5656
impl<S: Stage> NoArgsAttributeParser<S> for ColdParser {
5757
const PATH: &[Symbol] = &[sym::cold];
5858
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
59-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn, Target::Method(MethodKind::Trait { body: true }), Target::Method(MethodKind::Inherent)];
59+
const ALLOWED_TARGETS: &'static [Target] = &[
60+
Target::Fn,
61+
Target::Method(MethodKind::Trait { body: true }),
62+
Target::Method(MethodKind::Trait { body: false }),
63+
Target::Method(MethodKind::Inherent),
64+
Target::ForeignFn,
65+
Target::Closure
66+
];
6067
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Cold;
6168
}
6269

@@ -250,7 +257,11 @@ pub(crate) struct TrackCallerParser;
250257
impl<S: Stage> NoArgsAttributeParser<S> for TrackCallerParser {
251258
const PATH: &[Symbol] = &[sym::track_caller];
252259
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
253-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn, Target::Method(MethodKind::Inherent), Target::Method(MethodKind::Trait { body: true })];
260+
const ALLOWED_TARGETS: &'static [Target] = &[
261+
Target::Fn,
262+
Target::Method(MethodKind::Inherent),
263+
Target::Method(MethodKind::Trait { body: true }),
264+
];
254265
const CREATE: fn(Span) -> AttributeKind = AttributeKind::TrackCaller;
255266
}
256267

@@ -397,5 +408,5 @@ impl<S: Stage> CombineAttributeParser<S> for TargetFeatureParser {
397408
features
398409
}
399410

400-
const ALLOWED_TARGETS: &'static [Target] = &[];
411+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn];
401412
}

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_feature::{AttributeTemplate, template};
2-
use rustc_hir::{MethodKind, Target};
32
use rustc_hir::attrs::{AttributeKind, DeprecatedSince, Deprecation};
3+
use rustc_hir::{MethodKind, Target};
44
use rustc_span::{Span, Symbol, sym};
55

66
use super::util::parse_version;
@@ -49,6 +49,8 @@ impl<S: Stage> SingleAttributeParser<S> for DeprecationParser {
4949
Target::Static,
5050
Target::MacroDef,
5151
Target::Method(MethodKind::Inherent),
52+
Target::TyAlias,
53+
Target::Use,
5254
];
5355
const TEMPLATE: AttributeTemplate = template!(
5456
Word,

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub(crate) struct StdInternalSymbolParser;
9191
impl<S: Stage> NoArgsAttributeParser<S> for StdInternalSymbolParser {
9292
const PATH: &[Symbol] = &[sym::rustc_std_internal_symbol];
9393
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
94-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn];
94+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn, Target::ForeignFn];
9595
const CREATE: fn(Span) -> AttributeKind = AttributeKind::StdInternalSymbol;
9696
}
9797

compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) struct PubTransparentParser;
2222
impl<S: Stage> NoArgsAttributeParser<S> for PubTransparentParser {
2323
const PATH: &[Symbol] = &[sym::rustc_pub_transparent];
2424
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
25-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Struct];
25+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Struct, Target::Enum, Target::Union];
2626
const CREATE: fn(Span) -> AttributeKind = AttributeKind::PubTransparent;
2727
}
2828

compiler/rustc_attr_parsing/src/attributes/must_use.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ impl<S: Stage> SingleAttributeParser<S> for MustUseParser {
1515
const PATH: &[Symbol] = &[sym::must_use];
1616
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
1717
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
18-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn, Target::Method(MethodKind::Inherent)];
18+
const ALLOWED_TARGETS: &'static [Target] = &[
19+
Target::Fn,
20+
Target::Enum,
21+
Target::Struct,
22+
Target::Union,
23+
Target::Method(MethodKind::Trait { body: false }),
24+
Target::Method(MethodKind::Inherent),
25+
Target::ForeignFn,
26+
Target::Trait,
27+
];
1928
const TEMPLATE: AttributeTemplate = template!(Word, NameValueStr: "reason");
2029

2130
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {

compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ pub(crate) struct NonExhaustiveParser;
1010
impl<S: Stage> NoArgsAttributeParser<S> for NonExhaustiveParser {
1111
const PATH: &[Symbol] = &[sym::non_exhaustive];
1212
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
13-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Enum];
13+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Enum, Target::Struct, Target::Variant];
1414
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NonExhaustive;
1515
}

compiler/rustc_attr_parsing/src/attributes/stability.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ macro_rules! reject_outside_std {
2626
};
2727
}
2828

29-
const ALLOWED_TARGETS: &[Target] = &[Target::Fn,
29+
const ALLOWED_TARGETS: &[Target] = &[
30+
Target::Fn,
3031
Target::Struct,
32+
Target::Enum,
33+
Target::Union,
3134
Target::Method(MethodKind::Inherent),
3235
Target::Impl { of_trait: false },
3336
Target::Impl { of_trait: true },
@@ -37,8 +40,13 @@ const ALLOWED_TARGETS: &[Target] = &[Target::Fn,
3740
Target::Use,
3841
Target::Const,
3942
Target::AssocConst,
43+
Target::AssocTy,
4044
Target::Trait,
4145
Target::TraitAlias,
46+
Target::TyAlias,
47+
Target::Variant,
48+
Target::Field,
49+
Target::Param
4250
];
4351

4452
#[derive(Default)]

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -851,12 +851,20 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
851851

852852
(accept.accept_fn)(&mut cx, args);
853853

854-
if self.stage.should_emit().should_emit() && !accept.allowed_targets.contains(&target) {
855-
// if parts == &[sym::must_use] && target == (Target::Trait) {
856-
// panic!();
857-
// }
858-
859-
self.dcx().span_bug(n.item.span(), format!("{:?} target {target:?} not allowed {:?}", parts, accept.allowed_targets));
854+
if self.stage.should_emit().should_emit()
855+
&& !accept.allowed_targets.contains(&target)
856+
{
857+
if parts == &[sym::cold] && target == (Target::Expression) {
858+
panic!();
859+
}
860+
861+
self.dcx().span_bug(
862+
n.item.span(),
863+
format!(
864+
"{:?} target {target:?} not allowed {:?}",
865+
parts, accept.allowed_targets
866+
),
867+
);
860868
}
861869
} else {
862870
// If we're here, we must be compiling a tool attribute... Or someone

0 commit comments

Comments
 (0)