mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Implement template strings (#17851)
This PR implements template strings (t-strings) in the parser and formatter for Ruff. Minimal changes necessary to compile were made in other parts of the code (e.g. ty, the linter, etc.). These will be covered properly in follow-up PRs.
This commit is contained in:
parent
ad024f9a09
commit
9bbf4987e8
261 changed files with 18023 additions and 1802 deletions
|
@ -325,6 +325,7 @@ impl<'db> Visitor<'db> for ExportFinder<'db> {
|
|||
| ast::Expr::Yield(_)
|
||||
| ast::Expr::YieldFrom(_)
|
||||
| ast::Expr::FString(_)
|
||||
| ast::Expr::TString(_)
|
||||
| ast::Expr::Tuple(_)
|
||||
| ast::Expr::List(_)
|
||||
| ast::Expr::Slice(_)
|
||||
|
@ -389,6 +390,7 @@ impl<'db> Visitor<'db> for WalrusFinder<'_, 'db> {
|
|||
| ast::Expr::Yield(_)
|
||||
| ast::Expr::YieldFrom(_)
|
||||
| ast::Expr::FString(_)
|
||||
| ast::Expr::TString(_)
|
||||
| ast::Expr::Tuple(_)
|
||||
| ast::Expr::List(_)
|
||||
| ast::Expr::Slice(_)
|
||||
|
|
|
@ -116,6 +116,7 @@ impl_expression_has_type!(ast::ExprYieldFrom);
|
|||
impl_expression_has_type!(ast::ExprCompare);
|
||||
impl_expression_has_type!(ast::ExprCall);
|
||||
impl_expression_has_type!(ast::ExprFString);
|
||||
impl_expression_has_type!(ast::ExprTString);
|
||||
impl_expression_has_type!(ast::ExprStringLiteral);
|
||||
impl_expression_has_type!(ast::ExprBytesLiteral);
|
||||
impl_expression_has_type!(ast::ExprNumberLiteral);
|
||||
|
@ -152,6 +153,7 @@ impl HasType for ast::Expr {
|
|||
Expr::Compare(inner) => inner.inferred_type(model),
|
||||
Expr::Call(inner) => inner.inferred_type(model),
|
||||
Expr::FString(inner) => inner.inferred_type(model),
|
||||
Expr::TString(inner) => inner.inferred_type(model),
|
||||
Expr::StringLiteral(inner) => inner.inferred_type(model),
|
||||
Expr::BytesLiteral(inner) => inner.inferred_type(model),
|
||||
Expr::NumberLiteral(inner) => inner.inferred_type(model),
|
||||
|
|
|
@ -4328,6 +4328,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
self.infer_bytes_literal_expression(bytes_literal)
|
||||
}
|
||||
ast::Expr::FString(fstring) => self.infer_fstring_expression(fstring),
|
||||
ast::Expr::TString(tstring) => self.infer_tstring_expression(tstring),
|
||||
ast::Expr::EllipsisLiteral(literal) => self.infer_ellipsis_literal_expression(literal),
|
||||
ast::Expr::Tuple(tuple) => self.infer_tuple_expression(tuple),
|
||||
ast::Expr::List(list) => self.infer_list_expression(list),
|
||||
|
@ -4426,8 +4427,8 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
ast::FStringPart::FString(fstring) => {
|
||||
for element in &fstring.elements {
|
||||
match element {
|
||||
ast::FStringElement::Expression(expression) => {
|
||||
let ast::FStringExpressionElement {
|
||||
ast::InterpolatedStringElement::Interpolation(expression) => {
|
||||
let ast::InterpolatedElement {
|
||||
range: _,
|
||||
expression,
|
||||
debug_text: _,
|
||||
|
@ -4437,7 +4438,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
let ty = self.infer_expression(expression);
|
||||
|
||||
if let Some(format_spec) = format_spec {
|
||||
for element in format_spec.elements.expressions() {
|
||||
for element in format_spec.elements.interpolations() {
|
||||
self.infer_expression(&element.expression);
|
||||
}
|
||||
}
|
||||
|
@ -4456,7 +4457,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
}
|
||||
}
|
||||
}
|
||||
ast::FStringElement::Literal(literal) => {
|
||||
ast::InterpolatedStringElement::Literal(literal) => {
|
||||
collector.push_str(&literal.value);
|
||||
}
|
||||
}
|
||||
|
@ -4467,6 +4468,59 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
collector.string_type(self.db())
|
||||
}
|
||||
|
||||
fn infer_tstring_expression(&mut self, tstring: &ast::ExprTString) -> Type<'db> {
|
||||
let ast::ExprTString { value, .. } = tstring;
|
||||
for part in value {
|
||||
match part {
|
||||
ast::TStringPart::Literal(_) => {}
|
||||
ast::TStringPart::FString(fstring) => {
|
||||
for element in &fstring.elements {
|
||||
match element {
|
||||
ast::InterpolatedStringElement::Interpolation(expression) => {
|
||||
let ast::InterpolatedElement {
|
||||
expression,
|
||||
format_spec,
|
||||
..
|
||||
} = expression;
|
||||
self.infer_expression(expression);
|
||||
|
||||
if let Some(format_spec) = format_spec {
|
||||
for element in format_spec.elements.interpolations() {
|
||||
self.infer_expression(&element.expression);
|
||||
}
|
||||
}
|
||||
}
|
||||
ast::InterpolatedStringElement::Literal(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
ast::TStringPart::TString(tstring) => {
|
||||
for element in &tstring.elements {
|
||||
match element {
|
||||
ast::InterpolatedStringElement::Interpolation(
|
||||
tstring_interpolation_element,
|
||||
) => {
|
||||
let ast::InterpolatedElement {
|
||||
expression,
|
||||
format_spec,
|
||||
..
|
||||
} = tstring_interpolation_element;
|
||||
self.infer_expression(expression);
|
||||
if let Some(format_spec) = format_spec {
|
||||
for element in format_spec.elements.interpolations() {
|
||||
self.infer_expression(&element.expression);
|
||||
}
|
||||
}
|
||||
}
|
||||
ast::InterpolatedStringElement::Literal(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
todo_type!("Template")
|
||||
}
|
||||
|
||||
fn infer_ellipsis_literal_expression(
|
||||
&mut self,
|
||||
_literal: &ast::ExprEllipsisLiteral,
|
||||
|
@ -8285,6 +8339,14 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
)
|
||||
}
|
||||
|
||||
ast::Expr::TString(tstring) => {
|
||||
self.infer_tstring_expression(tstring);
|
||||
self.report_invalid_type_expression(
|
||||
expression,
|
||||
format_args!("T-strings are not allowed in type expressions"),
|
||||
)
|
||||
}
|
||||
|
||||
ast::Expr::Slice(slice) => {
|
||||
self.infer_slice_expression(slice);
|
||||
self.report_invalid_type_expression(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue