From ac26b883bf3824de42e7adbf0fa69e24f2586ddf Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 24 Jul 2024 20:29:28 +1000 Subject: [PATCH] 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. --- compiler/rustc_expand/src/config.rs | 56 +++++++++---------- tests/ui/proc-macro/cfg-eval-inner.stdout | 6 +- tests/ui/proc-macro/cfg-eval.stdout | 4 +- tests/ui/proc-macro/expand-to-derive.stdout | 2 +- tests/ui/proc-macro/inner-attrs.stdout | 2 +- .../proc-macro/issue-75930-derive-cfg.stdout | 6 +- .../proc-macro/macro-rules-derive-cfg.stdout | 6 +- 7 files changed, 41 insertions(+), 41 deletions(-) diff --git a/compiler/rustc_expand/src/config.rs b/compiler/rustc_expand/src/config.rs index 9da4aa84db525..6c02c237115c5 100644 --- a/compiler/rustc_expand/src/config.rs +++ b/compiler/rustc_expand/src/config.rs @@ -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. - - // 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, diff --git a/tests/ui/proc-macro/cfg-eval-inner.stdout b/tests/ui/proc-macro/cfg-eval-inner.stdout index 9fa8f437d0efa..1aac28b2ec2fb 100644 --- a/tests/ui/proc-macro/cfg-eval-inner.stdout +++ b/tests/ui/proc-macro/cfg-eval-inner.stdout @@ -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", diff --git a/tests/ui/proc-macro/cfg-eval.stdout b/tests/ui/proc-macro/cfg-eval.stdout index e26e16f5a8ce8..5d88297ad6880 100644 --- a/tests/ui/proc-macro/cfg-eval.stdout +++ b/tests/ui/proc-macro/cfg-eval.stdout @@ -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, diff --git a/tests/ui/proc-macro/expand-to-derive.stdout b/tests/ui/proc-macro/expand-to-derive.stdout index d59b7e5b88f45..81fc52ea22d7b 100644 --- a/tests/ui/proc-macro/expand-to-derive.stdout +++ b/tests/ui/proc-macro/expand-to-derive.stdout @@ -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", diff --git a/tests/ui/proc-macro/inner-attrs.stdout b/tests/ui/proc-macro/inner-attrs.stdout index c8d93babe3a48..ed47ee2cf5ac4 100644 --- a/tests/ui/proc-macro/inner-attrs.stdout +++ b/tests/ui/proc-macro/inner-attrs.stdout @@ -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", diff --git a/tests/ui/proc-macro/issue-75930-derive-cfg.stdout b/tests/ui/proc-macro/issue-75930-derive-cfg.stdout index cc712abf2a576..4dcf2b717d8a8 100644 --- a/tests/ui/proc-macro/issue-75930-derive-cfg.stdout +++ b/tests/ui/proc-macro/issue-75930-derive-cfg.stdout @@ -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", diff --git a/tests/ui/proc-macro/macro-rules-derive-cfg.stdout b/tests/ui/proc-macro/macro-rules-derive-cfg.stdout index 257d59974b88d..fadf210127e4d 100644 --- a/tests/ui/proc-macro/macro-rules-derive-cfg.stdout +++ b/tests/ui/proc-macro/macro-rules-derive-cfg.stdout @@ -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,