diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index c58f3746ec0..c39c11892f9 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -21,19 +21,7 @@ use itertools::Itertools; use malachite_bigint::BigInt; use num_complex::Complex; use num_traits::{Num, ToPrimitive}; -use ruff_python_ast::{ - Alias, Arguments, BoolOp, CmpOp, Comprehension, ConversionFlag, DebugText, Decorator, DictItem, - ExceptHandler, ExceptHandlerExceptHandler, Expr, ExprAttribute, ExprBoolOp, ExprContext, - ExprFString, ExprList, ExprName, ExprSlice, ExprStarred, ExprSubscript, ExprTString, ExprTuple, - ExprUnaryOp, FString, FStringFlags, FStringPart, Identifier, Int, InterpolatedStringElement, - InterpolatedStringElements, Keyword, MatchCase, ModExpression, ModModule, Operator, Parameters, - Pattern, PatternMatchAs, PatternMatchClass, PatternMatchMapping, PatternMatchOr, - PatternMatchSequence, PatternMatchSingleton, PatternMatchStar, PatternMatchValue, Singleton, - Stmt, StmtAnnAssign, StmtExpr, StmtFor, StmtIf, StmtMatch, StmtTry, StmtWhile, StmtWith, - TString, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams, - UnaryOp, WithItem, - visitor::{Visitor, walk_expr}, -}; +use ruff_python_ast as ast; use ruff_text_size::{Ranged, TextRange}; use std::collections::HashSet; @@ -72,7 +60,7 @@ pub enum FBlockType { pub enum FBlockDatum { None, /// For FinallyTry: stores the finally body statements to compile during unwind - FinallyBody(Vec), + FinallyBody(Vec), /// For HandlerCleanup: stores the exception variable name (e.g., "e" in "except X as e") ExceptionName(String), } @@ -82,8 +70,8 @@ pub enum FBlockDatum { enum SuperCallType<'a> { /// super(class, self) - explicit 2-argument form TwoArg { - class_arg: &'a Expr, - self_arg: &'a Expr, + class_arg: &'a ast::Expr, + self_arg: &'a ast::Expr, }, /// super() - implicit 0-argument form (uses __class__ cell) ZeroArg, @@ -179,7 +167,7 @@ enum ComprehensionType { Dict, } -fn validate_duplicate_params(params: &Parameters) -> Result<(), CodegenErrorType> { +fn validate_duplicate_params(params: &ast::Parameters) -> Result<(), CodegenErrorType> { let mut seen_params = HashSet::new(); for param in params { let param_name = param.name().as_str(); @@ -212,7 +200,7 @@ pub fn compile_top( /// Compile a standard Python program to bytecode pub fn compile_program( - ast: &ModModule, + ast: &ast::ModModule, source_file: SourceFile, opts: CompileOpts, ) -> CompileResult { @@ -227,7 +215,7 @@ pub fn compile_program( /// Compile a Python program to bytecode for the context of a REPL pub fn compile_program_single( - ast: &ModModule, + ast: &ast::ModModule, source_file: SourceFile, opts: CompileOpts, ) -> CompileResult { @@ -241,7 +229,7 @@ pub fn compile_program_single( } pub fn compile_block_expression( - ast: &ModModule, + ast: &ast::ModModule, source_file: SourceFile, opts: CompileOpts, ) -> CompileResult { @@ -255,7 +243,7 @@ pub fn compile_block_expression( } pub fn compile_expression( - ast: &ModExpression, + ast: &ast::ModExpression, source_file: SourceFile, opts: CompileOpts, ) -> CompileResult { @@ -437,13 +425,13 @@ impl Compiler { /// Check if the slice is a two-element slice (no step) // = is_two_element_slice - const fn is_two_element_slice(slice: &Expr) -> bool { - matches!(slice, Expr::Slice(s) if s.step.is_none()) + const fn is_two_element_slice(slice: &ast::Expr) -> bool { + matches!(slice, ast::Expr::Slice(s) if s.step.is_none()) } /// Compile a slice expression // = compiler_slice - fn compile_slice(&mut self, s: &ExprSlice) -> CompileResult { + fn compile_slice(&mut self, s: &ast::ExprSlice) -> CompileResult { // Compile lower if let Some(lower) = &s.lower { self.compile_expression(lower)?; @@ -472,9 +460,9 @@ impl Compiler { // = compiler_subscript fn compile_subscript( &mut self, - value: &Expr, - slice: &Expr, - ctx: ExprContext, + value: &ast::Expr, + slice: &ast::Expr, + ctx: ast::ExprContext, ) -> CompileResult<()> { // 1. Check subscripter and index for Load context // 2. VISIT value @@ -482,7 +470,7 @@ impl Compiler { // 4. Otherwise VISIT slice and emit appropriate instruction // For Load context, some checks are skipped for now - // if ctx == ExprContext::Load { + // if ctx == ast::ExprContext::Load { // check_subscripter(value); // check_index(value, slice); // } @@ -491,17 +479,19 @@ impl Compiler { self.compile_expression(value)?; // Handle two-element slice (for Load/Store, not Del) - if Self::is_two_element_slice(slice) && !matches!(ctx, ExprContext::Del) { + if Self::is_two_element_slice(slice) && !matches!(ctx, ast::ExprContext::Del) { let argc = match slice { - Expr::Slice(s) => self.compile_slice(s)?, - _ => unreachable!("is_two_element_slice should only return true for Expr::Slice"), + ast::Expr::Slice(s) => self.compile_slice(s)?, + _ => unreachable!( + "is_two_element_slice should only return true for ast::Expr::Slice" + ), }; match ctx { - ExprContext::Load => { + ast::ExprContext::Load => { emit!(self, Instruction::BuildSlice { argc }); emit!(self, Instruction::Subscript); } - ExprContext::Store => { + ast::ExprContext::Store => { emit!(self, Instruction::BuildSlice { argc }); emit!(self, Instruction::StoreSubscr); } @@ -513,10 +503,10 @@ impl Compiler { // Emit appropriate instruction based on context match ctx { - ExprContext::Load => emit!(self, Instruction::Subscript), - ExprContext::Store => emit!(self, Instruction::StoreSubscr), - ExprContext::Del => emit!(self, Instruction::DeleteSubscr), - ExprContext::Invalid => { + ast::ExprContext::Load => emit!(self, Instruction::Subscript), + ast::ExprContext::Store => emit!(self, Instruction::StoreSubscr), + ast::ExprContext::Del => emit!(self, Instruction::DeleteSubscr), + ast::ExprContext::Invalid => { return Err(self.error(CodegenErrorType::SyntaxError( "Invalid expression context".to_owned(), ))); @@ -529,7 +519,7 @@ impl Compiler { /// Helper function for compiling tuples/lists/sets with starred expressions /// - /// Parameters: + /// ast::Parameters: /// - elts: The elements to compile /// - pushed: Number of items already on the stack /// - collection_type: What type of collection to build (tuple, list, set) @@ -537,7 +527,7 @@ impl Compiler { // = starunpack_helper in compile.c fn starunpack_helper( &mut self, - elts: &[Expr], + elts: &[ast::Expr], pushed: u32, collection_type: CollectionType, ) -> CompileResult<()> { @@ -736,13 +726,11 @@ impl Compiler { /// Returns Some(SuperCallType) if optimization is possible, None otherwise fn can_optimize_super_call<'a>( &self, - value: &'a Expr, + value: &'a ast::Expr, attr: &str, ) -> Option> { - use ruff_python_ast::*; - // 1. value must be a Call expression - let Expr::Call(ExprCall { + let ast::Expr::Call(ast::ExprCall { func, arguments, .. }) = value else { @@ -750,7 +738,7 @@ impl Compiler { }; // 2. func must be Name("super") - let Expr::Name(ExprName { id, .. }) = func.as_ref() else { + let ast::Expr::Name(ast::ExprName { id, .. }) = func.as_ref() else { return None; }; if id.as_str() != "super" { @@ -792,7 +780,7 @@ impl Compiler { let args = &arguments.args; // No starred expressions allowed - if args.iter().any(|arg| matches!(arg, Expr::Starred(_))) { + if args.iter().any(|arg| matches!(arg, ast::Expr::Starred(_))) { return None; } @@ -1114,7 +1102,7 @@ impl Compiler { let _table = self.pop_symbol_table(); // Various scopes can have sub_tables: - // - TypeParams scope can have sub_tables (the function body's symbol table) + // - ast::TypeParams scope can have sub_tables (the function body's symbol table) // - Module scope can have sub_tables (for TypeAlias scopes, nested functions, classes) // - Function scope can have sub_tables (for nested functions, classes) // - Class scope can have sub_tables (for nested classes, methods) @@ -1554,7 +1542,7 @@ impl Compiler { let mut parent_idx = stack_size - 2; let mut parent = &self.code_stack[parent_idx]; - // If parent is TypeParams scope, look at grandparent + // If parent is ast::TypeParams scope, look at grandparent // Check if parent is a type params scope by name pattern if parent.metadata.name.starts_with(" CompileResult<()> { let size_before = self.code_stack.len(); @@ -1673,7 +1661,7 @@ impl Compiler { fn compile_program_single( &mut self, - body: &[Stmt], + body: &[ast::Stmt], symbol_table: SymbolTable, ) -> CompileResult<()> { // Set future_annotations from symbol table (detected during symbol table scan) @@ -1699,7 +1687,7 @@ impl Compiler { if let Some((last, body)) = body.split_last() { for statement in body { - if let Stmt::Expr(StmtExpr { value, .. }) = &statement { + if let ast::Stmt::Expr(ast::StmtExpr { value, .. }) = &statement { self.compile_expression(value)?; emit!( self, @@ -1714,7 +1702,7 @@ impl Compiler { } } - if let Stmt::Expr(StmtExpr { value, .. }) = &last { + if let ast::Stmt::Expr(ast::StmtExpr { value, .. }) = &last { self.compile_expression(value)?; emit!(self, Instruction::Copy { index: 1_u32 }); emit!( @@ -1739,7 +1727,7 @@ impl Compiler { fn compile_block_expr( &mut self, - body: &[Stmt], + body: &[ast::Stmt], symbol_table: SymbolTable, ) -> CompileResult<()> { self.symbol_table_stack.push(symbol_table); @@ -1748,10 +1736,10 @@ impl Compiler { if let Some(last_statement) = body.last() { match last_statement { - Stmt::Expr(_) => { + ast::Stmt::Expr(_) => { self.current_block().instructions.pop(); // pop Instruction::PopTop } - Stmt::FunctionDef(_) | Stmt::ClassDef(_) => { + ast::Stmt::FunctionDef(_) | ast::Stmt::ClassDef(_) => { let pop_instructions = self.current_block().instructions.pop(); let store_inst = compiler_unwrap_option(self, pop_instructions); // pop Instruction::Store emit!(self, Instruction::Copy { index: 1_u32 }); @@ -1768,7 +1756,7 @@ impl Compiler { // Compile statement in eval mode: fn compile_eval( &mut self, - expression: &ModExpression, + expression: &ast::ModExpression, symbol_table: SymbolTable, ) -> CompileResult<()> { self.symbol_table_stack.push(symbol_table); @@ -1777,7 +1765,7 @@ impl Compiler { Ok(()) } - fn compile_statements(&mut self, statements: &[Stmt]) -> CompileResult<()> { + fn compile_statements(&mut self, statements: &[ast::Stmt]) -> CompileResult<()> { for statement in statements { self.compile_statement(statement)? } @@ -1824,7 +1812,7 @@ impl Compiler { // Determine the operation type based on symbol scope let is_function_like = self.ctx.in_func(); - // Look up the symbol, handling TypeParams and Annotation scopes specially + // Look up the symbol, handling ast::TypeParams and Annotation scopes specially let (symbol_scope, can_see_class_scope) = { let current_table = self.current_symbol_table(); let is_typeparams = current_table.typ == CompilerScope::TypeParams; @@ -1834,7 +1822,7 @@ impl Compiler { // First try to find in current table let symbol = current_table.lookup(name.as_ref()); - // If not found and we're in TypeParams or Annotation scope, try parent scope + // If not found and we're in ast::TypeParams or Annotation scope, try parent scope let symbol = if symbol.is_none() && (is_typeparams || is_annotation) { self.symbol_table_stack .get(self.symbol_table_stack.len() - 2) // Try to get parent index @@ -1985,22 +1973,21 @@ impl Compiler { Ok(()) } - fn compile_statement(&mut self, statement: &Stmt) -> CompileResult<()> { - use ruff_python_ast::*; + fn compile_statement(&mut self, statement: &ast::Stmt) -> CompileResult<()> { trace!("Compiling {statement:?}"); self.set_source_range(statement.range()); match &statement { // we do this here because `from __future__` still executes that `from` statement at runtime, // we still need to compile the ImportFrom down below - Stmt::ImportFrom(StmtImportFrom { module, names, .. }) + ast::Stmt::ImportFrom(ast::StmtImportFrom { module, names, .. }) if module.as_ref().map(|id| id.as_str()) == Some("__future__") => { self.compile_future_features(names)? } // ignore module-level doc comments - Stmt::Expr(StmtExpr { value, .. }) - if matches!(&**value, Expr::StringLiteral(..)) + ast::Stmt::Expr(ast::StmtExpr { value, .. }) + if matches!(&**value, ast::Expr::StringLiteral(..)) && matches!(self.done_with_future_stmts, DoneWithFuture::No) => { self.done_with_future_stmts = DoneWithFuture::DoneWithDoc @@ -2010,7 +1997,7 @@ impl Compiler { } match &statement { - Stmt::Import(StmtImport { names, .. }) => { + ast::Stmt::Import(ast::StmtImport { names, .. }) => { // import a, b, c as d for name in names { let name = &name; @@ -2031,7 +2018,7 @@ impl Compiler { } } } - Stmt::ImportFrom(StmtImportFrom { + ast::Stmt::ImportFrom(ast::StmtImportFrom { level, module, names, @@ -2097,16 +2084,16 @@ impl Compiler { emit!(self, Instruction::PopTop); } } - Stmt::Expr(StmtExpr { value, .. }) => { + ast::Stmt::Expr(ast::StmtExpr { value, .. }) => { self.compile_expression(value)?; // Pop result of stack, since we not use it: emit!(self, Instruction::PopTop); } - Stmt::Global(_) | Stmt::Nonlocal(_) => { + ast::Stmt::Global(_) | ast::Stmt::Nonlocal(_) => { // Handled during symbol table construction. } - Stmt::If(StmtIf { + ast::Stmt::If(ast::StmtIf { test, body, elif_else_clauses, @@ -2162,16 +2149,16 @@ impl Compiler { } self.leave_conditional_block(); } - Stmt::While(StmtWhile { + ast::Stmt::While(ast::StmtWhile { test, body, orelse, .. }) => self.compile_while(test, body, orelse)?, - Stmt::With(StmtWith { + ast::Stmt::With(ast::StmtWith { items, body, is_async, .. }) => self.compile_with(items, body, *is_async)?, - Stmt::For(StmtFor { + ast::Stmt::For(ast::StmtFor { target, iter, body, @@ -2179,8 +2166,10 @@ impl Compiler { is_async, .. }) => self.compile_for(target, iter, body, orelse, *is_async)?, - Stmt::Match(StmtMatch { subject, cases, .. }) => self.compile_match(subject, cases)?, - Stmt::Raise(StmtRaise { + ast::Stmt::Match(ast::StmtMatch { subject, cases, .. }) => { + self.compile_match(subject, cases)? + } + ast::Stmt::Raise(ast::StmtRaise { exc, cause, range, .. }) => { let kind = match exc { @@ -2199,7 +2188,7 @@ impl Compiler { self.set_source_range(*range); emit!(self, Instruction::RaiseVarargs { kind }); } - Stmt::Try(StmtTry { + ast::Stmt::Try(ast::StmtTry { body, handlers, orelse, @@ -2215,7 +2204,7 @@ impl Compiler { } self.leave_conditional_block(); } - Stmt::FunctionDef(StmtFunctionDef { + ast::Stmt::FunctionDef(ast::StmtFunctionDef { name, parameters, body, @@ -2237,7 +2226,7 @@ impl Compiler { type_params.as_deref(), )? } - Stmt::ClassDef(StmtClassDef { + ast::Stmt::ClassDef(ast::StmtClassDef { name, body, decorator_list, @@ -2251,7 +2240,7 @@ impl Compiler { type_params.as_deref(), arguments.as_deref(), )?, - Stmt::Assert(StmtAssert { test, msg, .. }) => { + ast::Stmt::Assert(ast::StmtAssert { test, msg, .. }) => { // if some flag, ignore all assert statements! if self.opts.optimize == 0 { let after_block = self.new_block(); @@ -2279,15 +2268,15 @@ impl Compiler { self.switch_to_block(after_block); } } - Stmt::Break(_) => { + ast::Stmt::Break(_) => { // Unwind fblock stack until we find a loop, emitting cleanup for each fblock self.compile_break_continue(statement.range(), true)?; } - Stmt::Continue(_) => { + ast::Stmt::Continue(_) => { // Unwind fblock stack until we find a loop, emitting cleanup for each fblock self.compile_break_continue(statement.range(), false)?; } - Stmt::Return(StmtReturn { value, .. }) => { + ast::Stmt::Return(ast::StmtReturn { value, .. }) => { if !self.ctx.in_func() { return Err( self.error_ranged(CodegenErrorType::InvalidReturn, statement.range()) @@ -2319,7 +2308,7 @@ impl Compiler { } } } - Stmt::Assign(StmtAssign { targets, value, .. }) => { + ast::Stmt::Assign(ast::StmtAssign { targets, value, .. }) => { self.compile_expression(value)?; for (i, target) in targets.iter().enumerate() { @@ -2329,25 +2318,25 @@ impl Compiler { self.compile_store(target)?; } } - Stmt::AugAssign(StmtAugAssign { + ast::Stmt::AugAssign(ast::StmtAugAssign { target, op, value, .. }) => self.compile_augassign(target, op, value)?, - Stmt::AnnAssign(StmtAnnAssign { + ast::Stmt::AnnAssign(ast::StmtAnnAssign { target, annotation, value, simple, .. }) => self.compile_annotated_assign(target, annotation, value.as_deref(), *simple)?, - Stmt::Delete(StmtDelete { targets, .. }) => { + ast::Stmt::Delete(ast::StmtDelete { targets, .. }) => { for target in targets { self.compile_delete(target)?; } } - Stmt::Pass(_) => { + ast::Stmt::Pass(_) => { // No need to emit any code here :) } - Stmt::TypeAlias(StmtTypeAlias { + ast::Stmt::TypeAlias(ast::StmtTypeAlias { name, type_params, value, @@ -2403,31 +2392,33 @@ impl Compiler { ); self.store_name(&name_string)?; } - Stmt::IpyEscapeCommand(_) => todo!(), + ast::Stmt::IpyEscapeCommand(_) => todo!(), } Ok(()) } - fn compile_delete(&mut self, expression: &Expr) -> CompileResult<()> { - use ruff_python_ast::*; + fn compile_delete(&mut self, expression: &ast::Expr) -> CompileResult<()> { match &expression { - Expr::Name(ExprName { id, .. }) => self.compile_name(id.as_str(), NameUsage::Delete)?, - Expr::Attribute(ExprAttribute { value, attr, .. }) => { + ast::Expr::Name(ast::ExprName { id, .. }) => { + self.compile_name(id.as_str(), NameUsage::Delete)? + } + ast::Expr::Attribute(ast::ExprAttribute { value, attr, .. }) => { self.compile_expression(value)?; let idx = self.name(attr.as_str()); emit!(self, Instruction::DeleteAttr { idx }); } - Expr::Subscript(ExprSubscript { + ast::Expr::Subscript(ast::ExprSubscript { value, slice, ctx, .. }) => { self.compile_subscript(value, slice, *ctx)?; } - Expr::Tuple(ExprTuple { elts, .. }) | Expr::List(ExprList { elts, .. }) => { + ast::Expr::Tuple(ast::ExprTuple { elts, .. }) + | ast::Expr::List(ast::ExprList { elts, .. }) => { for element in elts { self.compile_delete(element)?; } } - Expr::BinOp(_) | Expr::UnaryOp(_) => { + ast::Expr::BinOp(_) | ast::Expr::UnaryOp(_) => { return Err(self.error(CodegenErrorType::Delete("expression"))); } _ => return Err(self.error(CodegenErrorType::Delete(expression.python_name()))), @@ -2435,7 +2426,7 @@ impl Compiler { Ok(()) } - fn enter_function(&mut self, name: &str, parameters: &Parameters) -> CompileResult<()> { + fn enter_function(&mut self, name: &str, parameters: &ast::Parameters) -> CompileResult<()> { // TODO: partition_in_place let mut kw_without_defaults = vec![]; let mut kw_with_defaults = vec![]; @@ -2479,7 +2470,7 @@ impl Compiler { /// Push decorators onto the stack in source order. /// For @dec1 @dec2 def foo(): stack becomes [dec1, NULL, dec2, NULL] - fn prepare_decorators(&mut self, decorator_list: &[Decorator]) -> CompileResult<()> { + fn prepare_decorators(&mut self, decorator_list: &[ast::Decorator]) -> CompileResult<()> { for decorator in decorator_list { self.compile_expression(&decorator.expression)?; emit!(self, Instruction::PushNull); @@ -2491,7 +2482,7 @@ impl Compiler { /// Stack [dec1, NULL, dec2, NULL, func] -> dec2(func) -> dec1(dec2(func)) /// The forward loop works because each Call pops from TOS, naturally /// applying decorators bottom-up (innermost first). - fn apply_decorators(&mut self, decorator_list: &[Decorator]) { + fn apply_decorators(&mut self, decorator_list: &[ast::Decorator]) { for _ in decorator_list { emit!(self, Instruction::Call { nargs: 1 }); } @@ -2500,7 +2491,7 @@ impl Compiler { /// Compile type parameter bound or default in a separate scope and return closure fn compile_type_param_bound_or_default( &mut self, - expr: &Expr, + expr: &ast::Expr, name: &str, allow_starred: bool, ) -> CompileResult<()> { @@ -2515,8 +2506,8 @@ impl Compiler { self.enter_scope(name, CompilerScope::TypeParams, key, lineno)?; // Compile the expression - if allow_starred && matches!(expr, Expr::Starred(_)) { - if let Expr::Starred(starred) = expr { + if allow_starred && matches!(expr, ast::Expr::Starred(_)) { + if let ast::Expr::Starred(starred) = expr { self.compile_expression(&starred.value)?; emit!(self, Instruction::UnpackSequence { size: 1 }); } @@ -2543,11 +2534,11 @@ impl Compiler { /// Store each type parameter so it is accessible to the current scope, and leave a tuple of /// all the type parameters on the stack. Handles default values per PEP 695. - fn compile_type_params(&mut self, type_params: &TypeParams) -> CompileResult<()> { + fn compile_type_params(&mut self, type_params: &ast::TypeParams) -> CompileResult<()> { // First, compile each type parameter and store it for type_param in &type_params.type_params { match type_param { - TypeParam::TypeVar(TypeParamTypeVar { + ast::TypeParam::TypeVar(ast::TypeParamTypeVar { name, bound, default, @@ -2594,7 +2585,7 @@ impl Compiler { emit!(self, Instruction::Copy { index: 1_u32 }); self.store_name(name.as_ref())?; } - TypeParam::ParamSpec(TypeParamParamSpec { name, default, .. }) => { + ast::TypeParam::ParamSpec(ast::TypeParamParamSpec { name, default, .. }) => { self.emit_load_const(ConstantData::Str { value: name.as_str().into(), }); @@ -2619,7 +2610,9 @@ impl Compiler { emit!(self, Instruction::Copy { index: 1_u32 }); self.store_name(name.as_ref())?; } - TypeParam::TypeVarTuple(TypeParamTypeVarTuple { name, default, .. }) => { + ast::TypeParam::TypeVarTuple(ast::TypeParamTypeVarTuple { + name, default, .. + }) => { self.emit_load_const(ConstantData::Str { value: name.as_str().into(), }); @@ -2658,10 +2651,10 @@ impl Compiler { fn compile_try_statement( &mut self, - body: &[Stmt], - handlers: &[ExceptHandler], - orelse: &[Stmt], - finalbody: &[Stmt], + body: &[ast::Stmt], + handlers: &[ast::ExceptHandler], + orelse: &[ast::Stmt], + finalbody: &[ast::Stmt], ) -> CompileResult<()> { let handler_block = self.new_block(); let finally_block = self.new_block(); @@ -2844,8 +2837,11 @@ impl Compiler { // PUSH_EXC_INFO transforms [exc] -> [prev_exc, exc] for PopExcept emit!(self, Instruction::PushExcInfo); for handler in handlers { - let ExceptHandler::ExceptHandler(ExceptHandlerExceptHandler { - type_, name, body, .. + let ast::ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler { + type_, + name, + body, + .. }) = &handler; let next_handler = self.new_block(); @@ -3117,10 +3113,10 @@ impl Compiler { fn compile_try_star_except( &mut self, - body: &[Stmt], - handlers: &[ExceptHandler], - orelse: &[Stmt], - finalbody: &[Stmt], + body: &[ast::Stmt], + handlers: &[ast::ExceptHandler], + orelse: &[ast::Stmt], + finalbody: &[ast::Stmt], ) -> CompileResult<()> { // compiler_try_star_except // Stack layout during handler processing: [prev_exc, orig, list, rest] @@ -3177,8 +3173,11 @@ impl Compiler { let n = handlers.len(); for (i, handler) in handlers.iter().enumerate() { - let ExceptHandler::ExceptHandler(ExceptHandlerExceptHandler { - type_, name, body, .. + let ast::ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler { + type_, + name, + body, + .. }) = handler; let no_match_block = self.new_block(); @@ -3198,7 +3197,7 @@ impl Compiler { // Compile exception type if let Some(exc_type) = type_ { // Check for unparenthesized tuple - if let Expr::Tuple(ExprTuple { elts, range, .. }) = exc_type.as_ref() + if let ast::Expr::Tuple(ast::ExprTuple { elts, range, .. }) = exc_type.as_ref() && let Some(first) = elts.first() && range.start().to_u32() == first.range().start().to_u32() { @@ -3435,7 +3434,7 @@ impl Compiler { // = compiler_default_arguments fn compile_default_arguments( &mut self, - parameters: &Parameters, + parameters: &ast::Parameters, ) -> CompileResult { let mut funcflags = bytecode::MakeFunctionFlags::empty(); @@ -3493,8 +3492,8 @@ impl Compiler { fn compile_function_body( &mut self, name: &str, - parameters: &Parameters, - body: &[Stmt], + parameters: &ast::Parameters, + body: &[ast::Stmt], is_async: bool, funcflags: bytecode::MakeFunctionFlags, ) -> CompileResult<()> { @@ -3540,7 +3539,7 @@ impl Compiler { // Emit None at end if needed match body.last() { - Some(Stmt::Return(_)) => {} + Some(ast::Stmt::Return(_)) => {} _ => { self.emit_return_const(ConstantData::None); } @@ -3565,8 +3564,8 @@ impl Compiler { fn compile_annotations_closure( &mut self, func_name: &str, - parameters: &Parameters, - returns: Option<&Expr>, + parameters: &ast::Parameters, + returns: Option<&ast::Expr>, ) -> CompileResult { // Try to enter annotation scope - returns false if no annotation_block exists if !self.enter_annotation_scope(func_name)? { @@ -3635,21 +3634,21 @@ impl Compiler { /// Returns list of (name, annotation_expr) pairs /// This must match the order that annotations are compiled to ensure /// conditional_annotation_index stays in sync with __annotate__ enumeration. - fn collect_simple_annotations(body: &[Stmt]) -> Vec<(&str, &Expr)> { - fn walk<'a>(stmts: &'a [Stmt], out: &mut Vec<(&'a str, &'a Expr)>) { + fn collect_simple_annotations(body: &[ast::Stmt]) -> Vec<(&str, &ast::Expr)> { + fn walk<'a>(stmts: &'a [ast::Stmt], out: &mut Vec<(&'a str, &'a ast::Expr)>) { for stmt in stmts { match stmt { - Stmt::AnnAssign(StmtAnnAssign { + ast::Stmt::AnnAssign(ast::StmtAnnAssign { target, annotation, simple, .. - }) if *simple && matches!(target.as_ref(), Expr::Name(_)) => { - if let Expr::Name(ExprName { id, .. }) = target.as_ref() { + }) if *simple && matches!(target.as_ref(), ast::Expr::Name(_)) => { + if let ast::Expr::Name(ast::ExprName { id, .. }) = target.as_ref() { out.push((id.as_str(), annotation.as_ref())); } } - Stmt::If(StmtIf { + ast::Stmt::If(ast::StmtIf { body, elif_else_clauses, .. @@ -3659,13 +3658,13 @@ impl Compiler { walk(&clause.body, out); } } - Stmt::For(StmtFor { body, orelse, .. }) - | Stmt::While(StmtWhile { body, orelse, .. }) => { + ast::Stmt::For(ast::StmtFor { body, orelse, .. }) + | ast::Stmt::While(ast::StmtWhile { body, orelse, .. }) => { walk(body, out); walk(orelse, out); } - Stmt::With(StmtWith { body, .. }) => walk(body, out), - Stmt::Try(StmtTry { + ast::Stmt::With(ast::StmtWith { body, .. }) => walk(body, out), + ast::Stmt::Try(ast::StmtTry { body, handlers, orelse, @@ -3674,16 +3673,15 @@ impl Compiler { }) => { walk(body, out); for handler in handlers { - let ExceptHandler::ExceptHandler(ExceptHandlerExceptHandler { - body, - .. - }) = handler; + let ast::ExceptHandler::ExceptHandler( + ast::ExceptHandlerExceptHandler { body, .. }, + ) = handler; walk(body, out); } walk(orelse, out); walk(finalbody, out); } - Stmt::Match(StmtMatch { cases, .. }) => { + ast::Stmt::Match(ast::StmtMatch { cases, .. }) => { for case in cases { walk(&case.body, out); } @@ -3699,7 +3697,7 @@ impl Compiler { /// Compile module-level __annotate__ function (PEP 649) /// Returns true if __annotate__ was created and stored - fn compile_module_annotate(&mut self, body: &[Stmt]) -> CompileResult { + fn compile_module_annotate(&mut self, body: &[ast::Stmt]) -> CompileResult { // Collect simple annotations from module body first let annotations = Self::collect_simple_annotations(body); @@ -3842,12 +3840,12 @@ impl Compiler { fn compile_function_def( &mut self, name: &str, - parameters: &Parameters, - body: &[Stmt], - decorator_list: &[Decorator], - returns: Option<&Expr>, // TODO: use type hint somehow.. + parameters: &ast::Parameters, + body: &[ast::Stmt], + decorator_list: &[ast::Decorator], + returns: Option<&ast::Expr>, // TODO: use type hint somehow.. is_async: bool, - type_params: Option<&TypeParams>, + type_params: Option<&ast::TypeParams>, ) -> CompileResult<()> { self.prepare_decorators(decorator_list)?; @@ -4163,15 +4161,14 @@ impl Compiler { } // Python/compile.c find_ann - fn find_ann(body: &[Stmt]) -> bool { - use ruff_python_ast::*; + fn find_ann(body: &[ast::Stmt]) -> bool { for statement in body { let res = match &statement { - Stmt::AnnAssign(_) => true, - Stmt::For(StmtFor { body, orelse, .. }) => { + ast::Stmt::AnnAssign(_) => true, + ast::Stmt::For(ast::StmtFor { body, orelse, .. }) => { Self::find_ann(body) || Self::find_ann(orelse) } - Stmt::If(StmtIf { + ast::Stmt::If(ast::StmtIf { body, elif_else_clauses, .. @@ -4179,11 +4176,11 @@ impl Compiler { Self::find_ann(body) || elif_else_clauses.iter().any(|x| Self::find_ann(&x.body)) } - Stmt::While(StmtWhile { body, orelse, .. }) => { + ast::Stmt::While(ast::StmtWhile { body, orelse, .. }) => { Self::find_ann(body) || Self::find_ann(orelse) } - Stmt::With(StmtWith { body, .. }) => Self::find_ann(body), - Stmt::Try(StmtTry { + ast::Stmt::With(ast::StmtWith { body, .. }) => Self::find_ann(body), + ast::Stmt::Try(ast::StmtTry { body, orelse, finalbody, @@ -4203,8 +4200,8 @@ impl Compiler { fn compile_class_body( &mut self, name: &str, - body: &[Stmt], - type_params: Option<&TypeParams>, + body: &[ast::Stmt], + type_params: Option<&ast::TypeParams>, firstlineno: u32, ) -> CompileResult { // 1. Enter class scope @@ -4318,10 +4315,10 @@ impl Compiler { fn compile_class_def( &mut self, name: &str, - body: &[Stmt], - decorator_list: &[Decorator], - type_params: Option<&TypeParams>, - arguments: Option<&Arguments>, + body: &[ast::Stmt], + decorator_list: &[ast::Decorator], + type_params: Option<&ast::TypeParams>, + arguments: Option<&ast::Arguments>, ) -> CompileResult<()> { self.prepare_decorators(decorator_list)?; @@ -4390,8 +4387,11 @@ impl Compiler { // Compile bases and call __build_class__ // Check for starred bases or **kwargs - let has_starred = arguments - .is_some_and(|args| args.args.iter().any(|arg| matches!(arg, Expr::Starred(_)))); + let has_starred = arguments.is_some_and(|args| { + args.args + .iter() + .any(|arg| matches!(arg, ast::Expr::Starred(_))) + }); let has_double_star = arguments.is_some_and(|args| args.keywords.iter().any(|kw| kw.arg.is_none())); @@ -4506,7 +4506,12 @@ impl Compiler { self.store_name(name) } - fn compile_while(&mut self, test: &Expr, body: &[Stmt], orelse: &[Stmt]) -> CompileResult<()> { + fn compile_while( + &mut self, + test: &ast::Expr, + body: &[ast::Stmt], + orelse: &[ast::Stmt], + ) -> CompileResult<()> { self.enter_conditional_block(); let while_block = self.new_block(); @@ -4544,8 +4549,8 @@ impl Compiler { fn compile_with( &mut self, - items: &[WithItem], - body: &[Stmt], + items: &[ast::WithItem], + body: &[ast::Stmt], is_async: bool, ) -> CompileResult<()> { self.enter_conditional_block(); @@ -4768,10 +4773,10 @@ impl Compiler { fn compile_for( &mut self, - target: &Expr, - iter: &Expr, - body: &[Stmt], - orelse: &[Stmt], + target: &ast::Expr, + iter: &ast::Expr, + body: &[ast::Stmt], + orelse: &[ast::Stmt], is_async: bool, ) -> CompileResult<()> { self.enter_conditional_block(); @@ -4956,7 +4961,7 @@ impl Compiler { /// to the list of captured names. fn pattern_helper_store_name( &mut self, - n: Option<&Identifier>, + n: Option<&ast::Identifier>, pc: &mut PatternContext, ) -> CompileResult<()> { match n { @@ -4990,7 +4995,7 @@ impl Compiler { } } - fn pattern_unpack_helper(&mut self, elts: &[Pattern]) -> CompileResult<()> { + fn pattern_unpack_helper(&mut self, elts: &[ast::Pattern]) -> CompileResult<()> { let n = elts.len(); let mut seen_star = false; for (i, elt) in elts.iter().enumerate() { @@ -5026,7 +5031,7 @@ impl Compiler { fn pattern_helper_sequence_unpack( &mut self, - patterns: &[Pattern], + patterns: &[ast::Pattern], _star: Option, pc: &mut PatternContext, ) -> CompileResult<()> { @@ -5046,7 +5051,7 @@ impl Compiler { fn pattern_helper_sequence_subscr( &mut self, - patterns: &[Pattern], + patterns: &[ast::Pattern], star: usize, pc: &mut PatternContext, ) -> CompileResult<()> { @@ -5094,7 +5099,7 @@ impl Compiler { fn compile_pattern_subpattern( &mut self, - p: &Pattern, + p: &ast::Pattern, pc: &mut PatternContext, ) -> CompileResult<()> { // Save the current allow_irrefutable state. @@ -5110,7 +5115,7 @@ impl Compiler { fn compile_pattern_as( &mut self, - p: &PatternMatchAs, + p: &ast::PatternMatchAs, pc: &mut PatternContext, ) -> CompileResult<()> { // If there is no sub-pattern, then it's an irrefutable match. @@ -5147,7 +5152,7 @@ impl Compiler { fn compile_pattern_star( &mut self, - p: &PatternMatchStar, + p: &ast::PatternMatchStar, pc: &mut PatternContext, ) -> CompileResult<()> { self.pattern_helper_store_name(p.name.as_ref(), pc)?; @@ -5158,8 +5163,8 @@ impl Compiler { /// and not duplicated. fn validate_kwd_attrs( &mut self, - attrs: &[Identifier], - _patterns: &[Pattern], + attrs: &[ast::Identifier], + _patterns: &[ast::Pattern], ) -> CompileResult<()> { let n_attrs = attrs.len(); for i in 0..n_attrs { @@ -5182,7 +5187,7 @@ impl Compiler { fn compile_pattern_class( &mut self, - p: &PatternMatchClass, + p: &ast::PatternMatchClass, pc: &mut PatternContext, ) -> CompileResult<()> { // Extract components from the MatchClass pattern. @@ -5261,7 +5266,7 @@ impl Compiler { for subpattern in patterns.iter().chain(kwd_patterns.iter()) { // Check if this is a true wildcard (underscore pattern without name binding) let is_true_wildcard = match subpattern { - Pattern::MatchAs(match_as) => { + ast::Pattern::MatchAs(match_as) => { // Only consider it wildcard if both pattern and name are None (i.e., "_") match_as.pattern.is_none() && match_as.name.is_none() } @@ -5284,7 +5289,7 @@ impl Compiler { fn compile_pattern_mapping( &mut self, - p: &PatternMatchMapping, + p: &ast::PatternMatchMapping, pc: &mut PatternContext, ) -> CompileResult<()> { let mapping = p; @@ -5357,14 +5362,14 @@ impl Compiler { // Validate and compile keys let mut seen = HashSet::new(); for key in keys { - let is_attribute = matches!(key, Expr::Attribute(_)); + let is_attribute = matches!(key, ast::Expr::Attribute(_)); let is_literal = matches!( key, - Expr::NumberLiteral(_) - | Expr::StringLiteral(_) - | Expr::BytesLiteral(_) - | Expr::BooleanLiteral(_) - | Expr::NoneLiteral(_) + ast::Expr::NumberLiteral(_) + | ast::Expr::StringLiteral(_) + | ast::Expr::BytesLiteral(_) + | ast::Expr::BooleanLiteral(_) + | ast::Expr::NoneLiteral(_) ); let key_repr = if is_literal { UnparseExpr::new(key, &self.source_file).to_string() @@ -5487,7 +5492,7 @@ impl Compiler { fn compile_pattern_or( &mut self, - p: &PatternMatchOr, + p: &ast::PatternMatchOr, pc: &mut PatternContext, ) -> CompileResult<()> { // Ensure the pattern is a MatchOr. @@ -5595,11 +5600,11 @@ impl Compiler { fn compile_pattern_sequence( &mut self, - p: &PatternMatchSequence, + p: &ast::PatternMatchSequence, pc: &mut PatternContext, ) -> CompileResult<()> { // Ensure the pattern is a MatchSequence. - let patterns = &p.patterns; // a slice of Pattern + let patterns = &p.patterns; // a slice of ast::Pattern let size = patterns.len(); let mut star: Option = None; let mut only_wildcard = true; @@ -5662,7 +5667,7 @@ impl Compiler { // Whatever comes next should consume the subject. pc.on_top -= 1; if only_wildcard { - // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. + // ast::Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. emit!(self, Instruction::PopTop); } else if star_wildcard { self.pattern_helper_sequence_subscr(patterns, star.unwrap(), pc)?; @@ -5674,7 +5679,7 @@ impl Compiler { fn compile_pattern_value( &mut self, - p: &PatternMatchValue, + p: &ast::PatternMatchValue, pc: &mut PatternContext, ) -> CompileResult<()> { // TODO: ensure literal or attribute lookup @@ -5692,14 +5697,14 @@ impl Compiler { fn compile_pattern_singleton( &mut self, - p: &PatternMatchSingleton, + p: &ast::PatternMatchSingleton, pc: &mut PatternContext, ) -> CompileResult<()> { // Load the singleton constant value. self.emit_load_const(match p.value { - Singleton::None => ConstantData::None, - Singleton::False => ConstantData::Boolean { value: false }, - Singleton::True => ConstantData::Boolean { value: true }, + ast::Singleton::None => ConstantData::None, + ast::Singleton::False => ConstantData::Boolean { value: false }, + ast::Singleton::True => ConstantData::Boolean { value: true }, }); // Compare using the "Is" operator. emit!(self, Instruction::IsOp(Invert::No)); @@ -5710,32 +5715,32 @@ impl Compiler { fn compile_pattern( &mut self, - pattern_type: &Pattern, + pattern_type: &ast::Pattern, pattern_context: &mut PatternContext, ) -> CompileResult<()> { match &pattern_type { - Pattern::MatchValue(pattern_type) => { + ast::Pattern::MatchValue(pattern_type) => { self.compile_pattern_value(pattern_type, pattern_context) } - Pattern::MatchSingleton(pattern_type) => { + ast::Pattern::MatchSingleton(pattern_type) => { self.compile_pattern_singleton(pattern_type, pattern_context) } - Pattern::MatchSequence(pattern_type) => { + ast::Pattern::MatchSequence(pattern_type) => { self.compile_pattern_sequence(pattern_type, pattern_context) } - Pattern::MatchMapping(pattern_type) => { + ast::Pattern::MatchMapping(pattern_type) => { self.compile_pattern_mapping(pattern_type, pattern_context) } - Pattern::MatchClass(pattern_type) => { + ast::Pattern::MatchClass(pattern_type) => { self.compile_pattern_class(pattern_type, pattern_context) } - Pattern::MatchStar(pattern_type) => { + ast::Pattern::MatchStar(pattern_type) => { self.compile_pattern_star(pattern_type, pattern_context) } - Pattern::MatchAs(pattern_type) => { + ast::Pattern::MatchAs(pattern_type) => { self.compile_pattern_as(pattern_type, pattern_context) } - Pattern::MatchOr(pattern_type) => { + ast::Pattern::MatchOr(pattern_type) => { self.compile_pattern_or(pattern_type, pattern_context) } } @@ -5743,8 +5748,8 @@ impl Compiler { fn compile_match_inner( &mut self, - subject: &Expr, - cases: &[MatchCase], + subject: &ast::Expr, + cases: &[ast::MatchCase], pattern_context: &mut PatternContext, ) -> CompileResult<()> { self.compile_expression(subject)?; @@ -5815,7 +5820,11 @@ impl Compiler { Ok(()) } - fn compile_match(&mut self, subject: &Expr, cases: &[MatchCase]) -> CompileResult<()> { + fn compile_match( + &mut self, + subject: &ast::Expr, + cases: &[ast::MatchCase], + ) -> CompileResult<()> { self.enter_conditional_block(); let mut pattern_context = PatternContext::new(); self.compile_match_inner(subject, cases, &mut pattern_context)?; @@ -5824,21 +5833,21 @@ impl Compiler { } /// [CPython `compiler_addcompare`](https://github.com/python/cpython/blob/627894459a84be3488a1789919679c997056a03c/Python/compile.c#L2880-L2924) - fn compile_addcompare(&mut self, op: &CmpOp) { + fn compile_addcompare(&mut self, op: &ast::CmpOp) { use bytecode::ComparisonOperator::*; match op { - CmpOp::Eq => emit!(self, Instruction::CompareOp { op: Equal }), - CmpOp::NotEq => emit!(self, Instruction::CompareOp { op: NotEqual }), - CmpOp::Lt => emit!(self, Instruction::CompareOp { op: Less }), - CmpOp::LtE => emit!(self, Instruction::CompareOp { op: LessOrEqual }), - CmpOp::Gt => emit!(self, Instruction::CompareOp { op: Greater }), - CmpOp::GtE => { + ast::CmpOp::Eq => emit!(self, Instruction::CompareOp { op: Equal }), + ast::CmpOp::NotEq => emit!(self, Instruction::CompareOp { op: NotEqual }), + ast::CmpOp::Lt => emit!(self, Instruction::CompareOp { op: Less }), + ast::CmpOp::LtE => emit!(self, Instruction::CompareOp { op: LessOrEqual }), + ast::CmpOp::Gt => emit!(self, Instruction::CompareOp { op: Greater }), + ast::CmpOp::GtE => { emit!(self, Instruction::CompareOp { op: GreaterOrEqual }) } - CmpOp::In => emit!(self, Instruction::ContainsOp(Invert::No)), - CmpOp::NotIn => emit!(self, Instruction::ContainsOp(Invert::Yes)), - CmpOp::Is => emit!(self, Instruction::IsOp(Invert::No)), - CmpOp::IsNot => emit!(self, Instruction::IsOp(Invert::Yes)), + ast::CmpOp::In => emit!(self, Instruction::ContainsOp(Invert::No)), + ast::CmpOp::NotIn => emit!(self, Instruction::ContainsOp(Invert::Yes)), + ast::CmpOp::Is => emit!(self, Instruction::IsOp(Invert::No)), + ast::CmpOp::IsNot => emit!(self, Instruction::IsOp(Invert::Yes)), } } @@ -5862,9 +5871,9 @@ impl Compiler { /// - [CPython `compiler_compare`](https://github.com/python/cpython/blob/627894459a84be3488a1789919679c997056a03c/Python/compile.c#L4678-L4717) fn compile_compare( &mut self, - left: &Expr, - ops: &[CmpOp], - comparators: &[Expr], + left: &ast::Expr, + ops: &[ast::CmpOp], + comparators: &[ast::Expr], ) -> CompileResult<()> { let (last_op, mid_ops) = ops.split_last().unwrap(); let (last_comparator, mid_comparators) = comparators.split_last().unwrap(); @@ -5917,7 +5926,7 @@ impl Compiler { Ok(()) } - fn compile_annotation(&mut self, annotation: &Expr) -> CompileResult<()> { + fn compile_annotation(&mut self, annotation: &ast::Expr) -> CompileResult<()> { if self.future_annotations { self.emit_load_const(ConstantData::Str { value: UnparseExpr::new(annotation, &self.source_file) @@ -5930,7 +5939,7 @@ impl Compiler { // Special handling for starred annotations (*Ts -> Unpack[Ts]) let result = match annotation { - Expr::Starred(ExprStarred { value, .. }) => { + ast::Expr::Starred(ast::ExprStarred { value, .. }) => { // *args: *Ts (where Ts is a TypeVarTuple). // Do [annotation_value] = [*Ts]. self.compile_expression(value)?; @@ -5948,9 +5957,9 @@ impl Compiler { fn compile_annotated_assign( &mut self, - target: &Expr, - annotation: &Expr, - value: Option<&Expr>, + target: &ast::Expr, + annotation: &ast::Expr, + value: Option<&ast::Expr>, simple: bool, ) -> CompileResult<()> { // Perform the actual assignment first @@ -5962,7 +5971,7 @@ impl Compiler { // If we have a simple name in module or class scope, store annotation if simple && !self.ctx.in_func() - && let Expr::Name(ExprName { id, .. }) = target + && let ast::Expr::Name(ast::ExprName { id, .. }) = target { if self.future_annotations { // PEP 563: Store stringified annotation directly to __annotations__ @@ -6011,25 +6020,26 @@ impl Compiler { Ok(()) } - fn compile_store(&mut self, target: &Expr) -> CompileResult<()> { + fn compile_store(&mut self, target: &ast::Expr) -> CompileResult<()> { match &target { - Expr::Name(ExprName { id, .. }) => self.store_name(id.as_str())?, - Expr::Subscript(ExprSubscript { + ast::Expr::Name(ast::ExprName { id, .. }) => self.store_name(id.as_str())?, + ast::Expr::Subscript(ast::ExprSubscript { value, slice, ctx, .. }) => { self.compile_subscript(value, slice, *ctx)?; } - Expr::Attribute(ExprAttribute { value, attr, .. }) => { + ast::Expr::Attribute(ast::ExprAttribute { value, attr, .. }) => { self.compile_expression(value)?; let idx = self.name(attr.as_str()); emit!(self, Instruction::StoreAttr { idx }); } - Expr::List(ExprList { elts, .. }) | Expr::Tuple(ExprTuple { elts, .. }) => { + ast::Expr::List(ast::ExprList { elts, .. }) + | ast::Expr::Tuple(ast::ExprTuple { elts, .. }) => { let mut seen_star = false; // Scan for star args: for (i, element) in elts.iter().enumerate() { - if let Expr::Starred(_) = &element { + if let ast::Expr::Starred(_) = &element { if seen_star { return Err(self.error(CodegenErrorType::MultipleStarArgs)); } else { @@ -6059,7 +6069,7 @@ impl Compiler { } for element in elts { - if let Expr::Starred(ExprStarred { value, .. }) = &element { + if let ast::Expr::Starred(ast::ExprStarred { value, .. }) = &element { self.compile_store(value)?; } else { self.compile_store(element)?; @@ -6068,7 +6078,7 @@ impl Compiler { } _ => { return Err(self.error(match target { - Expr::Starred(_) => CodegenErrorType::SyntaxError( + ast::Expr::Starred(_) => CodegenErrorType::SyntaxError( "starred assignment target must be in a list or tuple".to_owned(), ), _ => CodegenErrorType::Assign(target.python_name()), @@ -6081,9 +6091,9 @@ impl Compiler { fn compile_augassign( &mut self, - target: &Expr, - op: &Operator, - value: &Expr, + target: &ast::Expr, + op: &ast::Operator, + value: &ast::Expr, ) -> CompileResult<()> { enum AugAssignKind<'a> { Name { id: &'a str }, @@ -6092,12 +6102,12 @@ impl Compiler { } let kind = match &target { - Expr::Name(ExprName { id, .. }) => { + ast::Expr::Name(ast::ExprName { id, .. }) => { let id = id.as_str(); self.compile_name(id, NameUsage::Load)?; AugAssignKind::Name { id } } - Expr::Subscript(ExprSubscript { + ast::Expr::Subscript(ast::ExprSubscript { value, slice, ctx: _, @@ -6112,7 +6122,7 @@ impl Compiler { emit!(self, Instruction::Subscript); AugAssignKind::Subscript } - Expr::Attribute(ExprAttribute { value, attr, .. }) => { + ast::Expr::Attribute(ast::ExprAttribute { value, attr, .. }) => { let attr = attr.as_str(); self.compile_expression(value)?; emit!(self, Instruction::Copy { index: 1_u32 }); @@ -6149,21 +6159,21 @@ impl Compiler { Ok(()) } - fn compile_op(&mut self, op: &Operator, inplace: bool) { + fn compile_op(&mut self, op: &ast::Operator, inplace: bool) { let bin_op = match op { - Operator::Add => BinaryOperator::Add, - Operator::Sub => BinaryOperator::Subtract, - Operator::Mult => BinaryOperator::Multiply, - Operator::MatMult => BinaryOperator::MatrixMultiply, - Operator::Div => BinaryOperator::TrueDivide, - Operator::FloorDiv => BinaryOperator::FloorDivide, - Operator::Mod => BinaryOperator::Remainder, - Operator::Pow => BinaryOperator::Power, - Operator::LShift => BinaryOperator::Lshift, - Operator::RShift => BinaryOperator::Rshift, - Operator::BitOr => BinaryOperator::Or, - Operator::BitXor => BinaryOperator::Xor, - Operator::BitAnd => BinaryOperator::And, + ast::Operator::Add => BinaryOperator::Add, + ast::Operator::Sub => BinaryOperator::Subtract, + ast::Operator::Mult => BinaryOperator::Multiply, + ast::Operator::MatMult => BinaryOperator::MatrixMultiply, + ast::Operator::Div => BinaryOperator::TrueDivide, + ast::Operator::FloorDiv => BinaryOperator::FloorDivide, + ast::Operator::Mod => BinaryOperator::Remainder, + ast::Operator::Pow => BinaryOperator::Power, + ast::Operator::LShift => BinaryOperator::Lshift, + ast::Operator::RShift => BinaryOperator::Rshift, + ast::Operator::BitOr => BinaryOperator::Or, + ast::Operator::BitXor => BinaryOperator::Xor, + ast::Operator::BitAnd => BinaryOperator::And, }; let op = if inplace { bin_op.as_inplace() } else { bin_op }; @@ -6180,15 +6190,15 @@ impl Compiler { /// (indicated by the condition parameter). fn compile_jump_if( &mut self, - expression: &Expr, + expression: &ast::Expr, condition: bool, target_block: BlockIdx, ) -> CompileResult<()> { // Compile expression for test, and jump to label if false match &expression { - Expr::BoolOp(ExprBoolOp { op, values, .. }) => { + ast::Expr::BoolOp(ast::ExprBoolOp { op, values, .. }) => { match op { - BoolOp::And => { + ast::BoolOp::And => { if condition { // If all values are true. let end_block = self.new_block(); @@ -6209,7 +6219,7 @@ impl Compiler { } } } - BoolOp::Or => { + ast::BoolOp::Or => { if condition { // If any of the values is true. for value in values { @@ -6232,8 +6242,8 @@ impl Compiler { } } } - Expr::UnaryOp(ExprUnaryOp { - op: UnaryOp::Not, + ast::Expr::UnaryOp(ast::ExprUnaryOp { + op: ast::UnaryOp::Not, operand, .. }) => { @@ -6264,7 +6274,7 @@ impl Compiler { /// Compile a boolean operation as an expression. /// This means, that the last value remains on the stack. - fn compile_bool_op(&mut self, op: &BoolOp, values: &[Expr]) -> CompileResult<()> { + fn compile_bool_op(&mut self, op: &ast::BoolOp, values: &[ast::Expr]) -> CompileResult<()> { let after_block = self.new_block(); let (last_value, values) = values.split_last().unwrap(); @@ -6274,7 +6284,7 @@ impl Compiler { emit!(self, Instruction::Copy { index: 1_u32 }); match op { - BoolOp::And => { + ast::BoolOp::And => { emit!( self, Instruction::PopJumpIfFalse { @@ -6282,7 +6292,7 @@ impl Compiler { } ); } - BoolOp::Or => { + ast::BoolOp::Or => { emit!( self, Instruction::PopJumpIfTrue { @@ -6301,7 +6311,7 @@ impl Compiler { Ok(()) } - fn compile_dict(&mut self, items: &[DictItem]) -> CompileResult<()> { + fn compile_dict(&mut self, items: &[ast::DictItem]) -> CompileResult<()> { // FIXME: correct order to build map, etc d = {**a, 'key': 2} should override // 'key' in dict a let mut size = 0; @@ -6395,18 +6405,19 @@ impl Compiler { Ok(()) } - fn compile_expression(&mut self, expression: &Expr) -> CompileResult<()> { - use ruff_python_ast::*; + fn compile_expression(&mut self, expression: &ast::Expr) -> CompileResult<()> { trace!("Compiling {expression:?}"); let range = expression.range(); self.set_source_range(range); match &expression { - Expr::Call(ExprCall { + ast::Expr::Call(ast::ExprCall { func, arguments, .. }) => self.compile_call(func, arguments)?, - Expr::BoolOp(ExprBoolOp { op, values, .. }) => self.compile_bool_op(op, values)?, - Expr::BinOp(ExprBinOp { + ast::Expr::BoolOp(ast::ExprBoolOp { op, values, .. }) => { + self.compile_bool_op(op, values)? + } + ast::Expr::BinOp(ast::ExprBinOp { left, op, right, .. }) => { self.compile_expression(left)?; @@ -6415,31 +6426,31 @@ impl Compiler { // Perform operation: self.compile_op(op, false); } - Expr::Subscript(ExprSubscript { + ast::Expr::Subscript(ast::ExprSubscript { value, slice, ctx, .. }) => { self.compile_subscript(value, slice, *ctx)?; } - Expr::UnaryOp(ExprUnaryOp { op, operand, .. }) => { + ast::Expr::UnaryOp(ast::ExprUnaryOp { op, operand, .. }) => { self.compile_expression(operand)?; // Perform operation: match op { - UnaryOp::UAdd => emit!( + ast::UnaryOp::UAdd => emit!( self, Instruction::CallIntrinsic1 { func: bytecode::IntrinsicFunction1::UnaryPositive } ), - UnaryOp::USub => emit!(self, Instruction::UnaryNegative), - UnaryOp::Not => { + ast::UnaryOp::USub => emit!(self, Instruction::UnaryNegative), + ast::UnaryOp::Not => { emit!(self, Instruction::ToBool); emit!(self, Instruction::UnaryNot); } - UnaryOp::Invert => emit!(self, Instruction::UnaryInvert), + ast::UnaryOp::Invert => emit!(self, Instruction::UnaryInvert), }; } - Expr::Attribute(ExprAttribute { value, attr, .. }) => { + ast::Expr::Attribute(ast::ExprAttribute { value, attr, .. }) => { // Check for super() attribute access optimization if let Some(super_type) = self.can_optimize_super_call(value, attr.as_str()) { // super().attr or super(cls, self).attr optimization @@ -6463,7 +6474,7 @@ impl Compiler { emit!(self, Instruction::LoadAttr { idx }); } } - Expr::Compare(ExprCompare { + ast::Expr::Compare(ast::ExprCompare { left, ops, comparators, @@ -6471,25 +6482,25 @@ impl Compiler { }) => { self.compile_compare(left, ops, comparators)?; } - // Expr::Constant(ExprConstant { value, .. }) => { + // ast::Expr::Constant(ExprConstant { value, .. }) => { // self.emit_load_const(compile_constant(value)); // } - Expr::List(ExprList { elts, .. }) => { + ast::Expr::List(ast::ExprList { elts, .. }) => { self.starunpack_helper(elts, 0, CollectionType::List)?; } - Expr::Tuple(ExprTuple { elts, .. }) => { + ast::Expr::Tuple(ast::ExprTuple { elts, .. }) => { self.starunpack_helper(elts, 0, CollectionType::Tuple)?; } - Expr::Set(ExprSet { elts, .. }) => { + ast::Expr::Set(ast::ExprSet { elts, .. }) => { self.starunpack_helper(elts, 0, CollectionType::Set)?; } - Expr::Dict(ExprDict { items, .. }) => { + ast::Expr::Dict(ast::ExprDict { items, .. }) => { self.compile_dict(items)?; } - Expr::Slice(ExprSlice { + ast::Expr::Slice(ast::ExprSlice { lower, upper, step, .. }) => { - let mut compile_bound = |bound: Option<&Expr>| match bound { + let mut compile_bound = |bound: Option<&ast::Expr>| match bound { Some(exp) => self.compile_expression(exp), None => { self.emit_load_const(ConstantData::None); @@ -6507,7 +6518,7 @@ impl Compiler { }; emit!(self, Instruction::BuildSlice { argc }); } - Expr::Yield(ExprYield { value, .. }) => { + ast::Expr::Yield(ast::ExprYield { value, .. }) => { if !self.ctx.in_func() { return Err(self.error(CodegenErrorType::InvalidYield)); } @@ -6525,7 +6536,7 @@ impl Compiler { } ); } - Expr::Await(ExprAwait { value, .. }) => { + ast::Expr::Await(ast::ExprAwait { value, .. }) => { if self.ctx.func != FunctionContext::AsyncFunction { return Err(self.error(CodegenErrorType::InvalidAwait)); } @@ -6534,7 +6545,7 @@ impl Compiler { self.emit_load_const(ConstantData::None); self.compile_yield_from_sequence(true)?; } - Expr::YieldFrom(ExprYieldFrom { value, .. }) => { + ast::Expr::YieldFrom(ast::ExprYieldFrom { value, .. }) => { match self.ctx.func { FunctionContext::NoFunction => { return Err(self.error(CodegenErrorType::InvalidYieldFrom)); @@ -6550,11 +6561,11 @@ impl Compiler { self.emit_load_const(ConstantData::None); self.compile_yield_from_sequence(false)?; } - Expr::Name(ExprName { id, .. }) => self.load_name(id.as_str())?, - Expr::Lambda(ExprLambda { + ast::Expr::Name(ast::ExprName { id, .. }) => self.load_name(id.as_str())?, + ast::Expr::Lambda(ast::ExprLambda { parameters, body, .. }) => { - let default_params = Parameters::default(); + let default_params = ast::Parameters::default(); let params = parameters.as_deref().unwrap_or(&default_params); validate_duplicate_params(params).map_err(|e| self.error(e))?; @@ -6633,7 +6644,7 @@ impl Compiler { self.ctx = prev_ctx; } - Expr::ListComp(ExprListComp { + ast::Expr::ListComp(ast::ExprListComp { elt, generators, .. }) => { self.compile_comprehension( @@ -6659,7 +6670,7 @@ impl Compiler { Self::contains_await(elt) || Self::generators_contain_await(generators), )?; } - Expr::SetComp(ExprSetComp { + ast::Expr::SetComp(ast::ExprSetComp { elt, generators, .. }) => { self.compile_comprehension( @@ -6685,7 +6696,7 @@ impl Compiler { Self::contains_await(elt) || Self::generators_contain_await(generators), )?; } - Expr::DictComp(ExprDictComp { + ast::Expr::DictComp(ast::ExprDictComp { key, value, generators, @@ -6720,7 +6731,7 @@ impl Compiler { || Self::generators_contain_await(generators), )?; } - Expr::Generator(ExprGenerator { + ast::Expr::Generator(ast::ExprGenerator { elt, generators, .. }) => { // Check if element or generators contain async content @@ -6754,7 +6765,7 @@ impl Compiler { element_contains_await, )?; } - Expr::Starred(ExprStarred { value, .. }) => { + ast::Expr::Starred(ast::ExprStarred { value, .. }) => { if self.in_annotation { // In annotation context, starred expressions are allowed (PEP 646) // For now, just compile the inner value without wrapping with Unpack @@ -6764,7 +6775,7 @@ impl Compiler { return Err(self.error(CodegenErrorType::InvalidStarExpr)); } } - Expr::If(ExprIf { + ast::Expr::If(ast::ExprIf { test, body, orelse, .. }) => { let else_block = self.new_block(); @@ -6788,7 +6799,7 @@ impl Compiler { self.switch_to_block(after_block); } - Expr::Named(ExprNamed { + ast::Expr::Named(ast::ExprNamed { target, value, node_index: _, @@ -6798,13 +6809,13 @@ impl Compiler { emit!(self, Instruction::Copy { index: 1_u32 }); self.compile_store(target)?; } - Expr::FString(fstring) => { + ast::Expr::FString(fstring) => { self.compile_expr_fstring(fstring)?; } - Expr::TString(tstring) => { + ast::Expr::TString(tstring) => { self.compile_expr_tstring(tstring)?; } - Expr::StringLiteral(string) => { + ast::Expr::StringLiteral(string) => { let value = string.value.to_str(); if value.contains(char::REPLACEMENT_CHARACTER) { let value = string @@ -6823,42 +6834,42 @@ impl Compiler { }); } } - Expr::BytesLiteral(bytes) => { + ast::Expr::BytesLiteral(bytes) => { let iter = bytes.value.iter().flat_map(|x| x.iter().copied()); let v: Vec = iter.collect(); self.emit_load_const(ConstantData::Bytes { value: v }); } - Expr::NumberLiteral(number) => match &number.value { - Number::Int(int) => { + ast::Expr::NumberLiteral(number) => match &number.value { + ast::Number::Int(int) => { let value = ruff_int_to_bigint(int).map_err(|e| self.error(e))?; self.emit_load_const(ConstantData::Integer { value }); } - Number::Float(float) => { + ast::Number::Float(float) => { self.emit_load_const(ConstantData::Float { value: *float }); } - Number::Complex { real, imag } => { + ast::Number::Complex { real, imag } => { self.emit_load_const(ConstantData::Complex { value: Complex::new(*real, *imag), }); } }, - Expr::BooleanLiteral(b) => { + ast::Expr::BooleanLiteral(b) => { self.emit_load_const(ConstantData::Boolean { value: b.value }); } - Expr::NoneLiteral(_) => { + ast::Expr::NoneLiteral(_) => { self.emit_load_const(ConstantData::None); } - Expr::EllipsisLiteral(_) => { + ast::Expr::EllipsisLiteral(_) => { self.emit_load_const(ConstantData::Ellipsis); } - Expr::IpyEscapeCommand(_) => { + ast::Expr::IpyEscapeCommand(_) => { panic!("unexpected ipy escape command"); } } Ok(()) } - fn compile_keywords(&mut self, keywords: &[Keyword]) -> CompileResult<()> { + fn compile_keywords(&mut self, keywords: &[ast::Keyword]) -> CompileResult<()> { let mut size = 0; let groupby = keywords.iter().chunk_by(|e| e.arg.is_none()); for (is_unpacking, sub_keywords) in &groupby { @@ -6888,10 +6899,10 @@ impl Compiler { Ok(()) } - fn compile_call(&mut self, func: &Expr, args: &Arguments) -> CompileResult<()> { + fn compile_call(&mut self, func: &ast::Expr, args: &ast::Arguments) -> CompileResult<()> { // Method call: obj → LOAD_ATTR_METHOD → [method, self_or_null] → args → CALL // Regular call: func → PUSH_NULL → args → CALL - if let Expr::Attribute(ExprAttribute { value, attr, .. }) = &func { + if let ast::Expr::Attribute(ast::ExprAttribute { value, attr, .. }) = &func { // Check for super() method call optimization if let Some(super_type) = self.can_optimize_super_call(value, attr.as_str()) { // super().method() or super(cls, self).method() optimization @@ -6930,7 +6941,7 @@ impl Compiler { fn compile_call_helper( &mut self, additional_positional: u32, - arguments: &Arguments, + arguments: &ast::Arguments, ) -> CompileResult<()> { let args_count = u32::try_from(arguments.len()).expect("too many arguments"); let count = args_count @@ -6992,9 +7003,13 @@ impl Compiler { // Given a vector of expr / star expr generate code which gives either // a list of expressions on the stack, or a list of tuples. - fn gather_elements(&mut self, before: u32, elements: &[Expr]) -> CompileResult<(u32, bool)> { + fn gather_elements( + &mut self, + before: u32, + elements: &[ast::Expr], + ) -> CompileResult<(u32, bool)> { // First determine if we have starred elements: - let has_stars = elements.iter().any(|e| matches!(e, Expr::Starred(_))); + let has_stars = elements.iter().any(|e| matches!(e, ast::Expr::Starred(_))); let size = if has_stars { let mut size = 0; @@ -7002,14 +7017,17 @@ impl Compiler { let mut run_size = before; loop { - if iter.peek().is_none_or(|e| matches!(e, Expr::Starred(_))) { + if iter + .peek() + .is_none_or(|e| matches!(e, ast::Expr::Starred(_))) + { emit!(self, Instruction::BuildTuple { size: run_size }); run_size = 0; size += 1; } match iter.next() { - Some(Expr::Starred(ExprStarred { value, .. })) => { + Some(ast::Expr::Starred(ast::ExprStarred { value, .. })) => { self.compile_expression(value)?; // We need to collect each unpacked element into a // tuple, since any side-effects during the conversion @@ -7037,7 +7055,7 @@ impl Compiler { Ok((size, has_stars)) } - fn compile_comprehension_element(&mut self, element: &Expr) -> CompileResult<()> { + fn compile_comprehension_element(&mut self, element: &ast::Expr) -> CompileResult<()> { self.compile_expression(element).map_err(|e| { if let CodegenErrorType::InvalidStarExpr = e.error { self.error(CodegenErrorType::SyntaxError( @@ -7053,7 +7071,7 @@ impl Compiler { &mut self, name: &str, init_collection: Option, - generators: &[Comprehension], + generators: &[ast::Comprehension], compile_element: &dyn Fn(&mut Self) -> CompileResult<()>, comprehension_type: ComprehensionType, element_contains_await: bool, @@ -7244,25 +7262,25 @@ impl Compiler { } /// Collect variable names from an assignment target expression - fn collect_target_names(&self, target: &Expr, names: &mut Vec) { + fn collect_target_names(&self, target: &ast::Expr, names: &mut Vec) { match target { - Expr::Name(name) => { + ast::Expr::Name(name) => { let name_str = name.id.to_string(); if !names.contains(&name_str) { names.push(name_str); } } - Expr::Tuple(tuple) => { + ast::Expr::Tuple(tuple) => { for elt in &tuple.elts { self.collect_target_names(elt, names); } } - Expr::List(list) => { + ast::Expr::List(list) => { for elt in &list.elts { self.collect_target_names(elt, names); } } - Expr::Starred(starred) => { + ast::Expr::Starred(starred) => { self.collect_target_names(&starred.value, names); } _ => { @@ -7276,7 +7294,7 @@ impl Compiler { fn compile_inlined_comprehension( &mut self, init_collection: Option, - generators: &[Comprehension], + generators: &[ast::Comprehension], compile_element: &dyn Fn(&mut Self) -> CompileResult<()>, _has_an_async_gen: bool, ) -> CompileResult<()> { @@ -7461,7 +7479,7 @@ impl Compiler { Ok(()) } - fn compile_future_features(&mut self, features: &[Alias]) -> Result<(), CodegenError> { + fn compile_future_features(&mut self, features: &[ast::Alias]) -> Result<(), CodegenError> { if let DoneWithFuture::Yes = self.done_with_future_stmts { return Err(self.error(CodegenErrorType::InvalidFuturePlacement)); } @@ -7809,26 +7827,28 @@ impl Compiler { /// async for: ... /// ``` /// are statements, so we won't check for them here - fn contains_await(expression: &Expr) -> bool { + fn contains_await(expression: &ast::Expr) -> bool { + use ast::visitor::Visitor; + #[derive(Default)] struct AwaitVisitor { found: bool, } - impl Visitor<'_> for AwaitVisitor { - fn visit_expr(&mut self, expr: &Expr) { + impl ast::visitor::Visitor<'_> for AwaitVisitor { + fn visit_expr(&mut self, expr: &ast::Expr) { if self.found { return; } match expr { - Expr::Await(_) => self.found = true, + ast::Expr::Await(_) => self.found = true, // Note: We do NOT check for async comprehensions here. // Async list/set/dict comprehensions are handled by compile_comprehension // which already awaits the result. A generator expression containing // an async comprehension as its element does NOT become an async generator, // because the async comprehension is awaited when evaluating the element. - _ => walk_expr(self, expr), + _ => ast::visitor::walk_expr(self, expr), } } } @@ -7840,7 +7860,7 @@ impl Compiler { /// Check if any of the generators (except the first one's iter) contains an await expression. /// The first generator's iter is evaluated outside the comprehension scope. - fn generators_contain_await(generators: &[Comprehension]) -> bool { + fn generators_contain_await(generators: &[ast::Comprehension]) -> bool { for (i, generator) in generators.iter().enumerate() { // First generator's iter is evaluated outside the comprehension if i > 0 && Self::contains_await(&generator.iter) { @@ -7856,7 +7876,7 @@ impl Compiler { false } - fn compile_expr_fstring(&mut self, fstring: &ExprFString) -> CompileResult<()> { + fn compile_expr_fstring(&mut self, fstring: &ast::ExprFString) -> CompileResult<()> { let fstring = &fstring.value; for part in fstring { self.compile_fstring_part(part)?; @@ -7873,9 +7893,9 @@ impl Compiler { Ok(()) } - fn compile_fstring_part(&mut self, part: &FStringPart) -> CompileResult<()> { + fn compile_fstring_part(&mut self, part: &ast::FStringPart) -> CompileResult<()> { match part { - FStringPart::Literal(string) => { + ast::FStringPart::Literal(string) => { if string.value.contains(char::REPLACEMENT_CHARACTER) { // might have a surrogate literal; should reparse to be sure let source = self.source_file.slice(string.range); @@ -7891,24 +7911,24 @@ impl Compiler { } Ok(()) } - FStringPart::FString(fstring) => self.compile_fstring(fstring), + ast::FStringPart::FString(fstring) => self.compile_fstring(fstring), } } - fn compile_fstring(&mut self, fstring: &FString) -> CompileResult<()> { + fn compile_fstring(&mut self, fstring: &ast::FString) -> CompileResult<()> { self.compile_fstring_elements(fstring.flags, &fstring.elements) } fn compile_fstring_elements( &mut self, - flags: FStringFlags, - fstring_elements: &InterpolatedStringElements, + flags: ast::FStringFlags, + fstring_elements: &ast::InterpolatedStringElements, ) -> CompileResult<()> { let mut element_count = 0; for element in fstring_elements { element_count += 1; match element { - InterpolatedStringElement::Literal(string) => { + ast::InterpolatedStringElement::Literal(string) => { if string.value.contains(char::REPLACEMENT_CHARACTER) { // might have a surrogate literal; should reparse to be sure let source = self.source_file.slice(string.range); @@ -7925,15 +7945,15 @@ impl Compiler { }); } } - InterpolatedStringElement::Interpolation(fstring_expr) => { + ast::InterpolatedStringElement::Interpolation(fstring_expr) => { let mut conversion = match fstring_expr.conversion { - ConversionFlag::None => ConvertValueOparg::None, - ConversionFlag::Str => ConvertValueOparg::Str, - ConversionFlag::Repr => ConvertValueOparg::Repr, - ConversionFlag::Ascii => ConvertValueOparg::Ascii, + ast::ConversionFlag::None => ConvertValueOparg::None, + ast::ConversionFlag::Str => ConvertValueOparg::Str, + ast::ConversionFlag::Repr => ConvertValueOparg::Repr, + ast::ConversionFlag::Ascii => ConvertValueOparg::Ascii, }; - if let Some(DebugText { leading, trailing }) = &fstring_expr.debug_text { + if let Some(ast::DebugText { leading, trailing }) = &fstring_expr.debug_text { let range = fstring_expr.expression.range(); let source = self.source_file.slice(range); let text = [leading, source, trailing].concat(); @@ -7993,9 +8013,9 @@ impl Compiler { Ok(()) } - fn compile_expr_tstring(&mut self, expr_tstring: &ExprTString) -> CompileResult<()> { - // TStringValue can contain multiple TString parts (implicit concatenation) - // Each TString part should be compiled and the results merged into a single Template + fn compile_expr_tstring(&mut self, expr_tstring: &ast::ExprTString) -> CompileResult<()> { + // ast::TStringValue can contain multiple ast::TString parts (implicit concatenation) + // Each ast::TString part should be compiled and the results merged into a single Template let tstring_value = &expr_tstring.value; // Collect all strings and compile all interpolations @@ -8044,18 +8064,18 @@ impl Compiler { fn compile_tstring_into( &mut self, - tstring: &TString, + tstring: &ast::TString, strings: &mut Vec, current_string: &mut Wtf8Buf, interp_count: &mut u32, ) -> CompileResult<()> { for element in &tstring.elements { match element { - InterpolatedStringElement::Literal(lit) => { + ast::InterpolatedStringElement::Literal(lit) => { // Accumulate literal parts into current_string current_string.push_str(&lit.value); } - InterpolatedStringElement::Interpolation(interp) => { + ast::InterpolatedStringElement::Interpolation(interp) => { // Finish current string segment strings.push(std::mem::take(current_string)); @@ -8071,19 +8091,19 @@ impl Compiler { // Determine conversion code let conversion: u32 = match interp.conversion { - ConversionFlag::None => 0, - ConversionFlag::Str => 1, - ConversionFlag::Repr => 2, - ConversionFlag::Ascii => 3, + ast::ConversionFlag::None => 0, + ast::ConversionFlag::Str => 1, + ast::ConversionFlag::Repr => 2, + ast::ConversionFlag::Ascii => 3, }; // Handle format_spec let has_format_spec = interp.format_spec.is_some(); if let Some(format_spec) = &interp.format_spec { // Compile format_spec as a string using fstring element compilation - // Use default FStringFlags since format_spec syntax is independent of t-string flags + // Use default ast::FStringFlags since format_spec syntax is independent of t-string flags self.compile_fstring_elements( - FStringFlags::empty(), + ast::FStringFlags::empty(), &format_spec.elements, )?; } @@ -8193,12 +8213,12 @@ fn expandtabs(input: &str, tab_size: usize) -> String { expanded_str } -fn split_doc<'a>(body: &'a [Stmt], opts: &CompileOpts) -> (Option, &'a [Stmt]) { - if let Some((Stmt::Expr(expr), body_rest)) = body.split_first() { +fn split_doc<'a>(body: &'a [ast::Stmt], opts: &CompileOpts) -> (Option, &'a [ast::Stmt]) { + if let Some((ast::Stmt::Expr(expr), body_rest)) = body.split_first() { let doc_comment = match &*expr.value { - Expr::StringLiteral(value) => Some(&value.value), + ast::Expr::StringLiteral(value) => Some(&value.value), // f-strings are not allowed in Python doc comments. - Expr::FString(_) => None, + ast::Expr::FString(_) => None, _ => None, }; if let Some(doc) = doc_comment { @@ -8212,7 +8232,7 @@ fn split_doc<'a>(body: &'a [Stmt], opts: &CompileOpts) -> (Option, &'a [ (None, body) } -pub fn ruff_int_to_bigint(int: &Int) -> Result { +pub fn ruff_int_to_bigint(int: &ast::Int) -> Result { if let Some(small) = int.as_u64() { Ok(BigInt::from(small)) } else { @@ -8222,7 +8242,7 @@ pub fn ruff_int_to_bigint(int: &Int) -> Result { /// Converts a `ruff` ast integer into a `BigInt`. /// Unlike small integers, big integers may be stored in one of four possible radix representations. -fn parse_big_integer(int: &Int) -> Result { +fn parse_big_integer(int: &ast::Int) -> Result { // TODO: Improve ruff API // Can we avoid this copy? let s = format!("{int}"); @@ -8265,35 +8285,34 @@ impl ToU32 for usize { #[cfg(test)] mod ruff_tests { use super::*; - use ruff_python_ast::name::Name; - use ruff_python_ast::*; + use ast::name::Name; /// Test if the compiler can correctly identify fstrings containing an `await` expression. #[test] fn test_fstring_contains_await() { let range = TextRange::default(); - let flags = FStringFlags::empty(); + let flags = ast::FStringFlags::empty(); // f'{x}' - let expr_x = Expr::Name(ExprName { - node_index: AtomicNodeIndex::NONE, + let expr_x = ast::Expr::Name(ast::ExprName { + node_index: ast::AtomicNodeIndex::NONE, range, id: Name::new("x"), - ctx: ExprContext::Load, + ctx: ast::ExprContext::Load, }); - let not_present = &Expr::FString(ExprFString { - node_index: AtomicNodeIndex::NONE, + let not_present = &ast::Expr::FString(ast::ExprFString { + node_index: ast::AtomicNodeIndex::NONE, range, - value: FStringValue::single(FString { - node_index: AtomicNodeIndex::NONE, + value: ast::FStringValue::single(ast::FString { + node_index: ast::AtomicNodeIndex::NONE, range, - elements: vec![InterpolatedStringElement::Interpolation( - InterpolatedElement { - node_index: AtomicNodeIndex::NONE, + elements: vec![ast::InterpolatedStringElement::Interpolation( + ast::InterpolatedElement { + node_index: ast::AtomicNodeIndex::NONE, range, expression: Box::new(expr_x), debug_text: None, - conversion: ConversionFlag::None, + conversion: ast::ConversionFlag::None, format_spec: None, }, )] @@ -8304,29 +8323,29 @@ mod ruff_tests { assert!(!Compiler::contains_await(not_present)); // f'{await x}' - let expr_await_x = Expr::Await(ExprAwait { - node_index: AtomicNodeIndex::NONE, + let expr_await_x = ast::Expr::Await(ast::ExprAwait { + node_index: ast::AtomicNodeIndex::NONE, range, - value: Box::new(Expr::Name(ExprName { - node_index: AtomicNodeIndex::NONE, + value: Box::new(ast::Expr::Name(ast::ExprName { + node_index: ast::AtomicNodeIndex::NONE, range, id: Name::new("x"), - ctx: ExprContext::Load, + ctx: ast::ExprContext::Load, })), }); - let present = &Expr::FString(ExprFString { - node_index: AtomicNodeIndex::NONE, + let present = &ast::Expr::FString(ast::ExprFString { + node_index: ast::AtomicNodeIndex::NONE, range, - value: FStringValue::single(FString { - node_index: AtomicNodeIndex::NONE, + value: ast::FStringValue::single(ast::FString { + node_index: ast::AtomicNodeIndex::NONE, range, - elements: vec![InterpolatedStringElement::Interpolation( - InterpolatedElement { - node_index: AtomicNodeIndex::NONE, + elements: vec![ast::InterpolatedStringElement::Interpolation( + ast::InterpolatedElement { + node_index: ast::AtomicNodeIndex::NONE, range, expression: Box::new(expr_await_x), debug_text: None, - conversion: ConversionFlag::None, + conversion: ast::ConversionFlag::None, format_spec: None, }, )] @@ -8337,45 +8356,45 @@ mod ruff_tests { assert!(Compiler::contains_await(present)); // f'{x:{await y}}' - let expr_x = Expr::Name(ExprName { - node_index: AtomicNodeIndex::NONE, + let expr_x = ast::Expr::Name(ast::ExprName { + node_index: ast::AtomicNodeIndex::NONE, range, id: Name::new("x"), - ctx: ExprContext::Load, + ctx: ast::ExprContext::Load, }); - let expr_await_y = Expr::Await(ExprAwait { - node_index: AtomicNodeIndex::NONE, + let expr_await_y = ast::Expr::Await(ast::ExprAwait { + node_index: ast::AtomicNodeIndex::NONE, range, - value: Box::new(Expr::Name(ExprName { - node_index: AtomicNodeIndex::NONE, + value: Box::new(ast::Expr::Name(ast::ExprName { + node_index: ast::AtomicNodeIndex::NONE, range, id: Name::new("y"), - ctx: ExprContext::Load, + ctx: ast::ExprContext::Load, })), }); - let present = &Expr::FString(ExprFString { - node_index: AtomicNodeIndex::NONE, + let present = &ast::Expr::FString(ast::ExprFString { + node_index: ast::AtomicNodeIndex::NONE, range, - value: FStringValue::single(FString { - node_index: AtomicNodeIndex::NONE, + value: ast::FStringValue::single(ast::FString { + node_index: ast::AtomicNodeIndex::NONE, range, - elements: vec![InterpolatedStringElement::Interpolation( - InterpolatedElement { - node_index: AtomicNodeIndex::NONE, + elements: vec![ast::InterpolatedStringElement::Interpolation( + ast::InterpolatedElement { + node_index: ast::AtomicNodeIndex::NONE, range, expression: Box::new(expr_x), debug_text: None, - conversion: ConversionFlag::None, - format_spec: Some(Box::new(InterpolatedStringFormatSpec { - node_index: AtomicNodeIndex::NONE, + conversion: ast::ConversionFlag::None, + format_spec: Some(Box::new(ast::InterpolatedStringFormatSpec { + node_index: ast::AtomicNodeIndex::NONE, range, - elements: vec![InterpolatedStringElement::Interpolation( - InterpolatedElement { - node_index: AtomicNodeIndex::NONE, + elements: vec![ast::InterpolatedStringElement::Interpolation( + ast::InterpolatedElement { + node_index: ast::AtomicNodeIndex::NONE, range, expression: Box::new(expr_await_y), debug_text: None, - conversion: ConversionFlag::None, + conversion: ast::ConversionFlag::None, format_spec: None, }, )] diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index 34d3870ae91..9dd7384170a 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -18,7 +18,7 @@ pub mod symboltable; mod unparse; pub use compile::CompileOpts; -use ruff_python_ast::Expr; +use ruff_python_ast as ast; pub(crate) use compile::InternalResult; @@ -27,7 +27,7 @@ pub trait ToPythonName { fn python_name(&self) -> &'static str; } -impl ToPythonName for Expr { +impl ToPythonName for ast::Expr { fn python_name(&self) -> &'static str { match self { Self::BoolOp { .. } | Self::BinOp { .. } | Self::UnaryOp { .. } => "operator", diff --git a/crates/codegen/src/string_parser.rs b/crates/codegen/src/string_parser.rs index 175e75c1a26..7e1558d2b17 100644 --- a/crates/codegen/src/string_parser.rs +++ b/crates/codegen/src/string_parser.rs @@ -7,7 +7,7 @@ use core::convert::Infallible; -use ruff_python_ast::{AnyStringFlags, StringFlags}; +use ruff_python_ast::{self as ast, StringFlags as _}; use rustpython_wtf8::{CodePoint, Wtf8, Wtf8Buf}; // use ruff_python_parser::{LexicalError, LexicalErrorType}; @@ -24,11 +24,11 @@ struct StringParser { /// Current position of the parser in the source. cursor: usize, /// Flags that can be used to query information about the string. - flags: AnyStringFlags, + flags: ast::AnyStringFlags, } impl StringParser { - const fn new(source: Box, flags: AnyStringFlags) -> Self { + const fn new(source: Box, flags: ast::AnyStringFlags) -> Self { Self { source, cursor: 0, @@ -272,7 +272,7 @@ impl StringParser { } } -pub(crate) fn parse_string_literal(source: &str, flags: AnyStringFlags) -> Box { +pub(crate) fn parse_string_literal(source: &str, flags: ast::AnyStringFlags) -> Box { let source = &source[flags.opener_len().to_usize()..]; let source = &source[..source.len() - flags.quote_len().to_usize()]; StringParser::new(source.into(), flags) @@ -280,7 +280,10 @@ pub(crate) fn parse_string_literal(source: &str, flags: AnyStringFlags) -> Box, flags: AnyStringFlags) -> Box { +pub(crate) fn parse_fstring_literal_element( + source: Box, + flags: ast::AnyStringFlags, +) -> Box { StringParser::new(source, flags) .parse_fstring_middle() .unwrap_or_else(|x| match x {}) diff --git a/crates/codegen/src/symboltable.rs b/crates/codegen/src/symboltable.rs index 63b330db92f..129900133fa 100644 --- a/crates/codegen/src/symboltable.rs +++ b/crates/codegen/src/symboltable.rs @@ -13,12 +13,7 @@ use crate::{ }; use alloc::{borrow::Cow, fmt}; use bitflags::bitflags; -use ruff_python_ast::{ - self as ast, Comprehension, Decorator, Expr, Identifier, ModExpression, ModModule, Parameter, - ParameterWithDefault, Parameters, Pattern, PatternMatchAs, PatternMatchClass, - PatternMatchMapping, PatternMatchOr, PatternMatchSequence, PatternMatchStar, PatternMatchValue, - Stmt, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams, -}; +use ruff_python_ast as ast; use ruff_text_size::{Ranged, TextRange}; use rustpython_compiler_core::{PositionEncoding, SourceFile, SourceLocation}; use std::collections::HashSet; @@ -97,13 +92,19 @@ impl SymbolTable { } } - pub fn scan_program(program: &ModModule, source_file: SourceFile) -> SymbolTableResult { + pub fn scan_program( + program: &ast::ModModule, + source_file: SourceFile, + ) -> SymbolTableResult { let mut builder = SymbolTableBuilder::new(source_file); builder.scan_statements(program.body.as_ref())?; builder.finish() } - pub fn scan_expr(expr: &ModExpression, source_file: SourceFile) -> SymbolTableResult { + pub fn scan_expr( + expr: &ast::ModExpression, + source_file: SourceFile, + ) -> SymbolTableResult { let mut builder = SymbolTableBuilder::new(source_file); builder.scan_expression(expr.body.as_ref(), ExpressionContext::Load)?; builder.finish() @@ -973,21 +974,21 @@ impl SymbolTableBuilder { .get() as _ } - fn scan_statements(&mut self, statements: &[Stmt]) -> SymbolTableResult { + fn scan_statements(&mut self, statements: &[ast::Stmt]) -> SymbolTableResult { for statement in statements { self.scan_statement(statement)?; } Ok(()) } - fn scan_parameters(&mut self, parameters: &[ParameterWithDefault]) -> SymbolTableResult { + fn scan_parameters(&mut self, parameters: &[ast::ParameterWithDefault]) -> SymbolTableResult { for parameter in parameters { self.scan_parameter(¶meter.parameter)?; } Ok(()) } - fn scan_parameter(&mut self, parameter: &Parameter) -> SymbolTableResult { + fn scan_parameter(&mut self, parameter: &ast::Parameter) -> SymbolTableResult { self.check_name( parameter.name.as_str(), ExpressionContext::Store, @@ -1019,7 +1020,7 @@ impl SymbolTableBuilder { self.register_ident(¶meter.name, usage) } - fn scan_annotation(&mut self, annotation: &Expr) -> SymbolTableResult { + fn scan_annotation(&mut self, annotation: &ast::Expr) -> SymbolTableResult { let current_scope = self.tables.last().map(|t| t.typ); // PEP 649: Check if this is a conditional annotation @@ -1074,8 +1075,8 @@ impl SymbolTableBuilder { result } - fn scan_statement(&mut self, statement: &Stmt) -> SymbolTableResult { - use ruff_python_ast::*; + fn scan_statement(&mut self, statement: &ast::Stmt) -> SymbolTableResult { + use ast::*; if let Stmt::ImportFrom(StmtImportFrom { module, names, .. }) = &statement && module.as_ref().map(|id| id.as_str()) == Some("__future__") { @@ -1435,7 +1436,7 @@ impl SymbolTableBuilder { fn scan_decorators( &mut self, - decorators: &[Decorator], + decorators: &[ast::Decorator], context: ExpressionContext, ) -> SymbolTableResult { for decorator in decorators { @@ -1446,7 +1447,7 @@ impl SymbolTableBuilder { fn scan_expressions( &mut self, - expressions: &[Expr], + expressions: &[ast::Expr], context: ExpressionContext, ) -> SymbolTableResult { for expression in expressions { @@ -1457,10 +1458,10 @@ impl SymbolTableBuilder { fn scan_expression( &mut self, - expression: &Expr, + expression: &ast::Expr, context: ExpressionContext, ) -> SymbolTableResult { - use ruff_python_ast::*; + use ast::*; // Check for expressions not allowed in certain contexts // (type parameters, annotations, type aliases, TypeVar bounds/defaults) @@ -1837,9 +1838,9 @@ impl SymbolTableBuilder { fn scan_comprehension( &mut self, scope_name: &str, - elt1: &Expr, - elt2: Option<&Expr>, - generators: &[Comprehension], + elt1: &ast::Expr, + elt2: Option<&ast::Expr>, + generators: &[ast::Comprehension], range: TextRange, is_generator: bool, ) -> SymbolTableResult { @@ -1906,7 +1907,7 @@ impl SymbolTableBuilder { // = symtable_visit_type_param_bound_or_default fn scan_type_param_bound_or_default( &mut self, - expr: &Expr, + expr: &ast::Expr, scope_name: &str, scope_info: &'static str, ) -> SymbolTableResult { @@ -1932,14 +1933,14 @@ impl SymbolTableBuilder { result } - fn scan_type_params(&mut self, type_params: &TypeParams) -> SymbolTableResult { + fn scan_type_params(&mut self, type_params: &ast::TypeParams) -> SymbolTableResult { // Check for duplicate type parameter names let mut seen_names: std::collections::HashSet<&str> = std::collections::HashSet::new(); for type_param in &type_params.type_params { let (name, range) = match type_param { - TypeParam::TypeVar(tv) => (tv.name.as_str(), tv.range), - TypeParam::ParamSpec(ps) => (ps.name.as_str(), ps.range), - TypeParam::TypeVarTuple(tvt) => (tvt.name.as_str(), tvt.range), + ast::TypeParam::TypeVar(tv) => (tv.name.as_str(), tv.range), + ast::TypeParam::ParamSpec(ps) => (ps.name.as_str(), ps.range), + ast::TypeParam::TypeVarTuple(tvt) => (tvt.name.as_str(), tvt.range), }; if !seen_names.insert(name) { return Err(SymbolTableError { @@ -1959,7 +1960,7 @@ impl SymbolTableBuilder { // First register all type parameters for type_param in &type_params.type_params { match type_param { - TypeParam::TypeVar(TypeParamTypeVar { + ast::TypeParam::TypeVar(ast::TypeParamTypeVar { name, bound, range: type_var_range, @@ -1991,7 +1992,7 @@ impl SymbolTableBuilder { )?; } } - TypeParam::ParamSpec(TypeParamParamSpec { + ast::TypeParam::ParamSpec(ast::TypeParamParamSpec { name, range: param_spec_range, default, @@ -2009,7 +2010,7 @@ impl SymbolTableBuilder { )?; } } - TypeParam::TypeVarTuple(TypeParamTypeVarTuple { + ast::TypeParam::TypeVarTuple(ast::TypeParamTypeVarTuple { name, range: type_var_tuple_range, default, @@ -2032,22 +2033,24 @@ impl SymbolTableBuilder { Ok(()) } - fn scan_patterns(&mut self, patterns: &[Pattern]) -> SymbolTableResult { + fn scan_patterns(&mut self, patterns: &[ast::Pattern]) -> SymbolTableResult { for pattern in patterns { self.scan_pattern(pattern)?; } Ok(()) } - fn scan_pattern(&mut self, pattern: &Pattern) -> SymbolTableResult { - use Pattern::*; + fn scan_pattern(&mut self, pattern: &ast::Pattern) -> SymbolTableResult { + use ast::Pattern::*; match pattern { - MatchValue(PatternMatchValue { value, .. }) => { + MatchValue(ast::PatternMatchValue { value, .. }) => { self.scan_expression(value, ExpressionContext::Load)? } MatchSingleton(_) => {} - MatchSequence(PatternMatchSequence { patterns, .. }) => self.scan_patterns(patterns)?, - MatchMapping(PatternMatchMapping { + MatchSequence(ast::PatternMatchSequence { patterns, .. }) => { + self.scan_patterns(patterns)? + } + MatchMapping(ast::PatternMatchMapping { keys, patterns, rest, @@ -2059,19 +2062,19 @@ impl SymbolTableBuilder { self.register_ident(rest, SymbolUsage::Assigned)?; } } - MatchClass(PatternMatchClass { cls, arguments, .. }) => { + MatchClass(ast::PatternMatchClass { cls, arguments, .. }) => { self.scan_expression(cls, ExpressionContext::Load)?; self.scan_patterns(&arguments.patterns)?; for kw in &arguments.keywords { self.scan_pattern(&kw.pattern)?; } } - MatchStar(PatternMatchStar { name, .. }) => { + MatchStar(ast::PatternMatchStar { name, .. }) => { if let Some(name) = name { self.register_ident(name, SymbolUsage::Assigned)?; } } - MatchAs(PatternMatchAs { pattern, name, .. }) => { + MatchAs(ast::PatternMatchAs { pattern, name, .. }) => { if let Some(pattern) = pattern { self.scan_pattern(pattern)?; } @@ -2079,7 +2082,7 @@ impl SymbolTableBuilder { self.register_ident(name, SymbolUsage::Assigned)?; } } - MatchOr(PatternMatchOr { patterns, .. }) => self.scan_patterns(patterns)?, + MatchOr(ast::PatternMatchOr { patterns, .. }) => self.scan_patterns(patterns)?, } Ok(()) } @@ -2087,7 +2090,7 @@ impl SymbolTableBuilder { fn enter_scope_with_parameters( &mut self, name: &str, - parameters: &Parameters, + parameters: &ast::Parameters, line_number: u32, has_return_annotation: bool, ) -> SymbolTableResult { @@ -2174,7 +2177,7 @@ impl SymbolTableBuilder { Ok(()) } - fn register_ident(&mut self, ident: &Identifier, role: SymbolUsage) -> SymbolTableResult { + fn register_ident(&mut self, ident: &ast::Identifier, role: SymbolUsage) -> SymbolTableResult { self.register_name(ident.as_str(), role, ident.range) } diff --git a/crates/codegen/src/unparse.rs b/crates/codegen/src/unparse.rs index 7b26d229187..849544ab946 100644 --- a/crates/codegen/src/unparse.rs +++ b/crates/codegen/src/unparse.rs @@ -1,9 +1,6 @@ use alloc::fmt; use core::fmt::Display as _; -use ruff_python_ast::{ - self as ruff, Arguments, BoolOp, Comprehension, ConversionFlag, Expr, Identifier, Operator, - Parameter, ParameterWithDefault, Parameters, -}; +use ruff_python_ast as ast; use ruff_text_size::Ranged; use rustpython_compiler_core::SourceFile; use rustpython_literal::escape::{AsciiEscape, UnicodeEscape}; @@ -40,7 +37,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.f.write_str(s) } - fn p_id(&mut self, s: &Identifier) -> fmt::Result { + fn p_id(&mut self, s: &ast::Identifier) -> fmt::Result { self.f.write_str(s.as_str()) } @@ -59,7 +56,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.f.write_fmt(f) } - fn unparse_expr(&mut self, ast: &Expr, level: u8) -> fmt::Result { + fn unparse_expr(&mut self, ast: &ast::Expr, level: u8) -> fmt::Result { macro_rules! op_prec { ($op_ty:ident, $x:expr, $enu:path, $($var:ident($op:literal, $prec:ident)),*$(,)?) => { match $x { @@ -83,13 +80,13 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { }}; } match &ast { - Expr::BoolOp(ruff::ExprBoolOp { + ast::Expr::BoolOp(ast::ExprBoolOp { op, values, node_index: _, range: _range, }) => { - let (op, prec) = op_prec!(bin, op, BoolOp, And("and", AND), Or("or", OR)); + let (op, prec) = op_prec!(bin, op, ast::BoolOp, And("and", AND), Or("or", OR)); group_if!(prec, { let mut first = true; for val in values { @@ -98,7 +95,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { } }) } - Expr::Named(ruff::ExprNamed { + ast::Expr::Named(ast::ExprNamed { target, value, node_index: _, @@ -110,18 +107,18 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.unparse_expr(value, precedence::ATOM)?; }) } - Expr::BinOp(ruff::ExprBinOp { + ast::Expr::BinOp(ast::ExprBinOp { left, op, right, node_index: _, range: _range, }) => { - let right_associative = matches!(op, Operator::Pow); + let right_associative = matches!(op, ast::Operator::Pow); let (op, prec) = op_prec!( bin, op, - Operator, + ast::Operator, Add("+", ARITH), Sub("-", ARITH), Mult("*", TERM), @@ -142,7 +139,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.unparse_expr(right, prec + !right_associative as u8)?; }) } - Expr::UnaryOp(ruff::ExprUnaryOp { + ast::Expr::UnaryOp(ast::ExprUnaryOp { op, operand, node_index: _, @@ -151,7 +148,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { let (op, prec) = op_prec!( un, op, - ruff::UnaryOp, + ast::UnaryOp, Invert("~", FACTOR), Not("not ", NOT), UAdd("+", FACTOR), @@ -162,7 +159,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.unparse_expr(operand, prec)?; }) } - Expr::Lambda(ruff::ExprLambda { + ast::Expr::Lambda(ast::ExprLambda { parameters, body, node_index: _, @@ -178,7 +175,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { write!(self, ": {}", UnparseExpr::new(body, self.source))?; }) } - Expr::If(ruff::ExprIf { + ast::Expr::If(ast::ExprIf { test, body, orelse, @@ -193,7 +190,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.unparse_expr(orelse, precedence::TEST)?; }) } - Expr::Dict(ruff::ExprDict { + ast::Expr::Dict(ast::ExprDict { items, node_index: _, range: _range, @@ -211,7 +208,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { } self.p("}")?; } - Expr::Set(ruff::ExprSet { + ast::Expr::Set(ast::ExprSet { elts, node_index: _, range: _range, @@ -224,7 +221,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { } self.p("}")?; } - Expr::ListComp(ruff::ExprListComp { + ast::Expr::ListComp(ast::ExprListComp { elt, generators, node_index: _, @@ -235,7 +232,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.unparse_comp(generators)?; self.p("]")?; } - Expr::SetComp(ruff::ExprSetComp { + ast::Expr::SetComp(ast::ExprSetComp { elt, generators, node_index: _, @@ -246,7 +243,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.unparse_comp(generators)?; self.p("}")?; } - Expr::DictComp(ruff::ExprDictComp { + ast::Expr::DictComp(ast::ExprDictComp { key, value, generators, @@ -260,7 +257,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.unparse_comp(generators)?; self.p("}")?; } - Expr::Generator(ruff::ExprGenerator { + ast::Expr::Generator(ast::ExprGenerator { parenthesized: _, elt, generators, @@ -272,7 +269,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.unparse_comp(generators)?; self.p(")")?; } - Expr::Await(ruff::ExprAwait { + ast::Expr::Await(ast::ExprAwait { value, node_index: _, range: _range, @@ -282,7 +279,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.unparse_expr(value, precedence::ATOM)?; }) } - Expr::Yield(ruff::ExprYield { + ast::Expr::Yield(ast::ExprYield { value, node_index: _, range: _range, @@ -293,7 +290,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.p("(yield)")?; } } - Expr::YieldFrom(ruff::ExprYieldFrom { + ast::Expr::YieldFrom(ast::ExprYieldFrom { value, node_index: _, range: _range, @@ -304,7 +301,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { UnparseExpr::new(value, self.source) )?; } - Expr::Compare(ruff::ExprCompare { + ast::Expr::Compare(ast::ExprCompare { left, ops, comparators, @@ -322,9 +319,9 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { } }) } - Expr::Call(ruff::ExprCall { + ast::Expr::Call(ast::ExprCall { func, - arguments: Arguments { args, keywords, .. }, + arguments: ast::Arguments { args, keywords, .. }, node_index: _, range: _range, }) => { @@ -332,7 +329,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.p("(")?; if let ( [ - Expr::Generator(ruff::ExprGenerator { + ast::Expr::Generator(ast::ExprGenerator { elt, generators, node_index: _, @@ -365,9 +362,9 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { } self.p(")")?; } - Expr::FString(ruff::ExprFString { value, .. }) => self.unparse_fstring(value)?, - Expr::TString(_) => self.p("t\"\"")?, - Expr::StringLiteral(ruff::ExprStringLiteral { value, .. }) => { + ast::Expr::FString(ast::ExprFString { value, .. }) => self.unparse_fstring(value)?, + ast::Expr::TString(_) => self.p("t\"\"")?, + ast::Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) => { if value.is_unicode() { self.p("u")? } @@ -375,12 +372,12 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { .str_repr() .fmt(self.f)? } - Expr::BytesLiteral(ruff::ExprBytesLiteral { value, .. }) => { + ast::Expr::BytesLiteral(ast::ExprBytesLiteral { value, .. }) => { AsciiEscape::new_repr(&value.bytes().collect::>()) .bytes_repr() .fmt(self.f)? } - Expr::NumberLiteral(ruff::ExprNumberLiteral { value, .. }) => { + ast::Expr::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => { #[allow(clippy::correctness, clippy::assertions_on_constants)] const { assert!(f64::MAX_10_EXP == 308) @@ -388,28 +385,28 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { let inf_str = "1e309"; match value { - ruff::Number::Int(int) => int.fmt(self.f)?, - &ruff::Number::Float(fp) => { + ast::Number::Int(int) => int.fmt(self.f)?, + &ast::Number::Float(fp) => { if fp.is_infinite() { self.p(inf_str)? } else { self.p(&rustpython_literal::float::to_string(fp))? } } - &ruff::Number::Complex { real, imag } => self + &ast::Number::Complex { real, imag } => self .p(&rustpython_literal::complex::to_string(real, imag) .replace("inf", inf_str))?, } } - Expr::BooleanLiteral(ruff::ExprBooleanLiteral { value, .. }) => { + ast::Expr::BooleanLiteral(ast::ExprBooleanLiteral { value, .. }) => { self.p(if *value { "True" } else { "False" })? } - Expr::NoneLiteral(ruff::ExprNoneLiteral { .. }) => self.p("None")?, - Expr::EllipsisLiteral(ruff::ExprEllipsisLiteral { .. }) => self.p("...")?, - Expr::Attribute(ruff::ExprAttribute { value, attr, .. }) => { + ast::Expr::NoneLiteral(ast::ExprNoneLiteral { .. }) => self.p("None")?, + ast::Expr::EllipsisLiteral(ast::ExprEllipsisLiteral { .. }) => self.p("...")?, + ast::Expr::Attribute(ast::ExprAttribute { value, attr, .. }) => { self.unparse_expr(value, precedence::ATOM)?; - let period = if let Expr::NumberLiteral(ruff::ExprNumberLiteral { - value: ruff::Number::Int(_), + let period = if let ast::Expr::NumberLiteral(ast::ExprNumberLiteral { + value: ast::Number::Int(_), .. }) = value.as_ref() { @@ -420,19 +417,19 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.p(period)?; self.p_id(attr)?; } - Expr::Subscript(ruff::ExprSubscript { value, slice, .. }) => { + ast::Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => { self.unparse_expr(value, precedence::ATOM)?; let lvl = precedence::TUPLE; self.p("[")?; self.unparse_expr(slice, lvl)?; self.p("]")?; } - Expr::Starred(ruff::ExprStarred { value, .. }) => { + ast::Expr::Starred(ast::ExprStarred { value, .. }) => { self.p("*")?; self.unparse_expr(value, precedence::EXPR)?; } - Expr::Name(ruff::ExprName { id, .. }) => self.p(id.as_str())?, - Expr::List(ruff::ExprList { elts, .. }) => { + ast::Expr::Name(ast::ExprName { id, .. }) => self.p(id.as_str())?, + ast::Expr::List(ast::ExprList { elts, .. }) => { self.p("[")?; let mut first = true; for elt in elts { @@ -441,7 +438,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { } self.p("]")?; } - Expr::Tuple(ruff::ExprTuple { elts, .. }) => { + ast::Expr::Tuple(ast::ExprTuple { elts, .. }) => { if elts.is_empty() { self.p("()")?; } else { @@ -455,7 +452,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { }) } } - Expr::Slice(ruff::ExprSlice { + ast::Expr::Slice(ast::ExprSlice { lower, upper, step, @@ -474,12 +471,12 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.unparse_expr(step, precedence::TEST)?; } } - Expr::IpyEscapeCommand(_) => {} + ast::Expr::IpyEscapeCommand(_) => {} } Ok(()) } - fn unparse_arguments(&mut self, args: &Parameters) -> fmt::Result { + fn unparse_arguments(&mut self, args: &ast::Parameters) -> fmt::Result { let mut first = true; for (i, arg) in args.posonlyargs.iter().chain(&args.args).enumerate() { self.p_delim(&mut first, ", ")?; @@ -504,7 +501,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { } Ok(()) } - fn unparse_function_arg(&mut self, arg: &ParameterWithDefault) -> fmt::Result { + fn unparse_function_arg(&mut self, arg: &ast::ParameterWithDefault) -> fmt::Result { self.unparse_arg(&arg.parameter)?; if let Some(default) = &arg.default { write!(self, "={}", UnparseExpr::new(default, self.source))?; @@ -512,7 +509,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Ok(()) } - fn unparse_arg(&mut self, arg: &Parameter) -> fmt::Result { + fn unparse_arg(&mut self, arg: &ast::Parameter) -> fmt::Result { self.p_id(&arg.name)?; if let Some(ann) = &arg.annotation { write!(self, ": {}", UnparseExpr::new(ann, self.source))?; @@ -520,7 +517,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Ok(()) } - fn unparse_comp(&mut self, generators: &[Comprehension]) -> fmt::Result { + fn unparse_comp(&mut self, generators: &[ast::Comprehension]) -> fmt::Result { for comp in generators { self.p(if comp.is_async { " async for " @@ -538,10 +535,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Ok(()) } - fn unparse_fstring_body( - &mut self, - elements: &[ruff::InterpolatedStringElement], - ) -> fmt::Result { + fn unparse_fstring_body(&mut self, elements: &[ast::InterpolatedStringElement]) -> fmt::Result { for elem in elements { self.unparse_fstring_elem(elem)?; } @@ -550,15 +544,15 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { fn unparse_formatted( &mut self, - val: &Expr, - debug_text: Option<&ruff::DebugText>, - conversion: ConversionFlag, - spec: Option<&ruff::InterpolatedStringFormatSpec>, + val: &ast::Expr, + debug_text: Option<&ast::DebugText>, + conversion: ast::ConversionFlag, + spec: Option<&ast::InterpolatedStringFormatSpec>, ) -> fmt::Result { let buffered = to_string_fmt(|f| { Unparser::new(f, self.source).unparse_expr(val, precedence::TEST + 1) }); - if let Some(ruff::DebugText { leading, trailing }) = debug_text { + if let Some(ast::DebugText { leading, trailing }) = debug_text { self.p(leading)?; self.p(self.source.slice(val.range()))?; self.p(trailing)?; @@ -573,7 +567,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.p(&buffered)?; drop(buffered); - if conversion != ConversionFlag::None { + if conversion != ast::ConversionFlag::None { self.p("!")?; let buf = &[conversion as u8]; let c = core::str::from_utf8(buf).unwrap(); @@ -590,9 +584,9 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Ok(()) } - fn unparse_fstring_elem(&mut self, elem: &ruff::InterpolatedStringElement) -> fmt::Result { + fn unparse_fstring_elem(&mut self, elem: &ast::InterpolatedStringElement) -> fmt::Result { match elem { - ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement { + ast::InterpolatedStringElement::Interpolation(ast::InterpolatedElement { expression, debug_text, conversion, @@ -604,7 +598,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { *conversion, format_spec.as_deref(), ), - ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement { + ast::InterpolatedStringElement::Literal(ast::InterpolatedStringLiteralElement { value, .. }) => self.unparse_fstring_str(value), @@ -616,12 +610,12 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.p(&s) } - fn unparse_fstring(&mut self, value: &ruff::FStringValue) -> fmt::Result { + fn unparse_fstring(&mut self, value: &ast::FStringValue) -> fmt::Result { self.p("f")?; let body = to_string_fmt(|f| { value.iter().try_for_each(|part| match part { - ruff::FStringPart::Literal(lit) => f.write_str(lit), - ruff::FStringPart::FString(ruff::FString { elements, .. }) => { + ast::FStringPart::Literal(lit) => f.write_str(lit), + ast::FStringPart::FString(ast::FString { elements, .. }) => { Unparser::new(f, self.source).unparse_fstring_body(elements) } }) @@ -634,12 +628,12 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { } pub struct UnparseExpr<'a> { - expr: &'a Expr, + expr: &'a ast::Expr, source: &'a SourceFile, } impl<'a> UnparseExpr<'a> { - pub const fn new(expr: &'a Expr, source: &'a SourceFile) -> Self { + pub const fn new(expr: &'a ast::Expr, source: &'a SourceFile) -> Self { Self { expr, source } } } diff --git a/crates/vm/src/stdlib/ast.rs b/crates/vm/src/stdlib/ast.rs index 31aad306f96..116d239c033 100644 --- a/crates/vm/src/stdlib/ast.rs +++ b/crates/vm/src/stdlib/ast.rs @@ -19,7 +19,7 @@ use crate::{ convert::ToPyObject, }; use node::Node; -use ruff_python_ast as ruff; +use ruff_python_ast as ast; use ruff_text_size::{Ranged, TextRange, TextSize}; use rustpython_compiler_core::{ LineIndex, OneIndexed, PositionEncoding, SourceFile, SourceFileBuilder, SourceLocation, @@ -283,8 +283,8 @@ pub(crate) fn parse( })? .into_syntax(); let top = match top { - ruff::Mod::Module(m) => Mod::Module(m), - ruff::Mod::Expression(e) => Mod::Expression(e), + ast::Mod::Module(m) => Mod::Module(m), + ast::Mod::Expression(e) => Mod::Expression(e), }; Ok(top.ast_to_object(vm, &source_file)) } @@ -305,13 +305,13 @@ pub(crate) fn compile( let source_file = SourceFileBuilder::new(filename.to_owned(), "".to_owned()).finish(); let ast: Mod = Node::ast_from_object(vm, &source_file, object)?; let ast = match ast { - Mod::Module(m) => ruff::Mod::Module(m), - Mod::Interactive(ModInteractive { range, body }) => ruff::Mod::Module(ruff::ModModule { + Mod::Module(m) => ast::Mod::Module(m), + Mod::Interactive(ModInteractive { range, body }) => ast::Mod::Module(ast::ModModule { node_index: Default::default(), range, body, }), - Mod::Expression(e) => ruff::Mod::Expression(e), + Mod::Expression(e) => ast::Mod::Expression(e), Mod::FunctionType(_) => todo!(), }; // TODO: create a textual representation of the ast diff --git a/crates/vm/src/stdlib/ast/argument.rs b/crates/vm/src/stdlib/ast/argument.rs index a13200e6502..626024f5bd6 100644 --- a/crates/vm/src/stdlib/ast/argument.rs +++ b/crates/vm/src/stdlib/ast/argument.rs @@ -3,7 +3,7 @@ use rustpython_compiler_core::SourceFile; pub(super) struct PositionalArguments { pub range: TextRange, - pub args: Box<[ruff::Expr]>, + pub args: Box<[ast::Expr]>, } impl Node for PositionalArguments { @@ -27,7 +27,7 @@ impl Node for PositionalArguments { pub(super) struct KeywordArguments { pub range: TextRange, - pub keywords: Box<[ruff::Keyword]>, + pub keywords: Box<[ast::Keyword]>, } impl Node for KeywordArguments { @@ -53,10 +53,10 @@ impl Node for KeywordArguments { pub(super) fn merge_function_call_arguments( pos_args: PositionalArguments, key_args: KeywordArguments, -) -> ruff::Arguments { +) -> ast::Arguments { let range = pos_args.range.cover(key_args.range); - ruff::Arguments { + ast::Arguments { node_index: Default::default(), range, args: pos_args.args, @@ -65,9 +65,9 @@ pub(super) fn merge_function_call_arguments( } pub(super) fn split_function_call_arguments( - args: ruff::Arguments, + args: ast::Arguments, ) -> (PositionalArguments, KeywordArguments) { - let ruff::Arguments { + let ast::Arguments { node_index: _, range: _, args, @@ -100,13 +100,13 @@ pub(super) fn split_function_call_arguments( } pub(super) fn split_class_def_args( - args: Option>, + args: Option>, ) -> (Option, Option) { let args = match args { None => return (None, None), Some(args) => *args, }; - let ruff::Arguments { + let ast::Arguments { node_index: _, range: _, args, @@ -141,7 +141,7 @@ pub(super) fn split_class_def_args( pub(super) fn merge_class_def_args( positional_arguments: Option, keyword_arguments: Option, -) -> Option> { +) -> Option> { if positional_arguments.is_none() && keyword_arguments.is_none() { return None; } @@ -157,7 +157,7 @@ pub(super) fn merge_class_def_args( vec![].into_boxed_slice() }; - Some(Box::new(ruff::Arguments { + Some(Box::new(ast::Arguments { node_index: Default::default(), range: Default::default(), // TODO args, diff --git a/crates/vm/src/stdlib/ast/basic.rs b/crates/vm/src/stdlib/ast/basic.rs index d8565029d6c..612b6144eea 100644 --- a/crates/vm/src/stdlib/ast/basic.rs +++ b/crates/vm/src/stdlib/ast/basic.rs @@ -2,7 +2,7 @@ use super::*; use rustpython_codegen::compile::ruff_int_to_bigint; use rustpython_compiler_core::SourceFile; -impl Node for ruff::Identifier { +impl Node for ast::Identifier { fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { let id = self.as_str(); vm.ctx.new_str(id).into() @@ -18,7 +18,7 @@ impl Node for ruff::Identifier { } } -impl Node for ruff::Int { +impl Node for ast::Int { fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { vm.ctx.new_int(ruff_int_to_bigint(&self).unwrap()).into() } diff --git a/crates/vm/src/stdlib/ast/constant.rs b/crates/vm/src/stdlib/ast/constant.rs index 83b2a7f7015..a6aac224585 100644 --- a/crates/vm/src/stdlib/ast/constant.rs +++ b/crates/vm/src/stdlib/ast/constant.rs @@ -1,6 +1,6 @@ use super::*; use crate::builtins::{PyComplex, PyFrozenSet, PyTuple}; -use ruff::str_prefix::StringLiteralPrefix; +use ast::str_prefix::StringLiteralPrefix; use rustpython_compiler_core::SourceFile; #[derive(Debug)] @@ -22,7 +22,7 @@ impl Constant { } } - pub(super) const fn new_int(value: ruff::Int, range: TextRange) -> Self { + pub(super) const fn new_int(value: ast::Int, range: TextRange) -> Self { Self { range, value: ConstantLiteral::Int(value), @@ -71,7 +71,7 @@ impl Constant { } } - pub(crate) fn into_expr(self) -> ruff::Expr { + pub(crate) fn into_expr(self) -> ast::Expr { constant_to_ruff_expr(self) } } @@ -85,7 +85,7 @@ pub(crate) enum ConstantLiteral { prefix: StringLiteralPrefix, }, Bytes(Box<[u8]>), - Int(ruff::Int), + Int(ast::Int), Tuple(Vec), FrozenSet(Vec), Float(f64), @@ -244,48 +244,48 @@ impl Node for ConstantLiteral { } } -fn constant_to_ruff_expr(value: Constant) -> ruff::Expr { +fn constant_to_ruff_expr(value: Constant) -> ast::Expr { let Constant { value, range } = value; match value { - ConstantLiteral::None => ruff::Expr::NoneLiteral(ruff::ExprNoneLiteral { + ConstantLiteral::None => ast::Expr::NoneLiteral(ast::ExprNoneLiteral { node_index: Default::default(), range, }), - ConstantLiteral::Bool(value) => ruff::Expr::BooleanLiteral(ruff::ExprBooleanLiteral { + ConstantLiteral::Bool(value) => ast::Expr::BooleanLiteral(ast::ExprBooleanLiteral { node_index: Default::default(), range, value, }), ConstantLiteral::Str { value, prefix } => { - ruff::Expr::StringLiteral(ruff::ExprStringLiteral { + ast::Expr::StringLiteral(ast::ExprStringLiteral { node_index: Default::default(), range, - value: ruff::StringLiteralValue::single(ruff::StringLiteral { + value: ast::StringLiteralValue::single(ast::StringLiteral { node_index: Default::default(), range, value, - flags: ruff::StringLiteralFlags::empty().with_prefix(prefix), + flags: ast::StringLiteralFlags::empty().with_prefix(prefix), }), }) } ConstantLiteral::Bytes(value) => { - ruff::Expr::BytesLiteral(ruff::ExprBytesLiteral { + ast::Expr::BytesLiteral(ast::ExprBytesLiteral { node_index: Default::default(), range, - value: ruff::BytesLiteralValue::single(ruff::BytesLiteral { + value: ast::BytesLiteralValue::single(ast::BytesLiteral { node_index: Default::default(), range, value, - flags: ruff::BytesLiteralFlags::empty(), // TODO + flags: ast::BytesLiteralFlags::empty(), // TODO }), }) } - ConstantLiteral::Int(value) => ruff::Expr::NumberLiteral(ruff::ExprNumberLiteral { + ConstantLiteral::Int(value) => ast::Expr::NumberLiteral(ast::ExprNumberLiteral { node_index: Default::default(), range, - value: ruff::Number::Int(value), + value: ast::Number::Int(value), }), - ConstantLiteral::Tuple(value) => ruff::Expr::Tuple(ruff::ExprTuple { + ConstantLiteral::Tuple(value) => ast::Expr::Tuple(ast::ExprTuple { node_index: Default::default(), range, elts: value @@ -297,21 +297,21 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr { }) }) .collect(), - ctx: ruff::ExprContext::Load, + ctx: ast::ExprContext::Load, // TODO: Does this matter? parenthesized: true, }), - ConstantLiteral::FrozenSet(value) => ruff::Expr::Call(ruff::ExprCall { + ConstantLiteral::FrozenSet(value) => ast::Expr::Call(ast::ExprCall { node_index: Default::default(), range, // idk lol - func: Box::new(ruff::Expr::Name(ruff::ExprName { + func: Box::new(ast::Expr::Name(ast::ExprName { node_index: Default::default(), range: TextRange::default(), - id: ruff::name::Name::new_static("frozenset"), - ctx: ruff::ExprContext::Load, + id: ast::name::Name::new_static("frozenset"), + ctx: ast::ExprContext::Load, })), - arguments: ruff::Arguments { + arguments: ast::Arguments { node_index: Default::default(), range, args: value @@ -326,19 +326,19 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr { keywords: Box::default(), }, }), - ConstantLiteral::Float(value) => ruff::Expr::NumberLiteral(ruff::ExprNumberLiteral { + ConstantLiteral::Float(value) => ast::Expr::NumberLiteral(ast::ExprNumberLiteral { node_index: Default::default(), range, - value: ruff::Number::Float(value), + value: ast::Number::Float(value), }), ConstantLiteral::Complex { real, imag } => { - ruff::Expr::NumberLiteral(ruff::ExprNumberLiteral { + ast::Expr::NumberLiteral(ast::ExprNumberLiteral { node_index: Default::default(), range, - value: ruff::Number::Complex { real, imag }, + value: ast::Number::Complex { real, imag }, }) } - ConstantLiteral::Ellipsis => ruff::Expr::EllipsisLiteral(ruff::ExprEllipsisLiteral { + ConstantLiteral::Ellipsis => ast::Expr::EllipsisLiteral(ast::ExprEllipsisLiteral { node_index: Default::default(), range, }), @@ -348,17 +348,17 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr { pub(super) fn number_literal_to_object( vm: &VirtualMachine, source_file: &SourceFile, - constant: ruff::ExprNumberLiteral, + constant: ast::ExprNumberLiteral, ) -> PyObjectRef { - let ruff::ExprNumberLiteral { + let ast::ExprNumberLiteral { node_index: _, range, value, } = constant; let c = match value { - ruff::Number::Int(n) => Constant::new_int(n, range), - ruff::Number::Float(n) => Constant::new_float(n, range), - ruff::Number::Complex { real, imag } => Constant::new_complex(real, imag, range), + ast::Number::Int(n) => Constant::new_int(n, range), + ast::Number::Float(n) => Constant::new_float(n, range), + ast::Number::Complex { real, imag } => Constant::new_complex(real, imag, range), }; c.ast_to_object(vm, source_file) } @@ -366,9 +366,9 @@ pub(super) fn number_literal_to_object( pub(super) fn string_literal_to_object( vm: &VirtualMachine, source_file: &SourceFile, - constant: ruff::ExprStringLiteral, + constant: ast::ExprStringLiteral, ) -> PyObjectRef { - let ruff::ExprStringLiteral { + let ast::ExprStringLiteral { node_index: _, range, value, @@ -384,9 +384,9 @@ pub(super) fn string_literal_to_object( pub(super) fn bytes_literal_to_object( vm: &VirtualMachine, source_file: &SourceFile, - constant: ruff::ExprBytesLiteral, + constant: ast::ExprBytesLiteral, ) -> PyObjectRef { - let ruff::ExprBytesLiteral { + let ast::ExprBytesLiteral { node_index: _, range, value, @@ -399,9 +399,9 @@ pub(super) fn bytes_literal_to_object( pub(super) fn boolean_literal_to_object( vm: &VirtualMachine, source_file: &SourceFile, - constant: ruff::ExprBooleanLiteral, + constant: ast::ExprBooleanLiteral, ) -> PyObjectRef { - let ruff::ExprBooleanLiteral { + let ast::ExprBooleanLiteral { node_index: _, range, value, @@ -413,9 +413,9 @@ pub(super) fn boolean_literal_to_object( pub(super) fn none_literal_to_object( vm: &VirtualMachine, source_file: &SourceFile, - constant: ruff::ExprNoneLiteral, + constant: ast::ExprNoneLiteral, ) -> PyObjectRef { - let ruff::ExprNoneLiteral { + let ast::ExprNoneLiteral { node_index: _, range, } = constant; @@ -426,9 +426,9 @@ pub(super) fn none_literal_to_object( pub(super) fn ellipsis_literal_to_object( vm: &VirtualMachine, source_file: &SourceFile, - constant: ruff::ExprEllipsisLiteral, + constant: ast::ExprEllipsisLiteral, ) -> PyObjectRef { - let ruff::ExprEllipsisLiteral { + let ast::ExprEllipsisLiteral { node_index: _, range, } = constant; diff --git a/crates/vm/src/stdlib/ast/elif_else_clause.rs b/crates/vm/src/stdlib/ast/elif_else_clause.rs index e2a8789dd08..b27e956077e 100644 --- a/crates/vm/src/stdlib/ast/elif_else_clause.rs +++ b/crates/vm/src/stdlib/ast/elif_else_clause.rs @@ -2,12 +2,12 @@ use super::*; use rustpython_compiler_core::SourceFile; pub(super) fn ast_to_object( - clause: ruff::ElifElseClause, - mut rest: alloc::vec::IntoIter, + clause: ast::ElifElseClause, + mut rest: alloc::vec::IntoIter, vm: &VirtualMachine, source_file: &SourceFile, ) -> PyObjectRef { - let ruff::ElifElseClause { + let ast::ElifElseClause { node_index: _, range, test, @@ -48,18 +48,18 @@ pub(super) fn ast_from_object( vm: &VirtualMachine, source_file: &SourceFile, object: PyObjectRef, -) -> PyResult { +) -> PyResult { let test = Node::ast_from_object(vm, source_file, get_node_field(vm, &object, "test", "If")?)?; let body = Node::ast_from_object(vm, source_file, get_node_field(vm, &object, "body", "If")?)?; - let orelse: Vec = Node::ast_from_object( + let orelse: Vec = Node::ast_from_object( vm, source_file, get_node_field(vm, &object, "orelse", "If")?, )?; let range = range_from_object(vm, source_file, object, "If")?; - let elif_else_clauses = if let [ruff::Stmt::If(_)] = &*orelse { - let Some(ruff::Stmt::If(ruff::StmtIf { + let elif_else_clauses = if let [ast::Stmt::If(_)] = &*orelse { + let Some(ast::Stmt::If(ast::StmtIf { node_index: _, range, test, @@ -71,7 +71,7 @@ pub(super) fn ast_from_object( }; elif_else_clauses.insert( 0, - ruff::ElifElseClause { + ast::ElifElseClause { node_index: Default::default(), range, test: Some(*test), @@ -80,7 +80,7 @@ pub(super) fn ast_from_object( ); elif_else_clauses } else { - vec![ruff::ElifElseClause { + vec![ast::ElifElseClause { node_index: Default::default(), range, test: None, @@ -88,7 +88,7 @@ pub(super) fn ast_from_object( }] }; - Ok(ruff::StmtIf { + Ok(ast::StmtIf { node_index: Default::default(), test, body, diff --git a/crates/vm/src/stdlib/ast/exception.rs b/crates/vm/src/stdlib/ast/exception.rs index b5b3ca2709a..bdb8b7ad9ac 100644 --- a/crates/vm/src/stdlib/ast/exception.rs +++ b/crates/vm/src/stdlib/ast/exception.rs @@ -2,7 +2,7 @@ use super::*; use rustpython_compiler_core::SourceFile; // sum -impl Node for ruff::ExceptHandler { +impl Node for ast::ExceptHandler { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { Self::ExceptHandler(cons) => cons.ast_to_object(vm, source_file), @@ -16,7 +16,7 @@ impl Node for ruff::ExceptHandler { let _cls = _object.class(); Ok( if _cls.is(pyast::NodeExceptHandlerExceptHandler::static_type()) { - Self::ExceptHandler(ruff::ExceptHandlerExceptHandler::ast_from_object( + Self::ExceptHandler(ast::ExceptHandlerExceptHandler::ast_from_object( _vm, source_file, _object, @@ -32,7 +32,7 @@ impl Node for ruff::ExceptHandler { } // constructor -impl Node for ruff::ExceptHandlerExceptHandler { +impl Node for ast::ExceptHandlerExceptHandler { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, diff --git a/crates/vm/src/stdlib/ast/expression.rs b/crates/vm/src/stdlib/ast/expression.rs index c63d0e0df68..3bf1470795d 100644 --- a/crates/vm/src/stdlib/ast/expression.rs +++ b/crates/vm/src/stdlib/ast/expression.rs @@ -7,7 +7,7 @@ use crate::stdlib::ast::{ use rustpython_compiler_core::SourceFile; // sum -impl Node for ruff::Expr { +impl Node for ast::Expr { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { Self::BoolOp(cons) => cons.ast_to_object(vm, source_file), @@ -59,77 +59,69 @@ impl Node for ruff::Expr { ) -> PyResult { let cls = object.class(); Ok(if cls.is(pyast::NodeExprBoolOp::static_type()) { - Self::BoolOp(ruff::ExprBoolOp::ast_from_object(vm, source_file, object)?) + Self::BoolOp(ast::ExprBoolOp::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprNamedExpr::static_type()) { - Self::Named(ruff::ExprNamed::ast_from_object(vm, source_file, object)?) + Self::Named(ast::ExprNamed::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprBinOp::static_type()) { - Self::BinOp(ruff::ExprBinOp::ast_from_object(vm, source_file, object)?) + Self::BinOp(ast::ExprBinOp::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprUnaryOp::static_type()) { - Self::UnaryOp(ruff::ExprUnaryOp::ast_from_object(vm, source_file, object)?) + Self::UnaryOp(ast::ExprUnaryOp::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprLambda::static_type()) { - Self::Lambda(ruff::ExprLambda::ast_from_object(vm, source_file, object)?) + Self::Lambda(ast::ExprLambda::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprIfExp::static_type()) { - Self::If(ruff::ExprIf::ast_from_object(vm, source_file, object)?) + Self::If(ast::ExprIf::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprDict::static_type()) { - Self::Dict(ruff::ExprDict::ast_from_object(vm, source_file, object)?) + Self::Dict(ast::ExprDict::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprSet::static_type()) { - Self::Set(ruff::ExprSet::ast_from_object(vm, source_file, object)?) + Self::Set(ast::ExprSet::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprListComp::static_type()) { - Self::ListComp(ruff::ExprListComp::ast_from_object( - vm, - source_file, - object, - )?) + Self::ListComp(ast::ExprListComp::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprSetComp::static_type()) { - Self::SetComp(ruff::ExprSetComp::ast_from_object(vm, source_file, object)?) + Self::SetComp(ast::ExprSetComp::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprDictComp::static_type()) { - Self::DictComp(ruff::ExprDictComp::ast_from_object( - vm, - source_file, - object, - )?) + Self::DictComp(ast::ExprDictComp::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprGeneratorExp::static_type()) { - Self::Generator(ruff::ExprGenerator::ast_from_object( + Self::Generator(ast::ExprGenerator::ast_from_object( vm, source_file, object, )?) } else if cls.is(pyast::NodeExprAwait::static_type()) { - Self::Await(ruff::ExprAwait::ast_from_object(vm, source_file, object)?) + Self::Await(ast::ExprAwait::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprYield::static_type()) { - Self::Yield(ruff::ExprYield::ast_from_object(vm, source_file, object)?) + Self::Yield(ast::ExprYield::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprYieldFrom::static_type()) { - Self::YieldFrom(ruff::ExprYieldFrom::ast_from_object( + Self::YieldFrom(ast::ExprYieldFrom::ast_from_object( vm, source_file, object, )?) } else if cls.is(pyast::NodeExprCompare::static_type()) { - Self::Compare(ruff::ExprCompare::ast_from_object(vm, source_file, object)?) + Self::Compare(ast::ExprCompare::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprCall::static_type()) { - Self::Call(ruff::ExprCall::ast_from_object(vm, source_file, object)?) + Self::Call(ast::ExprCall::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprAttribute::static_type()) { - Self::Attribute(ruff::ExprAttribute::ast_from_object( + Self::Attribute(ast::ExprAttribute::ast_from_object( vm, source_file, object, )?) } else if cls.is(pyast::NodeExprSubscript::static_type()) { - Self::Subscript(ruff::ExprSubscript::ast_from_object( + Self::Subscript(ast::ExprSubscript::ast_from_object( vm, source_file, object, )?) } else if cls.is(pyast::NodeExprStarred::static_type()) { - Self::Starred(ruff::ExprStarred::ast_from_object(vm, source_file, object)?) + Self::Starred(ast::ExprStarred::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprName::static_type()) { - Self::Name(ruff::ExprName::ast_from_object(vm, source_file, object)?) + Self::Name(ast::ExprName::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprList::static_type()) { - Self::List(ruff::ExprList::ast_from_object(vm, source_file, object)?) + Self::List(ast::ExprList::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprTuple::static_type()) { - Self::Tuple(ruff::ExprTuple::ast_from_object(vm, source_file, object)?) + Self::Tuple(ast::ExprTuple::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprSlice::static_type()) { - Self::Slice(ruff::ExprSlice::ast_from_object(vm, source_file, object)?) + Self::Slice(ast::ExprSlice::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprConstant::static_type()) { Constant::ast_from_object(vm, source_file, object)?.into_expr() } else if cls.is(pyast::NodeExprJoinedStr::static_type()) { @@ -144,7 +136,7 @@ impl Node for ruff::Expr { } // constructor -impl Node for ruff::ExprBoolOp { +impl Node for ast::ExprBoolOp { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -187,7 +179,7 @@ impl Node for ruff::ExprBoolOp { } // constructor -impl Node for ruff::ExprNamed { +impl Node for ast::ExprNamed { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -230,7 +222,7 @@ impl Node for ruff::ExprNamed { } // constructor -impl Node for ruff::ExprBinOp { +impl Node for ast::ExprBinOp { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -281,7 +273,7 @@ impl Node for ruff::ExprBinOp { } // constructor -impl Node for ruff::ExprUnaryOp { +impl Node for ast::ExprUnaryOp { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -323,7 +315,7 @@ impl Node for ruff::ExprUnaryOp { } // constructor -impl Node for ruff::ExprLambda { +impl Node for ast::ExprLambda { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -366,7 +358,7 @@ impl Node for ruff::ExprLambda { } // constructor -impl Node for ruff::ExprIf { +impl Node for ast::ExprIf { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -417,7 +409,7 @@ impl Node for ruff::ExprIf { } // constructor -impl Node for ruff::ExprDict { +impl Node for ast::ExprDict { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -449,7 +441,7 @@ impl Node for ruff::ExprDict { source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { - let keys: Vec> = Node::ast_from_object( + let keys: Vec> = Node::ast_from_object( vm, source_file, get_node_field(vm, &object, "keys", "Dict")?, @@ -462,7 +454,7 @@ impl Node for ruff::ExprDict { let items = keys .into_iter() .zip(values) - .map(|(key, value)| ruff::DictItem { key, value }) + .map(|(key, value)| ast::DictItem { key, value }) .collect(); Ok(Self { node_index: Default::default(), @@ -473,7 +465,7 @@ impl Node for ruff::ExprDict { } // constructor -impl Node for ruff::ExprSet { +impl Node for ast::ExprSet { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -507,7 +499,7 @@ impl Node for ruff::ExprSet { } // constructor -impl Node for ruff::ExprListComp { +impl Node for ast::ExprListComp { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -550,7 +542,7 @@ impl Node for ruff::ExprListComp { } // constructor -impl Node for ruff::ExprSetComp { +impl Node for ast::ExprSetComp { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -593,7 +585,7 @@ impl Node for ruff::ExprSetComp { } // constructor -impl Node for ruff::ExprDictComp { +impl Node for ast::ExprDictComp { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -644,7 +636,7 @@ impl Node for ruff::ExprDictComp { } // constructor -impl Node for ruff::ExprGenerator { +impl Node for ast::ExprGenerator { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -690,7 +682,7 @@ impl Node for ruff::ExprGenerator { } // constructor -impl Node for ruff::ExprAwait { +impl Node for ast::ExprAwait { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -724,7 +716,7 @@ impl Node for ruff::ExprAwait { } // constructor -impl Node for ruff::ExprYield { +impl Node for ast::ExprYield { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -757,7 +749,7 @@ impl Node for ruff::ExprYield { } // constructor -impl Node for ruff::ExprYieldFrom { +impl Node for ast::ExprYieldFrom { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -792,7 +784,7 @@ impl Node for ruff::ExprYieldFrom { } // constructor -impl Node for ruff::ExprCompare { +impl Node for ast::ExprCompare { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -853,7 +845,7 @@ impl Node for ruff::ExprCompare { } // constructor -impl Node for ruff::ExprCall { +impl Node for ast::ExprCall { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -914,7 +906,7 @@ impl Node for ruff::ExprCall { } // constructor -impl Node for ruff::ExprAttribute { +impl Node for ast::ExprAttribute { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -965,7 +957,7 @@ impl Node for ruff::ExprAttribute { } // constructor -impl Node for ruff::ExprSubscript { +impl Node for ast::ExprSubscript { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1015,7 +1007,7 @@ impl Node for ruff::ExprSubscript { } // constructor -impl Node for ruff::ExprStarred { +impl Node for ast::ExprStarred { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1057,7 +1049,7 @@ impl Node for ruff::ExprStarred { } // constructor -impl Node for ruff::ExprName { +impl Node for ast::ExprName { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1095,7 +1087,7 @@ impl Node for ruff::ExprName { } // constructor -impl Node for ruff::ExprList { +impl Node for ast::ExprList { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1138,7 +1130,7 @@ impl Node for ruff::ExprList { } // constructor -impl Node for ruff::ExprTuple { +impl Node for ast::ExprTuple { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1183,7 +1175,7 @@ impl Node for ruff::ExprTuple { } // constructor -impl Node for ruff::ExprSlice { +impl Node for ast::ExprSlice { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1228,7 +1220,7 @@ impl Node for ruff::ExprSlice { } // sum -impl Node for ruff::ExprContext { +impl Node for ast::ExprContext { fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { let node_type = match self { Self::Load => pyast::NodeExprContextLoad::static_type(), @@ -1266,7 +1258,7 @@ impl Node for ruff::ExprContext { } // product -impl Node for ruff::Comprehension { +impl Node for ast::Comprehension { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, diff --git a/crates/vm/src/stdlib/ast/module.rs b/crates/vm/src/stdlib/ast/module.rs index 6fae8f10a33..78f897b8930 100644 --- a/crates/vm/src/stdlib/ast/module.rs +++ b/crates/vm/src/stdlib/ast/module.rs @@ -18,9 +18,9 @@ use rustpython_compiler_core::SourceFile; /// - `FunctionType`: A function signature with argument and return type /// annotations, representing the type hints of a function (e.g., `def add(x: int, y: int) -> int`). pub(super) enum Mod { - Module(ruff::ModModule), + Module(ast::ModModule), Interactive(ModInteractive), - Expression(ruff::ModExpression), + Expression(ast::ModExpression), FunctionType(ModFunctionType), } @@ -42,11 +42,11 @@ impl Node for Mod { ) -> PyResult { let cls = object.class(); Ok(if cls.is(pyast::NodeModModule::static_type()) { - Self::Module(ruff::ModModule::ast_from_object(vm, source_file, object)?) + Self::Module(ast::ModModule::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeModInteractive::static_type()) { Self::Interactive(ModInteractive::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeModExpression::static_type()) { - Self::Expression(ruff::ModExpression::ast_from_object( + Self::Expression(ast::ModExpression::ast_from_object( vm, source_file, object, @@ -63,7 +63,7 @@ impl Node for Mod { } // constructor -impl Node for ruff::ModModule { +impl Node for ast::ModModule { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -113,7 +113,7 @@ impl Node for ruff::ModModule { pub(super) struct ModInteractive { pub(crate) range: TextRange, - pub(crate) body: Vec, + pub(crate) body: Vec, } // constructor @@ -147,7 +147,7 @@ impl Node for ModInteractive { } // constructor -impl Node for ruff::ModExpression { +impl Node for ast::ModExpression { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -182,8 +182,8 @@ impl Node for ruff::ModExpression { } pub(super) struct ModFunctionType { - pub(crate) argtypes: Box<[ruff::Expr]>, - pub(crate) returns: ruff::Expr, + pub(crate) argtypes: Box<[ast::Expr]>, + pub(crate) returns: ast::Expr, pub(crate) range: TextRange, } diff --git a/crates/vm/src/stdlib/ast/operator.rs b/crates/vm/src/stdlib/ast/operator.rs index c394152da2c..23aa63c7031 100644 --- a/crates/vm/src/stdlib/ast/operator.rs +++ b/crates/vm/src/stdlib/ast/operator.rs @@ -2,7 +2,7 @@ use super::*; use rustpython_compiler_core::SourceFile; // sum -impl Node for ruff::BoolOp { +impl Node for ast::BoolOp { fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { let node_type = match self { Self::And => pyast::NodeBoolOpAnd::static_type(), @@ -34,7 +34,7 @@ impl Node for ruff::BoolOp { } // sum -impl Node for ruff::Operator { +impl Node for ast::Operator { fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { let node_type = match self { Self::Add => pyast::NodeOperatorAdd::static_type(), @@ -99,7 +99,7 @@ impl Node for ruff::Operator { } // sum -impl Node for ruff::UnaryOp { +impl Node for ast::UnaryOp { fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { let node_type = match self { Self::Invert => pyast::NodeUnaryOpInvert::static_type(), @@ -137,7 +137,7 @@ impl Node for ruff::UnaryOp { } // sum -impl Node for ruff::CmpOp { +impl Node for ast::CmpOp { fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { let node_type = match self { Self::Eq => pyast::NodeCmpOpEq::static_type(), diff --git a/crates/vm/src/stdlib/ast/other.rs b/crates/vm/src/stdlib/ast/other.rs index ce7d5fe4807..8a89a740682 100644 --- a/crates/vm/src/stdlib/ast/other.rs +++ b/crates/vm/src/stdlib/ast/other.rs @@ -1,7 +1,7 @@ use super::*; use rustpython_compiler_core::SourceFile; -impl Node for ruff::ConversionFlag { +impl Node for ast::ConversionFlag { fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { vm.ctx.new_int(self as u8).into() } @@ -24,7 +24,7 @@ impl Node for ruff::ConversionFlag { } // /// This is just a string, not strictly an AST node. But it makes AST conversions easier. -impl Node for ruff::name::Name { +impl Node for ast::name::Name { fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { vm.ctx.new_str(self.as_str()).to_pyobject(vm) } @@ -41,9 +41,9 @@ impl Node for ruff::name::Name { } } -impl Node for ruff::Decorator { +impl Node for ast::Decorator { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - ruff::Expr::ast_to_object(self.expression, vm, source_file) + ast::Expr::ast_to_object(self.expression, vm, source_file) } fn ast_from_object( @@ -51,7 +51,7 @@ impl Node for ruff::Decorator { source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { - let expression = ruff::Expr::ast_from_object(vm, source_file, object)?; + let expression = ast::Expr::ast_from_object(vm, source_file, object)?; let range = expression.range(); Ok(Self { node_index: Default::default(), @@ -62,7 +62,7 @@ impl Node for ruff::Decorator { } // product -impl Node for ruff::Alias { +impl Node for ast::Alias { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -103,7 +103,7 @@ impl Node for ruff::Alias { } // product -impl Node for ruff::WithItem { +impl Node for ast::WithItem { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, diff --git a/crates/vm/src/stdlib/ast/parameter.rs b/crates/vm/src/stdlib/ast/parameter.rs index 44fcbb2b464..1e411d41ab6 100644 --- a/crates/vm/src/stdlib/ast/parameter.rs +++ b/crates/vm/src/stdlib/ast/parameter.rs @@ -2,7 +2,7 @@ use super::*; use rustpython_compiler_core::SourceFile; // product -impl Node for ruff::Parameters { +impl Node for ast::Parameters { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -101,7 +101,7 @@ impl Node for ruff::Parameters { } // product -impl Node for ruff::Parameter { +impl Node for ast::Parameter { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -156,7 +156,7 @@ impl Node for ruff::Parameter { } // product -impl Node for ruff::Keyword { +impl Node for ast::Keyword { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -197,7 +197,7 @@ impl Node for ruff::Keyword { struct PositionalParameters { pub _range: TextRange, // TODO: Use this - pub args: Box<[ruff::Parameter]>, + pub args: Box<[ast::Parameter]>, } impl Node for PositionalParameters { @@ -220,7 +220,7 @@ impl Node for PositionalParameters { struct KeywordParameters { pub _range: TextRange, // TODO: Use this - pub keywords: Box<[ruff::Parameter]>, + pub keywords: Box<[ast::Parameter]>, } impl Node for KeywordParameters { @@ -243,7 +243,7 @@ impl Node for KeywordParameters { struct ParameterDefaults { pub _range: TextRange, // TODO: Use this - defaults: Box<[Option>]>, + defaults: Box<[Option>]>, } impl Node for ParameterDefaults { @@ -265,8 +265,8 @@ impl Node for ParameterDefaults { } fn extract_positional_parameter_defaults( - pos_only_args: Vec, - args: Vec, + pos_only_args: Vec, + args: Vec, ) -> ( PositionalParameters, PositionalParameters, @@ -325,15 +325,15 @@ fn merge_positional_parameter_defaults( args: PositionalParameters, defaults: ParameterDefaults, ) -> ( - Vec, - Vec, + Vec, + Vec, ) { let posonlyargs = posonlyargs.args; let args = args.args; let defaults = defaults.defaults; let mut posonlyargs: Vec<_> = as IntoIterator>::into_iter(posonlyargs) - .map(|parameter| ruff::ParameterWithDefault { + .map(|parameter| ast::ParameterWithDefault { node_index: Default::default(), range: Default::default(), parameter, @@ -341,7 +341,7 @@ fn merge_positional_parameter_defaults( }) .collect(); let mut args: Vec<_> = as IntoIterator>::into_iter(args) - .map(|parameter| ruff::ParameterWithDefault { + .map(|parameter| ast::ParameterWithDefault { node_index: Default::default(), range: Default::default(), parameter, @@ -366,7 +366,7 @@ fn merge_positional_parameter_defaults( } fn extract_keyword_parameter_defaults( - kw_only_args: Vec, + kw_only_args: Vec, ) -> (KeywordParameters, ParameterDefaults) { let mut defaults = vec![]; defaults.extend(kw_only_args.iter().map(|item| item.default.clone())); @@ -402,9 +402,9 @@ fn extract_keyword_parameter_defaults( fn merge_keyword_parameter_defaults( kw_only_args: KeywordParameters, defaults: ParameterDefaults, -) -> Vec { +) -> Vec { core::iter::zip(kw_only_args.keywords, defaults.defaults) - .map(|(parameter, default)| ruff::ParameterWithDefault { + .map(|(parameter, default)| ast::ParameterWithDefault { node_index: Default::default(), parameter, default, diff --git a/crates/vm/src/stdlib/ast/pattern.rs b/crates/vm/src/stdlib/ast/pattern.rs index d8128cb0622..4531a989cb3 100644 --- a/crates/vm/src/stdlib/ast/pattern.rs +++ b/crates/vm/src/stdlib/ast/pattern.rs @@ -2,7 +2,7 @@ use super::*; use rustpython_compiler_core::SourceFile; // product -impl Node for ruff::MatchCase { +impl Node for ast::MatchCase { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -50,7 +50,7 @@ impl Node for ruff::MatchCase { } // sum -impl Node for ruff::Pattern { +impl Node for ast::Pattern { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { Self::MatchValue(cons) => cons.ast_to_object(vm, source_file), @@ -70,49 +70,49 @@ impl Node for ruff::Pattern { ) -> PyResult { let _cls = _object.class(); Ok(if _cls.is(pyast::NodePatternMatchValue::static_type()) { - Self::MatchValue(ruff::PatternMatchValue::ast_from_object( + Self::MatchValue(ast::PatternMatchValue::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchSingleton::static_type()) { - Self::MatchSingleton(ruff::PatternMatchSingleton::ast_from_object( + Self::MatchSingleton(ast::PatternMatchSingleton::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchSequence::static_type()) { - Self::MatchSequence(ruff::PatternMatchSequence::ast_from_object( + Self::MatchSequence(ast::PatternMatchSequence::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchMapping::static_type()) { - Self::MatchMapping(ruff::PatternMatchMapping::ast_from_object( + Self::MatchMapping(ast::PatternMatchMapping::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchClass::static_type()) { - Self::MatchClass(ruff::PatternMatchClass::ast_from_object( + Self::MatchClass(ast::PatternMatchClass::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchStar::static_type()) { - Self::MatchStar(ruff::PatternMatchStar::ast_from_object( + Self::MatchStar(ast::PatternMatchStar::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchAs::static_type()) { - Self::MatchAs(ruff::PatternMatchAs::ast_from_object( + Self::MatchAs(ast::PatternMatchAs::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchOr::static_type()) { - Self::MatchOr(ruff::PatternMatchOr::ast_from_object( + Self::MatchOr(ast::PatternMatchOr::ast_from_object( _vm, source_file, _object, @@ -126,7 +126,7 @@ impl Node for ruff::Pattern { } } // constructor -impl Node for ruff::PatternMatchValue { +impl Node for ast::PatternMatchValue { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -161,7 +161,7 @@ impl Node for ruff::PatternMatchValue { } // constructor -impl Node for ruff::PatternMatchSingleton { +impl Node for ast::PatternMatchSingleton { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -198,12 +198,12 @@ impl Node for ruff::PatternMatchSingleton { } } -impl Node for ruff::Singleton { +impl Node for ast::Singleton { fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { match self { - ruff::Singleton::None => vm.ctx.none(), - ruff::Singleton::True => vm.ctx.new_bool(true).into(), - ruff::Singleton::False => vm.ctx.new_bool(false).into(), + ast::Singleton::None => vm.ctx.none(), + ast::Singleton::True => vm.ctx.new_bool(true).into(), + ast::Singleton::False => vm.ctx.new_bool(false).into(), } } @@ -213,11 +213,11 @@ impl Node for ruff::Singleton { object: PyObjectRef, ) -> PyResult { if vm.is_none(&object) { - Ok(ruff::Singleton::None) + Ok(ast::Singleton::None) } else if object.is(&vm.ctx.true_value) { - Ok(ruff::Singleton::True) + Ok(ast::Singleton::True) } else if object.is(&vm.ctx.false_value) { - Ok(ruff::Singleton::False) + Ok(ast::Singleton::False) } else { Err(vm.new_value_error(format!( "Expected None, True, or False, got {:?}", @@ -228,7 +228,7 @@ impl Node for ruff::Singleton { } // constructor -impl Node for ruff::PatternMatchSequence { +impl Node for ast::PatternMatchSequence { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -266,7 +266,7 @@ impl Node for ruff::PatternMatchSequence { } // constructor -impl Node for ruff::PatternMatchMapping { +impl Node for ast::PatternMatchMapping { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -318,7 +318,7 @@ impl Node for ruff::PatternMatchMapping { } // constructor -impl Node for ruff::PatternMatchClass { +impl Node for ast::PatternMatchClass { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -377,7 +377,7 @@ impl Node for ruff::PatternMatchClass { get_node_field(vm, &object, "cls", "MatchClass")?, )?, range: range_from_object(vm, source_file, object, "MatchClass")?, - arguments: ruff::PatternArguments { + arguments: ast::PatternArguments { node_index: Default::default(), range: Default::default(), patterns, @@ -387,7 +387,7 @@ impl Node for ruff::PatternMatchClass { } } -struct PatternMatchClassPatterns(Vec); +struct PatternMatchClassPatterns(Vec); impl Node for PatternMatchClassPatterns { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { @@ -403,7 +403,7 @@ impl Node for PatternMatchClassPatterns { } } -struct PatternMatchClassKeywordAttributes(Vec); +struct PatternMatchClassKeywordAttributes(Vec); impl Node for PatternMatchClassKeywordAttributes { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { @@ -419,7 +419,7 @@ impl Node for PatternMatchClassKeywordAttributes { } } -struct PatternMatchClassKeywordPatterns(Vec); +struct PatternMatchClassKeywordPatterns(Vec); impl Node for PatternMatchClassKeywordPatterns { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { @@ -435,7 +435,7 @@ impl Node for PatternMatchClassKeywordPatterns { } } // constructor -impl Node for ruff::PatternMatchStar { +impl Node for ast::PatternMatchStar { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -468,7 +468,7 @@ impl Node for ruff::PatternMatchStar { } // constructor -impl Node for ruff::PatternMatchAs { +impl Node for ast::PatternMatchAs { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -507,7 +507,7 @@ impl Node for ruff::PatternMatchAs { } // constructor -impl Node for ruff::PatternMatchOr { +impl Node for ast::PatternMatchOr { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -541,7 +541,7 @@ impl Node for ruff::PatternMatchOr { } fn split_pattern_match_class( - arguments: ruff::PatternArguments, + arguments: ast::PatternArguments, ) -> ( PatternMatchClassPatterns, PatternMatchClassKeywordAttributes, @@ -562,12 +562,12 @@ fn merge_pattern_match_class( patterns: PatternMatchClassPatterns, kwd_attrs: PatternMatchClassKeywordAttributes, kwd_patterns: PatternMatchClassKeywordPatterns, -) -> (Vec, Vec) { +) -> (Vec, Vec) { let keywords = kwd_attrs .0 .into_iter() .zip(kwd_patterns.0) - .map(|(attr, pattern)| ruff::PatternKeyword { + .map(|(attr, pattern)| ast::PatternKeyword { range: Default::default(), node_index: Default::default(), attr, diff --git a/crates/vm/src/stdlib/ast/statement.rs b/crates/vm/src/stdlib/ast/statement.rs index f1d36c52e2e..7716181fc5b 100644 --- a/crates/vm/src/stdlib/ast/statement.rs +++ b/crates/vm/src/stdlib/ast/statement.rs @@ -3,7 +3,7 @@ use crate::stdlib::ast::argument::{merge_class_def_args, split_class_def_args}; use rustpython_compiler_core::SourceFile; // sum -impl Node for ruff::Stmt { +impl Node for ast::Stmt { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { Self::FunctionDef(cons) => cons.ast_to_object(vm, source_file), @@ -44,117 +44,93 @@ impl Node for ruff::Stmt { ) -> PyResult { let _cls = _object.class(); Ok(if _cls.is(pyast::NodeStmtFunctionDef::static_type()) { - Self::FunctionDef(ruff::StmtFunctionDef::ast_from_object( + Self::FunctionDef(ast::StmtFunctionDef::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodeStmtAsyncFunctionDef::static_type()) { - Self::FunctionDef(ruff::StmtFunctionDef::ast_from_object( + Self::FunctionDef(ast::StmtFunctionDef::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodeStmtClassDef::static_type()) { - Self::ClassDef(ruff::StmtClassDef::ast_from_object( + Self::ClassDef(ast::StmtClassDef::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodeStmtReturn::static_type()) { - Self::Return(ruff::StmtReturn::ast_from_object( - _vm, - source_file, - _object, - )?) + Self::Return(ast::StmtReturn::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtDelete::static_type()) { - Self::Delete(ruff::StmtDelete::ast_from_object( - _vm, - source_file, - _object, - )?) + Self::Delete(ast::StmtDelete::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtAssign::static_type()) { - Self::Assign(ruff::StmtAssign::ast_from_object( - _vm, - source_file, - _object, - )?) + Self::Assign(ast::StmtAssign::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtTypeAlias::static_type()) { - Self::TypeAlias(ruff::StmtTypeAlias::ast_from_object( + Self::TypeAlias(ast::StmtTypeAlias::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodeStmtAugAssign::static_type()) { - Self::AugAssign(ruff::StmtAugAssign::ast_from_object( + Self::AugAssign(ast::StmtAugAssign::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodeStmtAnnAssign::static_type()) { - Self::AnnAssign(ruff::StmtAnnAssign::ast_from_object( + Self::AnnAssign(ast::StmtAnnAssign::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodeStmtFor::static_type()) { - Self::For(ruff::StmtFor::ast_from_object(_vm, source_file, _object)?) + Self::For(ast::StmtFor::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtAsyncFor::static_type()) { - Self::For(ruff::StmtFor::ast_from_object(_vm, source_file, _object)?) + Self::For(ast::StmtFor::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtWhile::static_type()) { - Self::While(ruff::StmtWhile::ast_from_object(_vm, source_file, _object)?) + Self::While(ast::StmtWhile::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtIf::static_type()) { - Self::If(ruff::StmtIf::ast_from_object(_vm, source_file, _object)?) + Self::If(ast::StmtIf::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtWith::static_type()) { - Self::With(ruff::StmtWith::ast_from_object(_vm, source_file, _object)?) + Self::With(ast::StmtWith::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtAsyncWith::static_type()) { - Self::With(ruff::StmtWith::ast_from_object(_vm, source_file, _object)?) + Self::With(ast::StmtWith::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtMatch::static_type()) { - Self::Match(ruff::StmtMatch::ast_from_object(_vm, source_file, _object)?) + Self::Match(ast::StmtMatch::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtRaise::static_type()) { - Self::Raise(ruff::StmtRaise::ast_from_object(_vm, source_file, _object)?) + Self::Raise(ast::StmtRaise::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtTry::static_type()) { - Self::Try(ruff::StmtTry::ast_from_object(_vm, source_file, _object)?) + Self::Try(ast::StmtTry::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtTryStar::static_type()) { - Self::Try(ruff::StmtTry::ast_from_object(_vm, source_file, _object)?) + Self::Try(ast::StmtTry::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtAssert::static_type()) { - Self::Assert(ruff::StmtAssert::ast_from_object( - _vm, - source_file, - _object, - )?) + Self::Assert(ast::StmtAssert::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtImport::static_type()) { - Self::Import(ruff::StmtImport::ast_from_object( - _vm, - source_file, - _object, - )?) + Self::Import(ast::StmtImport::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtImportFrom::static_type()) { - Self::ImportFrom(ruff::StmtImportFrom::ast_from_object( + Self::ImportFrom(ast::StmtImportFrom::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodeStmtGlobal::static_type()) { - Self::Global(ruff::StmtGlobal::ast_from_object( - _vm, - source_file, - _object, - )?) + Self::Global(ast::StmtGlobal::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtNonlocal::static_type()) { - Self::Nonlocal(ruff::StmtNonlocal::ast_from_object( + Self::Nonlocal(ast::StmtNonlocal::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodeStmtExpr::static_type()) { - Self::Expr(ruff::StmtExpr::ast_from_object(_vm, source_file, _object)?) + Self::Expr(ast::StmtExpr::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtPass::static_type()) { - Self::Pass(ruff::StmtPass::ast_from_object(_vm, source_file, _object)?) + Self::Pass(ast::StmtPass::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtBreak::static_type()) { - Self::Break(ruff::StmtBreak::ast_from_object(_vm, source_file, _object)?) + Self::Break(ast::StmtBreak::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtContinue::static_type()) { - Self::Continue(ruff::StmtContinue::ast_from_object( + Self::Continue(ast::StmtContinue::ast_from_object( _vm, source_file, _object, @@ -169,7 +145,7 @@ impl Node for ruff::Stmt { } // constructor -impl Node for ruff::StmtFunctionDef { +impl Node for ast::StmtFunctionDef { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -266,7 +242,7 @@ impl Node for ruff::StmtFunctionDef { } // constructor -impl Node for ruff::StmtClassDef { +impl Node for ast::StmtClassDef { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -348,7 +324,7 @@ impl Node for ruff::StmtClassDef { } } // constructor -impl Node for ruff::StmtReturn { +impl Node for ast::StmtReturn { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -379,7 +355,7 @@ impl Node for ruff::StmtReturn { } } // constructor -impl Node for ruff::StmtDelete { +impl Node for ast::StmtDelete { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -413,7 +389,7 @@ impl Node for ruff::StmtDelete { } // constructor -impl Node for ruff::StmtAssign { +impl Node for ast::StmtAssign { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -461,7 +437,7 @@ impl Node for ruff::StmtAssign { } // constructor -impl Node for ruff::StmtTypeAlias { +impl Node for ast::StmtTypeAlias { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -516,7 +492,7 @@ impl Node for ruff::StmtTypeAlias { } // constructor -impl Node for ruff::StmtAugAssign { +impl Node for ast::StmtAugAssign { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -566,7 +542,7 @@ impl Node for ruff::StmtAugAssign { } // constructor -impl Node for ruff::StmtAnnAssign { +impl Node for ast::StmtAnnAssign { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -626,7 +602,7 @@ impl Node for ruff::StmtAnnAssign { } // constructor -impl Node for ruff::StmtFor { +impl Node for ast::StmtFor { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -704,7 +680,7 @@ impl Node for ruff::StmtFor { } // constructor -impl Node for ruff::StmtWhile { +impl Node for ast::StmtWhile { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -754,7 +730,7 @@ impl Node for ruff::StmtWhile { } } // constructor -impl Node for ruff::StmtIf { +impl Node for ast::StmtIf { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -764,7 +740,7 @@ impl Node for ruff::StmtIf { elif_else_clauses, } = self; elif_else_clause::ast_to_object( - ruff::ElifElseClause { + ast::ElifElseClause { node_index: Default::default(), range, test: Some(*test), @@ -784,7 +760,7 @@ impl Node for ruff::StmtIf { } } // constructor -impl Node for ruff::StmtWith { +impl Node for ast::StmtWith { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -844,7 +820,7 @@ impl Node for ruff::StmtWith { } } // constructor -impl Node for ruff::StmtMatch { +impl Node for ast::StmtMatch { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -885,7 +861,7 @@ impl Node for ruff::StmtMatch { } } // constructor -impl Node for ruff::StmtRaise { +impl Node for ast::StmtRaise { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -922,7 +898,7 @@ impl Node for ruff::StmtRaise { } } // constructor -impl Node for ruff::StmtTry { +impl Node for ast::StmtTry { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -996,7 +972,7 @@ impl Node for ruff::StmtTry { } } // constructor -impl Node for ruff::StmtAssert { +impl Node for ast::StmtAssert { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1035,7 +1011,7 @@ impl Node for ruff::StmtAssert { } } // constructor -impl Node for ruff::StmtImport { +impl Node for ast::StmtImport { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1068,7 +1044,7 @@ impl Node for ruff::StmtImport { } } // constructor -impl Node for ruff::StmtImportFrom { +impl Node for ast::StmtImportFrom { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1117,7 +1093,7 @@ impl Node for ruff::StmtImportFrom { } } // constructor -impl Node for ruff::StmtGlobal { +impl Node for ast::StmtGlobal { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1150,7 +1126,7 @@ impl Node for ruff::StmtGlobal { } } // constructor -impl Node for ruff::StmtNonlocal { +impl Node for ast::StmtNonlocal { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1183,7 +1159,7 @@ impl Node for ruff::StmtNonlocal { } } // constructor -impl Node for ruff::StmtExpr { +impl Node for ast::StmtExpr { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1216,7 +1192,7 @@ impl Node for ruff::StmtExpr { } } // constructor -impl Node for ruff::StmtPass { +impl Node for ast::StmtPass { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1241,7 +1217,7 @@ impl Node for ruff::StmtPass { } } // constructor -impl Node for ruff::StmtBreak { +impl Node for ast::StmtBreak { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -1268,7 +1244,7 @@ impl Node for ruff::StmtBreak { } // constructor -impl Node for ruff::StmtContinue { +impl Node for ast::StmtContinue { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, diff --git a/crates/vm/src/stdlib/ast/string.rs b/crates/vm/src/stdlib/ast/string.rs index c68e378bc74..2533fb8c6b9 100644 --- a/crates/vm/src/stdlib/ast/string.rs +++ b/crates/vm/src/stdlib/ast/string.rs @@ -2,14 +2,13 @@ use super::constant::{Constant, ConstantLiteral}; use super::*; fn ruff_fstring_element_into_iter( - mut fstring_element: ruff::InterpolatedStringElements, -) -> impl Iterator { - let default = - ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement { - node_index: Default::default(), - range: Default::default(), - value: Default::default(), - }); + mut fstring_element: ast::InterpolatedStringElements, +) -> impl Iterator { + let default = ast::InterpolatedStringElement::Literal(ast::InterpolatedStringLiteralElement { + node_index: Default::default(), + range: Default::default(), + value: Default::default(), + }); fstring_element .iter_mut() .map(move |elem| core::mem::replace(elem, default.clone())) @@ -18,19 +17,19 @@ fn ruff_fstring_element_into_iter( } fn ruff_fstring_element_to_joined_str_part( - element: ruff::InterpolatedStringElement, + element: ast::InterpolatedStringElement, ) -> JoinedStrPart { match element { - ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement { + ast::InterpolatedStringElement::Literal(ast::InterpolatedStringLiteralElement { range, value, node_index: _, }) => JoinedStrPart::Constant(Constant::new_str( value, - ruff::str_prefix::StringLiteralPrefix::Empty, + ast::str_prefix::StringLiteralPrefix::Empty, range, )), - ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement { + ast::InterpolatedStringElement::Interpolation(ast::InterpolatedElement { range, expression, debug_text: _, // TODO: What is this? @@ -47,12 +46,12 @@ fn ruff_fstring_element_to_joined_str_part( } fn ruff_format_spec_to_joined_str( - format_spec: Option>, + format_spec: Option>, ) -> Option> { match format_spec { None => None, Some(format_spec) => { - let ruff::InterpolatedStringFormatSpec { + let ast::InterpolatedStringFormatSpec { range, elements, node_index: _, @@ -67,36 +66,36 @@ fn ruff_format_spec_to_joined_str( } fn ruff_fstring_element_to_ruff_fstring_part( - element: ruff::InterpolatedStringElement, -) -> ruff::FStringPart { + element: ast::InterpolatedStringElement, +) -> ast::FStringPart { match element { - ruff::InterpolatedStringElement::Literal(value) => { - let ruff::InterpolatedStringLiteralElement { + ast::InterpolatedStringElement::Literal(value) => { + let ast::InterpolatedStringLiteralElement { node_index, range, value, } = value; - ruff::FStringPart::Literal(ruff::StringLiteral { + ast::FStringPart::Literal(ast::StringLiteral { node_index, range, value, - flags: ruff::StringLiteralFlags::empty(), + flags: ast::StringLiteralFlags::empty(), }) } - ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement { + ast::InterpolatedStringElement::Interpolation(ast::InterpolatedElement { range, .. - }) => ruff::FStringPart::FString(ruff::FString { + }) => ast::FStringPart::FString(ast::FString { node_index: Default::default(), range, elements: vec![element].into(), - flags: ruff::FStringFlags::empty(), + flags: ast::FStringFlags::empty(), }), } } fn joined_str_to_ruff_format_spec( joined_str: Option>, -) -> Option> { +) -> Option> { match joined_str { None => None, Some(joined_str) => { @@ -104,7 +103,7 @@ fn joined_str_to_ruff_format_spec( let elements: Vec<_> = Box::into_iter(values) .map(joined_str_part_to_ruff_fstring_element) .collect(); - let format_spec = ruff::InterpolatedStringFormatSpec { + let format_spec = ast::InterpolatedStringFormatSpec { node_index: Default::default(), range, elements: elements.into(), @@ -121,32 +120,32 @@ pub(super) struct JoinedStr { } impl JoinedStr { - pub(super) fn into_expr(self) -> ruff::Expr { + pub(super) fn into_expr(self) -> ast::Expr { let Self { range, values } = self; - ruff::Expr::FString(ruff::ExprFString { + ast::Expr::FString(ast::ExprFString { node_index: Default::default(), range: Default::default(), value: match values.len() { // ruff represents an empty fstring like this: - 0 => ruff::FStringValue::single(ruff::FString { + 0 => ast::FStringValue::single(ast::FString { node_index: Default::default(), range, elements: vec![].into(), - flags: ruff::FStringFlags::empty(), + flags: ast::FStringFlags::empty(), }), - 1 => ruff::FStringValue::single( + 1 => ast::FStringValue::single( Box::<[_]>::into_iter(values) .map(joined_str_part_to_ruff_fstring_element) - .map(|element| ruff::FString { + .map(|element| ast::FString { node_index: Default::default(), range, elements: vec![element].into(), - flags: ruff::FStringFlags::empty(), + flags: ast::FStringFlags::empty(), }) .next() .expect("FString has exactly one part"), ), - _ => ruff::FStringValue::concatenated( + _ => ast::FStringValue::concatenated( Box::<[_]>::into_iter(values) .map(joined_str_part_to_ruff_fstring_element) .map(ruff_fstring_element_to_ruff_fstring_part) @@ -157,10 +156,10 @@ impl JoinedStr { } } -fn joined_str_part_to_ruff_fstring_element(part: JoinedStrPart) -> ruff::InterpolatedStringElement { +fn joined_str_part_to_ruff_fstring_element(part: JoinedStrPart) -> ast::InterpolatedStringElement { match part { JoinedStrPart::FormattedValue(value) => { - ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement { + ast::InterpolatedStringElement::Interpolation(ast::InterpolatedElement { node_index: Default::default(), range: value.range, expression: value.value.clone(), @@ -170,7 +169,7 @@ fn joined_str_part_to_ruff_fstring_element(part: JoinedStrPart) -> ruff::Interpo }) } JoinedStrPart::Constant(value) => { - ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement { + ast::InterpolatedStringElement::Literal(ast::InterpolatedStringLiteralElement { node_index: Default::default(), range: value.range, value: match value.value { @@ -254,8 +253,8 @@ impl Node for JoinedStrPart { #[derive(Debug)] pub(super) struct FormattedValue { - value: Box, - conversion: ruff::ConversionFlag, + value: Box, + conversion: ast::ConversionFlag, format_spec: Option>, range: TextRange, } @@ -313,24 +312,24 @@ impl Node for FormattedValue { pub(super) fn fstring_to_object( vm: &VirtualMachine, source_file: &SourceFile, - expression: ruff::ExprFString, + expression: ast::ExprFString, ) -> PyObjectRef { - let ruff::ExprFString { + let ast::ExprFString { range, mut value, node_index: _, } = expression; - let default_part = ruff::FStringPart::FString(ruff::FString { + let default_part = ast::FStringPart::FString(ast::FString { node_index: Default::default(), range: Default::default(), elements: Default::default(), - flags: ruff::FStringFlags::empty(), + flags: ast::FStringFlags::empty(), }); let mut values = Vec::new(); for i in 0..value.as_slice().len() { let part = core::mem::replace(value.iter_mut().nth(i).unwrap(), default_part.clone()); match part { - ruff::FStringPart::Literal(ruff::StringLiteral { + ast::FStringPart::Literal(ast::StringLiteral { range, value, flags, @@ -342,7 +341,7 @@ pub(super) fn fstring_to_object( range, ))); } - ruff::FStringPart::FString(ruff::FString { + ast::FStringPart::FString(ast::FString { range: _, elements, flags: _, @@ -364,20 +363,20 @@ pub(super) fn fstring_to_object( // ===== TString (Template String) Support ===== fn ruff_tstring_element_to_template_str_part( - element: ruff::InterpolatedStringElement, + element: ast::InterpolatedStringElement, source_file: &SourceFile, ) -> TemplateStrPart { match element { - ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement { + ast::InterpolatedStringElement::Literal(ast::InterpolatedStringLiteralElement { range, value, node_index: _, }) => TemplateStrPart::Constant(Constant::new_str( value, - ruff::str_prefix::StringLiteralPrefix::Empty, + ast::str_prefix::StringLiteralPrefix::Empty, range, )), - ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement { + ast::InterpolatedStringElement::Interpolation(ast::InterpolatedElement { range, expression, debug_text, @@ -401,13 +400,13 @@ fn ruff_tstring_element_to_template_str_part( } fn ruff_format_spec_to_template_str( - format_spec: Option>, + format_spec: Option>, source_file: &SourceFile, ) -> Option> { match format_spec { None => None, Some(format_spec) => { - let ruff::InterpolatedStringFormatSpec { + let ast::InterpolatedStringFormatSpec { range, elements, node_index: _, @@ -499,9 +498,9 @@ impl Node for TemplateStrPart { #[derive(Debug)] pub(super) struct TStringInterpolation { - value: Box, + value: Box, str: String, - conversion: ruff::ConversionFlag, + conversion: ast::ConversionFlag, format_spec: Option>, range: TextRange, } @@ -565,18 +564,18 @@ impl Node for TStringInterpolation { pub(super) fn tstring_to_object( vm: &VirtualMachine, source_file: &SourceFile, - expression: ruff::ExprTString, + expression: ast::ExprTString, ) -> PyObjectRef { - let ruff::ExprTString { + let ast::ExprTString { range, mut value, node_index: _, } = expression; - let default_tstring = ruff::TString { + let default_tstring = ast::TString { node_index: Default::default(), range: Default::default(), elements: Default::default(), - flags: ruff::TStringFlags::empty(), + flags: ast::TStringFlags::empty(), }; let mut values = Vec::new(); for i in 0..value.as_slice().len() { diff --git a/crates/vm/src/stdlib/ast/type_parameters.rs b/crates/vm/src/stdlib/ast/type_parameters.rs index 017470f7e64..4801a9a4b28 100644 --- a/crates/vm/src/stdlib/ast/type_parameters.rs +++ b/crates/vm/src/stdlib/ast/type_parameters.rs @@ -1,7 +1,7 @@ use super::*; use rustpython_compiler_core::SourceFile; -impl Node for ruff::TypeParams { +impl Node for ast::TypeParams { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { self.type_params.ast_to_object(vm, source_file) } @@ -11,7 +11,7 @@ impl Node for ruff::TypeParams { _source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { - let type_params: Vec = Node::ast_from_object(_vm, _source_file, _object)?; + let type_params: Vec = Node::ast_from_object(_vm, _source_file, _object)?; let range = Option::zip(type_params.first(), type_params.last()) .map(|(first, last)| first.range().cover(last.range())) .unwrap_or_default(); @@ -28,7 +28,7 @@ impl Node for ruff::TypeParams { } // sum -impl Node for ruff::TypeParam { +impl Node for ast::TypeParam { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { Self::TypeVar(cons) => cons.ast_to_object(vm, source_file), @@ -44,19 +44,19 @@ impl Node for ruff::TypeParam { ) -> PyResult { let _cls = _object.class(); Ok(if _cls.is(pyast::NodeTypeParamTypeVar::static_type()) { - Self::TypeVar(ruff::TypeParamTypeVar::ast_from_object( + Self::TypeVar(ast::TypeParamTypeVar::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodeTypeParamParamSpec::static_type()) { - Self::ParamSpec(ruff::TypeParamParamSpec::ast_from_object( + Self::ParamSpec(ast::TypeParamParamSpec::ast_from_object( _vm, source_file, _object, )?) } else if _cls.is(pyast::NodeTypeParamTypeVarTuple::static_type()) { - Self::TypeVarTuple(ruff::TypeParamTypeVarTuple::ast_from_object( + Self::TypeVarTuple(ast::TypeParamTypeVarTuple::ast_from_object( _vm, source_file, _object, @@ -71,7 +71,7 @@ impl Node for ruff::TypeParam { } // constructor -impl Node for ruff::TypeParamTypeVar { +impl Node for ast::TypeParamTypeVar { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -118,7 +118,7 @@ impl Node for ruff::TypeParamTypeVar { } // constructor -impl Node for ruff::TypeParamParamSpec { +impl Node for ast::TypeParamParamSpec { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _, @@ -165,7 +165,7 @@ impl Node for ruff::TypeParamParamSpec { } // constructor -impl Node for ruff::TypeParamTypeVarTuple { +impl Node for ast::TypeParamTypeVarTuple { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { node_index: _,