Skip to content

Commit c8b3e96

Browse files
committed
make sure the right target is passed to #[cfg()] when it is parsed
1 parent dab86f7 commit c8b3e96

3 files changed

Lines changed: 93 additions & 9 deletions

File tree

compiler/rustc_expand/src/expand.rs

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,8 @@ trait InvocationCollectorNode: HasAttrs + HasNodeId + Sized {
13061306
fn declared_idents(&self) -> Vec<Ident> {
13071307
vec![]
13081308
}
1309+
1310+
fn as_target(&self) -> Target;
13091311
}
13101312

13111313
impl InvocationCollectorNode for Box<ast::Item> {
@@ -1457,6 +1459,10 @@ impl InvocationCollectorNode for Box<ast::Item> {
14571459
self.kind.ident().into_iter().collect()
14581460
}
14591461
}
1462+
1463+
fn as_target(&self) -> Target {
1464+
Target::from_ast_item(&*self)
1465+
}
14601466
}
14611467

14621468
struct TraitItemTag;
@@ -1498,6 +1504,9 @@ impl InvocationCollectorNode for AstNodeWrapper<Box<ast::AssocItem>, TraitItemTa
14981504
fn flatten_outputs(items: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
14991505
items.flatten().collect()
15001506
}
1507+
fn as_target(&self) -> Target {
1508+
Target::from_assoc_item_kind(&self.wrapped.kind, AssocCtxt::Trait)
1509+
}
15011510
}
15021511

15031512
struct ImplItemTag;
@@ -1539,6 +1548,9 @@ impl InvocationCollectorNode for AstNodeWrapper<Box<ast::AssocItem>, ImplItemTag
15391548
fn flatten_outputs(items: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
15401549
items.flatten().collect()
15411550
}
1551+
fn as_target(&self) -> Target {
1552+
Target::from_assoc_item_kind(&self.wrapped.kind, AssocCtxt::Impl { of_trait: false })
1553+
}
15421554
}
15431555

15441556
struct TraitImplItemTag;
@@ -1580,6 +1592,9 @@ impl InvocationCollectorNode for AstNodeWrapper<Box<ast::AssocItem>, TraitImplIt
15801592
fn flatten_outputs(items: impl Iterator<Item = Self::OutputTy>) -> Self::OutputTy {
15811593
items.flatten().collect()
15821594
}
1595+
fn as_target(&self) -> Target {
1596+
Target::from_assoc_item_kind(&self.wrapped.kind, AssocCtxt::Impl { of_trait: true })
1597+
}
15831598
}
15841599

15851600
impl InvocationCollectorNode for Box<ast::ForeignItem> {
@@ -1602,6 +1617,14 @@ impl InvocationCollectorNode for Box<ast::ForeignItem> {
16021617
_ => unreachable!(),
16031618
}
16041619
}
1620+
fn as_target(&self) -> Target {
1621+
match &self.kind {
1622+
ForeignItemKind::Static(_) => Target::ForeignStatic,
1623+
ForeignItemKind::Fn(_) => Target::ForeignFn,
1624+
ForeignItemKind::TyAlias(_) => Target::ForeignTy,
1625+
ForeignItemKind::MacCall(_) => Target::MacroCall,
1626+
}
1627+
}
16051628
}
16061629

16071630
impl InvocationCollectorNode for ast::Variant {
@@ -1615,6 +1638,9 @@ impl InvocationCollectorNode for ast::Variant {
16151638
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16161639
walk_flat_map_variant(collector, self)
16171640
}
1641+
fn as_target(&self) -> Target {
1642+
Target::Variant
1643+
}
16181644
}
16191645

16201646
impl InvocationCollectorNode for ast::WherePredicate {
@@ -1628,6 +1654,9 @@ impl InvocationCollectorNode for ast::WherePredicate {
16281654
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16291655
walk_flat_map_where_predicate(collector, self)
16301656
}
1657+
fn as_target(&self) -> Target {
1658+
Target::WherePredicate
1659+
}
16311660
}
16321661

