Skip to content

Commit 4bdb861

Browse files
wip1
1 parent ca77504 commit 4bdb861

Some content is hidden

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

47 files changed

+959
-1423
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,6 +3480,7 @@ dependencies = [
34803480
name = "rustc_attr_parsing"
34813481
version = "0.0.0"
34823482
dependencies = [
3483+
"itertools",
34833484
"rustc_abi",
34843485
"rustc_ast",
34853486
"rustc_ast_pretty",

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_ast::{Block, BlockCheckMode, Local, LocalKind, Stmt, StmtKind};
22
use rustc_hir as hir;
3+
use rustc_hir::Target;
34
use rustc_span::sym;
45
use smallvec::SmallVec;
56

@@ -109,7 +110,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
109110
};
110111
let span = self.lower_span(l.span);
111112
let source = hir::LocalSource::Normal;
112-
self.lower_attrs(hir_id, &l.attrs, l.span);
113+
self.lower_attrs(hir_id, &l.attrs, l.span, Target::Statement);
113114
self.arena.alloc(hir::LetStmt { hir_id, super_, ty, pat, init, els, span, source })
114115
}
115116

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
88
use rustc_hir as hir;
99
use rustc_hir::attrs::AttributeKind;
1010
use rustc_hir::def::{DefKind, Res};
11-
use rustc_hir::{HirId, find_attr};
11+
use rustc_hir::{HirId, Target, find_attr};
1212
use rustc_middle::span_bug;
1313
use rustc_middle::ty::TyCtxt;
1414
use rustc_session::errors::report_lit_error;
@@ -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)
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);
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)),
@@ -640,7 +640,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
640640
let guard = arm.guard.as_ref().map(|cond| self.lower_expr(cond));
641641
let hir_id = self.next_id();
642642
let span = self.lower_span(arm.span);
643-
self.lower_attrs(hir_id, &arm.attrs, arm.span);
643+
self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm);
644644
let is_never_pattern = pat.is_never_pattern();
645645
// We need to lower the body even if it's unneeded for never pattern in match,
646646
// ensure that we can get HirId for DefId if need (issue #137708).
@@ -821,6 +821,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
821821
span: unstable_span,
822822
}],
823823
span,
824+
Target::Fn,
824825
);
825826
}
826827
}
@@ -1655,7 +1656,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16551656

16561657
fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> {
16571658
let hir_id = self.lower_node_id(f.id);
1658-
self.lower_attrs(hir_id, &f.attrs, f.span);
1659+
self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField);
16591660
hir::ExprField {
16601661
hir_id,
16611662
ident: self.lower_ident(f.ident),
@@ -1911,7 +1912,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19111912
//
19121913
// Also, add the attributes to the outer returned expr node.
19131914
let expr = self.expr_drop_temps_mut(for_span, match_expr);
1914-
self.lower_attrs(expr.hir_id, &e.attrs, e.span);
1915+
self.lower_attrs(expr.hir_id, &e.attrs, e.span, Target::from_expr(e));
19151916
expr
19161917
}
19171918

@@ -1968,7 +1969,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19681969
let val_ident = Ident::with_dummy_span(sym::val);
19691970
let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
19701971
let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
1971-
self.lower_attrs(val_expr.hir_id, &attrs, span);
1972+
self.lower_attrs(val_expr.hir_id, &attrs, span, Target::Expression);
19721973
let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
19731974
self.arm(continue_pat, val_expr)
19741975
};
@@ -1999,7 +2000,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19992000
let ret_expr = self.checked_return(Some(from_residual_expr));
20002001
self.arena.alloc(self.expr(try_span, ret_expr))
20012002
};
2002-
self.lower_attrs(ret_expr.hir_id, &attrs, span);
2003+
self.lower_attrs(ret_expr.hir_id, &attrs, span, Target::Expression);
20032004

