Add an implicit concatenation flag to string and bytes constants (#6512)

## Summary

Per the discussion in
https://github.com/astral-sh/ruff/discussions/6183, this PR adds an
`implicit_concatenated` flag to the string and bytes constant variants.
It's not actually _used_ anywhere as of this PR, but it is covered by
the tests.

Specifically, we now use a struct for the string and bytes cases, along
with the `Expr::FString` node. That struct holds the value, plus the
flag:

```rust
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Constant {
    Str(StringConstant),
    Bytes(BytesConstant),
    ...
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StringConstant {
    /// The string value as resolved by the parser (i.e., without quotes, or escape sequences, or
    /// implicit concatenations).
    pub value: String,
    /// Whether the string contains multiple string tokens that were implicitly concatenated.
    pub implicit_concatenated: bool,
}

impl Deref for StringConstant {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        self.value.as_str()
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BytesConstant {
    /// The bytes value as resolved by the parser (i.e., without quotes, or escape sequences, or
    /// implicit concatenations).
    pub value: Vec<u8>,
    /// Whether the string contains multiple string tokens that were implicitly concatenated.
    pub implicit_concatenated: bool,
}

impl Deref for BytesConstant {
    type Target = [u8];
    fn deref(&self) -> &Self::Target {
        self.value.as_slice()
    }
}
```

## Test Plan

`cargo test`
This commit is contained in:
Charlie Marsh 2023-08-14 09:46:54 -04:00 committed by GitHub
parent fc0c9507d0
commit f16e780e0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
88 changed files with 1252 additions and 761 deletions

View file

@ -418,9 +418,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
if checker.enabled(Rule::BadStringFormatCharacter) { if checker.enabled(Rule::BadStringFormatCharacter) {
pylint::rules::bad_string_format_character::call( pylint::rules::bad_string_format_character::call(
checker, checker, val, location,
val.as_str(),
location,
); );
} }
} }
@ -918,7 +916,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
pylint::rules::await_outside_async(checker, expr); pylint::rules::await_outside_async(checker, expr);
} }
} }
Expr::FString(ast::ExprFString { values, range: _ }) => { Expr::FString(ast::ExprFString { values, .. }) => {
if checker.enabled(Rule::FStringMissingPlaceholders) { if checker.enabled(Rule::FStringMissingPlaceholders) {
pyflakes::rules::f_string_missing_placeholders(expr, values, checker); pyflakes::rules::f_string_missing_placeholders(expr, values, checker);
} }
@ -945,7 +943,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
range: _, range: _,
}) => { }) => {
if let Expr::Constant(ast::ExprConstant { if let Expr::Constant(ast::ExprConstant {
value: Constant::Str(value), value: Constant::Str(ast::StringConstant { value, .. }),
.. ..
}) = left.as_ref() }) = left.as_ref()
{ {

View file

@ -1275,7 +1275,7 @@ where
fn visit_format_spec(&mut self, format_spec: &'b Expr) { fn visit_format_spec(&mut self, format_spec: &'b Expr) {
match format_spec { match format_spec {
Expr::FString(ast::ExprFString { values, range: _ }) => { Expr::FString(ast::ExprFString { values, .. }) => {
for value in values { for value in values {
self.visit_expr(value); self.visit_expr(value);
} }

View file

@ -80,7 +80,7 @@ pub(crate) fn variable_name_task_id(
// If the keyword argument is not a string, we can't do anything. // If the keyword argument is not a string, we can't do anything.
let task_id = match &keyword.value { let task_id = match &keyword.value {
Expr::Constant(constant) => match &constant.value { Expr::Constant(constant) => match &constant.value {
Constant::Str(value) => value, Constant::Str(ast::StringConstant { value, .. }) => value,
_ => return None, _ => return None,
}, },
_ => return None, _ => return None,

View file

@ -112,7 +112,7 @@ pub(crate) fn check_positional_boolean_in_def(
let hint = match expr.as_ref() { let hint = match expr.as_ref() {
Expr::Name(name) => &name.id == "bool", Expr::Name(name) => &name.id == "bool",
Expr::Constant(ast::ExprConstant { Expr::Constant(ast::ExprConstant {
value: Constant::Str(value), value: Constant::Str(ast::StringConstant { value, .. }),
.. ..
}) => value == "bool", }) => value == "bool",
_ => false, _ => false,

View file

@ -64,7 +64,7 @@ pub(crate) fn getattr_with_constant(
return; return;
}; };
let Expr::Constant(ast::ExprConstant { let Expr::Constant(ast::ExprConstant {
value: Constant::Str(value), value: Constant::Str(ast::StringConstant { value, .. }),
.. ..
}) = arg }) = arg
else { else {

View file

@ -88,7 +88,7 @@ pub(crate) fn setattr_with_constant(
if !is_identifier(name) { if !is_identifier(name) {
return; return;
} }
if is_mangled_private(name.as_str()) { if is_mangled_private(name) {
return; return;
} }
// We can only replace a `setattr` call (which is an `Expr`) with an assignment // We can only replace a `setattr` call (which is an `Expr`) with an assignment

View file

@ -68,13 +68,13 @@ pub(crate) fn unreliable_callable_check(
return; return;
}; };
let Expr::Constant(ast::ExprConstant { let Expr::Constant(ast::ExprConstant {
value: Constant::Str(string), value: Constant::Str(ast::StringConstant { value, .. }),
.. ..
}) = attr }) = attr
else { else {
return; return;
}; };
if string != "__call__" { if value != "__call__" {
return; return;
} }

View file

@ -83,13 +83,13 @@ pub(crate) fn all_with_model_form(
continue; continue;
}; };
match value { match value {
Constant::Str(s) => { Constant::Str(ast::StringConstant { value, .. }) => {
if s == "__all__" { if value == "__all__" {
return Some(Diagnostic::new(DjangoAllWithModelForm, element.range())); return Some(Diagnostic::new(DjangoAllWithModelForm, element.range()));
} }
} }
Constant::Bytes(b) => { Constant::Bytes(ast::BytesConstant { value, .. }) => {
if b == "__all__".as_bytes() { if value == "__all__".as_bytes() {
return Some(Diagnostic::new(DjangoAllWithModelForm, element.range())); return Some(Diagnostic::new(DjangoAllWithModelForm, element.range()));
} }
} }

View file

@ -123,7 +123,7 @@ pub(crate) fn unrecognized_platform(checker: &mut Checker, test: &Expr) {
} }
if let Expr::Constant(ast::ExprConstant { if let Expr::Constant(ast::ExprConstant {
value: Constant::Str(value), value: Constant::Str(ast::StringConstant { value, .. }),
.. ..
}) = right }) = right
{ {

View file

@ -46,11 +46,11 @@ pub(super) fn is_pytest_parametrize(decorator: &Decorator, semantic: &SemanticMo
pub(super) fn keyword_is_literal(keyword: &Keyword, literal: &str) -> bool { pub(super) fn keyword_is_literal(keyword: &Keyword, literal: &str) -> bool {
if let Expr::Constant(ast::ExprConstant { if let Expr::Constant(ast::ExprConstant {
value: Constant::Str(string), value: Constant::Str(ast::StringConstant { value, .. }),
.. ..
}) = &keyword.value }) = &keyword.value
{ {
string == literal value == literal
} else { } else {
false false
} }
@ -63,7 +63,7 @@ pub(super) fn is_empty_or_null_string(expr: &Expr) -> bool {
.. ..
}) => string.is_empty(), }) => string.is_empty(),
Expr::Constant(constant) if constant.value.is_none() => true, Expr::Constant(constant) if constant.value.is_none() => true,
Expr::FString(ast::ExprFString { values, range: _ }) => { Expr::FString(ast::ExprFString { values, .. }) => {
values.iter().all(is_empty_or_null_string) values.iter().all(is_empty_or_null_string)
} }
_ => false, _ => false,

View file

@ -50,9 +50,9 @@ impl Violation for PytestParametrizeValuesWrongType {
} }
fn elts_to_csv(elts: &[Expr], generator: Generator) -> Option<String> { fn elts_to_csv(elts: &[Expr], generator: Generator) -> Option<String> {
let all_literals = elts.iter().all(|e| { let all_literals = elts.iter().all(|expr| {
matches!( matches!(
e, expr,
Expr::Constant(ast::ExprConstant { Expr::Constant(ast::ExprConstant {
value: Constant::Str(_), value: Constant::Str(_),
.. ..
@ -65,19 +65,23 @@ fn elts_to_csv(elts: &[Expr], generator: Generator) -> Option<String> {
} }
let node = Expr::Constant(ast::ExprConstant { let node = Expr::Constant(ast::ExprConstant {
value: Constant::Str(elts.iter().fold(String::new(), |mut acc, elt| { value: elts
if let Expr::Constant(ast::ExprConstant { .iter()
value: Constant::Str(ref s), .fold(String::new(), |mut acc, elt| {
.. if let Expr::Constant(ast::ExprConstant {
}) = elt value: Constant::Str(ast::StringConstant { value, .. }),
{ ..
if !acc.is_empty() { }) = elt
acc.push(','); {
if !acc.is_empty() {
acc.push(',');
}
acc.push_str(value.as_str());
} }
acc.push_str(s); acc
} })
acc .into(),
})),
kind: None, kind: None,
range: TextRange::default(), range: TextRange::default(),
}); });
@ -166,7 +170,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) {
.iter() .iter()
.map(|name| { .map(|name| {
Expr::Constant(ast::ExprConstant { Expr::Constant(ast::ExprConstant {
value: Constant::Str((*name).to_string()), value: (*name).to_string().into(),
kind: None, kind: None,
range: TextRange::default(), range: TextRange::default(),
}) })
@ -201,7 +205,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) {
.iter() .iter()
.map(|name| { .map(|name| {
Expr::Constant(ast::ExprConstant { Expr::Constant(ast::ExprConstant {
value: Constant::Str((*name).to_string()), value: (*name).to_string().into(),
kind: None, kind: None,
range: TextRange::default(), range: TextRange::default(),
}) })

View file

@ -115,7 +115,7 @@ pub(crate) fn use_capital_environment_variables(checker: &mut Checker, expr: &Ex
return; return;
}; };
let Expr::Constant(ast::ExprConstant { let Expr::Constant(ast::ExprConstant {
value: Constant::Str(env_var), value: Constant::Str(ast::StringConstant { value: env_var, .. }),
.. ..
}) = arg }) = arg
else { else {
@ -167,7 +167,7 @@ fn check_os_environ_subscript(checker: &mut Checker, expr: &Expr) {
return; return;
} }
let Expr::Constant(ast::ExprConstant { let Expr::Constant(ast::ExprConstant {
value: Constant::Str(env_var), value: Constant::Str(ast::StringConstant { value: env_var, .. }),
kind, kind,
range: _, range: _,
}) = slice.as_ref() }) = slice.as_ref()

View file

@ -264,15 +264,13 @@ fn is_main_check(expr: &Expr) -> bool {
{ {
if let Expr::Name(ast::ExprName { id, .. }) = left.as_ref() { if let Expr::Name(ast::ExprName { id, .. }) = left.as_ref() {
if id == "__name__" { if id == "__name__" {
if comparators.len() == 1 { if let [Expr::Constant(ast::ExprConstant {
if let Expr::Constant(ast::ExprConstant { value: Constant::Str(ast::StringConstant { value, .. }),
value: Constant::Str(value), ..
.. })] = comparators.as_slice()
}) = &comparators[0] {
{ if value == "__main__" {
if value == "__main__" { return true;
return true;
}
} }
} }
} }

View file

@ -1,4 +1,4 @@
use ruff_python_ast::{Arguments, Constant, Expr, ExprCall, ExprConstant}; use ruff_python_ast::{self as ast, Arguments, Constant, Expr, ExprCall, ExprConstant};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation}; use ruff_macros::{derive_message_formats, violation};
@ -67,7 +67,7 @@ pub(crate) fn path_constructor_current_directory(checker: &mut Checker, expr: &E
} }
let [Expr::Constant(ExprConstant { let [Expr::Constant(ExprConstant {
value: Constant::Str(value), value: Constant::Str(ast::StringConstant { value, .. }),
kind: _, kind: _,
range, range,
})] = args.as_slice() })] = args.as_slice()

View file

@ -16,7 +16,7 @@ fn to_formatted_value_expr(inner: &Expr) -> Expr {
/// Convert a string to a constant string expression. /// Convert a string to a constant string expression.
pub(super) fn to_constant_string(s: &str) -> Expr { pub(super) fn to_constant_string(s: &str) -> Expr {
let node = ast::ExprConstant { let node = ast::ExprConstant {
value: Constant::Str(s.to_owned()), value: s.to_owned().into(),
kind: None, kind: None,
range: TextRange::default(), range: TextRange::default(),
}; };

View file

@ -61,22 +61,21 @@ fn build_fstring(joiner: &str, joinees: &[Expr]) -> Option<Expr> {
) )
}) { }) {
let node = ast::ExprConstant { let node = ast::ExprConstant {
value: Constant::Str( value: joinees
joinees .iter()
.iter() .filter_map(|expr| {
.filter_map(|expr| { if let Expr::Constant(ast::ExprConstant {
if let Expr::Constant(ast::ExprConstant { value: Constant::Str(ast::StringConstant { value, .. }),
value: Constant::Str(string), ..
.. }) = expr
}) = expr {
{ Some(value.as_str())
Some(string.as_str()) } else {
} else { None
None }
} })
}) .join(joiner)
.join(joiner), .into(),
),
range: TextRange::default(), range: TextRange::default(),
kind: None, kind: None,
}; };
@ -100,6 +99,7 @@ fn build_fstring(joiner: &str, joinees: &[Expr]) -> Option<Expr> {
let node = ast::ExprFString { let node = ast::ExprFString {
values: fstring_elems, values: fstring_elems,
implicit_concatenated: false,
range: TextRange::default(), range: TextRange::default(),
}; };
Some(node.into()) Some(node.into())

View file

@ -51,14 +51,14 @@ pub(crate) fn use_of_read_table(checker: &mut Checker, call: &ast::ExprCall) {
.is_some_and(|call_path| matches!(call_path.as_slice(), ["pandas", "read_table"])) .is_some_and(|call_path| matches!(call_path.as_slice(), ["pandas", "read_table"]))
{ {
if let Some(Expr::Constant(ast::ExprConstant { if let Some(Expr::Constant(ast::ExprConstant {
value: Constant::Str(value), value: Constant::Str(ast::StringConstant { value, .. }),
.. ..
})) = call })) = call
.arguments .arguments
.find_keyword("sep") .find_keyword("sep")
.map(|keyword| &keyword.value) .map(|keyword| &keyword.value)
{ {
if value.as_str() == "," { if value == "," {
checker checker
.diagnostics .diagnostics
.push(Diagnostic::new(PandasUseOfDotReadTable, call.func.range())); .push(Diagnostic::new(PandasUseOfDotReadTable, call.func.range()));

View file

@ -583,10 +583,10 @@ pub(crate) fn percent_format_extra_named_arguments(
.enumerate() .enumerate()
.filter_map(|(index, key)| match key { .filter_map(|(index, key)| match key {
Some(Expr::Constant(ast::ExprConstant { Some(Expr::Constant(ast::ExprConstant {
value: Constant::Str(value), value: Constant::Str(ast::StringConstant { value, .. }),
.. ..
})) => { })) => {
if summary.keywords.contains(value) { if summary.keywords.contains(value.as_str()) {
None None
} else { } else {
Some((index, value.as_str())) Some((index, value.as_str()))
@ -646,7 +646,7 @@ pub(crate) fn percent_format_missing_arguments(
for key in keys.iter().flatten() { for key in keys.iter().flatten() {
match key { match key {
Expr::Constant(ast::ExprConstant { Expr::Constant(ast::ExprConstant {
value: Constant::Str(value), value: Constant::Str(ast::StringConstant { value, .. }),
.. ..
}) => { }) => {
keywords.insert(value); keywords.insert(value);

View file

@ -71,7 +71,7 @@ pub(crate) fn assert_on_string_literal(checker: &mut Checker, test: &Expr) {
} }
_ => {} _ => {}
}, },
Expr::FString(ast::ExprFString { values, range: _ }) => { Expr::FString(ast::ExprFString { values, .. }) => {
checker.diagnostics.push(Diagnostic::new( checker.diagnostics.push(Diagnostic::new(
AssertOnStringLiteral { AssertOnStringLiteral {
kind: if values.iter().all(|value| match value { kind: if values.iter().all(|value| match value {

View file

@ -187,7 +187,10 @@ fn is_valid_dict(
return true; return true;
}; };
if let Expr::Constant(ast::ExprConstant { if let Expr::Constant(ast::ExprConstant {
value: Constant::Str(mapping_key), value:
Constant::Str(ast::StringConstant {
value: mapping_key, ..
}),
.. ..
}) = key }) = key
{ {

View file

@ -109,7 +109,7 @@ pub(crate) fn logging_call(checker: &mut Checker, call: &ast::ExprCall) {
} }
let Some(Expr::Constant(ast::ExprConstant { let Some(Expr::Constant(ast::ExprConstant {
value: Constant::Str(value), value: Constant::Str(ast::StringConstant { value, .. }),
.. ..
})) = call.arguments.find_positional(0) })) = call.arguments.find_positional(0)
else { else {

View file

@ -79,7 +79,9 @@ fn is_magic_value(constant: &Constant, allowed_types: &[ConstantType]) -> bool {
Constant::Bool(_) => false, Constant::Bool(_) => false,
Constant::Ellipsis => false, Constant::Ellipsis => false,
// Otherwise, special-case some common string and integer types. // Otherwise, special-case some common string and integer types.
Constant::Str(value) => !matches!(value.as_str(), "" | "__main__"), Constant::Str(ast::StringConstant { value, .. }) => {
!matches!(value.as_str(), "" | "__main__")
}
Constant::Int(value) => !matches!(value.try_into(), Ok(0 | 1)), Constant::Int(value) => !matches!(value.try_into(), Ok(0 | 1)),
Constant::Bytes(_) => true, Constant::Bytes(_) => true,
Constant::Float(_) => true, Constant::Float(_) => true,

View file

@ -130,7 +130,10 @@ fn create_properties_from_fields_arg(fields: &Expr) -> Result<Vec<Stmt>> {
bail!("Expected `elts` to have exactly two elements") bail!("Expected `elts` to have exactly two elements")
}; };
let Expr::Constant(ast::ExprConstant { let Expr::Constant(ast::ExprConstant {
value: Constant::Str(property), value:
Constant::Str(ast::StringConstant {
value: property, ..
}),
.. ..
}) = &field_name }) = &field_name
else { else {

View file

@ -147,7 +147,10 @@ fn properties_from_dict_literal(keys: &[Option<Expr>], values: &[Expr]) -> Resul
.zip(values.iter()) .zip(values.iter())
.map(|(key, value)| match key { .map(|(key, value)| match key {
Some(Expr::Constant(ast::ExprConstant { Some(Expr::Constant(ast::ExprConstant {
value: Constant::Str(property), value:
Constant::Str(ast::StringConstant {
value: property, ..
}),
.. ..
})) => { })) => {
if !is_identifier(property) { if !is_identifier(property) {

View file

@ -38,8 +38,14 @@ impl FromStr for LiteralType {
impl From<LiteralType> for Constant { impl From<LiteralType> for Constant {
fn from(value: LiteralType) -> Self { fn from(value: LiteralType) -> Self {
match value { match value {
LiteralType::Str => Constant::Str(String::new()), LiteralType::Str => Constant::Str(ast::StringConstant {
LiteralType::Bytes => Constant::Bytes(vec![]), value: String::new(),
implicit_concatenated: false,
}),
LiteralType::Bytes => Constant::Bytes(ast::BytesConstant {
value: Vec::new(),
implicit_concatenated: false,
}),
LiteralType::Int => Constant::Int(BigInt::from(0)), LiteralType::Int => Constant::Int(BigInt::from(0)),
LiteralType::Float => Constant::Float(0.0), LiteralType::Float => Constant::Float(0.0),
LiteralType::Bool => Constant::Bool(false), LiteralType::Bool => Constant::Bool(false),

View file

@ -202,7 +202,10 @@ fn clean_params_dictionary(
match key { match key {
Some(key) => { Some(key) => {
if let Expr::Constant(ast::ExprConstant { if let Expr::Constant(ast::ExprConstant {
value: Constant::Str(key_string), value:
Constant::Str(ast::StringConstant {
value: key_string, ..
}),
.. ..
}) = key }) = key
{ {

View file

@ -73,11 +73,15 @@ pub(crate) fn redundant_open_modes(checker: &mut Checker, call: &ast::ExprCall)
if !call.arguments.is_empty() { if !call.arguments.is_empty() {
if let Some(keyword) = call.arguments.find_keyword(MODE_KEYWORD_ARGUMENT) { if let Some(keyword) = call.arguments.find_keyword(MODE_KEYWORD_ARGUMENT) {
if let Expr::Constant(ast::ExprConstant { if let Expr::Constant(ast::ExprConstant {
value: Constant::Str(mode_param_value), value:
Constant::Str(ast::StringConstant {
value: mode_param_value,
..
}),
.. ..
}) = &keyword.value }) = &keyword.value
{ {
if let Ok(mode) = OpenMode::from_str(mode_param_value.as_str()) { if let Ok(mode) = OpenMode::from_str(mode_param_value) {
checker.diagnostics.push(create_check( checker.diagnostics.push(create_check(
call, call,
&keyword.value, &keyword.value,
@ -97,7 +101,7 @@ pub(crate) fn redundant_open_modes(checker: &mut Checker, call: &ast::ExprCall)
.. ..
}) = &mode_param }) = &mode_param
{ {
if let Ok(mode) = OpenMode::from_str(value.as_str()) { if let Ok(mode) = OpenMode::from_str(value) {
checker.diagnostics.push(create_check( checker.diagnostics.push(create_check(
call, call,
mode_param, mode_param,

View file

@ -54,7 +54,7 @@ where
F: (Fn(&str) -> bool) + Copy, F: (Fn(&str) -> bool) + Copy,
{ {
match expr { match expr {
Expr::FString(ast::ExprFString { values, range: _ }) => { Expr::FString(ast::ExprFString { values, .. }) => {
for value in values { for value in values {
if any_string(value, predicate) { if any_string(value, predicate) {
return true; return true;
@ -62,10 +62,10 @@ where
} }
} }
Expr::Constant(ast::ExprConstant { Expr::Constant(ast::ExprConstant {
value: Constant::Str(val), value: Constant::Str(value),
.. ..
}) => { }) => {
if predicate(val.as_str()) { if predicate(value) {
return true; return true;
} }
} }

View file

@ -24,7 +24,7 @@ where
fn add_to_names<'a>(elts: &'a [Expr], names: &mut Vec<&'a str>, flags: &mut DunderAllFlags) { fn add_to_names<'a>(elts: &'a [Expr], names: &mut Vec<&'a str>, flags: &mut DunderAllFlags) {
for elt in elts { for elt in elts {
if let Expr::Constant(ast::ExprConstant { if let Expr::Constant(ast::ExprConstant {
value: Constant::Str(value), value: Constant::Str(ast::StringConstant { value, .. }),
.. ..
}) = elt }) = elt
{ {

View file

@ -326,8 +326,18 @@ impl<'a> From<&'a ast::Constant> for ComparableConstant<'a> {
match constant { match constant {
ast::Constant::None => Self::None, ast::Constant::None => Self::None,
ast::Constant::Bool(value) => Self::Bool(value), ast::Constant::Bool(value) => Self::Bool(value),
ast::Constant::Str(value) => Self::Str(value), ast::Constant::Str(ast::StringConstant {
ast::Constant::Bytes(value) => Self::Bytes(value), value,
// Compare strings based on resolved value, not representation (i.e., ignore whether
// the string was implicitly concatenated).
implicit_concatenated: _,
}) => Self::Str(value),
ast::Constant::Bytes(ast::BytesConstant {
value,
// Compare bytes based on resolved value, not representation (i.e., ignore whether
// the bytes were implicitly concatenated).
implicit_concatenated: _,
}) => Self::Bytes(value),
ast::Constant::Int(value) => Self::Int(value), ast::Constant::Int(value) => Self::Int(value),
ast::Constant::Float(value) => Self::Float(value.to_bits()), ast::Constant::Float(value) => Self::Float(value.to_bits()),
ast::Constant::Complex { real, imag } => Self::Complex { ast::Constant::Complex { real, imag } => Self::Complex {
@ -865,11 +875,13 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
debug_text: debug_text.as_ref(), debug_text: debug_text.as_ref(),
format_spec: format_spec.as_ref().map(Into::into), format_spec: format_spec.as_ref().map(Into::into),
}), }),
ast::Expr::FString(ast::ExprFString { values, range: _ }) => { ast::Expr::FString(ast::ExprFString {
Self::FString(ExprFString { values,
values: values.iter().map(Into::into).collect(), implicit_concatenated: _,
}) range: _,
} }) => Self::FString(ExprFString {
values: values.iter().map(Into::into).collect(),
}),
ast::Expr::Constant(ast::ExprConstant { ast::Expr::Constant(ast::ExprConstant {
value, value,
kind, kind,

View file

@ -123,10 +123,8 @@ where
return true; return true;
} }
match expr { match expr {
Expr::BoolOp(ast::ExprBoolOp { Expr::BoolOp(ast::ExprBoolOp { values, .. })
values, range: _, .. | Expr::FString(ast::ExprFString { values, .. }) => {
})
| Expr::FString(ast::ExprFString { values, range: _ }) => {
values.iter().any(|expr| any_over_expr(expr, func)) values.iter().any(|expr| any_over_expr(expr, func))
} }
Expr::NamedExpr(ast::ExprNamedExpr { Expr::NamedExpr(ast::ExprNamedExpr {
@ -1087,25 +1085,26 @@ impl Truthiness {
Expr::Constant(ast::ExprConstant { value, .. }) => match value { Expr::Constant(ast::ExprConstant { value, .. }) => match value {
Constant::Bool(value) => Some(*value), Constant::Bool(value) => Some(*value),
Constant::None => Some(false), Constant::None => Some(false),
Constant::Str(string) => Some(!string.is_empty()), Constant::Str(ast::StringConstant { value, .. }) => Some(!value.is_empty()),
Constant::Bytes(bytes) => Some(!bytes.is_empty()), Constant::Bytes(bytes) => Some(!bytes.is_empty()),
Constant::Int(int) => Some(!int.is_zero()), Constant::Int(int) => Some(!int.is_zero()),
Constant::Float(float) => Some(*float != 0.0), Constant::Float(float) => Some(*float != 0.0),
Constant::Complex { real, imag } => Some(*real != 0.0 || *imag != 0.0), Constant::Complex { real, imag } => Some(*real != 0.0 || *imag != 0.0),
Constant::Ellipsis => Some(true), Constant::Ellipsis => Some(true),
}, },
Expr::FString(ast::ExprFString { values, range: _ }) => { Expr::FString(ast::ExprFString { values, .. }) => {
if values.is_empty() { if values.is_empty() {
Some(false) Some(false)
} else if values.iter().any(|value| { } else if values.iter().any(|value| {
let Expr::Constant(ast::ExprConstant { if let Expr::Constant(ast::ExprConstant {
value: Constant::Str(string), value: Constant::Str(ast::StringConstant { value, .. }),
.. ..
}) = &value }) = &value
else { {
return false; !value.is_empty()
}; } else {
!string.is_empty() false
}
}) { }) {
Some(true) Some(true)
} else { } else {

View file

@ -2676,7 +2676,11 @@ impl AstNode for ast::ExprFString {
where where
V: PreorderVisitor<'a> + ?Sized, V: PreorderVisitor<'a> + ?Sized,
{ {
let ast::ExprFString { values, range: _ } = self; let ast::ExprFString {
values,
implicit_concatenated: _,
range: _,
} = self;
for expr in values { for expr in values {
visitor.visit_expr(expr); visitor.visit_expr(expr);

View file

@ -881,6 +881,8 @@ pub struct DebugText {
pub struct ExprFString { pub struct ExprFString {
pub range: TextRange, pub range: TextRange,
pub values: Vec<Expr>, pub values: Vec<Expr>,
/// Whether the f-string contains multiple string tokens that were implicitly concatenated.
pub implicit_concatenated: bool,
} }
impl From<ExprFString> for Expr { impl From<ExprFString> for Expr {
@ -2463,8 +2465,8 @@ impl std::cmp::PartialEq<usize> for Int {
pub enum Constant { pub enum Constant {
None, None,
Bool(bool), Bool(bool),
Str(String), Str(StringConstant),
Bytes(Vec<u8>), Bytes(BytesConstant),
Int(BigInt), Int(BigInt),
Float(f64), Float(f64),
Complex { real: f64, imag: f64 }, Complex { real: f64, imag: f64 },
@ -2472,38 +2474,68 @@ pub enum Constant {
} }
impl Constant { impl Constant {
pub fn is_true(self) -> bool { /// Returns `true` if the constant is a string or bytes constant that contains multiple,
self.bool().is_some_and(|b| b) /// implicitly concatenated string tokens.
} pub fn is_implicit_concatenated(&self) -> bool {
pub fn is_false(self) -> bool {
self.bool().is_some_and(|b| !b)
}
pub fn complex(self) -> Option<(f64, f64)> {
match self { match self {
Constant::Complex { real, imag } => Some((real, imag)), Constant::Str(value) => value.implicit_concatenated,
_ => None, Constant::Bytes(value) => value.implicit_concatenated,
_ => false,
} }
} }
} }
impl From<String> for Constant { #[derive(Clone, Debug, PartialEq, Eq)]
fn from(s: String) -> Constant { pub struct StringConstant {
Self::Str(s) /// The string value as resolved by the parser (i.e., without quotes, or escape sequences, or
/// implicit concatenations).
pub value: String,
/// Whether the string contains multiple string tokens that were implicitly concatenated.
pub implicit_concatenated: bool,
}
impl Deref for StringConstant {
type Target = str;
fn deref(&self) -> &Self::Target {
self.value.as_str()
} }
} }
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BytesConstant {
/// The bytes value as resolved by the parser (i.e., without quotes, or escape sequences, or
/// implicit concatenations).
pub value: Vec<u8>,
/// Whether the string contains multiple string tokens that were implicitly concatenated.
pub implicit_concatenated: bool,
}
impl Deref for BytesConstant {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.value.as_slice()
}
}
impl From<Vec<u8>> for Constant { impl From<Vec<u8>> for Constant {
fn from(b: Vec<u8>) -> Constant { fn from(value: Vec<u8>) -> Constant {
Self::Bytes(b) Self::Bytes(BytesConstant {
value,
implicit_concatenated: false,
})
}
}
impl From<String> for Constant {
fn from(value: String) -> Constant {
Self::Str(StringConstant {
value,
implicit_concatenated: false,
})
} }
} }
impl From<bool> for Constant { impl From<bool> for Constant {
fn from(b: bool) -> Constant { fn from(value: bool) -> Constant {
Self::Bool(b) Self::Bool(value)
}
}
impl From<BigInt> for Constant {
fn from(i: BigInt) -> Constant {
Self::Int(i)
} }
} }
@ -3056,7 +3088,7 @@ mod size_assertions {
assert_eq_size!(StmtClassDef, [u8; 104]); assert_eq_size!(StmtClassDef, [u8; 104]);
assert_eq_size!(StmtTry, [u8; 104]); assert_eq_size!(StmtTry, [u8; 104]);
assert_eq_size!(Expr, [u8; 80]); assert_eq_size!(Expr, [u8; 80]);
assert_eq_size!(Constant, [u8; 32]); assert_eq_size!(Constant, [u8; 40]);
assert_eq_size!(Pattern, [u8; 96]); assert_eq_size!(Pattern, [u8; 96]);
assert_eq_size!(Mod, [u8; 32]); assert_eq_size!(Mod, [u8; 32]);
} }

View file

@ -140,7 +140,7 @@ pub fn relocate_expr(expr: &mut Expr, location: TextRange) {
relocate_expr(expr, location); relocate_expr(expr, location);
} }
} }
Expr::FString(nodes::ExprFString { values, range }) => { Expr::FString(nodes::ExprFString { values, range, .. }) => {
*range = location; *range = location;
for expr in values { for expr in values {
relocate_expr(expr, location); relocate_expr(expr, location);

View file

@ -476,7 +476,7 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
visitor.visit_format_spec(expr); visitor.visit_format_spec(expr);
} }
} }
Expr::FString(ast::ExprFString { values, range: _ }) => { Expr::FString(ast::ExprFString { values, .. }) => {
for expr in values { for expr in values {
visitor.visit_expr(expr); visitor.visit_expr(expr);
} }

View file

@ -1104,7 +1104,7 @@ impl<'a> Generator<'a> {
*conversion, *conversion,
format_spec.as_deref(), format_spec.as_deref(),
), ),
Expr::FString(ast::ExprFString { values, range: _ }) => { Expr::FString(ast::ExprFString { values, .. }) => {
self.unparse_f_string(values, false); self.unparse_f_string(values, false);
} }
Expr::Constant(ast::ExprConstant { Expr::Constant(ast::ExprConstant {
@ -1197,8 +1197,8 @@ impl<'a> Generator<'a> {
Constant::Bytes(b) => { Constant::Bytes(b) => {
self.p_bytes_repr(b); self.p_bytes_repr(b);
} }
Constant::Str(s) => { Constant::Str(ast::StringConstant { value, .. }) => {
self.p_str_repr(s); self.p_str_repr(value);
} }
Constant::None => self.p("None"), Constant::None => self.p("None"),
Constant::Bool(b) => self.p(if *b { "True" } else { "False" }), Constant::Bool(b) => self.p(if *b { "True" } else { "False" }),
@ -1339,13 +1339,13 @@ impl<'a> Generator<'a> {
fn unparse_f_string_elem(&mut self, expr: &Expr, is_spec: bool) { fn unparse_f_string_elem(&mut self, expr: &Expr, is_spec: bool) {
match expr { match expr {
Expr::Constant(ast::ExprConstant { value, .. }) => { Expr::Constant(ast::ExprConstant { value, .. }) => {
if let Constant::Str(s) = value { if let Constant::Str(ast::StringConstant { value, .. }) = value {
self.unparse_f_string_literal(s); self.unparse_f_string_literal(value);
} else { } else {
unreachable!() unreachable!()
} }
} }
Expr::FString(ast::ExprFString { values, range: _ }) => { Expr::FString(ast::ExprFString { values, .. }) => {
self.unparse_f_string(values, is_spec); self.unparse_f_string(values, is_spec);
} }
Expr::FormattedValue(ast::ExprFormattedValue { Expr::FormattedValue(ast::ExprFormattedValue {

View file

@ -11,7 +11,10 @@ Dict(
ExprConstant { ExprConstant {
range: 1..4, range: 1..4,
value: Str( value: Str(
"a", StringConstant {
value: "a",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -23,7 +26,10 @@ Dict(
ExprConstant { ExprConstant {
range: 16..19, range: 16..19,
value: Str( value: Str(
"d", StringConstant {
value: "d",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -35,7 +41,10 @@ Dict(
ExprConstant { ExprConstant {
range: 6..9, range: 6..9,
value: Str( value: Str(
"b", StringConstant {
value: "b",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -51,7 +60,10 @@ Dict(
ExprConstant { ExprConstant {
range: 21..24, range: 21..24,
value: Str( value: Str(
"e", StringConstant {
value: "e",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -12,7 +12,10 @@ Call(
ExprConstant { ExprConstant {
range: 0..3, range: 0..3,
value: Str( value: Str(
" ", StringConstant {
value: " ",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -68,7 +71,10 @@ Call(
ExprConstant { ExprConstant {
range: 43..53, range: 43..53,
value: Str( value: Str(
"LIMIT %d", StringConstant {
value: "LIMIT %d",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -109,7 +115,10 @@ Call(
ExprConstant { ExprConstant {
range: 91..102, range: 91..102,
value: Str( value: Str(
"OFFSET %d", StringConstant {
value: "OFFSET %d",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -15,7 +15,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 8..14, range: 8..14,
value: Str( value: Str(
"test", StringConstant {
value: "test",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -100,7 +103,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 81..88, range: 81..88,
value: Str( value: Str(
"label", StringConstant {
value: "label",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -112,7 +118,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 90..96, range: 90..96,
value: Str( value: Str(
"test", StringConstant {
value: "test",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -131,7 +140,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 118..125, range: 118..125,
value: Str( value: Str(
"label", StringConstant {
value: "label",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -117,7 +117,10 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
ExprConstant { ExprConstant {
range: 80..89, range: 80..89,
value: Str( value: Str(
"default", StringConstant {
value: "default",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -14,12 +14,16 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 2..13, range: 2..13,
value: Str( value: Str(
"Hello world", StringConstant {
value: "Hello world",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
}, },

View file

@ -23,7 +23,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 8..20, range: 8..20,
value: Str( value: Str(
"positional", StringConstant {
value: "positional",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -23,7 +23,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 6..19, range: 6..19,
value: Str( value: Str(
"Hello world", StringConstant {
value: "Hello world",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -23,7 +23,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 6..19, range: 6..19,
value: Str( value: Str(
"Hello world", StringConstant {
value: "Hello world",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -10,7 +10,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..13, range: 0..13,
value: Str( value: Str(
"Hello world", StringConstant {
value: "Hello world",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -82,7 +82,10 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
ExprConstant { ExprConstant {
range: 48..61, range: 48..61,
value: Str( value: Str(
"ForwardRefY", StringConstant {
value: "ForwardRefY",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -522,7 +522,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 484..489, range: 484..489,
value: Str( value: Str(
"seq", StringConstant {
value: "seq",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -552,7 +555,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 518..523, range: 518..523,
value: Str( value: Str(
"map", StringConstant {
value: "map",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -857,7 +863,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 664..667, range: 664..667,
value: Str( value: Str(
"X", StringConstant {
value: "X",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -1617,7 +1626,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 1287..1292, range: 1287..1292,
value: Str( value: Str(
"foo", StringConstant {
value: "foo",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -2565,7 +2577,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 2036..2038, range: 2036..2038,
value: Str( value: Str(
"", StringConstant {
value: "",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -2611,7 +2626,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 2064..2066, range: 2064..2066,
value: Str( value: Str(
"", StringConstant {
value: "",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -3251,7 +3269,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 2449..2452, range: 2449..2452,
value: Str( value: Str(
"X", StringConstant {
value: "X",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -87,7 +87,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 64..71, range: 64..71,
value: Str( value: Str(
"caught ", StringConstant {
value: "caught ",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -126,6 +129,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
], ],
@ -181,7 +185,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 116..123, range: 116..123,
value: Str( value: Str(
"caught ", StringConstant {
value: "caught ",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -220,6 +227,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
], ],

View file

@ -28,7 +28,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 30..34, range: 30..34,
value: Str( value: Str(
"eg", StringConstant {
value: "eg",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -203,7 +206,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 135..142, range: 135..142,
value: Str( value: Str(
"caught ", StringConstant {
value: "caught ",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -245,7 +251,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 151..164, range: 151..164,
value: Str( value: Str(
" with nested ", StringConstant {
value: " with nested ",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -276,6 +285,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
], ],
@ -331,7 +341,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 215..222, range: 215..222,
value: Str( value: Str(
"caught ", StringConstant {
value: "caught ",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -373,7 +386,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 231..244, range: 231..244,
value: Str( value: Str(
" with nested ", StringConstant {
value: " with nested ",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -404,6 +420,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
], ],

View file

@ -10,7 +10,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..15, range: 0..15,
value: Str( value: Str(
"\u{8}", StringConstant {
value: "\u{8}",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -10,7 +10,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..9, range: 0..9,
value: Str( value: Str(
"\u{7}", StringConstant {
value: "\u{7}",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -10,7 +10,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..21, range: 0..21,
value: Str( value: Str(
"\r", StringConstant {
value: "\r",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -10,7 +10,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..45, range: 0..45,
value: Str( value: Str(
"\u{89}", StringConstant {
value: "\u{89}",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -10,7 +10,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..12, range: 0..12,
value: Str( value: Str(
"\u{7f}", StringConstant {
value: "\u{7f}",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -10,264 +10,267 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..738, range: 0..738,
value: Bytes( value: Bytes(
[ BytesConstant {
0, value: [
1, 0,
2, 1,
3, 2,
4, 3,
5, 4,
6, 5,
7, 6,
8, 7,
9, 8,
10, 9,
11, 10,
12, 11,
13, 12,
14, 13,
15, 14,
16, 15,
17, 16,
18, 17,
19, 18,
20, 19,
21, 20,
22, 21,
23, 22,
24, 23,
25, 24,
26, 25,
27, 26,
28, 27,
29, 28,
30, 29,
31, 30,
32, 31,
33, 32,
34, 33,
35, 34,
36, 35,
37, 36,
38, 37,
39, 38,
40, 39,
41, 40,
42, 41,
43, 42,
44, 43,
45, 44,
46, 45,
47, 46,
48, 47,
49, 48,
50, 49,
51, 50,
52, 51,
53, 52,
54, 53,
55, 54,
56, 55,
57, 56,
58, 57,
59, 58,
60, 59,
61, 60,
62, 61,
63, 62,
64, 63,
65, 64,
66, 65,
67, 66,
68, 67,
69, 68,
70, 69,
71, 70,
72, 71,
73, 72,
74, 73,
75, 74,
76, 75,
77, 76,
78, 77,
79, 78,
80, 79,
81, 80,
82, 81,
83, 82,
84, 83,
85, 84,
86, 85,
87, 86,
88, 87,
89, 88,
90, 89,
91, 90,
92, 91,
93, 92,
94, 93,
95, 94,
96, 95,
97, 96,
98, 97,
99, 98,
100, 99,
101, 100,
102, 101,
103, 102,
104, 103,
105, 104,
106, 105,
107, 106,
108, 107,
109, 108,
110, 109,
111, 110,
112, 111,
113, 112,
114, 113,
115, 114,
116, 115,
117, 116,
118, 117,
119, 118,
120, 119,
121, 120,
122, 121,
123, 122,
124, 123,
125, 124,
126, 125,
127, 126,
128, 127,
129, 128,
130, 129,
131, 130,
132, 131,
133, 132,
134, 133,
135, 134,
136, 135,
137, 136,
138, 137,
139, 138,
140, 139,
141, 140,
142, 141,
143, 142,
144, 143,
145, 144,
146, 145,
147, 146,
148, 147,
149, 148,
150, 149,
151, 150,
152, 151,
153, 152,
154, 153,
155, 154,
156, 155,
157, 156,
158, 157,
159, 158,
160, 159,
161, 160,
162, 161,
163, 162,
164, 163,
165, 164,
166, 165,
167, 166,
168, 167,
169, 168,
170, 169,
171, 170,
172, 171,
173, 172,
174, 173,
175, 174,
176, 175,
177, 176,
178, 177,
179, 178,
180, 179,
181, 180,
182, 181,
183, 182,
184, 183,
185, 184,
186, 185,
187, 186,
188, 187,
189, 188,
190, 189,
191, 190,
192, 191,
193, 192,
194, 193,
195, 194,
196, 195,
197, 196,
198, 197,
199, 198,
200, 199,
201, 200,
202, 201,
203, 202,
204, 203,
205, 204,
206, 205,
207, 206,
208, 207,
209, 208,
210, 209,
211, 210,
212, 211,
213, 212,
214, 213,
215, 214,
216, 215,
217, 216,
218, 217,
219, 218,
220, 219,
221, 220,
222, 221,
223, 222,
224, 223,
225, 224,
226, 225,
227, 226,
228, 227,
229, 228,
230, 229,
231, 230,
232, 231,
233, 232,
234, 233,
235, 234,
236, 235,
237, 236,
238, 237,
239, 238,
240, 239,
241, 240,
242, 241,
243, 242,
244, 243,
245, 244,
246, 245,
247, 246,
248, 247,
249, 248,
250, 249,
251, 250,
252, 251,
253, 252,
254, 253,
255, 254,
], 255,
],
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -10,7 +10,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..12, range: 0..12,
value: Str( value: Str(
"\u{1b}", StringConstant {
value: "\u{1b}",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -10,18 +10,21 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..13, range: 0..13,
value: Bytes( value: Bytes(
[ BytesConstant {
111, value: [
109, 111,
107, 109,
109, 107,
111, 109,
107, 111,
92, 107,
88, 92,
97, 88,
97, 97,
], 97,
],
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -10,13 +10,16 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..14, range: 0..14,
value: Bytes( value: Bytes(
[ BytesConstant {
35, value: [
97, 35,
4, 97,
83, 4,
52, 83,
], 52,
],
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -10,7 +10,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..15, range: 0..15,
value: Str( value: Str(
"\u{c}", StringConstant {
value: "\u{c}",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -14,7 +14,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 2..5, range: 2..5,
value: Str( value: Str(
"aaa", StringConstant {
value: "aaa",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -38,7 +41,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 10..13, range: 10..13,
value: Str( value: Str(
"ccc", StringConstant {
value: "ccc",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -62,12 +68,16 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 18..21, range: 18..21,
value: Str( value: Str(
"eee", StringConstant {
value: "eee",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
}, },

View file

@ -14,7 +14,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 2..4, range: 2..4,
value: Str( value: Str(
"\\", StringConstant {
value: "\\",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -35,6 +38,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
}, },

View file

@ -14,7 +14,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 2..4, range: 2..4,
value: Str( value: Str(
"\n", StringConstant {
value: "\n",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -35,6 +38,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
}, },

View file

@ -14,7 +14,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 3..5, range: 3..5,
value: Str( value: Str(
"\\\n", StringConstant {
value: "\\\n",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -35,6 +38,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
}, },

View file

@ -7,7 +7,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 2..6, range: 2..6,
value: Str( value: Str(
"mix ", StringConstant {
value: "mix ",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -36,7 +39,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 13..28, range: 13..28,
value: Str( value: Str(
" with text and ", StringConstant {
value: " with text and ",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -29,12 +29,16 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 9..12, range: 9..12,
value: Str( value: Str(
">10", StringConstant {
value: ">10",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
), ),

View file

@ -14,7 +14,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 4..5, range: 4..5,
value: Str( value: Str(
"\n", StringConstant {
value: "\n",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -35,6 +38,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
}, },

View file

@ -10,7 +10,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..9, range: 0..9,
value: Str( value: Str(
"\u{88}", StringConstant {
value: "\u{88}",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -14,12 +14,16 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 1..16, range: 1..16,
value: Str( value: Str(
"Hello world", StringConstant {
value: "Hello world",
implicit_concatenated: true,
},
), ),
kind: None, kind: None,
}, },
), ),
], ],
implicit_concatenated: true,
}, },
), ),
}, },

View file

@ -14,12 +14,16 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 1..16, range: 1..16,
value: Str( value: Str(
"Hello world", StringConstant {
value: "Hello world",
implicit_concatenated: true,
},
), ),
kind: None, kind: None,
}, },
), ),
], ],
implicit_concatenated: true,
}, },
), ),
}, },

View file

@ -14,7 +14,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 1..16, range: 1..16,
value: Str( value: Str(
"Hello world", StringConstant {
value: "Hello world",
implicit_concatenated: true,
},
), ),
kind: None, kind: None,
}, },
@ -26,7 +29,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 17..20, range: 17..20,
value: Str( value: Str(
"!", StringConstant {
value: "!",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
@ -37,6 +43,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: true,
}, },
), ),
}, },

View file

@ -0,0 +1,63 @@
---
source: crates/ruff_python_parser/src/string.rs
expression: parse_ast
---
[
Expr(
StmtExpr {
range: 0..31,
value: FString(
ExprFString {
range: 0..31,
values: [
Constant(
ExprConstant {
range: 1..16,
value: Str(
StringConstant {
value: "Hello world",
implicit_concatenated: true,
},
),
kind: None,
},
),
FormattedValue(
ExprFormattedValue {
range: 16..21,
value: Constant(
ExprConstant {
range: 17..20,
value: Str(
StringConstant {
value: "!",
implicit_concatenated: false,
},
),
kind: None,
},
),
debug_text: None,
conversion: None,
format_spec: None,
},
),
Constant(
ExprConstant {
range: 24..30,
value: Str(
StringConstant {
value: "again!",
implicit_concatenated: true,
},
),
kind: None,
},
),
],
implicit_concatenated: true,
},
),
},
),
]

View file

@ -37,7 +37,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 10..17, range: 10..17,
value: Str( value: Str(
"{foo}", StringConstant {
value: "{foo}",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -0,0 +1,50 @@
---
source: crates/ruff_python_parser/src/string.rs
expression: parse_ast
---
[
FormattedValue(
ExprFormattedValue {
range: 2..15,
value: Name(
ExprName {
range: 3..6,
id: "foo",
ctx: Load,
},
),
debug_text: None,
conversion: None,
format_spec: Some(
FString(
ExprFString {
range: 7..14,
values: [
FormattedValue(
ExprFormattedValue {
range: 7..14,
value: Constant(
ExprConstant {
range: 8..13,
value: Str(
StringConstant {
value: "",
implicit_concatenated: true,
},
),
kind: None,
},
),
debug_text: None,
conversion: None,
format_spec: None,
},
),
],
implicit_concatenated: false,
},
),
),
},
),
]

View file

@ -36,6 +36,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
), ),

View file

@ -0,0 +1,50 @@
---
source: crates/ruff_python_parser/src/string.rs
expression: parse_ast
---
[
FormattedValue(
ExprFormattedValue {
range: 2..12,
value: Name(
ExprName {
range: 3..6,
id: "foo",
ctx: Load,
},
),
debug_text: None,
conversion: None,
format_spec: Some(
FString(
ExprFString {
range: 7..11,
values: [
FormattedValue(
ExprFormattedValue {
range: 7..11,
value: Constant(
ExprConstant {
range: 8..10,
value: Str(
StringConstant {
value: "",
implicit_concatenated: false,
},
),
kind: None,
},
),
debug_text: None,
conversion: None,
format_spec: None,
},
),
],
implicit_concatenated: false,
},
),
),
},
),
]

View file

@ -24,12 +24,16 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 7..11, range: 7..11,
value: Str( value: Str(
"spec", StringConstant {
value: "spec",
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
), ),

View file

@ -10,7 +10,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..16, range: 0..16,
value: Str( value: Str(
"Hello world", StringConstant {
value: "Hello world",
implicit_concatenated: true,
},
), ),
kind: None, kind: None,
}, },

View file

@ -10,7 +10,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..20, range: 0..20,
value: Str( value: Str(
"Hello, world!", StringConstant {
value: "Hello, world!",
implicit_concatenated: false,
},
), ),
kind: Some( kind: Some(
"u", "u",

View file

@ -14,7 +14,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 2..17, range: 2..17,
value: Str( value: Str(
"Hello world", StringConstant {
value: "Hello world",
implicit_concatenated: true,
},
), ),
kind: Some( kind: Some(
"u", "u",
@ -22,6 +25,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: true,
}, },
), ),
}, },

View file

@ -14,7 +14,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 2..21, range: 2..21,
value: Str( value: Str(
"Hello world!", StringConstant {
value: "Hello world!",
implicit_concatenated: true,
},
), ),
kind: Some( kind: Some(
"u", "u",
@ -22,6 +25,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: true,
}, },
), ),
}, },

View file

@ -10,7 +10,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..17, range: 0..17,
value: Str( value: Str(
"Hello world", StringConstant {
value: "Hello world",
implicit_concatenated: true,
},
), ),
kind: None, kind: None,
}, },

View file

@ -10,7 +10,10 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..17, range: 0..17,
value: Str( value: Str(
"Hello world", StringConstant {
value: "Hello world",
implicit_concatenated: true,
},
), ),
kind: Some( kind: Some(
"u", "u",

View file

@ -10,12 +10,15 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..8, range: 0..8,
value: Bytes( value: Bytes(
[ BytesConstant {
92, value: [
120, 92,
49, 120,
122, 49,
], 122,
],
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -10,10 +10,13 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..6, range: 0..6,
value: Bytes( value: Bytes(
[ BytesConstant {
92, value: [
92, 92,
], 92,
],
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -26,6 +26,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
}, },

View file

@ -10,264 +10,267 @@ expression: parse_ast
ExprConstant { ExprConstant {
range: 0..738, range: 0..738,
value: Bytes( value: Bytes(
[ BytesConstant {
0, value: [
1, 0,
2, 1,
3, 2,
4, 3,
5, 4,
6, 5,
7, 6,
8, 7,
9, 8,
10, 9,
11, 10,
12, 11,
13, 12,
14, 13,
15, 14,
16, 15,
17, 16,
18, 17,
19, 18,
20, 19,
21, 20,
22, 21,
23, 22,
24, 23,
25, 24,
26, 25,
27, 26,
28, 27,
29, 28,
30, 29,
31, 30,
32, 31,
33, 32,
34, 33,
35, 34,
36, 35,
37, 36,
38, 37,
39, 38,
40, 39,
41, 40,
42, 41,
43, 42,
44, 43,
45, 44,
46, 45,
47, 46,
48, 47,
49, 48,
50, 49,
51, 50,
52, 51,
53, 52,
54, 53,
55, 54,
56, 55,
57, 56,
58, 57,
59, 58,
60, 59,
61, 60,
62, 61,
63, 62,
64, 63,
65, 64,
66, 65,
67, 66,
68, 67,
69, 68,
70, 69,
71, 70,
72, 71,
73, 72,
74, 73,
75, 74,
76, 75,
77, 76,
78, 77,
79, 78,
80, 79,
81, 80,
82, 81,
83, 82,
84, 83,
85, 84,
86, 85,
87, 86,
88, 87,
89, 88,
90, 89,
91, 90,
92, 91,
93, 92,
94, 93,
95, 94,
96, 95,
97, 96,
98, 97,
99, 98,
100, 99,
101, 100,
102, 101,
103, 102,
104, 103,
105, 104,
106, 105,
107, 106,
108, 107,
109, 108,
110, 109,
111, 110,
112, 111,
113, 112,
114, 113,
115, 114,
116, 115,
117, 116,
118, 117,
119, 118,
120, 119,
121, 120,
122, 121,
123, 122,
124, 123,
125, 124,
126, 125,
127, 126,
128, 127,
129, 128,
130, 129,
131, 130,
132, 131,
133, 132,
134, 133,
135, 134,
136, 135,
137, 136,
138, 137,
139, 138,
140, 139,
141, 140,
142, 141,
143, 142,
144, 143,
145, 144,
146, 145,
147, 146,
148, 147,
149, 148,
150, 149,
151, 150,
152, 151,
153, 152,
154, 153,
155, 154,
156, 155,
157, 156,
158, 157,
159, 158,
160, 159,
161, 160,
162, 161,
163, 162,
164, 163,
165, 164,
166, 165,
167, 166,
168, 167,
169, 168,
170, 169,
171, 170,
172, 171,
173, 172,
174, 173,
175, 174,
176, 175,
177, 176,
178, 177,
179, 178,
180, 179,
181, 180,
182, 181,
183, 182,
184, 183,
185, 184,
186, 185,
187, 186,
188, 187,
189, 188,
190, 189,
191, 190,
192, 191,
193, 192,
194, 193,
195, 194,
196, 195,
197, 196,
198, 197,
199, 198,
200, 199,
201, 200,
202, 201,
203, 202,
204, 203,
205, 204,
206, 205,
207, 206,
208, 207,
209, 208,
210, 209,
211, 210,
212, 211,
213, 212,
214, 213,
215, 214,
216, 215,
217, 216,
218, 217,
219, 218,
220, 219,
221, 220,
222, 221,
223, 222,
224, 223,
225, 224,
226, 225,
227, 226,
228, 227,
229, 228,
230, 229,
231, 230,
232, 231,
233, 232,
234, 233,
235, 234,
236, 235,
237, 236,
238, 237,
239, 238,
240, 239,
241, 240,
242, 241,
243, 242,
244, 243,
245, 244,
246, 245,
247, 246,
248, 247,
249, 248,
250, 249,
251, 250,
252, 251,
253, 252,
254, 253,
255, 254,
], 255,
],
implicit_concatenated: false,
},
), ),
kind: None, kind: None,
}, },

View file

@ -26,6 +26,7 @@ expression: parse_ast
}, },
), ),
], ],
implicit_concatenated: false,
}, },
), ),
}, },

View file

@ -1,6 +1,4 @@
use itertools::Itertools; use ruff_python_ast::{self as ast, BytesConstant, Constant, Expr, StringConstant};
use ruff_python_ast::{self as ast, Constant, Expr};
use ruff_python_ast::{ConversionFlag, Ranged}; use ruff_python_ast::{ConversionFlag, Ranged};
use ruff_text_size::{TextLen, TextRange, TextSize}; use ruff_text_size::{TextLen, TextRange, TextSize};
@ -245,6 +243,7 @@ impl<'a> StringParser<'a> {
spec = Some(Box::new(Expr::from(ast::ExprFString { spec = Some(Box::new(Expr::from(ast::ExprFString {
values: parsed_spec, values: parsed_spec,
implicit_concatenated: false,
range: self.range(start_location), range: self.range(start_location),
}))); })));
} }
@ -513,25 +512,25 @@ impl<'a> StringParser<'a> {
} }
Ok(Expr::from(ast::ExprConstant { Ok(Expr::from(ast::ExprConstant {
value: Constant::Bytes(content.chars().map(|c| c as u8).collect()), value: content.chars().map(|c| c as u8).collect::<Vec<u8>>().into(),
kind: None, kind: None,
range: self.range(start_location), range: self.range(start_location),
})) }))
} }
fn parse_string(&mut self) -> Result<Expr, LexicalError> { fn parse_string(&mut self) -> Result<Expr, LexicalError> {
let mut content = String::new(); let mut value = String::new();
let start_location = self.get_pos(); let start_location = self.get_pos();
while let Some(ch) = self.next_char() { while let Some(ch) = self.next_char() {
match ch { match ch {
'\\' if !self.kind.is_raw() => { '\\' if !self.kind.is_raw() => {
content.push_str(&self.parse_escaped_char()?); value.push_str(&self.parse_escaped_char()?);
} }
ch => content.push(ch), ch => value.push(ch),
} }
} }
Ok(Expr::from(ast::ExprConstant { Ok(Expr::from(ast::ExprConstant {
value: Constant::Str(content), value: value.into(),
kind: self.kind.is_unicode().then(|| "u".to_string()), kind: self.kind.is_unicode().then(|| "u".to_string()),
range: self.range(start_location), range: self.range(start_location),
})) }))
@ -577,6 +576,7 @@ pub(crate) fn parse_strings(
.filter(|(_, (_, kind, ..), _)| kind.is_any_bytes()) .filter(|(_, (_, kind, ..), _)| kind.is_any_bytes())
.count(); .count();
let has_bytes = num_bytes > 0; let has_bytes = num_bytes > 0;
let implicit_concatenated = values.len() > 1;
if has_bytes && num_bytes < values.len() { if has_bytes && num_bytes < values.len() {
return Err(LexicalError { return Err(LexicalError {
@ -593,7 +593,7 @@ pub(crate) fn parse_strings(
for value in parse_string(&source, kind, triple_quoted, start)? { for value in parse_string(&source, kind, triple_quoted, start)? {
match value { match value {
Expr::Constant(ast::ExprConstant { Expr::Constant(ast::ExprConstant {
value: Constant::Bytes(value), value: Constant::Bytes(BytesConstant { value, .. }),
.. ..
}) => content.extend(value), }) => content.extend(value),
_ => unreachable!("Unexpected non-bytes expression."), _ => unreachable!("Unexpected non-bytes expression."),
@ -601,7 +601,10 @@ pub(crate) fn parse_strings(
} }
} }
return Ok(ast::ExprConstant { return Ok(ast::ExprConstant {
value: Constant::Bytes(content), value: Constant::Bytes(BytesConstant {
value: content,
implicit_concatenated,
}),
kind: None, kind: None,
range: TextRange::new(initial_start, last_end), range: TextRange::new(initial_start, last_end),
} }
@ -614,7 +617,7 @@ pub(crate) fn parse_strings(
for value in parse_string(&source, kind, triple_quoted, start)? { for value in parse_string(&source, kind, triple_quoted, start)? {
match value { match value {
Expr::Constant(ast::ExprConstant { Expr::Constant(ast::ExprConstant {
value: Constant::Str(value), value: Constant::Str(StringConstant { value, .. }),
.. ..
}) => content.push(value), }) => content.push(value),
_ => unreachable!("Unexpected non-string expression."), _ => unreachable!("Unexpected non-string expression."),
@ -622,7 +625,10 @@ pub(crate) fn parse_strings(
} }
} }
return Ok(ast::ExprConstant { return Ok(ast::ExprConstant {
value: Constant::Str(content.join("")), value: Constant::Str(StringConstant {
value: content.join(""),
implicit_concatenated,
}),
kind: initial_kind, kind: initial_kind,
range: TextRange::new(initial_start, last_end), range: TextRange::new(initial_start, last_end),
} }
@ -637,7 +643,10 @@ pub(crate) fn parse_strings(
let take_current = |current: &mut Vec<String>, start, end| -> Expr { let take_current = |current: &mut Vec<String>, start, end| -> Expr {
Expr::Constant(ast::ExprConstant { Expr::Constant(ast::ExprConstant {
value: Constant::Str(current.drain(..).join("")), value: Constant::Str(StringConstant {
value: current.drain(..).collect::<String>(),
implicit_concatenated,
}),
kind: initial_kind.clone(), kind: initial_kind.clone(),
range: TextRange::new(start, end), range: TextRange::new(start, end),
}) })
@ -654,14 +663,14 @@ pub(crate) fn parse_strings(
deduped.push(value); deduped.push(value);
} }
Expr::Constant(ast::ExprConstant { Expr::Constant(ast::ExprConstant {
value: Constant::Str(inner), value: Constant::Str(StringConstant { value, .. }),
.. ..
}) => { }) => {
if current.is_empty() { if current.is_empty() {
current_start = value_range.start(); current_start = value_range.start();
} }
current_end = value_range.end(); current_end = value_range.end();
current.push(inner); current.push(value);
} }
_ => unreachable!("Unexpected non-string expression."), _ => unreachable!("Unexpected non-string expression."),
} }
@ -673,6 +682,7 @@ pub(crate) fn parse_strings(
Ok(Expr::FString(ast::ExprFString { Ok(Expr::FString(ast::ExprFString {
values: deduped, values: deduped,
implicit_concatenated,
range: TextRange::new(initial_start, last_end), range: TextRange::new(initial_start, last_end),
})) }))
} }
@ -963,6 +973,13 @@ mod tests {
insta::assert_debug_snapshot!(parse_ast); insta::assert_debug_snapshot!(parse_ast);
} }
#[test]
fn test_parse_f_string_concat_4() {
let source = "'Hello ' f'world{\"!\"}' 'again!'";
let parse_ast = parse_suite(source, "<test>").unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test] #[test]
fn test_parse_u_f_string_concat_1() { fn test_parse_u_f_string_concat_1() {
let source = "u'Hello ' f'world'"; let source = "u'Hello ' f'world'";
@ -1080,6 +1097,22 @@ mod tests {
insta::assert_debug_snapshot!(parse_ast); insta::assert_debug_snapshot!(parse_ast);
} }
#[test]
fn test_parse_fstring_nested_string_spec() {
let source = "{foo:{''}}";
let parse_ast = parse_fstring(source).unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_parse_fstring_nested_concatenation_string_spec() {
let source = "{foo:{'' ''}}";
let parse_ast = parse_fstring(source).unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
macro_rules! test_aliases_parse { macro_rules! test_aliases_parse {
($($name:ident: $alias:expr,)*) => { ($($name:ident: $alias:expr,)*) => {
$( $(