Skip to content

Commit

Permalink
Rollup merge of #128133 - nnethercote:fix-cfg_attr-spans, r=petrochenkov
Browse files Browse the repository at this point in the history
Improve spans on evaluated `cfg_attr`s.

When converting something like `#![cfg_attr(cond, attr)]` into `#![attr]`, we currently duplicate the `#` token and the `!` token. But weirdly, there is also this comment:

// We don't really have a good span to use for the synthesized `[]`
// in `#[attr]`, so just use the span of the `#` token.

Maybe that comment used to be true? But now it is false: we can duplicate the existing delimiters (and their spans and spacing), much like we do for the `#` and `!`.

This commit does that, thus removing the incorrect comment, and improving the spans on `Group`s in a few proc-macro tests.

`@petrochenkov`
  • Loading branch information
matthiaskrgr authored Jul 24, 2024
2 parents f3a7c3f + ac26b88 commit 2dc88bf
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 41 deletions.
56 changes: 28 additions & 28 deletions compiler/rustc_expand/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::errors::{
};
use rustc_ast::ptr::P;
use rustc_ast::token::{Delimiter, Token, TokenKind};
use rustc_ast::tokenstream::{AttrTokenStream, AttrTokenTree, DelimSpacing, DelimSpan, Spacing};
use rustc_ast::tokenstream::{AttrTokenStream, AttrTokenTree, Spacing};
use rustc_ast::tokenstream::{LazyAttrTokenStream, TokenTree};
use rustc_ast::NodeId;
use rustc_ast::{self as ast, AttrStyle, Attribute, HasAttrs, HasTokens, MetaItem};
Expand Down Expand Up @@ -298,47 +298,47 @@ impl<'a> StripUnconfigured<'a> {
cfg_attr: &Attribute,
(item, item_span): (ast::AttrItem, Span),
) -> Attribute {
// We are taking an attribute of the form `#[cfg_attr(pred, attr)]`
// and producing an attribute of the form `#[attr]`. We
// have captured tokens for `attr` itself, but we need to
// synthesize tokens for the wrapper `#` and `[]`, which
// we do below.

// Use the `#` in `#[cfg_attr(pred, attr)]` as the `#` token
// for `attr` when we expand it to `#[attr]`
// Convert `#[cfg_attr(pred, attr)]` to `#[attr]`.

// Use the `#` from `#[cfg_attr(pred, attr)]` in the result `#[attr]`.
let mut orig_trees = cfg_attr.token_trees().into_iter();
let TokenTree::Token(pound_token @ Token { kind: TokenKind::Pound, .. }, _) =
orig_trees.next().unwrap().clone()
let Some(TokenTree::Token(pound_token @ Token { kind: TokenKind::Pound, .. }, _)) =
orig_trees.next()
else {
panic!("Bad tokens for attribute {cfg_attr:?}");
};

// We don't really have a good span to use for the synthesized `[]`
// in `#[attr]`, so just use the span of the `#` token.
let bracket_group = AttrTokenTree::Delimited(
DelimSpan::from_single(pound_token.span),
DelimSpacing::new(Spacing::JointHidden, Spacing::Alone),
Delimiter::Bracket,
item.tokens
.as_ref()
.unwrap_or_else(|| panic!("Missing tokens for {item:?}"))
.to_attr_token_stream(),
);
let trees = if cfg_attr.style == AttrStyle::Inner {
// For inner attributes, we do the same thing for the `!` in `#![some_attr]`
let TokenTree::Token(bang_token @ Token { kind: TokenKind::Not, .. }, _) =
orig_trees.next().unwrap().clone()
// For inner attributes, we do the same thing for the `!` in `#![attr]`.
let mut trees = if cfg_attr.style == AttrStyle::Inner {
let Some(TokenTree::Token(bang_token @ Token { kind: TokenKind::Not, .. }, _)) =
orig_trees.next()
else {
panic!("Bad tokens for attribute {cfg_attr:?}");
};
vec![
AttrTokenTree::Token(pound_token, Spacing::Joint),
AttrTokenTree::Token(bang_token, Spacing::JointHidden),
bracket_group,
]
} else {
vec![AttrTokenTree::Token(pound_token, Spacing::JointHidden), bracket_group]
vec![AttrTokenTree::Token(pound_token, Spacing::JointHidden)]
};

// And the same thing for the `[`/`]` delimiters in `#[attr]`.
let Some(TokenTree::Delimited(delim_span, delim_spacing, Delimiter::Bracket, _)) =
orig_trees.next()
else {
panic!("Bad tokens for attribute {cfg_attr:?}");
};
trees.push(AttrTokenTree::Delimited(
delim_span,
delim_spacing,
Delimiter::Bracket,
item.tokens
.as_ref()
.unwrap_or_else(|| panic!("Missing tokens for {item:?}"))
.to_attr_token_stream(),
));

let tokens = Some(LazyAttrTokenStream::new(AttrTokenStream::new(trees)));
let attr = attr::mk_attr_from_item(
&self.sess.psess.attr_id_generator,
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/proc-macro/cfg-eval-inner.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/cfg-eval-inner.rs:19:40: 19:54 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:19:5: 19:6 (#0),
span: $DIR/cfg-eval-inner.rs:19:7: 19:56 (#0),
},
Punct {
ch: '#',
Expand Down Expand Up @@ -168,7 +168,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/cfg-eval-inner.rs:23:48: 23:70 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:23:13: 23:14 (#0),
span: $DIR/cfg-eval-inner.rs:23:15: 23:72 (#0),
},
Literal {
kind: Integer,
Expand Down Expand Up @@ -233,7 +233,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/cfg-eval-inner.rs:32:40: 32:56 (#0),
},
],
span: $DIR/cfg-eval-inner.rs:32:5: 32:6 (#0),
span: $DIR/cfg-eval-inner.rs:32:7: 32:58 (#0),
},
Ident {
ident: "fn",
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/proc-macro/cfg-eval.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/cfg-eval.rs:22:36: 22:38 (#0),
},
],
span: $DIR/cfg-eval.rs:22:5: 22:6 (#0),
span: $DIR/cfg-eval.rs:22:6: 22:40 (#0),
},
Ident {
ident: "field_true",
Expand Down Expand Up @@ -99,7 +99,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/cfg-eval.rs:35:62: 35:73 (#0),
},
],
span: $DIR/cfg-eval.rs:35:39: 35:40 (#0),
span: $DIR/cfg-eval.rs:35:40: 35:75 (#0),
},
Group {
delimiter: Parenthesis,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/proc-macro/expand-to-derive.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
span: $DIR/expand-to-derive.rs:27:28: 27:39 (#0),
},
],
span: $DIR/expand-to-derive.rs:27:5: 27:6 (#0),
span: $DIR/expand-to-derive.rs:27:6: 27:41 (#0),
},
Ident {
ident: "struct",
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/proc-macro/inner-attrs.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
span: $DIR/inner-attrs.rs:41:52: 41:59 (#0),
},
],
span: $DIR/inner-attrs.rs:41:17: 41:18 (#0),
span: $DIR/inner-attrs.rs:41:19: 41:61 (#0),
},
Ident {
ident: "true",
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/proc-macro/issue-75930-derive-cfg.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/issue-75930-derive-cfg.rs:50:29: 50:40 (#0),
},
],
span: $DIR/issue-75930-derive-cfg.rs:50:1: 50:2 (#0),
span: $DIR/issue-75930-derive-cfg.rs:50:2: 50:42 (#0),
},
Punct {
ch: '#',
Expand Down Expand Up @@ -1395,7 +1395,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
span: $DIR/issue-75930-derive-cfg.rs:50:29: 50:40 (#0),
},
],
span: $DIR/issue-75930-derive-cfg.rs:50:1: 50:2 (#0),
span: $DIR/issue-75930-derive-cfg.rs:50:2: 50:42 (#0),
},
Punct {
ch: '#',
Expand Down Expand Up @@ -1571,7 +1571,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
span: $DIR/issue-75930-derive-cfg.rs:63:41: 63:51 (#0),
},
],
span: $DIR/issue-75930-derive-cfg.rs:63:13: 63:14 (#0),
span: $DIR/issue-75930-derive-cfg.rs:63:14: 63:53 (#0),
},
Ident {
ident: "false",
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/proc-macro/macro-rules-derive-cfg.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
span: $DIR/macro-rules-derive-cfg.rs:19:59: 19:66 (#3),
},
],
span: $DIR/macro-rules-derive-cfg.rs:19:25: 19:26 (#3),
span: $DIR/macro-rules-derive-cfg.rs:19:26: 19:68 (#3),
},
Punct {
ch: '#',
Expand All @@ -113,7 +113,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
span: $DIR/macro-rules-derive-cfg.rs:26:47: 26:55 (#0),
},
],
span: $DIR/macro-rules-derive-cfg.rs:26:13: 26:14 (#0),
span: $DIR/macro-rules-derive-cfg.rs:26:14: 26:57 (#0),
},
Group {
delimiter: Brace,
Expand Down Expand Up @@ -146,7 +146,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
span: $DIR/macro-rules-derive-cfg.rs:27:34: 27:42 (#0),
},
],
span: $DIR/macro-rules-derive-cfg.rs:27:5: 27:6 (#0),
span: $DIR/macro-rules-derive-cfg.rs:27:7: 27:44 (#0),
},
Literal {
kind: Integer,
Expand Down

0 comments on commit 2dc88bf

Please sign in to comment.