diff --git a/ast/asdl_rs.py b/ast/asdl_rs.py index bb98706..24c679d 100755 --- a/ast/asdl_rs.py +++ b/ast/asdl_rs.py @@ -425,7 +425,7 @@ class FoldTraitDefVisitor(EmitVisitor): self.map_user(user) } #[cfg(not(feature = "more-attributes"))] - fn map_user_cfg(&mut self, _user: U) -> Result, Self::Error> { + fn map_user_cfg(&mut self, _user: std::marker::PhantomData) -> Result, Self::Error> { Ok(std::marker::PhantomData) } """, @@ -496,11 +496,12 @@ class FoldImplVisitor(EmitVisitor): f"{fields_pattern[0]} {{ {fields_pattern[1]}}} {fields_pattern[2]} => {{", depth + 2, ) - if not type_info.has_attributes: - self.emit('#[cfg(not(feature = "more-attributes"))]', depth + 3) - self.emit("let custom = std::marker::PhantomData;", depth + 3) - self.emit('#[cfg(feature = "more-attributes")]', depth + 3) - self.emit("let custom = folder.map_user(custom)?;", depth + 3) + + map_user_suffix = "" if type_info.has_attributes else "_cfg" + self.emit( + f"let custom = folder.map_user{map_user_suffix}(custom)?;", depth + 3 + ) + self.gen_construction( fields_pattern[0], cons.fields, fields_pattern[2], depth + 3 ) @@ -533,11 +534,8 @@ class FoldImplVisitor(EmitVisitor): fields_pattern = self.make_pattern(struct_name, struct_name, product.fields) self.emit(f"let {struct_name} {{ {fields_pattern[1]} }} = node;", depth + 1) - if not has_attributes: - self.emit('#[cfg(not(feature = "more-attributes"))]', depth + 3) - self.emit("let custom = std::marker::PhantomData;", depth + 3) - self.emit('#[cfg(feature = "more-attributes")]', depth + 3) - self.emit("let custom = folder.map_user(custom)?;", depth + 3) + map_user_suffix = "" if has_attributes else "_cfg" + self.emit(f"let custom = folder.map_user{map_user_suffix}(custom)?;", depth + 3) self.gen_construction(struct_name, product.fields, "", depth + 1) @@ -956,11 +954,7 @@ def write_ast_def(mod, type_info, f): def write_fold_def(mod, type_info, f): - f.write( - """ -use crate::generic::Custom; -""" - ) + f.write("use crate::generic::Custom;") FoldModuleVisitor(f, type_info).visit(mod) diff --git a/ast/src/gen/fold.rs b/ast/src/gen/fold.rs index 238e749..b9faa6d 100644 --- a/ast/src/gen/fold.rs +++ b/ast/src/gen/fold.rs @@ -14,7 +14,7 @@ pub trait Fold { #[cfg(not(feature = "more-attributes"))] fn map_user_cfg( &mut self, - _user: U, + _user: std::marker::PhantomData, ) -> Result, Self::Error> { Ok(std::marker::PhantomData) } @@ -111,10 +111,7 @@ pub fn fold_mod + ?Sized>( type_ignores, custom, }) => { - #[cfg(not(feature = "more-attributes"))] - let custom = std::marker::PhantomData; - #[cfg(feature = "more-attributes")] - let custom = folder.map_user(custom)?; + let custom = folder.map_user_cfg(custom)?; Ok(Mod::Module(ModModule { body: Foldable::fold(body, folder)?, type_ignores: Foldable::fold(type_ignores, folder)?, @@ -122,20 +119,14 @@ pub fn fold_mod + ?Sized>( })) } Mod::Interactive(ModInteractive { body, custom }) => { - #[cfg(not(feature = "more-attributes"))] - let custom = std::marker::PhantomData; - #[cfg(feature = "more-attributes")] - let custom = folder.map_user(custom)?; + let custom = folder.map_user_cfg(custom)?; Ok(Mod::Interactive(ModInteractive { body: Foldable::fold(body, folder)?, custom, })) } Mod::Expression(ModExpression { body, custom }) => { - #[cfg(not(feature = "more-attributes"))] - let custom = std::marker::PhantomData; - #[cfg(feature = "more-attributes")] - let custom = folder.map_user(custom)?; + let custom = folder.map_user_cfg(custom)?; Ok(Mod::Expression(ModExpression { body: Foldable::fold(body, folder)?, custom, @@ -146,10 +137,7 @@ pub fn fold_mod + ?Sized>( returns, custom, }) => { - #[cfg(not(feature = "more-attributes"))] - let custom = std::marker::PhantomData; - #[cfg(feature = "more-attributes")] - let custom = folder.map_user(custom)?; + let custom = folder.map_user_cfg(custom)?; Ok(Mod::FunctionType(ModFunctionType { argtypes: Foldable::fold(argtypes, folder)?, returns: Foldable::fold(returns, folder)?, @@ -900,10 +888,7 @@ pub fn fold_comprehension + ?Sized>( is_async, custom, } = node; - #[cfg(not(feature = "more-attributes"))] - let custom = std::marker::PhantomData; - #[cfg(feature = "more-attributes")] - let custom = folder.map_user(custom)?; + let custom = folder.map_user_cfg(custom)?; Ok(Comprehension { target: Foldable::fold(target, folder)?, iter: Foldable::fold(iter, folder)?, @@ -965,10 +950,7 @@ pub fn fold_arguments + ?Sized>( defaults, custom, } = node; - #[cfg(not(feature = "more-attributes"))] - let custom = std::marker::PhantomData; - #[cfg(feature = "more-attributes")] - let custom = folder.map_user(custom)?; + let custom = folder.map_user_cfg(custom)?; Ok(Arguments { posonlyargs: Foldable::fold(posonlyargs, folder)?, args: Foldable::fold(args, folder)?, @@ -1071,10 +1053,7 @@ pub fn fold_withitem + ?Sized>( optional_vars, custom, } = node; - #[cfg(not(feature = "more-attributes"))] - let custom = std::marker::PhantomData; - #[cfg(feature = "more-attributes")] - let custom = folder.map_user(custom)?; + let custom = folder.map_user_cfg(custom)?; Ok(Withitem { context_expr: Foldable::fold(context_expr, folder)?, optional_vars: Foldable::fold(optional_vars, folder)?, @@ -1100,10 +1079,7 @@ pub fn fold_match_case + ?Sized>( body, custom, } = node; - #[cfg(not(feature = "more-attributes"))] - let custom = std::marker::PhantomData; - #[cfg(feature = "more-attributes")] - let custom = folder.map_user(custom)?; + let custom = folder.map_user_cfg(custom)?; Ok(MatchCase { pattern: Foldable::fold(pattern, folder)?, guard: Foldable::fold(guard, folder)?, @@ -1223,10 +1199,7 @@ pub fn fold_type_ignore + ?Sized>( tag, custom, }) => { - #[cfg(not(feature = "more-attributes"))] - let custom = std::marker::PhantomData; - #[cfg(feature = "more-attributes")] - let custom = folder.map_user(custom)?; + let custom = folder.map_user_cfg(custom)?; Ok(TypeIgnore::TypeIgnore(TypeIgnoreTypeIgnore { lineno: Foldable::fold(lineno, folder)?, tag: Foldable::fold(tag, folder)?,