mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 16:24:46 +00:00
Rollup merge of #128133 - nnethercote:fix-cfg_attr-spans, r=petrochenkov
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`
This commit is contained in:
commit
2dc88bf88a
@ -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};
|
||||
@ -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.
|
||||
// Convert `#[cfg_attr(pred, attr)]` to `#[attr]`.
|
||||
|
||||
// Use the `#` in `#[cfg_attr(pred, attr)]` as the `#` token
|
||||
// for `attr` when we expand it 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,
|
||||
|
@ -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: '#',
|
||||
@ -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,
|
||||
@ -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",
|
||||
|
@ -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",
|
||||
@ -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,
|
||||
|
@ -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",
|
||||
|
@ -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",
|
||||
|
@ -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: '#',
|
||||
@ -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: '#',
|
||||
@ -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",
|
||||
|
@ -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: '#',
|
||||
@ -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,
|
||||
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user