Skip to content

Fix the end location of an implicitly-concatenated string #4324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1978,8 +1978,6 @@ def test_multi_line_str(self):
self._check_end_pos(assign, 3, 40)
self._check_end_pos(assign.value, 3, 40)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_continued_str(self):
s = dedent('''
x = "first part" \\
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 8 additions & 10 deletions compiler/parser/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ pub fn parse_strings(
) -> Result<Expr, LexicalError> {
// Preserve the initial location and kind.
let initial_start = values[0].0;
let initial_end = values[0].2;
let last_end = values.last().unwrap().2;
let initial_kind = (values[0].1 .1 == StringKind::U).then(|| "u".to_owned());

// Optimization: fast-track the common case of a single string.
if matches!(&*values, [(_, (_, StringKind::Normal | StringKind::U), _)]) {
let value = values.into_iter().last().unwrap().1 .0;
return Ok(Expr::new(
initial_start,
initial_end,
last_end,
ExprKind::Constant {
value: Constant::Str(value),
kind: initial_kind,
Expand All @@ -38,7 +38,7 @@ pub fn parse_strings(
let take_current = |current: &mut Vec<String>| -> Expr {
Expr::new(
initial_start,
initial_end,
last_end,
ExprKind::Constant {
value: Constant::Str(current.drain(..).join("")),
kind: initial_kind.clone(),
Expand Down Expand Up @@ -81,18 +81,16 @@ pub fn parse_strings(
deduped.push(take_current(&mut current));
}

Ok(if has_fstring {
Expr::new(
initial_start,
initial_end,
ExprKind::JoinedStr { values: deduped },
)
let node = if has_fstring {
ExprKind::JoinedStr { values: deduped }
} else {
deduped
.into_iter()
.exactly_one()
.expect("String must be concatenated to a single element.")
})
.node
};
Ok(Expr::new(initial_start, last_end, node))
}

#[cfg(test)]
Expand Down