Skip to content

Commit 7ff1e20

Browse files
committed
Remove _ast conversion context wrappers
1 parent 1540bd6 commit 7ff1e20

17 files changed

Lines changed: 910 additions & 1053 deletions

crates/vm/src/stdlib/_ast.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
compiler::{CompileError, ParseError},
2020
convert::ToPyObject,
2121
};
22-
use node::{AstFromObjectContext, AstToObjectContext, Node};
22+
use node::Node;
2323
use ruff_python_ast as ast;
2424
use ruff_text_size::{Ranged, TextRange, TextSize};
2525
use rustpython_compiler_core::{
@@ -98,7 +98,7 @@ fn get_node_field_required(
9898
}
9999

100100
fn get_required_identifier_field<T: Node>(
101-
ctx: &AstFromObjectContext<'_>,
101+
ctx: &VirtualMachine,
102102
source_file: &SourceFile,
103103
obj: &PyObject,
104104
field: &'static str,
@@ -112,7 +112,7 @@ fn get_required_identifier_field<T: Node>(
112112
}
113113

114114
fn get_required_node_field<T: Node>(
115-
ctx: &AstFromObjectContext<'_>,
115+
ctx: &VirtualMachine,
116116
source_file: &SourceFile,
117117
obj: &PyObject,
118118
field: &'static str,
@@ -139,7 +139,7 @@ fn get_node_field_opt(
139139
}
140140

141141
fn get_node_list_field<T: Node>(
142-
ctx: &AstFromObjectContext<'_>,
142+
ctx: &VirtualMachine,
143143
source_file: &SourceFile,
144144
obj: &PyObject,
145145
field: &'static str,
@@ -169,7 +169,7 @@ fn get_node_list_field_object(
169169
}
170170

171171
fn convert_node_list_field<T: Node>(
172-
ctx: &AstFromObjectContext<'_>,
172+
ctx: &VirtualMachine,
173173
source_file: &SourceFile,
174174
list: &PyList,
175175
field: &'static str,
@@ -201,7 +201,7 @@ fn convert_node_list_field<T: Node>(
201201
}
202202

203203
fn get_node_boxed_slice_field<T: Node>(
204-
ctx: &AstFromObjectContext<'_>,
204+
ctx: &VirtualMachine,
205205
source_file: &SourceFile,
206206
obj: &PyObject,
207207
field: &'static str,
@@ -1906,8 +1906,7 @@ pub(crate) fn parse(
19061906
}),
19071907
ast::Mod::Expression(e) => Mod::Expression(e),
19081908
};
1909-
let ctx = AstToObjectContext::new(vm, &source_file);
1910-
let obj = top.ast_to_object(&ctx);
1909+
let obj = top.ast_to_object(vm, &source_file);
19111910
if let Some(lines) = &type_comment_source
19121911
&& obj.class().is(pyast::NodeModModule::static_type())
19131912
{
@@ -2073,8 +2072,7 @@ pub(crate) fn parse_func_type(
20732072
runtime_argtypes: None,
20742073
};
20752074
let source_file = SourceFileBuilder::new("".to_owned(), source.to_owned()).finish();
2076-
let ctx = AstToObjectContext::new(vm, &source_file);
2077-
Ok(func_type.ast_to_object(&ctx))
2075+
Ok(func_type.ast_to_object(vm, &source_file))
20782076
}
20792077

20802078
fn type_ignores_from_source(
@@ -2346,9 +2344,7 @@ pub(crate) fn preprocess_ast_object(
23462344
let original_object = object.clone();
23472345
let text = synthetic_source_from_ast_object(vm, &object)?;
23482346
let source_file = SourceFileBuilder::new(filename.to_owned(), text).finish();
2349-
let ast = constant::with_ast_from_object_context(vm, |from_ctx| {
2350-
Node::ast_from_object(from_ctx, &source_file, object)
2351-
})?;
2347+
let ast = Node::ast_from_object(vm, &source_file, object)?;
23522348
validate::validate_mod(vm, &ast)?;
23532349
let syntax_check_only = !optimized_ast;
23542350

@@ -2405,8 +2401,7 @@ pub(crate) fn preprocess_ast_object(
24052401
}
24062402
Mod::FunctionType(function_type) => Mod::FunctionType(function_type),
24072403
};
2408-
let ctx = AstToObjectContext::new(vm, &source_file);
2409-
let result = ast.ast_to_object(&ctx);
2404+
let result = ast.ast_to_object(vm, &source_file);
24102405
copy_ast_passthrough_fields(vm, &original_object, &result)?;
24112406
Ok(result)
24122407
}
@@ -2421,9 +2416,7 @@ pub(crate) fn compile(
24212416
) -> PyResult {
24222417
let text = synthetic_source_from_ast_object(vm, &object)?;
24232418
let source_file = SourceFileBuilder::new(filename.to_owned(), text.clone()).finish();
2424-
let ast = constant::with_ast_from_object_context(vm, |from_ctx| {
2425-
Node::ast_from_object(from_ctx, &source_file, object)
2426-
})?;
2419+
let ast = Node::ast_from_object(vm, &source_file, object)?;
24272420
validate::validate_mod(vm, &ast)?;
24282421
let ast = match ast {
24292422
Mod::Module(m) => ast::Mod::Module(m.module),
@@ -2486,9 +2479,7 @@ pub(crate) fn compile(
24862479
#[cfg(not(feature = "rustpython-codegen"))]
24872480
pub(crate) fn validate_ast_object(vm: &VirtualMachine, object: PyObjectRef) -> PyResult<()> {
24882481
let source_file = SourceFileBuilder::new("<ast>".to_owned(), "".to_owned()).finish();
2489-
let ast = constant::with_ast_from_object_context(vm, |from_ctx| {
2490-
Node::ast_from_object(from_ctx, &source_file, object)
2491-
})?;
2482+
let ast = Node::ast_from_object(vm, &source_file, object)?;
24922483
validate::validate_mod(vm, &ast)?;
24932484
Ok(())
24942485
}

crates/vm/src/stdlib/_ast/argument.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub(super) struct PositionalArguments {
99

1010
impl PositionalArguments {
1111
pub(super) fn ast_from_field(
12-
ctx: &AstFromObjectContext<'_>,
12+
ctx: &VirtualMachine,
1313
source_file: &SourceFile,
1414
object: &PyObject,
1515
field: &'static str,
@@ -27,22 +27,21 @@ impl PositionalArguments {
2727
}
2828

2929
impl Node for PositionalArguments {
30-
fn ast_to_object(self, to_ctx: &AstToObjectContext<'_>) -> PyObjectRef {
31-
let _vm = to_ctx.vm;
32-
let _source_file = to_ctx.source_file;
30+
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
31+
let _source_file = source_file;
3332
let Self {
3433
runtime_values,
3534
args,
3635
range: _,
3736
} = self;
3837
runtime_values.map_or_else(
39-
|| BoxedSlice(args).ast_to_object(to_ctx),
40-
|values| values.ast_to_object(to_ctx),
38+
|| BoxedSlice(args).ast_to_object(vm, source_file),
39+
|values| values.ast_to_object(vm, source_file),
4140
)
4241
}
4342

4443
fn ast_from_object(
45-
ctx: &AstFromObjectContext<'_>,
44+
ctx: &VirtualMachine,
4645
source_file: &SourceFile,
4746
object: PyObjectRef,
4847
) -> PyResult<Self> {
@@ -62,7 +61,7 @@ pub(super) struct KeywordArguments {
6261

6362
impl KeywordArguments {
6463
pub(super) fn ast_from_field(
65-
ctx: &AstFromObjectContext<'_>,
64+
ctx: &VirtualMachine,
6665
source_file: &SourceFile,
6766
object: &PyObject,
6867
field: &'static str,
@@ -76,16 +75,15 @@ impl KeywordArguments {
7675
}
7776

7877
impl Node for KeywordArguments {
79-
fn ast_to_object(self, to_ctx: &AstToObjectContext<'_>) -> PyObjectRef {
80-
let _vm = to_ctx.vm;
81-
let _source_file = to_ctx.source_file;
78+
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
79+
let _source_file = source_file;
8280
let Self { keywords, range: _ } = self;
8381
// TODO: use range
84-
BoxedSlice(keywords).ast_to_object(to_ctx)
82+
BoxedSlice(keywords).ast_to_object(vm, source_file)
8583
}
8684

8785
fn ast_from_object(
88-
ctx: &AstFromObjectContext<'_>,
86+
ctx: &VirtualMachine,
8987
source_file: &SourceFile,
9088
object: PyObjectRef,
9189
) -> PyResult<Self> {

crates/vm/src/stdlib/_ast/basic.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ use rustpython_codegen::compile::ruff_int_to_bigint;
44
use rustpython_compiler_core::SourceFile;
55

66
impl Node for ast::Identifier {
7-
fn ast_to_object(self, to_ctx: &AstToObjectContext<'_>) -> PyObjectRef {
8-
let vm = to_ctx.vm;
9-
let _source_file = to_ctx.source_file;
7+
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
8+
let _source_file = source_file;
109
let id = self.as_str();
1110
vm.ctx.intern_str(id).to_object()
1211
}
1312

1413
fn ast_from_object(
15-
ctx: &AstFromObjectContext<'_>,
14+
ctx: &VirtualMachine,
1615
_source_file: &SourceFile,
1716
object: PyObjectRef,
1817
) -> PyResult<Self> {
@@ -26,14 +25,13 @@ impl Node for ast::Identifier {
2625
}
2726

2827
impl Node for ast::Int {
29-
fn ast_to_object(self, to_ctx: &AstToObjectContext<'_>) -> PyObjectRef {
30-
let vm = to_ctx.vm;
31-
let _source_file = to_ctx.source_file;
28+
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
29+
let _source_file = source_file;
3230
vm.ctx.new_int(ruff_int_to_bigint(&self).unwrap()).into()
3331
}
3432

3533
fn ast_from_object(
36-
ctx: &AstFromObjectContext<'_>,
34+
ctx: &VirtualMachine,
3735
_source_file: &SourceFile,
3836
object: PyObjectRef,
3937
) -> PyResult<Self> {
@@ -44,14 +42,13 @@ impl Node for ast::Int {
4442
}
4543

4644
impl Node for bool {
47-
fn ast_to_object(self, to_ctx: &AstToObjectContext<'_>) -> PyObjectRef {
48-
let vm = to_ctx.vm;
49-
let _source_file = to_ctx.source_file;
45+
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
46+
let _source_file = source_file;
5047
vm.ctx.new_int(self as u8).into()
5148
}
5249

5350
fn ast_from_object(
54-
ctx: &AstFromObjectContext<'_>,
51+
ctx: &VirtualMachine,
5552
_source_file: &SourceFile,
5653
object: PyObjectRef,
5754
) -> PyResult<Self> {

0 commit comments

Comments
 (0)