20042005
let break_pat = self.pat_cf_break(try_span, residual_local);
20052006
self.arm(break_pat, ret_expr)

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
66
use rustc_hir::attrs::AttributeKind;
77
use rustc_hir::def::{DefKind, PerNS, Res};
88
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
9-
use rustc_hir::{self as hir, HirId, LifetimeSource, PredicateOrigin, find_attr};
9+
use rustc_hir::{self as hir, HirId, LifetimeSource, PredicateOrigin, Target, find_attr};
1010
use rustc_index::{IndexSlice, IndexVec};
1111
use rustc_middle::span_bug;
1212
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
@@ -81,7 +81,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
8181
self.with_lctx(CRATE_NODE_ID, |lctx| {
8282
let module = lctx.lower_mod(&c.items, &c.spans);
8383
// FIXME(jdonszelman): is dummy span ever a problem here?
84-
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP);
84+
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP, Target::Crate);
8585
hir::OwnerNode::Crate(module)
8686
})
8787
}
@@ -137,7 +137,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
137137
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
138138
let vis_span = self.lower_span(i.vis.span);
139139
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
140-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
140+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, Target::from_ast_item(i));
141141
let kind = self.lower_item_kind(i.span, i.id, hir_id, attrs, vis_span, &i.kind);
142142
let item = hir::Item {
143143
owner_id: hir_id.expect_owner(),
@@ -650,7 +650,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
650650
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
651651
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
652652
let owner_id = hir_id.expect_owner();
653-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
653+
let attrs =
654+
self.lower_attrs(hir_id, &i.attrs, i.span, Target::from_foreign_item_kind(&i.kind));
654655
let (ident, kind) = match &i.kind {
655656
ForeignItemKind::Fn(box Fn { sig, ident, generics, define_opaque, .. }) => {
656657
let fdec = &sig.decl;
@@ -719,7 +720,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
719720

720721
fn lower_variant(&mut self, item_kind: &ItemKind, v: &Variant) -> hir::Variant<'hir> {
721722
let hir_id = self.lower_node_id(v.id);
722-
self.lower_attrs(hir_id, &v.attrs, v.span);
723+
self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant);
723724
hir::Variant {
724725
hir_id,
725726
def_id: self.local_def_id(v.id),
@@ -802,7 +803,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
802803
) -> hir::FieldDef<'hir> {
803804
let ty = self.lower_ty(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy));
804805
let hir_id = self.lower_node_id(f.id);
805-
self.lower_attrs(hir_id, &f.attrs, f.span);
806+
self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field);
806807
hir::FieldDef {
807808
span: self.lower_span(f.span),
808809
hir_id,
@@ -821,7 +822,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
821822

822823
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
823824
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
824-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
825+
let attrs = self.lower_attrs(
826+
hir_id,
827+
&i.attrs,
828+
i.span,
829+
Target::from_assoc_item_kind(&i.kind, AssocCtxt::Trait),
830+
);
825831
let trait_item_def_id = hir_id.expect_owner();
826832

827833
let (ident, generics, kind, has_default) = match &i.kind {
@@ -992,7 +998,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
992998
let has_value = true;
993999
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
9941000
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
995-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
1001+
let attrs = self.lower_attrs(
1002+
hir_id,
1003+
&i.attrs,
1004+
i.span,
1005+
Target::from_assoc_item_kind(&i.kind, AssocCtxt::Impl { of_trait: is_in_trait_impl }),
1006+
);
9961007

9971008
let (ident, (generics, kind)) = match &i.kind {
9981009
AssocItemKind::Const(box ConstItem {
@@ -1162,7 +1173,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11621173

11631174
fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
11641175
let hir_id = self.lower_node_id(param.id);
1165-
self.lower_attrs(hir_id, &param.attrs, param.span);
1176+
self.lower_attrs(hir_id, &param.attrs, param.span, Target::Param);
11661177
hir::Param {
11671178
hir_id,
11681179
pat: self.lower_pat(&param.pat),
@@ -1842,7 +1853,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18421853
) -> hir::WherePredicate<'hir> {
18431854
let hir_id = self.lower_node_id(pred.id);
18441855
let span = self.lower_span(pred.span);
1845-
self.lower_attrs(hir_id, &pred.attrs, span);
1856+
self.lower_attrs(hir_id, &pred.attrs, span, Target::WherePredicate);
18461857
let kind = self.arena.alloc(match &pred.kind {
18471858
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
18481859
bound_generic_params,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5454
use rustc_hir::lints::DelayedLint;
5555
use rustc_hir::{
5656
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
57-
LifetimeSyntax, ParamName, TraitCandidate,
57+
LifetimeSyntax, ParamName, Target, TraitCandidate,
5858
};
5959
use rustc_index::{Idx, IndexSlice, IndexVec};
6060
use rustc_macros::extension;
@@ -942,11 +942,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
942942
id: HirId,
943943
attrs: &[Attribute],
944944
target_span: Span,
945+
target: Target,
945946
) -> &'hir [hir::Attribute] {
946947
if attrs.is_empty() {
947948
&[]
948949
} else {
949-
let lowered_attrs = self.lower_attrs_vec(attrs, self.lower_span(target_span), id);
950+
let lowered_attrs =
951+
self.lower_attrs_vec(attrs, self.lower_span(target_span), id, target);
950952

951953
assert_eq!(id.owner, self.current_hir_id_owner);
952954
let ret = self.arena.alloc_from_iter(lowered_attrs);
@@ -971,12 +973,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
971973
attrs: &[Attribute],
972974
target_span: Span,
973975
target_hir_id: HirId,
976+
target: Target,
974977
) -> Vec<hir::Attribute> {
975978
let l = self.span_lowerer();
976979
self.attribute_parser.parse_attribute_list(
977980
attrs,
978981
target_span,
979982
target_hir_id,
983+
target,
980984
OmitDoc::Lower,
981985
|s| l.lower(s),
982986
|l| {
@@ -1939,7 +1943,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19391943
let (name, kind) = self.lower_generic_param_kind(param, source);
19401944

19411945
let hir_id = self.lower_node_id(param.id);
1942-
self.lower_attrs(hir_id, &param.attrs, param.span());
1946+
self.lower_attrs(hir_id, &param.attrs, param.span(), Target::Param);
19431947
hir::GenericParam {
19441948
hir_id,
19451949
def_id: self.local_def_id(param.id),

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_ast::ptr::P;
44
use rustc_ast::*;
55
use rustc_data_structures::stack::ensure_sufficient_stack;
66
use rustc_hir::def::{DefKind, Res};
7-
use rustc_hir::{self as hir, LangItem};
7+
use rustc_hir::{self as hir, LangItem, Target};
88
use rustc_middle::span_bug;
99
use rustc_span::source_map::{Spanned, respan};
1010
use rustc_span::{DesugaringKind, Ident, Span};
@@ -94,7 +94,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
9494

9595
let fs = self.arena.alloc_from_iter(fields.iter().map(|f| {
9696
let hir_id = self.lower_node_id(f.id);
97-
self.lower_attrs(hir_id, &f.attrs, f.span);
97+
self.lower_attrs(hir_id, &f.attrs, f.span, Target::PatField);
9898

9999
hir::PatField {
100100
hir_id,

compiler/rustc_attr_parsing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
# tidy-alphabetical-start
8+
itertools = "0.12"
89
rustc_abi = { path = "../rustc_abi" }
910
rustc_ast = { path = "../rustc_ast" }
1011
rustc_ast_pretty = { path = "../rustc_ast_pretty" }

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ attr_parsing_empty_attribute =
1010
unused attribute
1111
.suggestion = remove this attribute
1212
13+
attr_parsing_invalid_target = `#[{$name}]` attribute cannot be used on {$target}
14+
.help = `#[{$name}]` can {$only}be applied to {$applied}
15+
attr_parsing_invalid_target_lint = `#[{$name}]` attribute cannot be used on {$target}
16+
.warn = {-attr_parsing_previously_accepted}
17+
.help = `#[{$name}]` can be applied to...
18+
1319
attr_parsing_empty_confusables =
1420
expected at least one confusable name
1521
attr_parsing_expected_one_cfg_pattern =

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use std::iter;
22

33
use rustc_feature::{AttributeTemplate, template};
44
use rustc_hir::attrs::AttributeKind;
5+
use rustc_hir::{MethodKind, Target};
56
use rustc_span::{Span, Symbol, sym};
67

78
use super::{CombineAttributeParser, ConvertFn};
8-
use crate::context::{AcceptContext, Stage};
9+
use crate::context::MaybeWarn::{Allow, Warn};
10+
use crate::context::{AcceptContext, AllowedTargets, Stage};
911
use crate::parser::ArgParser;
1012
use crate::session_diagnostics;
1113

@@ -15,6 +17,12 @@ impl<S: Stage> CombineAttributeParser<S> for AllowInternalUnstableParser {
1517
type Item = (Symbol, Span);
1618
const CONVERT: ConvertFn<Self::Item> =
1719
|items, span| AttributeKind::AllowInternalUnstable(items, span);
20+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
21+
Allow(Target::MacroDef),
22+
Allow(Target::Fn),
23+
Warn(Target::Field),
24+
Warn(Target::Arm),
25+
]);
1826
const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
1927

2028
fn extend<'c>(
@@ -32,6 +40,8 @@ impl<S: Stage> CombineAttributeParser<S> for UnstableFeatureBoundParser {
3240
const PATH: &'static [rustc_span::Symbol] = &[sym::unstable_feature_bound];
3341
type Item = (Symbol, Span);
3442
const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::UnstableFeatureBound(items);
43+
const ALLOWED_TARGETS: AllowedTargets =
44+
AllowedTargets::AllowList(&[Allow(Target::Fn), Allow(Target::Impl { of_trait: true })]);
3545
const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
3646

3747
fn extend<'c>(
@@ -53,6 +63,13 @@ impl<S: Stage> CombineAttributeParser<S> for AllowConstFnUnstableParser {
5363
type Item = Symbol;
5464
const CONVERT: ConvertFn<Self::Item> =
5565
|items, first_span| AttributeKind::AllowConstFnUnstable(items, first_span);
66+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
67+
Allow(Target::Fn),
68+
Allow(Target::Method(MethodKind::Inherent)),
69+
Allow(Target::Method(MethodKind::Trait { body: false })),
70+
Allow(Target::Method(MethodKind::Trait { body: true })),
71+
Allow(Target::Method(MethodKind::TraitImpl)),
72+
]);
5673
const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
5774

5875
fn extend<'c>(
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
//! Attributes that can be found in function body.
22
3+
use rustc_hir::Target;
34
use rustc_hir::attrs::AttributeKind;
45
use rustc_span::{Symbol, sym};
56

67
use super::{NoArgsAttributeParser, OnDuplicate};
7-
use crate::context::Stage;
8+
use crate::context::MaybeWarn::Allow;
9+
use crate::context::{AllowedTargets, Stage};
810

911
pub(crate) struct CoroutineParser;
1012

1113
impl<S: Stage> NoArgsAttributeParser<S> for CoroutineParser {
1214
const PATH: &[Symbol] = &[sym::coroutine];
1315
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
16+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Closure)]);
1417
const CREATE: fn(rustc_span::Span) -> AttributeKind = |span| AttributeKind::Coroutine(span);
1518
}

0 commit comments

Comments
 (0)