@@ -24,16 +24,16 @@ use ruff_python_ast::{
2424 Alias , Arguments , BoolOp , CmpOp , Comprehension , ConversionFlag , DebugText , Decorator , DictItem ,
2525 ExceptHandler , ExceptHandlerExceptHandler , Expr , ExprAttribute , ExprBoolOp , ExprContext ,
2626 ExprFString , ExprList , ExprName , ExprSlice , ExprStarred , ExprSubscript , ExprTuple , ExprUnaryOp ,
27- FString , FStringElement , FStringElements , FStringFlags , FStringPart , Identifier , Int , Keyword ,
28- MatchCase , ModExpression , ModModule , Operator , Parameters , Pattern , PatternMatchAs ,
29- PatternMatchClass , PatternMatchMapping , PatternMatchOr , PatternMatchSequence ,
30- PatternMatchSingleton , PatternMatchStar , PatternMatchValue , Singleton , Stmt , StmtExpr ,
31- TypeParam , TypeParamParamSpec , TypeParamTypeVar , TypeParamTypeVarTuple , TypeParams , UnaryOp ,
32- WithItem ,
27+ FString , FStringFlags , FStringPart , Identifier , Int , InterpolatedElement ,
28+ InterpolatedStringElement , InterpolatedStringElements , Keyword , MatchCase , ModExpression ,
29+ ModModule , Operator , Parameters , Pattern , PatternMatchAs , PatternMatchClass ,
30+ PatternMatchMapping , PatternMatchOr , PatternMatchSequence , PatternMatchSingleton ,
31+ PatternMatchStar , PatternMatchValue , Singleton , Stmt , StmtExpr , TypeParam , TypeParamParamSpec ,
32+ TypeParamTypeVar , TypeParamTypeVarTuple , TypeParams , UnaryOp , WithItem ,
3333} ;
3434use ruff_text_size:: { Ranged , TextRange } ;
3535use rustpython_compiler_core:: {
36- Mode , OneIndexed , SourceFile , SourceLocation ,
36+ Mode , OneIndexed , PositionEncoding , SourceFile , SourceLocation ,
3737 bytecode:: {
3838 self , Arg as OpArgMarker , BinaryOperator , CodeObject , ComparisonOperator , ConstantData ,
3939 Instruction , OpArg , OpArgType , UnpackExArgs ,
@@ -240,18 +240,18 @@ fn eprint_location(zelf: &Compiler) {
240240 let start = zelf
241241 . source_file
242242 . to_source_code ( )
243- . source_location ( zelf. current_source_range . start ( ) ) ;
243+ . source_location ( zelf. current_source_range . start ( ) , PositionEncoding :: Utf8 ) ;
244244 let end = zelf
245245 . source_file
246246 . to_source_code ( )
247- . source_location ( zelf. current_source_range . end ( ) ) ;
247+ . source_location ( zelf. current_source_range . end ( ) , PositionEncoding :: Utf8 ) ;
248248 eprintln ! (
249249 "LOCATION: {} from {}:{} to {}:{}" ,
250250 zelf. source_file. name( ) ,
251- start. row ,
252- start. column ,
253- end. row ,
254- end. column
251+ start. line ,
252+ start. character_offset ,
253+ end. line ,
254+ end. character_offset
255255 ) ;
256256}
257257
@@ -531,7 +531,7 @@ impl Compiler {
531531 let location = self
532532 . source_file
533533 . to_source_code ( )
534- . source_location ( range. start ( ) ) ;
534+ . source_location ( range. start ( ) , PositionEncoding :: Utf8 ) ;
535535 CodegenError {
536536 error,
537537 location : Some ( location) ,
@@ -631,8 +631,8 @@ impl Compiler {
631631 ) -> CompileResult < ( ) > {
632632 // Create location
633633 let location = SourceLocation {
634- row : OneIndexed :: new ( lineno as usize ) . unwrap_or ( OneIndexed :: MIN ) ,
635- column : OneIndexed :: new ( 1 ) . unwrap ( ) ,
634+ line : OneIndexed :: new ( lineno as usize ) . unwrap_or ( OneIndexed :: MIN ) ,
635+ character_offset : OneIndexed :: MIN ,
636636 } ;
637637
638638 // Allocate a new compiler unit
@@ -769,8 +769,8 @@ impl Compiler {
769769 let _resume_loc = if scope_type == CompilerScope :: Module {
770770 // Module scope starts with lineno 0
771771 SourceLocation {
772- row : OneIndexed :: MIN ,
773- column : OneIndexed :: MIN ,
772+ line : OneIndexed :: MIN ,
773+ character_offset : OneIndexed :: MIN ,
774774 }
775775 } else {
776776 location
@@ -4872,6 +4872,7 @@ impl Compiler {
48724872 Expr :: Named ( ExprNamed {
48734873 target,
48744874 value,
4875+ node_index : _,
48754876 range : _,
48764877 } ) => {
48774878 self . compile_expression ( value) ?;
@@ -4881,6 +4882,9 @@ impl Compiler {
48814882 Expr :: FString ( fstring) => {
48824883 self . compile_expr_fstring ( fstring) ?;
48834884 }
4885+ Expr :: TString ( _) => {
4886+ return Err ( self . error ( CodegenErrorType :: NotImplementedYet ) ) ;
4887+ }
48844888 Expr :: StringLiteral ( string) => {
48854889 let value = string. value . to_str ( ) ;
48864890 if value. contains ( char:: REPLACEMENT_CHARACTER ) {
@@ -5334,7 +5338,7 @@ impl Compiler {
53345338 let location = self
53355339 . source_file
53365340 . to_source_code ( )
5337- . source_location ( range. start ( ) ) ;
5341+ . source_location ( range. start ( ) , PositionEncoding :: Utf8 ) ;
53385342 // TODO: insert source filename
53395343 self . current_block ( ) . instructions . push ( ir:: InstructionInfo {
53405344 instr,
@@ -5524,27 +5528,14 @@ impl Compiler {
55245528 Expr :: Named ( ExprNamed {
55255529 target,
55265530 value,
5531+ node_index : _,
55275532 range : _,
55285533 } ) => Self :: contains_await ( target) || Self :: contains_await ( value) ,
5529- Expr :: FString ( ExprFString { value, range : _ } ) => {
5530- fn expr_element_contains_await < F : Copy + Fn ( & Expr ) -> bool > (
5531- expr_element : & FStringExpressionElement ,
5532- contains_await : F ,
5533- ) -> bool {
5534- contains_await ( & expr_element. expression )
5535- || expr_element
5536- . format_spec
5537- . iter ( )
5538- . flat_map ( |spec| spec. elements . expressions ( ) )
5539- . any ( |element| expr_element_contains_await ( element, contains_await) )
5540- }
5541-
5542- value. elements ( ) . any ( |element| match element {
5543- FStringElement :: Expression ( expr_element) => {
5544- expr_element_contains_await ( expr_element, Self :: contains_await)
5545- }
5546- FStringElement :: Literal ( _) => false ,
5547- } )
5534+ Expr :: FString ( fstring) => {
5535+ Self :: interpolated_string_contains_await ( fstring. value . elements ( ) )
5536+ }
5537+ Expr :: TString ( tstring) => {
5538+ Self :: interpolated_string_contains_await ( tstring. value . elements ( ) )
55485539 }
55495540 Expr :: StringLiteral ( _)
55505541 | Expr :: BytesLiteral ( _)
@@ -5556,6 +5547,29 @@ impl Compiler {
55565547 }
55575548 }
55585549
5550+ fn interpolated_string_contains_await < ' a > (
5551+ mut elements : impl Iterator < Item = & ' a InterpolatedStringElement > ,
5552+ ) -> bool {
5553+ fn interpolated_element_contains_await < F : Copy + Fn ( & Expr ) -> bool > (
5554+ expr_element : & InterpolatedElement ,
5555+ contains_await : F ,
5556+ ) -> bool {
5557+ contains_await ( & expr_element. expression )
5558+ || expr_element
5559+ . format_spec
5560+ . iter ( )
5561+ . flat_map ( |spec| spec. elements . interpolations ( ) )
5562+ . any ( |element| interpolated_element_contains_await ( element, contains_await) )
5563+ }
5564+
5565+ elements. any ( |element| match element {
5566+ InterpolatedStringElement :: Interpolation ( expr_element) => {
5567+ interpolated_element_contains_await ( expr_element, Self :: contains_await)
5568+ }
5569+ InterpolatedStringElement :: Literal ( _) => false ,
5570+ } )
5571+ }
5572+
55595573 fn compile_expr_fstring ( & mut self , fstring : & ExprFString ) -> CompileResult < ( ) > {
55605574 let fstring = & fstring. value ;
55615575 for part in fstring {
@@ -5602,13 +5616,13 @@ impl Compiler {
56025616 fn compile_fstring_elements (
56035617 & mut self ,
56045618 flags : FStringFlags ,
5605- fstring_elements : & FStringElements ,
5619+ fstring_elements : & InterpolatedStringElements ,
56065620 ) -> CompileResult < ( ) > {
56075621 let mut element_count = 0 ;
56085622 for element in fstring_elements {
56095623 element_count += 1 ;
56105624 match element {
5611- FStringElement :: Literal ( string) => {
5625+ InterpolatedStringElement :: Literal ( string) => {
56125626 if string. value . contains ( char:: REPLACEMENT_CHARACTER ) {
56135627 // might have a surrogate literal; should reparse to be sure
56145628 let source = self . source_file . slice ( string. range ) ;
@@ -5625,7 +5639,7 @@ impl Compiler {
56255639 } ) ;
56265640 }
56275641 }
5628- FStringElement :: Expression ( fstring_expr) => {
5642+ InterpolatedStringElement :: Interpolation ( fstring_expr) => {
56295643 let mut conversion = fstring_expr. conversion ;
56305644
56315645 if let Some ( DebugText { leading, trailing } ) = & fstring_expr. debug_text {
@@ -5858,21 +5872,27 @@ mod ruff_tests {
58585872
58595873 // f'{x}'
58605874 let expr_x = Expr :: Name ( ExprName {
5875+ node_index : AtomicNodeIndex :: NONE ,
58615876 range,
58625877 id : Name :: new ( "x" ) ,
58635878 ctx : ExprContext :: Load ,
58645879 } ) ;
58655880 let not_present = & Expr :: FString ( ExprFString {
5881+ node_index : AtomicNodeIndex :: NONE ,
58665882 range,
58675883 value : FStringValue :: single ( FString {
5884+ node_index : AtomicNodeIndex :: NONE ,
58685885 range,
5869- elements : vec ! [ FStringElement :: Expression ( FStringExpressionElement {
5870- range,
5871- expression: Box :: new( expr_x) ,
5872- debug_text: None ,
5873- conversion: ConversionFlag :: None ,
5874- format_spec: None ,
5875- } ) ]
5886+ elements : vec ! [ InterpolatedStringElement :: Interpolation (
5887+ InterpolatedElement {
5888+ node_index: AtomicNodeIndex :: NONE ,
5889+ range,
5890+ expression: Box :: new( expr_x) ,
5891+ debug_text: None ,
5892+ conversion: ConversionFlag :: None ,
5893+ format_spec: None ,
5894+ } ,
5895+ ) ]
58765896 . into ( ) ,
58775897 flags,
58785898 } ) ,
@@ -5881,24 +5901,31 @@ mod ruff_tests {
58815901
58825902 // f'{await x}'
58835903 let expr_await_x = Expr :: Await ( ExprAwait {
5904+ node_index : AtomicNodeIndex :: NONE ,
58845905 range,
58855906 value : Box :: new ( Expr :: Name ( ExprName {
5907+ node_index : AtomicNodeIndex :: NONE ,
58865908 range,
58875909 id : Name :: new ( "x" ) ,
58885910 ctx : ExprContext :: Load ,
58895911 } ) ) ,
58905912 } ) ;
58915913 let present = & Expr :: FString ( ExprFString {
5914+ node_index : AtomicNodeIndex :: NONE ,
58925915 range,
58935916 value : FStringValue :: single ( FString {
5917+ node_index : AtomicNodeIndex :: NONE ,
58945918 range,
5895- elements : vec ! [ FStringElement :: Expression ( FStringExpressionElement {
5896- range,
5897- expression: Box :: new( expr_await_x) ,
5898- debug_text: None ,
5899- conversion: ConversionFlag :: None ,
5900- format_spec: None ,
5901- } ) ]
5919+ elements : vec ! [ InterpolatedStringElement :: Interpolation (
5920+ InterpolatedElement {
5921+ node_index: AtomicNodeIndex :: NONE ,
5922+ range,
5923+ expression: Box :: new( expr_await_x) ,
5924+ debug_text: None ,
5925+ conversion: ConversionFlag :: None ,
5926+ format_spec: None ,
5927+ } ,
5928+ ) ]
59025929 . into ( ) ,
59035930 flags,
59045931 } ) ,
@@ -5907,39 +5934,51 @@ mod ruff_tests {
59075934
59085935 // f'{x:{await y}}'
59095936 let expr_x = Expr :: Name ( ExprName {
5937+ node_index : AtomicNodeIndex :: NONE ,
59105938 range,
59115939 id : Name :: new ( "x" ) ,
59125940 ctx : ExprContext :: Load ,
59135941 } ) ;
59145942 let expr_await_y = Expr :: Await ( ExprAwait {
5943+ node_index : AtomicNodeIndex :: NONE ,
59155944 range,
59165945 value : Box :: new ( Expr :: Name ( ExprName {
5946+ node_index : AtomicNodeIndex :: NONE ,
59175947 range,
59185948 id : Name :: new ( "y" ) ,
59195949 ctx : ExprContext :: Load ,
59205950 } ) ) ,
59215951 } ) ;
59225952 let present = & Expr :: FString ( ExprFString {
5953+ node_index : AtomicNodeIndex :: NONE ,
59235954 range,
59245955 value : FStringValue :: single ( FString {
5956+ node_index : AtomicNodeIndex :: NONE ,
59255957 range,
5926- elements : vec ! [ FStringElement :: Expression ( FStringExpressionElement {
5927- range,
5928- expression: Box :: new( expr_x) ,
5929- debug_text: None ,
5930- conversion: ConversionFlag :: None ,
5931- format_spec: Some ( Box :: new( FStringFormatSpec {
5958+ elements : vec ! [ InterpolatedStringElement :: Interpolation (
5959+ InterpolatedElement {
5960+ node_index: AtomicNodeIndex :: NONE ,
59325961 range,
5933- elements: vec![ FStringElement :: Expression ( FStringExpressionElement {
5962+ expression: Box :: new( expr_x) ,
5963+ debug_text: None ,
5964+ conversion: ConversionFlag :: None ,
5965+ format_spec: Some ( Box :: new( InterpolatedStringFormatSpec {
5966+ node_index: AtomicNodeIndex :: NONE ,
59345967 range,
5935- expression: Box :: new( expr_await_y) ,
5936- debug_text: None ,
5937- conversion: ConversionFlag :: None ,
5938- format_spec: None ,
5939- } ) ]
5940- . into( ) ,
5941- } ) ) ,
5942- } ) ]
5968+ elements: vec![ InterpolatedStringElement :: Interpolation (
5969+ InterpolatedElement {
5970+ node_index: AtomicNodeIndex :: NONE ,
5971+ range,
5972+ expression: Box :: new( expr_await_y) ,
5973+ debug_text: None ,
5974+ conversion: ConversionFlag :: None ,
5975+ format_spec: None ,
5976+ } ,
5977+ ) ]
5978+ . into( ) ,
5979+ } ) ) ,
5980+ } ,
5981+ ) ]
59435982 . into ( ) ,
59445983 flags,
59455984 } ) ,
0 commit comments