This commit is contained in:
Jeong YunWon 2023-05-12 05:15:55 +09:00 committed by Micha Reiser
parent 3a9abf5b9c
commit 30c67da455
No known key found for this signature in database
2 changed files with 20 additions and 53 deletions

View file

@ -425,7 +425,7 @@ class FoldTraitDefVisitor(EmitVisitor):
self.map_user(user) self.map_user(user)
} }
#[cfg(not(feature = "more-attributes"))] #[cfg(not(feature = "more-attributes"))]
fn map_user_cfg(&mut self, _user: U) -> Result<std::marker::PhantomData<Self::TargetU>, Self::Error> { fn map_user_cfg(&mut self, _user: std::marker::PhantomData<U>) -> Result<std::marker::PhantomData<Self::TargetU>, Self::Error> {
Ok(std::marker::PhantomData) Ok(std::marker::PhantomData)
} }
""", """,
@ -496,11 +496,12 @@ class FoldImplVisitor(EmitVisitor):
f"{fields_pattern[0]} {{ {fields_pattern[1]}}} {fields_pattern[2]} => {{", f"{fields_pattern[0]} {{ {fields_pattern[1]}}} {fields_pattern[2]} => {{",
depth + 2, depth + 2,
) )
if not type_info.has_attributes:
self.emit('#[cfg(not(feature = "more-attributes"))]', depth + 3) map_user_suffix = "" if type_info.has_attributes else "_cfg"
self.emit("let custom = std::marker::PhantomData;", depth + 3) self.emit(
self.emit('#[cfg(feature = "more-attributes")]', depth + 3) f"let custom = folder.map_user{map_user_suffix}(custom)?;", depth + 3
self.emit("let custom = folder.map_user(custom)?;", depth + 3) )
self.gen_construction( self.gen_construction(
fields_pattern[0], cons.fields, fields_pattern[2], depth + 3 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) fields_pattern = self.make_pattern(struct_name, struct_name, product.fields)
self.emit(f"let {struct_name} {{ {fields_pattern[1]} }} = node;", depth + 1) self.emit(f"let {struct_name} {{ {fields_pattern[1]} }} = node;", depth + 1)
if not has_attributes: map_user_suffix = "" if has_attributes else "_cfg"
self.emit('#[cfg(not(feature = "more-attributes"))]', depth + 3) self.emit(f"let custom = folder.map_user{map_user_suffix}(custom)?;", 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)
self.gen_construction(struct_name, product.fields, "", depth + 1) 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): def write_fold_def(mod, type_info, f):
f.write( f.write("use crate::generic::Custom;")
"""
use crate::generic::Custom;
"""
)
FoldModuleVisitor(f, type_info).visit(mod) FoldModuleVisitor(f, type_info).visit(mod)

View file