16331662
impl InvocationCollectorNode for ast::FieldDef {
@@ -1641,6 +1670,9 @@ impl InvocationCollectorNode for ast::FieldDef {
16411670
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16421671
walk_flat_map_field_def(collector, self)
16431672
}
1673+
fn as_target(&self) -> Target {
1674+
Target::Field
1675+
}
16441676
}
16451677

16461678
impl InvocationCollectorNode for ast::PatField {
@@ -1654,6 +1686,9 @@ impl InvocationCollectorNode for ast::PatField {
16541686
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16551687
walk_flat_map_pat_field(collector, self)
16561688
}
1689+
fn as_target(&self) -> Target {
1690+
Target::PatField
1691+
}
16571692
}
16581693

16591694
impl InvocationCollectorNode for ast::ExprField {
@@ -1667,6 +1702,9 @@ impl InvocationCollectorNode for ast::ExprField {
16671702
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16681703
walk_flat_map_expr_field(collector, self)
16691704
}
1705+
fn as_target(&self) -> Target {
1706+
Target::ExprField
1707+
}
16701708
}
16711709

16721710
impl InvocationCollectorNode for ast::Param {
@@ -1680,6 +1718,9 @@ impl InvocationCollectorNode for ast::Param {
16801718
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16811719
walk_flat_map_param(collector, self)
16821720
}
1721+
fn as_target(&self) -> Target {
1722+
Target::Param
1723+
}
16831724
}
16841725

16851726
impl InvocationCollectorNode for ast::GenericParam {
@@ -1693,6 +1734,25 @@ impl InvocationCollectorNode for ast::GenericParam {
16931734
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16941735
walk_flat_map_generic_param(collector, self)
16951736
}
1737+
fn as_target(&self) -> Target {
1738+
let mut has_default = false;
1739+
Target::GenericParam {
1740+
kind: match &self.kind {
1741+
rustc_ast::GenericParamKind::Lifetime => {
1742+
rustc_hir::target::GenericParamKind::Lifetime
1743+
}
1744+
rustc_ast::GenericParamKind::Type { default } => {
1745+
has_default = default.is_some();
1746+
rustc_hir::target::GenericParamKind::Type
1747+
}
1748+
rustc_ast::GenericParamKind::Const { default, .. } => {
1749+
has_default = default.is_some();
1750+
rustc_hir::target::GenericParamKind::Const
1751+
}
1752+
},
1753+
has_default,
1754+
}
1755+
}
16961756
}
16971757

16981758
impl InvocationCollectorNode for ast::Arm {
@@ -1706,6 +1766,9 @@ impl InvocationCollectorNode for ast::Arm {
17061766
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
17071767
walk_flat_map_arm(collector, self)
17081768
}
1769+
fn as_target(&self) -> Target {
1770+
Target::Arm
1771+
}
17091772
}
17101773

17111774
impl InvocationCollectorNode for ast::Stmt {
@@ -1779,6 +1842,9 @@ impl InvocationCollectorNode for ast::Stmt {
17791842
}
17801843
}
17811844
}
1845+
fn as_target(&self) -> Target {
1846+
Target::Statement
1847+
}
17821848
}
17831849

17841850
impl InvocationCollectorNode for ast::Crate {
@@ -1805,6 +1871,9 @@ impl InvocationCollectorNode for ast::Crate {
18051871
// Standard prelude imports are left in the crate for backward compatibility.
18061872
self.items.truncate(collector.cx.num_standard_library_imports);
18071873
}
1874+
fn as_target(&self) -> Target {
1875+
Target::Crate
1876+
}
18081877
}
18091878

18101879
impl InvocationCollectorNode for ast::Ty {
@@ -1838,6 +1907,10 @@ impl InvocationCollectorNode for ast::Ty {
18381907
_ => unreachable!(),
18391908
}
18401909
}
1910+
fn as_target(&self) -> Target {
1911+
// This is only used for attribute parsing, which are not allowed on types.
1912+
unreachable!()
1913+
}
18411914
}
18421915

18431916
impl InvocationCollectorNode for ast::Pat {
@@ -1861,6 +1934,9 @@ impl InvocationCollectorNode for ast::Pat {
18611934
_ => unreachable!(),
18621935
}
18631936
}
1937+
fn as_target(&self) -> Target {
1938+
todo!();
1939+
}
18641940
}
18651941

