Skip to content

Commit 0723271

Browse files
authored
Merge pull request RustPython#4324 from harupy/fix-end-location-of-concatenated-string
Fix the end location of an implicitly-concatenated string
2 parents 77e5d0c + d3ba7e0 commit 0723271

10 files changed

+21
-25
lines changed

Lib/test/test_ast.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,8 +1978,6 @@ def test_multi_line_str(self):
19781978
self._check_end_pos(assign, 3, 40)
19791979
self._check_end_pos(assign.value, 3, 40)
19801980

1981-
# TODO: RUSTPYTHON
1982-
@unittest.expectedFailure
19831981
def test_continued_str(self):
19841982
s = dedent('''
19851983
x = "first part" \\

compiler/parser/src/snapshots/rustpython_parser__string__tests__parse_f_string_concat_1.snap

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

compiler/parser/src/snapshots/rustpython_parser__string__tests__parse_f_string_concat_2.snap

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

compiler/parser/src/snapshots/rustpython_parser__string__tests__parse_f_string_concat_3.snap

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

compiler/parser/src/snapshots/rustpython_parser__string__tests__parse_string_concat.snap

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/parser/src/snapshots/rustpython_parser__string__tests__parse_u_f_string_concat_1.snap

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

compiler/parser/src/snapshots/rustpython_parser__string__tests__parse_u_f_string_concat_2.snap

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

compiler/parser/src/snapshots/rustpython_parser__string__tests__parse_u_string_concat_1.snap

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/parser/src/snapshots/rustpython_parser__string__tests__parse_u_string_concat_2.snap

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/parser/src/string.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ pub fn parse_strings(
1111
) -> Result<Expr, LexicalError> {
1212
// Preserve the initial location and kind.
1313
let initial_start = values[0].0;
14-
let initial_end = values[0].2;
14+
let last_end = values.last().unwrap().2;
1515
let initial_kind = (values[0].1 .1 == StringKind::U).then(|| "u".to_owned());
1616

1717
// Optimization: fast-track the common case of a single string.
1818
if matches!(&*values, [(_, (_, StringKind::Normal | StringKind::U), _)]) {
1919
let value = values.into_iter().last().unwrap().1 .0;
2020
return Ok(Expr::new(
2121
initial_start,
22-
initial_end,
22+
last_end,
2323
ExprKind::Constant {
2424
value: Constant::Str(value),
2525
kind: initial_kind,
@@ -38,7 +38,7 @@ pub fn parse_strings(
3838
let take_current = |current: &mut Vec<String>| -> Expr {
3939
Expr::new(
4040
initial_start,
41-
initial_end,
41+
last_end,
4242
ExprKind::Constant {
4343
value: Constant::Str(current.drain(..).join("")),
4444
kind: initial_kind.clone(),
@@ -81,18 +81,16 @@ pub fn parse_strings(
8181
deduped.push(take_current(&mut current));
8282
}
8383

84-
Ok(if has_fstring {
85-
Expr::new(
86-
initial_start,
87-
initial_end,
88-
ExprKind::JoinedStr { values: deduped },
89-
)
84+
let node = if has_fstring {
85+
ExprKind::JoinedStr { values: deduped }
9086
} else {
9187
deduped
9288
.into_iter()
9389
.exactly_one()
9490
.expect("String must be concatenated to a single element.")
95-
})
91+
.node
92+
};
93+
Ok(Expr::new(initial_start, last_end, node))
9694
}
9795

9896
#[cfg(test)]

0 commit comments

Comments
 (0)