Skip to content

Commit 93db6f1

Browse files
committed
make sure the right target is passed to #[cfg()] when it is parsed
1 parent 7428af6 commit 93db6f1

File tree

3 files changed

+96
-9
lines changed

3 files changed

+96
-9
lines changed

compiler/rustc_expand/src/expand.rs

Lines changed: 90 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,17 @@ 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(_) => {
1626+
// Turned into another kind of foreign item during macro expansion.
1627+
unreachable!();
1628+
}
1629+
}
1630+
}
16051631
}
16061632

16071633
impl InvocationCollectorNode for ast::Variant {
@@ -1615,6 +1641,9 @@ impl InvocationCollectorNode for ast::Variant {
16151641
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16161642
walk_flat_map_variant(collector, self)
16171643
}
1644+
fn as_target(&self) -> Target {
1645+
Target::Variant
1646+
}
16181647
}
16191648

16201649
impl InvocationCollectorNode for ast::WherePredicate {
@@ -1628,6 +1657,9 @@ impl InvocationCollectorNode for ast::WherePredicate {
16281657
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16291658
walk_flat_map_where_predicate(collector, self)
16301659
}
1660+
fn as_target(&self) -> Target {
1661+
Target::WherePredicate
1662+
}
16311663
}
16321664

16331665
impl InvocationCollectorNode for ast::FieldDef {
@@ -1641,6 +1673,9 @@ impl InvocationCollectorNode for ast::FieldDef {
16411673
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16421674
walk_flat_map_field_def(collector, self)
16431675
}
1676+
fn as_target(&self) -> Target {
1677+
Target::Field
1678+
}
16441679
}
16451680

16461681
impl InvocationCollectorNode for ast::PatField {
@@ -1654,6 +1689,9 @@ impl InvocationCollectorNode for ast::PatField {
16541689
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16551690
walk_flat_map_pat_field(collector, self)
16561691
}
1692+
fn as_target(&self) -> Target {
1693+
Target::PatField
1694+
}
16571695
}
16581696

16591697
impl InvocationCollectorNode for ast::ExprField {
@@ -1667,6 +1705,9 @@ impl InvocationCollectorNode for ast::ExprField {
16671705
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16681706
walk_flat_map_expr_field(collector, self)
16691707
}
1708+
fn as_target(&self) -> Target {
1709+
Target::ExprField
1710+
}
16701711
}
16711712

16721713
impl InvocationCollectorNode for ast::Param {
@@ -1680,6 +1721,9 @@ impl InvocationCollectorNode for ast::Param {
16801721
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16811722
walk_flat_map_param(collector, self)
16821723
}
1724+
fn as_target(&self) -> Target {
1725+
Target::Param
1726+
}
16831727
}
16841728

16851729
impl InvocationCollectorNode for ast::GenericParam {
@@ -1693,6 +1737,25 @@ impl InvocationCollectorNode for ast::GenericParam {
16931737
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
16941738
walk_flat_map_generic_param(collector, self)
16951739
}
1740+
fn as_target(&self) -> Target {
1741+
let mut has_default = false;
1742+
Target::GenericParam {
1743+
kind: match &self.kind {
1744+
rustc_ast::GenericParamKind::Lifetime => {
1745+
rustc_hir::target::GenericParamKind::Lifetime
1746+
}
1747+
rustc_ast::GenericParamKind::Type { default } => {
1748+
has_default = default.is_some();
1749+
rustc_hir::target::GenericParamKind::Type
1750+
}
1751+
rustc_ast::GenericParamKind::Const { default, .. } => {
1752+
has_default = default.is_some();
1753+
rustc_hir::target::GenericParamKind::Const
1754+
}
1755+
},
1756+
has_default,
1757+
}
1758+
}
16961759
}
16971760