18661942
impl InvocationCollectorNode for ast::Expr {
@@ -1887,6 +1963,9 @@ impl InvocationCollectorNode for ast::Expr {
18871963
_ => unreachable!(),
18881964
}
18891965
}
1966+
fn as_target(&self) -> Target {
1967+
Target::Expression
1968+
}
18901969
}
18911970

18921971
struct OptExprTag;
@@ -1916,6 +1995,9 @@ impl InvocationCollectorNode for AstNodeWrapper<Box<ast::Expr>, OptExprTag> {
19161995
fn pre_flat_map_node_collect_attr(cfg: &StripUnconfigured<'_>, attr: &ast::Attribute) {
19171996
cfg.maybe_emit_expr_attr_err(attr);
19181997
}
1998+
fn as_target(&self) -> Target {
1999+
Target::Expression
2000+
}
19192001
}
19202002

19212003
/// This struct is a hack to workaround unstable of `stmt_expr_attributes`.
@@ -1947,6 +2029,9 @@ impl InvocationCollectorNode for AstNodeWrapper<ast::Expr, MethodReceiverTag> {
19472029
_ => unreachable!(),
19482030
}
19492031
}
2032+
fn as_target(&self) -> Target {
2033+
Target::Expression
2034+
}
19502035
}
19512036

19522037
fn build_single_delegations<'a, Node: InvocationCollectorNode>(
@@ -2210,7 +2295,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
22102295

22112296
fn expand_cfg_true(
22122297
&mut self,
2213-
node: &mut (impl HasAttrs + HasNodeId),
2298+
node: &mut impl InvocationCollectorNode,
22142299
attr: ast::Attribute,
22152300
pos: usize,
22162301
) -> EvalConfigResult {
@@ -2219,8 +2304,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
22192304
&attr,
22202305
attr.span,
22212306
self.cfg().lint_node_id,
2222-
// Target doesn't matter for `cfg` parsing.
2223-
Target::Crate,
2307+
node.as_target(),
22242308
self.cfg().features,
22252309
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
22262310
parse_cfg,

tests/ui/cfg/suggest-any-or-all.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ LL | #[cfg(foo, bar)]
77
| expected a single argument here
88
|
99
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
10-
help: if the crate should be enabled when all these predicates are, wrap them in `all`
10+
help: if the function should be enabled when all these predicates are, wrap them in `all`
1111
|
1212
LL - #[cfg(foo, bar)]
1313
LL + #[cfg(all(foo, bar))]
1414
|
15-
help: alternately, if the crate should be enabled when any these predicates are, wrap them in `any`
15+
help: alternately, if the function should be enabled when any these predicates are, wrap them in `any`
1616
|
1717
LL - #[cfg(foo, bar)]
1818
LL + #[cfg(any(foo, bar))]
@@ -27,7 +27,7 @@ LL | #[cfg()]
2727
| expected a single argument here
2828
|
2929
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
30-
help: if the crate should be disabled, use `#[cfg(false)]`
30+
help: if the struct should be disabled, use `#[cfg(false)]`
3131
|
3232
LL | #[cfg(false)]
3333
| +++++

tests/ui/conditional-compilation/cfg-attr-syntax-validation.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LL | #[cfg()]
2929
| expected a single argument here
3030
|
3131
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
32-
help: if the crate should be disabled, use `#[cfg(false)]`
32+
help: if the struct should be disabled, use `#[cfg(false)]`
3333
|
3434
LL | #[cfg(false)]
3535
| +++++
@@ -43,12 +43,12 @@ LL | #[cfg(a, b)]
4343
| expected a single argument here
4444
|
4545
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
46-
help: if the crate should be enabled when all these predicates are, wrap them in `all`
46+
help: if the struct should be enabled when all these predicates are, wrap them in `all`
4747
|
4848
LL - #[cfg(a, b)]
4949
LL + #[cfg(all(a, b))]
5050
|
51-
help: alternately, if the crate should be enabled when any these predicates are, wrap them in `any`
51+
help: alternately, if the struct should be enabled when any these predicates are, wrap them in `any`
5252
|
5353
LL - #[cfg(a, b)]
5454
LL + #[cfg(any(a, b))]

0 commit comments

Comments
 (0)