Skip to content

Commit dc82ea1

Browse files
committed
Address review comments.
1 parent ec1a240 commit dc82ea1

4 files changed

Lines changed: 12 additions & 16 deletions

File tree

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3455,10 +3455,10 @@ pub struct AttrItem {
34553455
pub args: AttrArgs,
34563456
}
34573457

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.
3458+
/// Synthetic attributes are inserted by the compiler. They cannot be written in source code, and
3459+
/// so cannot be pretty-printed by the AST pretty printer (because its output should be valid Rust
3460+
/// code). They receive special treatment because they must not affect observable language
3461+
/// behaviour: they are invisible to proc macros and are unable to re-enter the parser.
34623462
#[derive(Clone, Encodable, Decodable, Debug, StableHash)]
34633463
pub enum SyntheticAttr {
34643464
/// This synthetic attribute is added by the compiler when a `cfg` attribute is expanded so that

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ impl Attribute {
6565
}
6666
}
6767

68-
pub fn convert_normal_to_synthetic(&mut self, synthetic_attr: SyntheticAttr) {
68+
pub fn convert_normal_to_synthetic(self, synthetic_attr: SyntheticAttr) -> Attribute {
6969
match self.kind {
7070
AttrKind::Normal(..) => {
71-
self.kind = AttrKind::Synthetic(Box::new(synthetic_attr));
71+
Attribute { kind: AttrKind::Synthetic(Box::new(synthetic_attr)), ..self }
7272
}
7373
AttrKind::Synthetic(..) | AttrKind::DocComment(..) => unreachable!(),
7474
}
@@ -141,8 +141,7 @@ impl AttributeExt for Attribute {
141141
.zip(name)
142142
.all(|(s, n)| s.args.is_none() && s.ident.name == *n)
143143
}
144-
AttrKind::Synthetic(..) => false,
145-
AttrKind::DocComment(..) => false,
144+
AttrKind::Synthetic(..) | AttrKind::DocComment(..) => false,
146145
}
147146
}
148147

@@ -168,8 +167,7 @@ impl AttributeExt for Attribute {
168167
fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
169168
match &self.kind {
170169
AttrKind::Normal(normal) => normal.item.meta_item_list(),
171-
AttrKind::Synthetic(..) => None,
172-
AttrKind::DocComment(..) => None,
170+
AttrKind::Synthetic(..) | AttrKind::DocComment(..) => None,
173171
}
174172
}
175173

@@ -280,8 +278,7 @@ impl Attribute {
280278
pub fn meta(&self) -> Option<MetaItem> {
281279
match &self.kind {
282280
AttrKind::Normal(normal) => normal.item.meta(self.span),
283-
AttrKind::Synthetic(..) => None,
284-
AttrKind::DocComment(..) => None,
281+
AttrKind::Synthetic(..) | AttrKind::DocComment(..) => None,
285282
}
286283
}
287284

@@ -301,6 +298,7 @@ impl Attribute {
301298
.unwrap_or_else(|| panic!("attribute is missing tokens: {self:?}"))
302299
.to_attr_token_stream()
303300
.to_token_trees(),
301+
// Empty tokens here ensures synthetic attributes are invisible to proc macros.
304302
AttrKind::Synthetic(..) => vec![],
305303
AttrKind::DocComment(comment_kind, data) => vec![TokenTree::token_alone(
306304
token::DocComment(comment_kind, self.style, data),

compiler/rustc_expand/src/config.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ impl<'a> StripUnconfigured<'a> {
250250
pub(crate) fn expand_cfg_attr(&self, cfg_attr: &Attribute, recursive: bool) -> Vec<Attribute> {
251251
// A synthetic trace attribute left in AST in place of the original `cfg_attr` attribute.
252252
// It can later be used by lints or other diagnostics.
253-
let mut trace_attr = cfg_attr.clone();
254-
trace_attr.convert_normal_to_synthetic(SyntheticAttr::CfgAttrTrace);
253+
let trace_attr = cfg_attr.clone().convert_normal_to_synthetic(SyntheticAttr::CfgAttrTrace);
255254

256255
let Some((cfg_predicate, expanded_attrs)) = rustc_attr_parsing::parse_cfg_attr(
257256
cfg_attr,

compiler/rustc_expand/src/expand.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,8 +2297,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
22972297
if res.as_bool() {
22982298
// A synthetic trace attribute left in AST in place of the original `cfg` attribute.
22992299
// It can later be used by lints or other diagnostics.
2300-
let mut trace_attr = attr;
2301-
trace_attr.convert_normal_to_synthetic(SyntheticAttr::CfgTrace(cfg));
2300+
let trace_attr = attr.convert_normal_to_synthetic(SyntheticAttr::CfgTrace(cfg));
23022301
node.visit_attrs(|attrs| attrs.insert(pos, trace_attr));
23032302
}
23042303

0 commit comments

Comments
 (0)