Skip to content

Commit

Permalink
fix: cache the scope if condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Jul 2, 2024
1 parent 07b7bad commit 6945378
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions tasks/ast_codegen/src/generators/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,14 @@ impl<'a> VisitBuilder<'a> {
.transpose()
.unwrap()
.map_or_else(Default::default, |scope_args| {
let cond = scope_args.r#if.map(|cond| {
let cond = cond.to_token_stream().replace_ident("self", &format_ident!("it"));
quote!(let scope_events_cond = #cond;)
});
let maybe_conditional = |tk: TokenStream| {
if let Some(cond) = &scope_args.r#if {
let cond =
cond.to_token_stream().replace_ident("self", &format_ident!("it"));
if cond.is_some() {
quote! {
if #cond {
if scope_events_cond {
#tk
}
}
Expand All @@ -394,8 +396,9 @@ impl<'a> VisitBuilder<'a> {
} else {
flags
};
let enter = maybe_conditional(quote! {visitor.enter_scope(#args);});
let leave = maybe_conditional(quote! {visitor.leave_scope();});
let mut enter = cond.as_ref().into_token_stream();
enter.extend(maybe_conditional(quote!(visitor.enter_scope(#args);)));
let leave = maybe_conditional(quote!(visitor.leave_scope();));
(Some(enter), Some(leave))
});
let mut entered_scope = false;
Expand Down Expand Up @@ -474,7 +477,7 @@ impl<'a> VisitBuilder<'a> {
let unused =
if fields_visits.is_empty() { Some(quote!(let _ = (visitor, it);)) } else { None };
quote! {
insert!("// TODO: AstKind doesn't exists");
insert!("// NOTE: AstKind doesn't exists!");
#(#fields_visits)*
#unused
}
Expand All @@ -490,13 +493,11 @@ impl<'a> VisitBuilder<'a> {
match (scope_enter, scope_leave, entered_scope) {
(_, Some(leave), true) => quote! {
#body
insert!("// TODO: cache this check!");
#leave
},
(Some(enter), Some(leave), false) => quote! {
#enter
#body
insert!("// TODO: cache this check!");
#leave
},
_ => body,
Expand Down Expand Up @@ -649,7 +650,6 @@ impl Parse for ScopeArgs {
fn parse_as_visit_args(attr: &Attribute) -> Vec<(Ident, TokenStream)> {
debug_assert!(attr.path().is_ident("visit_args"));
let mut result = Vec::new();
// TODO impl Punctuated<MetaNameValue, Token![,]> when we need more than one argument.
let args: MetaNameValue = attr.parse_args().expect("Invalid `visit_args` input!");
let ident = args.path.get_ident().unwrap().clone();
let value = args.value.to_token_stream();
Expand Down

0 comments on commit 6945378

Please sign in to comment.