Skip to content

Commit 38c2997

Browse files
committed
Auto merge of #154205 - traviscross:TC/crater-for-format_args-deduplication, r=<try>
CRATER: Analyze effect of `format_args!` deduplication
2 parents 8a70352 + df7beb8 commit 38c2997

7 files changed

Lines changed: 574 additions & 18 deletions

File tree

compiler/rustc_ast/src/format.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,35 @@ pub struct FormatArguments {
7777
arguments: Vec<FormatArgument>,
7878
num_unnamed_args: usize,
7979
num_explicit_args: usize,
80-
names: FxHashMap<Symbol, usize>,
80+
explicit_names: FxHashMap<Symbol, usize>,
8181
}
8282

8383
impl FormatArguments {
8484
pub fn new() -> Self {
8585
Self {
8686
arguments: Vec::new(),
87-
names: FxHashMap::default(),
87+
explicit_names: FxHashMap::default(),
8888
num_unnamed_args: 0,
8989
num_explicit_args: 0,
9090
}
9191
}
9292

9393
pub fn add(&mut self, arg: FormatArgument) -> usize {
9494
let index = self.arguments.len();
95-
if let Some(name) = arg.kind.ident() {
96-
self.names.insert(name.name, index);
97-
} else if self.names.is_empty() {
98-
// Only count the unnamed args before the first named arg.
99-
// (Any later ones are errors.)
100-
self.num_unnamed_args += 1;
95+
match arg.kind {
96+
FormatArgumentKind::Normal => {
97+
// Only count the unnamed args before the first named arg.
98+
// (Any later ones are errors.)
99+
if self.explicit_names.is_empty() {
100+
self.num_unnamed_args += 1;
101+
}
102+
}
103+
FormatArgumentKind::Named(ident) => {
104+
self.explicit_names.insert(ident.name, index);
105+
}
106+
FormatArgumentKind::Captured(_) => {
107+
// Don't record the name yet, to keep duplicate captures until AST->HIR lowering.
108+
}
101109
}
102110
if !matches!(arg.kind, FormatArgumentKind::Captured(..)) {
103111
// This is an explicit argument.
@@ -113,8 +121,8 @@ impl FormatArguments {
113121
index
114122
}
115123

116-
pub fn by_name(&self, name: Symbol) -> Option<(usize, &FormatArgument)> {
117-
let i = *self.names.get(&name)?;
124+
pub fn by_explicit_name(&self, name: Symbol) -> Option<(usize, &FormatArgument)> {
125+
let i = *self.explicit_names.get(&name)?;
118126
Some((i, &self.arguments[i]))
119127
}
120128

0 commit comments

Comments
 (0)