@@ -300,10 +300,13 @@ def emit_range(self, has_attributes, depth):
300
300
301
301
def visitModule (self , mod ):
302
302
self .emit_attrs (0 )
303
- self .emit ("""
303
+ self .emit (
304
+ """
304
305
#[derive(is_macro::Is)]
305
306
pub enum Ast<R=TextRange> {
306
- """ , 0 )
307
+ """ ,
308
+ 0 ,
309
+ )
307
310
for dfn in mod .dfns :
308
311
rust_name = rust_type_name (dfn .name )
309
312
generics = "" if self .type_info [dfn .name ].is_simple else "<R>"
@@ -315,23 +318,29 @@ def visitModule(self, mod):
315
318
# "ast_" prefix to everywhere seems less useful.
316
319
self .emit ('#[is(name = "module")]' , 1 )
317
320
self .emit (f"{ rust_name } ({ rust_name } { generics } )," , 1 )
318
- self .emit ("""
319
- }
320
- impl<R> Node for Ast<R> {
321
- const NAME: &'static str = "AST";
322
- const FIELD_NAMES: &'static [&'static str] = &[];
323
- }
324
- """ , 0 )
321
+ self .emit (
322
+ """
323
+ }
324
+ impl<R> Node for Ast<R> {
325
+ const NAME: &'static str = "AST";
326
+ const FIELD_NAMES: &'static [&'static str] = &[];
327
+ }
328
+ """ ,
329
+ 0 ,
330
+ )
325
331
for dfn in mod .dfns :
326
332
rust_name = rust_type_name (dfn .name )
327
333
generics = "" if self .type_info [dfn .name ].is_simple else "<R>"
328
- self .emit (f"""
334
+ self .emit (
335
+ f"""
329
336
impl<R> From<{ rust_name } { generics } > for Ast<R> {{
330
337
fn from(node: { rust_name } { generics } ) -> Self {{
331
338
Ast::{ rust_name } (node)
332
339
}}
333
340
}}
334
- """ , 0 )
341
+ """ ,
342
+ 0 ,
343
+ )
335
344
336
345
for dfn in mod .dfns :
337
346
self .visit (dfn )
@@ -663,9 +672,7 @@ def visitConstructor(self, cons, type, depth):
663
672
664
673
cons_type_name = f"{ enum_name } { cons .name } "
665
674
666
- self .emit (
667
- f"impl<T, U> Foldable<T, U> for { cons_type_name } { apply_t } {{" , depth
668
- )
675
+ self .emit (f"impl<T, U> Foldable<T, U> for { cons_type_name } { apply_t } {{" , depth )
669
676
self .emit (f"type Mapped = { cons_type_name } { apply_u } ;" , depth + 1 )
670
677
self .emit (
671
678
"fn fold<F: Fold<T, TargetU = U> + ?Sized>(self, folder: &mut F) -> Result<Self::Mapped, F::Error> {" ,
@@ -1097,7 +1104,7 @@ def visitSum(self, sum, type):
1097
1104
f"""
1098
1105
impl ToPyo3Ast for crate::generic::{ rust_name } { self .generics } {{
1099
1106
#[inline]
1100
- fn to_pyo3_ast(&self, { "_" if simple else "" } py: Python) -> PyResult<Py< PyAny> > {{
1107
+ fn to_pyo3_ast<'py> (&self, { "_" if simple else "" } py: Python<'py> ) -> PyResult<&'py PyAny> {{
1101
1108
let instance = match &self {{
1102
1109
""" ,
1103
1110
0 ,
@@ -1130,7 +1137,7 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
1130
1137
f"""
1131
1138
impl ToPyo3Ast for crate::{ name } { self .generics } {{
1132
1139
#[inline]
1133
- fn to_pyo3_ast(&self, py: Python) -> PyResult<Py< PyAny> > {{
1140
+ fn to_pyo3_ast<'py> (&self, py: Python<'py> ) -> PyResult<&'py PyAny> {{
1134
1141
let cache = Self::py_type_cache().get().unwrap();
1135
1142
""" ,
1136
1143
0 ,
@@ -1144,10 +1151,37 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
1144
1151
1 ,
1145
1152
)
1146
1153
self .emit (
1147
- "let instance = cache.0.call1(py, (" ,
1154
+ """
1155
+ let instance = Py::<PyAny>::as_ref(&cache.0, py).call1((
1156
+ """ ,
1148
1157
1 ,
1149
1158
)
1150
1159
for field in cons .fields :
1160
+ if field .type == "constant" :
1161
+ self .emit (
1162
+ f"{ rust_field (field .name )} .to_object(py)," ,
1163
+ 3 ,
1164
+ )
1165
+ continue
1166
+ if field .type == "int" :
1167
+ if field .name == "level" :
1168
+ assert field .opt
1169
+ self .emit (
1170
+ f"{ rust_field (field .name )} .map_or_else(|| py.None(), |level| level.to_u32().to_object(py))," ,
1171
+ 3 ,
1172
+ )
1173
+ continue
1174
+ if field .name in (
1175
+ "lineno" ,
1176
+ "col_offset" ,
1177
+ "end_lineno" ,
1178
+ "end_col_offset" ,
1179
+ ):
1180
+ self .emit (
1181
+ f"{ rust_field (field .name )} .to_u32().to_object(py)," ,
1182
+ 3 ,
1183
+ )
1184
+ continue
1151
1185
self .emit (
1152
1186
f"{ rust_field (field .name )} .to_pyo3_ast(py)?," ,
1153
1187
3 ,
@@ -1158,7 +1192,7 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
1158
1192
)
1159
1193
else :
1160
1194
self .emit (
1161
- "let instance = cache.0.call0(py )?;" ,
1195
+ "let instance = Py::<PyAny>::as_ref(& cache.0, py) .call0()?;" ,
1162
1196
1 ,
1163
1197
)
1164
1198
self .emit (
@@ -1168,12 +1202,12 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
1168
1202
if type .value .attributes and self .namespace == "located" :
1169
1203
self .emit (
1170
1204
"""
1171
- let cache = ast_key_cache().get().unwrap ();
1172
- instance.setattr(py, cache.lineno.as_ref(py), _range.start.row.get())?;
1173
- instance.setattr(py, cache.col_offset.as_ref(py), _range.start.column.get())?;
1205
+ let cache = ast_cache ();
1206
+ instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
1207
+ instance.setattr(cache.col_offset.as_ref(py), _range.start.column.get())?;
1174
1208
if let Some(end) = _range.end {
1175
- instance.setattr(py, cache.end_lineno.as_ref(py), end.row.get())?;
1176
- instance.setattr(py, cache.end_col_offset.as_ref(py), end.column.get())?;
1209
+ instance.setattr(cache.end_lineno.as_ref(py), end.row.get())?;
1210
+ instance.setattr(cache.end_col_offset.as_ref(py), end.column.get())?;
1177
1211
}
1178
1212
""" ,
1179
1213
1 ,
@@ -1858,7 +1892,7 @@ def write_to_pyo3_simple(type_info, f):
1858
1892
f"""
1859
1893
impl ToPyo3Ast for crate::generic::{ rust_name } {{
1860
1894
#[inline]
1861
- fn to_pyo3_ast(&self, _py : Python) -> PyResult<Py< PyAny> > {{
1895
+ fn to_pyo3_ast<'py> (&self, py : Python<'py> ) -> PyResult<&'py PyAny> {{
1862
1896
let cell = match &self {{
1863
1897
""" ,
1864
1898
)
@@ -1869,7 +1903,7 @@ def write_to_pyo3_simple(type_info, f):
1869
1903
f .write (
1870
1904
"""
1871
1905
};
1872
- Ok(cell.get().unwrap().1.clone( ))
1906
+ Ok(Py::<PyAny>::as_ref(& cell.get().unwrap().1, py ))
1873
1907
}
1874
1908
}
1875
1909
""" ,
0 commit comments