Skip to content

Commit

Permalink
Fix incorrect gating of nonterminals in key-value attributes
Browse files Browse the repository at this point in the history
Fixes #85432

When processing a `#[derive]` or `#[cfg_eval]` attribute, we need to
re-parse our attribute target, which requires flattenting all
`Nonterminals`. However, this caused us to incorrectly gate on a
(flattented) nonterminal in a key-value attribute, which is supposed to
be allowed.

Since we already perform this gating during the initial parse, we
suppress it in `capture_cfg` mode.
  • Loading branch information
Aaron1011 committed May 18, 2021
1 parent a5560a6 commit fd1e19e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,10 @@ impl<'a> Parser<'a> {

match &expr.kind {
// Not gated to support things like `doc = $expr` that work on stable.
_ if is_interpolated_expr => {}
// Do not gate in `capture_cfg` mode, since we flatten all nontemrinals
// before parsing. `capture_cfg` mode is only used to reparse existing
// tokens, so the gating will be performed by the initial parse
_ if is_interpolated_expr || self.capture_cfg => {}
ExprKind::Lit(lit) if lit.kind.is_unsuffixed() => {}
_ => self.sess.gated_spans.gate(sym::extended_key_value_attributes, span),
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/ui/macros/issue-85432-ungated-attr-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// check-pass
// Regression test for issue #85432
// Ensures that we don't incorrectly gate nonterminals
// in key-value macros when we need to reparse due to
// the presence of `#[derive]`

macro_rules! with_doc_comment {
($comment:expr, $item:item) => {
#[doc = $comment]
$item
};
}

macro_rules! database_table_doc {
() => {
""
};
}

with_doc_comment! {
database_table_doc!(),
#[derive(Debug)]
struct Image {
#[cfg(FALSE)]
_f: (),
}

}

fn main() {}

0 comments on commit fd1e19e

Please sign in to comment.