16981761
impl InvocationCollectorNode for ast::Arm {
@@ -1706,6 +1769,9 @@ impl InvocationCollectorNode for ast::Arm {
17061769
fn walk_flat_map(self, collector: &mut InvocationCollector<'_, '_>) -> Self::OutputTy {
17071770
walk_flat_map_arm(collector, self)
17081771
}
1772+
fn as_target(&self) -> Target {
1773+
Target::Arm
1774+
}
17091775
}
17101776

17111777
impl InvocationCollectorNode for ast::Stmt {
@@ -1779,6 +1845,9 @@ impl InvocationCollectorNode for ast::Stmt {
17791845
}
17801846
}
17811847
}
1848+
fn as_target(&self) -> Target {
1849+
Target::Statement
1850+
}
17821851
}
17831852

17841853
impl InvocationCollectorNode for ast::Crate {
@@ -1805,6 +1874,9 @@ impl InvocationCollectorNode for ast::Crate {
18051874
// Standard prelude imports are left in the crate for backward compatibility.
18061875
self.items.truncate(collector.cx.num_standard_library_imports);
18071876
}
1877+
fn as_target(&self) -> Target {
1878+
Target::Crate
1879+
}
18081880
}
18091881

18101882
impl InvocationCollectorNode for ast::Ty {
@@ -1838,6 +1910,10 @@ impl InvocationCollectorNode for ast::Ty {
18381910
_ => unreachable!(),
18391911
}
18401912
}
1913+
fn as_target(&self) -> Target {
1914+
// This is only used for attribute parsing, which are not allowed on types.
1915+
unreachable!()
1916+
}
18411917
}
18421918

18431919
impl InvocationCollectorNode for ast::Pat {
@@ -1861,6 +1937,9 @@ impl InvocationCollectorNode for ast::Pat {
18611937
_ => unreachable!(),
18621938
}
18631939
}
1940+
fn as_target(&self) -> Target {
1941+
todo!();
1942+
}
18641943
}
18651944

18661945
impl InvocationCollectorNode for ast::Expr {
@@ -1887,6 +1966,9 @@ impl InvocationCollectorNode for ast::Expr {
18871966
_ => unreachable!(),
18881967
}
18891968
}
1969+
fn as_target(&self) -> Target {
1970+
Target::Expression
1971+
}
18901972
}
18911973

18921974
struct OptExprTag;
@@ -1916,6 +1998,9 @@ impl InvocationCollectorNode for AstNodeWrapper<Box<ast::Expr>, OptExprTag> {
19161998
fn pre_flat_map_node_collect_attr(cfg: &StripUnconfigured<'_>, attr: &ast::Attribute) {
19171999
cfg.maybe_emit_expr_attr_err(attr);
19182000
}
2001+
fn as_target(&self) -> Target {
2002+
Target::Expression
2003+
}
19192004
}
19202005

19212006
/// This struct is a hack to workaround unstable of `stmt_expr_attributes`.
@@ -1947,6 +2032,9 @@ impl InvocationCollectorNode for AstNodeWrapper<ast::Expr, MethodReceiverTag> {
19472032
_ => unreachable!(),
19482033
}
19492034
}
2035+
fn as_target(&self) -> Target {
2036+
Target::Expression
2037+
}
19502038
}
19512039

19522040
fn build_single_delegations<'a, Node: InvocationCollectorNode>(
@@ -2210,7 +2298,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
22102298

22112299
fn expand_cfg_true(
22122300
&mut self,
2213-
node: &mut (impl HasAttrs + HasNodeId),
2301+
node: &mut impl InvocationCollectorNode,
22142302
attr: ast::Attribute,
22152303
pos: usize,
22162304
) -> EvalConfigResult {
@@ -2219,8 +2307,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
22192307
&attr,
22202308
attr.span,
22212309
self.cfg().lint_node_id,
2222-
// Target doesn't matter for `cfg` parsing.
2223-
Target::Crate,
2310+
node.as_target(),
22242311
self.cfg().features,
22252312
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
22262313
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)