@ -14,7 +14,7 @@ pub trait Fold<U> {
#[cfg(not(feature = "more-attributes"))] #[cfg(not(feature = "more-attributes"))]
fn map_user_cfg( fn map_user_cfg(
&mut self, &mut self,
_user: U, _user: std::marker::PhantomData<U>,
) -> Result<std::marker::PhantomData<Self::TargetU>, Self::Error> { ) -> Result<std::marker::PhantomData<Self::TargetU>, Self::Error> {
Ok(std::marker::PhantomData) Ok(std::marker::PhantomData)
} }
@ -111,10 +111,7 @@ pub fn fold_mod<U, F: Fold<U> + ?Sized>(
type_ignores, type_ignores,
custom, custom,
}) => { }) => {
#[cfg(not(feature = "more-attributes"))] let custom = folder.map_user_cfg(custom)?;
let custom = std::marker::PhantomData;
#[cfg(feature = "more-attributes")]
let custom = folder.map_user(custom)?;
Ok(Mod::Module(ModModule { Ok(Mod::Module(ModModule {
body: Foldable::fold(body, folder)?, body: Foldable::fold(body, folder)?,
type_ignores: Foldable::fold(type_ignores, folder)?, type_ignores: Foldable::fold(type_ignores, folder)?,
@ -122,20 +119,14 @@ pub fn fold_mod<U, F: Fold<U> + ?Sized>(
})) }))
} }
Mod::Interactive(ModInteractive { body, custom }) => { Mod::Interactive(ModInteractive { body, custom }) => {
#[cfg(not(feature = "more-attributes"))] let custom = folder.map_user_cfg(custom)?;
let custom = std::marker::PhantomData;
#[cfg(feature = "more-attributes")]
let custom = folder.map_user(custom)?;
Ok(Mod::Interactive(ModInteractive { Ok(Mod::Interactive(ModInteractive {
body: Foldable::fold(body, folder)?, body: Foldable::fold(body, folder)?,
custom, custom,
})) }))
} }
Mod::Expression(ModExpression { body, custom }) => { Mod::Expression(ModExpression { body, custom }) => {
#[cfg(not(feature = "more-attributes"))] let custom = folder.map_user_cfg(custom)?;
let custom = std::marker::PhantomData;
#[cfg(feature = "more-attributes")]
let custom = folder.map_user(custom)?;
Ok(Mod::Expression(ModExpression { Ok(Mod::Expression(ModExpression {
body: Foldable::fold(body, folder)?, body: Foldable::fold(body, folder)?,
custom, custom,
@ -146,10 +137,7 @@ pub fn fold_mod<U, F: Fold<U> + ?Sized>(
returns, returns,
custom, custom,
}) => { }) => {
#[cfg(not(feature = "more-attributes"))] let custom = folder.map_user_cfg(custom)?;
let custom = std::marker::PhantomData;
#[cfg(feature = "more-attributes")]
let custom = folder.map_user(custom)?;
Ok(Mod::FunctionType(ModFunctionType { Ok(Mod::FunctionType(ModFunctionType {
argtypes: Foldable::fold(argtypes, folder)?, argtypes: Foldable::fold(argtypes, folder)?,
returns: Foldable::fold(returns, folder)?, returns: Foldable::fold(returns, folder)?,
@ -900,10 +888,7 @@ pub fn fold_comprehension<U, F: Fold<U> + ?Sized>(
is_async, is_async,
custom, custom,
} = node; } = node;
#[cfg(not(feature = "more-attributes"))] let custom = folder.map_user_cfg(custom)?;
let custom = std::marker::PhantomData;
#[cfg(feature = "more-attributes")]
let custom = folder.map_user(custom)?;
Ok(Comprehension { Ok(Comprehension {
target: Foldable::fold(target, folder)?, target: Foldable::fold(target, folder)?,
iter: Foldable::fold(iter, folder)?, iter: Foldable::fold(iter, folder)?,
@ -965,10 +950,7 @@ pub fn fold_arguments<U, F: Fold<U> + ?Sized>(
defaults, defaults,
custom, custom,
} = node; } = node;
#[cfg(not(feature = "more-attributes"))] let custom = folder.map_user_cfg(custom)?;
let custom = std::marker::PhantomData;
#[cfg(feature = "more-attributes")]
let custom = folder.map_user(custom)?;
Ok(Arguments { Ok(Arguments {
posonlyargs: Foldable::fold(posonlyargs, folder)?, posonlyargs: Foldable::fold(posonlyargs, folder)?,
args: Foldable::fold(args, folder)?, args: Foldable::fold(args, folder)?,
@ -1071,10 +1053,7 @@ pub fn fold_withitem<U, F: Fold<U> + ?Sized>(
optional_vars, optional_vars,
custom, custom,
} = node; } = node;
#[cfg(not(feature = "more-attributes"))] let custom = folder.map_user_cfg(custom)?;
let custom = std::marker::PhantomData;
#[cfg(feature = "more-attributes")]
let custom = folder.map_user(custom)?;
Ok(Withitem { Ok(Withitem {
context_expr: Foldable::fold(context_expr, folder)?, context_expr: Foldable::fold(context_expr, folder)?,
optional_vars: Foldable::fold(optional_vars, folder)?, optional_vars: Foldable::fold(optional_vars, folder)?,
@ -1100,10 +1079,7 @@ pub fn fold_match_case<U, F: Fold<U> + ?Sized>(
body, body,
custom, custom,
} = node; } = node;
#[cfg(not(feature = "more-attributes"))] let custom = folder.map_user_cfg(custom)?;
let custom = std::marker::PhantomData;
#[cfg(feature = "more-attributes")]
let custom = folder.map_user(custom)?;
Ok(MatchCase { Ok(MatchCase {
pattern: Foldable::fold(pattern, folder)?, pattern: Foldable::fold(pattern, folder)?,
guard: Foldable::fold(guard, folder)?, guard: Foldable::fold(guard, folder)?,
@ -1223,10 +1199,7 @@ pub fn fold_type_ignore<U, F: Fold<U> + ?Sized>(
tag, tag,
custom, custom,
}) => { }) => {
#[cfg(not(feature = "more-attributes"))] let custom = folder.map_user_cfg(custom)?;
let custom = std::marker::PhantomData;
#[cfg(feature = "more-attributes")]
let custom = folder.map_user(custom)?;
Ok(TypeIgnore::TypeIgnore(TypeIgnoreTypeIgnore { Ok(TypeIgnore::TypeIgnore(TypeIgnoreTypeIgnore {
lineno: Foldable::fold(lineno, folder)?, lineno: Foldable::fold(lineno, folder)?,
tag: Foldable::fold(tag, folder)?, tag: Foldable::fold(tag, folder)?,