Skip to content

Commit 0abecda

Browse files
author
Charles Marsh
committed
Avoid creating unused JoinedStr in FStringParser
1 parent c7ad41e commit 0abecda

14 files changed

Lines changed: 567 additions & 744 deletions

‎compiler/parser/src/fstring.rs‎

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,13 @@ impl<'a> FStringParser<'a> {
105105
nested -= 1;
106106
if nested == 0 {
107107
formatted_value_piece.push(next);
108-
spec_constructor.push(
109-
self.expr(ExprKind::FormattedValue {
110-
value: Box::new(
111-
FStringParser::new(
112-
&formatted_value_piece,
113-
Location::default(),
114-
&self.recurse_lvl + 1,
115-
)
116-
.parse()?,
117-
),
118-
conversion: ConversionFlag::None as _,
119-
format_spec: None,
120-
}),
108+
spec_constructor.extend(
109+
FStringParser::new(
110+
&format!("{{{}}}", formatted_value_piece),
111+
Location::default(),
112+
&self.recurse_lvl + 1,
113+
)
114+
.parse()?,
121115
);
122116
formatted_value_piece.clear();
123117
} else {
@@ -129,11 +123,13 @@ impl<'a> FStringParser<'a> {
129123
}
130124
'{' => {
131125
nested += 1;
132-
spec_constructor.push(self.expr(ExprKind::Constant {
133-
value: constant_piece.to_owned().into(),
134-
kind: None,
135-
}));
136-
constant_piece.clear();
126+
if !constant_piece.is_empty() {
127+
spec_constructor.push(self.expr(ExprKind::Constant {
128+
value: constant_piece.to_owned().into(),
129+
kind: None,
130+
}));
131+
constant_piece.clear();
132+
}
137133
formatted_value_piece.push(next);
138134
formatted_value_piece.push(' ');
139135
}
@@ -144,11 +140,13 @@ impl<'a> FStringParser<'a> {
144140
}
145141
self.chars.next();
146142
}
147-
spec_constructor.push(self.expr(ExprKind::Constant {
148-
value: constant_piece.to_owned().into(),
149-
kind: None,
150-
}));
151-
constant_piece.clear();
143+
if !constant_piece.is_empty() {
144+
spec_constructor.push(self.expr(ExprKind::Constant {
145+
value: constant_piece.to_owned().into(),
146+
kind: None,
147+
}));
148+
constant_piece.clear();
149+
}
152150
if nested > 0 {
153151
return Err(UnclosedLbrace);
154152
}
@@ -241,7 +239,7 @@ impl<'a> FStringParser<'a> {
241239
Err(UnclosedLbrace)
242240
}
243241

244-
fn parse(mut self) -> Result<Expr, FStringErrorType> {
242+
fn parse(mut self) -> Result<Vec<Expr>, FStringErrorType> {
245243
if self.recurse_lvl >= 2 {
246244
return Err(ExpressionNestedTooDeeply);
247245
}
@@ -287,7 +285,7 @@ impl<'a> FStringParser<'a> {
287285
}))
288286
}
289287

290-
Ok(self.expr(ExprKind::JoinedStr { values }))
288+
Ok(values)
291289
}
292290
}
293291

@@ -298,7 +296,7 @@ fn parse_fstring_expr(source: &str) -> Result<Expr, ParseError> {
298296

299297
/// Parse an fstring from a string, located at a certain position in the sourcecode.
300298
/// In case of errors, we will get the location and the error returned.
301-
pub fn parse_located_fstring(source: &str, location: Location) -> Result<Expr, FStringError> {
299+
pub fn parse_located_fstring(source: &str, location: Location) -> Result<Vec<Expr>, FStringError> {
302300
FStringParser::new(source, location, 0)
303301
.parse()
304302
.map_err(|error| FStringError { error, location })
@@ -308,7 +306,7 @@ pub fn parse_located_fstring(source: &str, location: Location) -> Result<Expr, F
308306
mod tests {
309307
use super::*;
310308

311-
fn parse_fstring(source: &str) -> Result<Expr, FStringErrorType> {
309+
fn parse_fstring(source: &str) -> Result<Vec<Expr>, FStringErrorType> {
312310
FStringParser::new(source, Location::default(), 0).parse()
313311
}
314312

‎compiler/parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_base.snap‎

Lines changed: 43 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)