Disallow implicit concatenation of t-strings and other string types (#19485)
As of [this cpython PR](https://github.com/python/cpython/pull/135996), it is not allowed to concatenate t-strings with non-t-strings, implicitly or explicitly. Expressions such as `"foo" t"{bar}"` are now syntax errors. This PR updates some AST nodes and parsing to reflect this change. The structural change is that `TStringPart` is no longer needed, since, as in the case of `BytesStringLiteral`, the only possibilities are that we have a single `TString` or a vector of such (representing an implicit concatenation of t-strings). This removes a level of nesting from many AST expressions (which is what all the snapshot changes reflect), and simplifies some logic in the implementation of visitors, for example. The other change of note is in the parser. When we meet an implicit concatenation of string-like literals, we now count the number of t-string literals. If these do not exhaust the total number of implicitly concatenated pieces, then we emit a syntax error. To recover from this syntax error, we encode any t-string pieces as _invalid_ string literals (which means we flag them as invalid, record their range, and record the value as `""`). Note that if at least one of the pieces is an f-string we prefer to parse the entire string as an f-string; otherwise we parse it as a string. This logic is exactly the same as how we currently treat `BytesStringLiteral` parsing and error recovery - and carries with it the same pros and cons. Finally, note that I have not implemented any changes in the implementation of the formatter. As far as I can tell, none are needed. I did change a few of the fixtures so that we are always concatenating t-strings with t-strings.
This commit is contained in:
@@ -708,23 +708,10 @@ pub struct ComparableTString<'a> {
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::TStringValue> for ComparableTString<'a> {
|
||||
// The approach taken below necessarily deviates from the
|
||||
// corresponding implementation for [`ast::FStringValue`].
|
||||
// The reason is that a t-string value is composed of _three_
|
||||
// non-comparable parts: literals, f-string expressions, and
|
||||
// t-string interpolations. Since we have merged the AST nodes
|
||||
// that capture f-string expressions and t-string interpolations
|
||||
// into the shared [`ast::InterpolatedElement`], we must
|
||||
// be careful to distinguish between them here.
|
||||
// We model a [`ComparableTString`] on the actual
|
||||
// [CPython implementation] of a `string.templatelib.Template` object.
|
||||
//
|
||||
// Consequently, we model a [`ComparableTString`] on the actual
|
||||
// [CPython implementation] of a `string.templatelib.Template` object:
|
||||
// it is composed of `strings` and `interpolations`. In CPython,
|
||||
// the `strings` field is a tuple of honest strings (since f-strings
|
||||
// are evaluated). Our `strings` field will house both f-string
|
||||
// expressions and string literals.
|
||||
//
|
||||
// Finally, as in CPython, we must be careful to ensure that the length
|
||||
// As in CPython, we must be careful to ensure that the length
|
||||
// of `strings` is always one more than the length of `interpolations` -
|
||||
// that way we can recover the original reading order by interleaving
|
||||
// starting with `strings`. This is how we can tell the
|
||||
@@ -768,19 +755,6 @@ impl<'a> From<&'a ast::TStringValue> for ComparableTString<'a> {
|
||||
.push(ComparableInterpolatedStringElement::Literal("".into()));
|
||||
}
|
||||
|
||||
fn push_fstring_expression(&mut self, expression: &'a ast::InterpolatedElement) {
|
||||
if let Some(ComparableInterpolatedStringElement::Literal(last_literal)) =
|
||||
self.strings.last()
|
||||
{
|
||||
// Recall that we insert empty strings after
|
||||
// each interpolation. If we encounter an f-string
|
||||
// expression, we replace the empty string with it.
|
||||
if last_literal.is_empty() {
|
||||
self.strings.pop();
|
||||
}
|
||||
}
|
||||
self.strings.push(expression.into());
|
||||
}
|
||||
fn push_tstring_interpolation(&mut self, expression: &'a ast::InterpolatedElement) {
|
||||
self.interpolations.push(expression.into());
|
||||
self.start_new_literal();
|
||||
@@ -789,34 +763,13 @@ impl<'a> From<&'a ast::TStringValue> for ComparableTString<'a> {
|
||||
|
||||
let mut collector = Collector::default();
|
||||
|
||||
for part in value {
|
||||
match part {
|
||||
ast::TStringPart::Literal(string_literal) => {
|
||||
collector.push_literal(&string_literal.value);
|
||||
for element in value.elements() {
|
||||
match element {
|
||||
ast::InterpolatedStringElement::Literal(literal) => {
|
||||
collector.push_literal(&literal.value);
|
||||
}
|
||||
ast::TStringPart::TString(fstring) => {
|
||||
for element in &fstring.elements {
|
||||
match element {
|
||||
ast::InterpolatedStringElement::Literal(literal) => {
|
||||
collector.push_literal(&literal.value);
|
||||
}
|
||||
ast::InterpolatedStringElement::Interpolation(interpolation) => {
|
||||
collector.push_tstring_interpolation(interpolation);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ast::TStringPart::FString(fstring) => {
|
||||
for element in &fstring.elements {
|
||||
match element {
|
||||
ast::InterpolatedStringElement::Literal(literal) => {
|
||||
collector.push_literal(&literal.value);
|
||||
}
|
||||
ast::InterpolatedStringElement::Interpolation(expression) => {
|
||||
collector.push_fstring_expression(expression);
|
||||
}
|
||||
}
|
||||
}
|
||||
ast::InterpolatedStringElement::Interpolation(interpolation) => {
|
||||
collector.push_tstring_interpolation(interpolation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user