Skip to content

Commit ec1a240

Browse files
committed
Replace "early parsed" terminology
AST attributes use "early parsed"/"parsed" terminology to refer to the `CfgTrace` and `CfgAttrTrace` attributes. I think this terminology is meant to echo the terminology used for HIR attributes, i.e. `hir::Attribute::{Parsed,Unparsed}`, probably because `hir::Attribute::Parsed` is used for attributes that aren't stored in a token-based form. But this naming is misleading. "Early parsed" attributes aren't parsed at all because they are inserted by the compiler and cannot be written in source code. There are also two comments that claim that these attributes are kept in parsed form "so they don't have to be reparsed every time they're used, for performance", which is simply incorrect. This commit renames these as "synthetic" attributes, which better reflects their nature. The commit also fixes the incorrect comments. Note that `is_parsed_attribute` is unchanged, because it refers to the HIR attribute meaning. (And the removal of the synthetic attributes from it in the previous commit is now more obviously correct.)
1 parent 9e65a16 commit ec1a240

23 files changed

Lines changed: 98 additions & 99 deletions

File tree

compiler/rustc_ast/src/ast.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3415,13 +3415,11 @@ pub struct Attribute {
34153415

34163416
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
34173417
pub enum AttrKind {
3418-
/// A normal (non-doc comment) attribute, with attributes in unparsed form.
3418+
/// A normal attribute.
34193419
Normal(Box<NormalAttr>),
34203420

3421-
/// A normal (non-doc comment) attribute, with attributes in parsed form, so they don't have to
3422-
/// be reparsed every time they're used, for performance. Only used for a small number of
3423-
/// attribute kinds.
3424-
Parsed(Box<EarlyParsedAttribute>),
3421+
/// A synthetic attribute inserted by the compiler.
3422+
Synthetic(Box<SyntheticAttr>),
34253423

34263424
/// A doc comment (e.g. `/// ...`, `//! ...`, `/** ... */`, `/*! ... */`).
34273425
/// Doc attributes (e.g. `#[doc="..."]`) are represented with the `Normal`
@@ -3457,29 +3455,29 @@ pub struct AttrItem {
34573455
pub args: AttrArgs,
34583456
}
34593457

3460-
/// Some attributes are stored in parsed form in the AST.
3461-
/// This is done for performance reasons, so the attributes don't need to be reparsed on every use.
3458+
/// Synthetic attributes are inserted by the compiler and cannot be written in source code. They
3459+
/// receive special treatment in various ways because they must not affect observable behaviour:
3460+
/// they are invisible to proc macros, cannot be pretty-printed, and are unable to re-enter the
3461+
/// parser.
34623462
#[derive(Clone, Encodable, Decodable, Debug, StableHash)]
3463-
pub enum EarlyParsedAttribute {
3464-
/// This special attribute is added by the compiler when a `cfg` attribute is expanded so that
3463+
pub enum SyntheticAttr {
3464+
/// This synthetic attribute is added by the compiler when a `cfg` attribute is expanded so that
34653465
/// subsequent code can tell that conditional compilation occurred. A `#[cfg(pred)]` with a
34663466
/// true predicate is replaced by a synthetic `CfgTrace` attribute that records the parsed
34673467
/// predicate. A `#[cfg(pred)]` with a false predicate leaves no trace because there is no node
34683468
/// left to annotate.
34693469
///
34703470
/// The attribute is used for some diagnostics, by rustdoc (for detecting feature usage), and
3471-
/// by some clippy lints. It is treated specially in various places because it must not be
3472-
/// observable in any way that could change behaviour. For example, it is never pretty-printed
3473-
/// and it is hidden from proc macros.
3471+
/// by some clippy lints.
34743472
CfgTrace(CfgEntry),
34753473

3476-
/// This special attribute is added by the compiler when a `cfg_attr` attribute is expanded so
3474+
/// This synthetic attribute is added by the compiler when a `cfg_attr` attribute is expanded so
34773475
/// that subsequent code can tell that conditional compilation occurred. A `#[cfg_attr(pred,
34783476
/// attrs)]` is replaced by a synthetic `CfgAttrTrace` attribute whether the predicate
34793477
/// evaluated true or not (or even failed to parse). The `pred` and `attrs` are not recorded
34803478
/// because they are not needed.
34813479
///
3482-
/// In all other respects, it is the same as `CfgTrace`.
3480+
/// The attribute is used by some clippy lints.
34833481
CfgAttrTrace,
34843482
}
34853483

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@ impl HasTokens for Attribute {
170170
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
171171
match &self.kind {
172172
AttrKind::Normal(normal) => normal.tokens.as_ref(),
173-
AttrKind::Parsed(..) | AttrKind::DocComment(..) => unreachable!(),
173+
AttrKind::Synthetic(..) | AttrKind::DocComment(..) => unreachable!(),
174174
}
175175
}
176176
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
177177
Some(match &mut self.kind {
178178
AttrKind::Normal(normal) => &mut normal.tokens,
179-
AttrKind::Parsed(..) | AttrKind::DocComment(..) => unreachable!(),
179+
AttrKind::Synthetic(..) | AttrKind::DocComment(..) => unreachable!(),
180180
})
181181
}
182182
}

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use thin_vec::{ThinVec, thin_vec};
1313

1414
use crate::ast::{
1515
AttrArgs, AttrId, AttrItem, AttrKind, AttrStyle, AttrVec, Attribute, DUMMY_NODE_ID, DelimArgs,
16-
EarlyParsedAttribute, Expr, ExprKind, LitKind, MetaItem, MetaItemInner, MetaItemKind,
17-
MetaItemLit, NormalAttr, Path, PathSegment, Safety,
16+
Expr, ExprKind, LitKind, MetaItem, MetaItemInner, MetaItemKind, MetaItemLit, NormalAttr, Path,
17+
PathSegment, Safety, SyntheticAttr,
1818
};
1919
use crate::token::{
2020
self, CommentKind, Delimiter, DocFragmentKind, InvisibleOrigin, MetaVarKind, Token,
@@ -61,16 +61,16 @@ impl Attribute {
6161
pub fn get_normal_item(&self) -> &AttrItem {
6262
match &self.kind {
6363
AttrKind::Normal(normal) => &normal.item,
64-
AttrKind::Parsed(..) | AttrKind::DocComment(..) => unreachable!(),
64+
AttrKind::Synthetic(..) | AttrKind::DocComment(..) => unreachable!(),
6565
}
6666
}
6767

68-
pub fn convert_normal_to_parsed(&mut self, early_parsed_attribute: EarlyParsedAttribute) {
68+
pub fn convert_normal_to_synthetic(&mut self, synthetic_attr: SyntheticAttr) {
6969
match self.kind {
7070
AttrKind::Normal(..) => {
71-
self.kind = AttrKind::Parsed(Box::new(early_parsed_attribute));
71+
self.kind = AttrKind::Synthetic(Box::new(synthetic_attr));
7272
}
73-
AttrKind::Parsed(..) | AttrKind::DocComment(..) => unreachable!(),
73+
AttrKind::Synthetic(..) | AttrKind::DocComment(..) => unreachable!(),
7474
}
7575
}
7676
}
@@ -86,7 +86,7 @@ impl AttributeExt for Attribute {
8686
AttrArgs::Eq { expr, .. } => Some(expr.span),
8787
_ => None,
8888
},
89-
AttrKind::Parsed(..) | AttrKind::DocComment(..) => None,
89+
AttrKind::Synthetic(..) | AttrKind::DocComment(..) => None,
9090
}
9191
}
9292

@@ -95,36 +95,36 @@ impl AttributeExt for Attribute {
9595
/// a doc comment) will return `false`.
9696
fn is_doc_comment(&self) -> Option<Span> {
9797
match self.kind {
98-
AttrKind::Normal(..) | AttrKind::Parsed(..) => None,
98+
AttrKind::Normal(..) | AttrKind::Synthetic(..) => None,
9999
AttrKind::DocComment(..) => Some(self.span),
100100
}
101101
}
102102

103103
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
104104
fn name(&self) -> Option<Symbol> {
105-
use EarlyParsedAttribute::*;
105+
use SyntheticAttr::*;
106106
match &self.kind {
107107
AttrKind::Normal(normal) => normal.item.name(),
108-
AttrKind::Parsed(CfgTrace(_) | CfgAttrTrace) => None,
108+
AttrKind::Synthetic(CfgTrace(_) | CfgAttrTrace) => None,
109109
AttrKind::DocComment(..) => None,
110110
}
111111
}
112112

113113
fn symbol_path(&self) -> Option<SmallVec<[Symbol; 1]>> {
114-
use EarlyParsedAttribute::*;
114+
use SyntheticAttr::*;
115115
match &self.kind {
116116
AttrKind::Normal(normal) => {
117117
Some(normal.item.path.segments.iter().map(|i| i.ident.name).collect())
118118
}
119-
AttrKind::Parsed(CfgTrace(_) | CfgAttrTrace) => None,
119+
AttrKind::Synthetic(CfgTrace(_) | CfgAttrTrace) => None,
120120
AttrKind::DocComment(_, _) => None,
121121
}
122122
}
123123

124124
fn path_span(&self) -> Option<Span> {
125125
match &self.kind {
126126
AttrKind::Normal(attr) => Some(attr.item.path.span),
127-
AttrKind::Parsed(..) => unreachable!(),
127+
AttrKind::Synthetic(..) => unreachable!(),
128128
AttrKind::DocComment(_, _) => None,
129129
}
130130
}
@@ -141,7 +141,7 @@ impl AttributeExt for Attribute {
141141
.zip(name)
142142
.all(|(s, n)| s.args.is_none() && s.ident.name == *n)
143143
}
144-
AttrKind::Parsed(..) => false,
144+
AttrKind::Synthetic(..) => false,
145145
AttrKind::DocComment(..) => false,
146146
}
147147
}
@@ -153,7 +153,7 @@ impl AttributeExt for Attribute {
153153
fn is_word(&self) -> bool {
154154
match &self.kind {
155155
AttrKind::Normal(normal) => matches!(normal.item.args, AttrArgs::Empty),
156-
AttrKind::Parsed(..) => unreachable!(),
156+
AttrKind::Synthetic(..) => unreachable!(),
157157
AttrKind::DocComment(..) => false,
158158
}
159159
}
@@ -168,7 +168,7 @@ impl AttributeExt for Attribute {
168168
fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
169169
match &self.kind {
170170
AttrKind::Normal(normal) => normal.item.meta_item_list(),
171-
AttrKind::Parsed(..) => None,
171+
AttrKind::Synthetic(..) => None,
172172
AttrKind::DocComment(..) => None,
173173
}
174174
}
@@ -191,7 +191,7 @@ impl AttributeExt for Attribute {
191191
fn value_str(&self) -> Option<Symbol> {
192192
match &self.kind {
193193
AttrKind::Normal(normal) => normal.item.value_str(),
194-
AttrKind::Parsed(..) => unreachable!(),
194+
AttrKind::Synthetic(..) => unreachable!(),
195195
AttrKind::DocComment(..) => None,
196196
}
197197
}
@@ -211,7 +211,7 @@ impl AttributeExt for Attribute {
211211
{
212212
Some((value, DocFragmentKind::Raw(value_span)))
213213
}
214-
AttrKind::Normal(..) | AttrKind::Parsed(..) => None,
214+
AttrKind::Normal(..) | AttrKind::Synthetic(..) => None,
215215
}
216216
}
217217

@@ -280,15 +280,15 @@ impl Attribute {
280280
pub fn meta(&self) -> Option<MetaItem> {
281281
match &self.kind {
282282
AttrKind::Normal(normal) => normal.item.meta(self.span),
283-
AttrKind::Parsed(..) => None,
283+
AttrKind::Synthetic(..) => None,
284284
AttrKind::DocComment(..) => None,
285285
}
286286
}
287287

288288
pub fn meta_kind(&self) -> Option<MetaItemKind> {
289289
match &self.kind {
290290
AttrKind::Normal(normal) => normal.item.meta_kind(),
291-
AttrKind::Parsed(..) => unreachable!(),
291+
AttrKind::Synthetic(..) => unreachable!(),
292292
AttrKind::DocComment(..) => None,
293293
}
294294
}
@@ -301,7 +301,7 @@ impl Attribute {
301301
.unwrap_or_else(|| panic!("attribute is missing tokens: {self:?}"))
302302
.to_attr_token_stream()
303303
.to_token_trees(),
304-
AttrKind::Parsed(..) => vec![],
304+
AttrKind::Synthetic(..) => vec![],
305305
AttrKind::DocComment(comment_kind, data) => vec![TokenTree::token_alone(
306306
token::DocComment(comment_kind, self.style, data),
307307
self.span,

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ macro_rules! common_visitor_and_walkers {
365365
crate::token::LitKind,
366366
crate::tokenstream::LazyAttrTokenStream,
367367
crate::tokenstream::TokenStream,
368-
EarlyParsedAttribute,
369368
Movability,
370369
Mutability,
371370
Pinnedness,
@@ -374,6 +373,7 @@ macro_rules! common_visitor_and_walkers {
374373
rustc_span::ErrorGuaranteed,
375374
std::borrow::Cow<'_, str>,
376375
Symbol,
376+
SyntheticAttr,
377377
u8,
378378
usize,
379379
);

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ impl<'a> AstValidator<'a> {
514514
}
515515

516516
fn check_decl_attrs(&self, fn_decl: &FnDecl) {
517-
use EarlyParsedAttribute::*;
517+
use SyntheticAttr::*;
518518
fn_decl
519519
.inputs
520520
.iter()
@@ -525,7 +525,7 @@ impl<'a> AstValidator<'a> {
525525
[sym::allow, sym::deny, sym::expect, sym::forbid, sym::splat, sym::warn];
526526
!attr.has_any_name(&arr) && rustc_attr_parsing::is_builtin_attr(&normal.item)
527527
}
528-
AttrKind::Parsed(CfgTrace(_) | CfgAttrTrace) => false,
528+
AttrKind::Synthetic(CfgTrace(_) | CfgAttrTrace) => false,
529529
AttrKind::DocComment(..) => true,
530530
})
531531
.for_each(|attr| {

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
670670
}
671671

672672
fn print_attribute_inline(&mut self, attr: &ast::Attribute, is_inline: bool) -> bool {
673-
use ast::EarlyParsedAttribute::*;
673+
use ast::SyntheticAttr::*;
674674
match attr.kind {
675-
AttrKind::Parsed(CfgTrace(_) | CfgAttrTrace) => {
675+
AttrKind::Synthetic(CfgTrace(_) | CfgAttrTrace) => {
676676
// These are internal synthetic attributes with no syntax, so avoid printing them
677677
// to keep the printed code reasonably parse-able.
678678
return false;
@@ -692,7 +692,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
692692
self.print_attr_item(&normal.item, attr.span);
693693
self.word("]");
694694
}
695-
ast::AttrKind::Parsed(..) => unreachable!(), // due to early return above
695+
ast::AttrKind::Synthetic(..) => unreachable!(), // due to early return above
696696
ast::AttrKind::DocComment(comment_kind, data) => {
697697
self.word(doc_comment_to_string(
698698
DocFragmentKind::Sugared(*comment_kind),

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ use crate::attributes::AttributeSafety;
2020
use crate::context::{
2121
ATTRIBUTE_PARSERS, AcceptContext, FinalizeContext, FinalizeFn, SharedContext,
2222
};
23-
use crate::early_parsed::EarlyParsedState;
2423
use crate::parser::{AllowExprMetavar, ArgParser, PathParser, RefPathParser};
2524
use crate::session_diagnostics::ParsedDescription;
25+
use crate::synthetic::SyntheticAttrState;
2626
use crate::{AttributeTemplate, OmitDoc, ShouldEmit};
2727

2828
pub struct EmitAttribute(
@@ -290,7 +290,7 @@ impl<'sess> AttributeParser<'sess> {
290290
) -> Vec<Attribute> {
291291
let mut attributes = Vec::new();
292292
let mut attr_paths: Vec<RefPathParser<'_>> = Vec::new();
293-
let mut early_parsed_state = EarlyParsedState::default();
293+
let mut synthetic_attr_state = SyntheticAttrState::default();
294294

295295
let mut finalizers: Vec<FinalizeFn> = Vec::with_capacity(attrs.len());
296296

@@ -326,8 +326,8 @@ impl<'sess> AttributeParser<'sess> {
326326
comment: *symbol,
327327
}));
328328
}
329-
ast::AttrKind::Parsed(parsed) => {
330-
early_parsed_state.accept_early_parsed_attribute(attr_span, lower_span, parsed);
329+
ast::AttrKind::Synthetic(synthetic) => {
330+
synthetic_attr_state.accept_synthetic_attr(attr_span, lower_span, synthetic);
331331
continue;
332332
}
333333
ast::AttrKind::Normal(n) => {
@@ -448,7 +448,7 @@ impl<'sess> AttributeParser<'sess> {
448448
}
449449
}
450450

451-
early_parsed_state.finalize_early_parsed_attributes(&mut attributes);
451+
synthetic_attr_state.finalize_synthetic_attrs(&mut attributes);
452452
for f in &finalizers {
453453
if let Some(attr) = f(&mut FinalizeContext {
454454
shared: SharedContext {
@@ -497,7 +497,8 @@ impl<'sess> AttributeParser<'sess> {
497497
/// The list of attributes that are parsed attributes,
498498
/// even though they don't have a parser in `Late::parsers()`
499499
const SPECIAL_ATTRIBUTES: &[&[Symbol]] = &[
500-
// Cfg attrs are removed after being early-parsed, so don't need to be in the parser list
500+
// Cfg attrs are removed after being converted into synthetic attrs and don't need to
501+
// be in the parser list.
501502
&[sym::cfg],
502503
&[sym::cfg_attr],
503504
];

compiler/rustc_attr_parsing/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ mod attributes;
9999
mod check_cfg;
100100
mod context;
101101
mod diagnostics;
102-
mod early_parsed;
103102
mod interface;
104103
pub mod parser;
105104
mod safety;
106105
mod session_diagnostics;
107106
mod stability;
107+
mod synthetic;
108108
mod target_checking;
109109
mod template;
110110
pub mod validate_attr;

0 commit comments

Comments
 (0)