Skip to content

Commit fa72316

Browse files
committed
Auto merge of #95715 - nnethercote:shrink-Nonterminal, r=davidtwco
Shrink `Nonterminal` Small consistency and performance improvements. r? `@petrochenkov`
2 parents ed6c958 + d9592c2 commit fa72316

File tree

10 files changed

+18
-16
lines changed

10 files changed

+18
-16
lines changed

compiler/rustc_ast/src/attr/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl MetaItem {
439439
}
440440
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. })) => match *nt {
441441
token::Nonterminal::NtMeta(ref item) => return item.meta(item.path.span),
442-
token::Nonterminal::NtPath(ref path) => path.clone(),
442+
token::Nonterminal::NtPath(ref path) => (**path).clone(),
443443
_ => return None,
444444
},
445445
_ => return None,

compiler/rustc_ast/src/mut_visit.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,9 @@ pub fn visit_interpolated<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut
772772
token::NtBlock(block) => vis.visit_block(block),
773773
token::NtStmt(stmt) => visit_clobber(stmt, |stmt| {
774774
// See reasoning above.
775-
vis.flat_map_stmt(stmt).expect_one("expected visitor to produce exactly one item")
775+
stmt.map(|stmt| {
776+
vis.flat_map_stmt(stmt).expect_one("expected visitor to produce exactly one item")
777+
})
776778
}),
777779
token::NtPat(pat) => vis.visit_pat(pat),
778780
token::NtExpr(expr) => vis.visit_expr(expr),

compiler/rustc_ast/src/token.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ impl PartialEq<TokenKind> for Token {
668668
pub enum Nonterminal {
669669
NtItem(P<ast::Item>),
670670
NtBlock(P<ast::Block>),
671-
NtStmt(ast::Stmt),
671+
NtStmt(P<ast::Stmt>),
672672
NtPat(P<ast::Pat>),
673673
NtExpr(P<ast::Expr>),
674674
NtTy(P<ast::Ty>),
@@ -677,13 +677,13 @@ pub enum Nonterminal {
677677
NtLiteral(P<ast::Expr>),
678678
/// Stuff inside brackets for attributes
679679
NtMeta(P<ast::AttrItem>),
680-
NtPath(ast::Path),
681-
NtVis(ast::Visibility),
680+
NtPath(P<ast::Path>),
681+
NtVis(P<ast::Visibility>),
682682
}
683683

684684
// `Nonterminal` is used a lot. Make sure it doesn't unintentionally get bigger.
685685
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
686-
rustc_data_structures::static_assert_size!(Nonterminal, 48);
686+
rustc_data_structures::static_assert_size!(Nonterminal, 16);
687687

688688
#[derive(Debug, Copy, Clone, PartialEq, Encodable, Decodable)]
689689
pub enum NonterminalKind {

compiler/rustc_expand/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Annotatable {
118118
Annotatable::ForeignItem(item) => {
119119
token::NtItem(P(item.and_then(ast::ForeignItem::into_item)))
120120
}
121-
Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
121+
Annotatable::Stmt(stmt) => token::NtStmt(stmt),
122122
Annotatable::Expr(expr) => token::NtExpr(expr),
123123
Annotatable::Arm(..)
124124
| Annotatable::ExprField(..)

compiler/rustc_expand/src/proc_macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl MultiItemModifier for ProcMacroDerive {
9090
// A proc macro can't observe the fact that we're passing
9191
// them an `NtStmt` - it can only see the underlying tokens
9292
// of the wrapped item
93-
token::NtStmt(stmt.into_inner())
93+
token::NtStmt(stmt)
9494
}
9595
_ => unreachable!(),
9696
};

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ macro_rules! maybe_whole_expr {
4141
return Ok(e);
4242
}
4343
token::NtPath(path) => {
44-
let path = path.clone();
44+
let path = (**path).clone();
4545
$p.bump();
4646
return Ok($p.mk_expr(
4747
$p.prev_token.span,

compiler/rustc_parse/src/parser/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ impl<'a> Parser<'a> {
12891289
/// so emit a proper diagnostic.
12901290
// Public for rustfmt usage.
12911291
pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> {
1292-
maybe_whole!(self, NtVis, |x| x);
1292+
maybe_whole!(self, NtVis, |x| x.into_inner());
12931293

12941294
self.expected_tokens.push(TokenType::Keyword(kw::Crate));
12951295
if self.is_crate_vis() {

compiler/rustc_parse/src/parser/nonterminal.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'a> Parser<'a> {
118118
token::NtBlock(self.collect_tokens_no_attrs(|this| this.parse_block())?)
119119
}
120120
NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
121-
Some(s) => token::NtStmt(s),
121+
Some(s) => token::NtStmt(P(s)),
122122
None => {
123123
return Err(self.struct_span_err(self.token.span, "expected a statement"));
124124
}
@@ -161,11 +161,11 @@ impl<'a> Parser<'a> {
161161
return Err(self.struct_span_err(self.token.span, msg));
162162
}
163163
NonterminalKind::Path => token::NtPath(
164-
self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?,
164+
P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?),
165165
),
166166
NonterminalKind::Meta => token::NtMeta(P(self.parse_attr_item(true)?)),
167167
NonterminalKind::Vis => token::NtVis(
168-
self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?,
168+
P(self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?),
169169
),
170170
NonterminalKind::Lifetime => {
171171
if self.check_lifetime() {

compiler/rustc_parse/src/parser/path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'a> Parser<'a> {
165165

166166
maybe_whole!(self, NtPath, |path| {
167167
reject_generics_if_mod_style(self, &path);
168-
path
168+
path.into_inner()
169169
});
170170

171171
if let token::Interpolated(nt) = &self.token.kind {

compiler/rustc_parse/src/parser/stmt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'a> Parser<'a> {
5454
stmt.visit_attrs(|stmt_attrs| {
5555
attrs.prepend_to_nt_inner(stmt_attrs);
5656
});
57-
return Ok(Some(stmt));
57+
return Ok(Some(stmt.into_inner()));
5858
}
5959

6060
Ok(Some(if self.token.is_keyword(kw::Let) {
@@ -535,7 +535,7 @@ impl<'a> Parser<'a> {
535535
recover: AttemptLocalParseRecovery,
536536
) -> PResult<'a, Option<Stmt>> {
537537
// Skip looking for a trailing semicolon when we have an interpolated statement.
538-
maybe_whole!(self, NtStmt, |x| Some(x));
538+
maybe_whole!(self, NtStmt, |x| Some(x.into_inner()));
539539

540540
let Some(mut stmt) = self.parse_stmt_without_recovery(true, ForceCollect::No)? else {
541541
return Ok(None);

0 commit comments

Comments
 (0)