@@ -4442,55 +4442,28 @@ impl Compiler {
44424442 /// order as symbol-table construction so the annotation scope's
44434443 /// `sub_tables` cursor stays aligned.
44444444 fn collect_annotations(body: &[ast::Stmt]) -> Vec<&ast::StmtAnnAssign> {
4445- fn walk<'a>(stmts: &'a [ast::Stmt], out: &mut Vec<&'a ast::StmtAnnAssign>) {
4446- for stmt in stmts {
4445+ use ast::visitor::Visitor;
4446+
4447+ #[derive(Default)]
4448+ struct AnnotationsVisitor<'a> {
4449+ annotations: Vec<&'a ast::StmtAnnAssign>,
4450+ }
4451+
4452+ impl<'a> Visitor<'a> for AnnotationsVisitor<'a> {
4453+ fn visit_stmt(&mut self, stmt: &'a ast::Stmt) {
44474454 match stmt {
4448- ast::Stmt::AnnAssign(stmt) => out.push(stmt),
4449- ast::Stmt::If(ast::StmtIf {
4450- body,
4451- elif_else_clauses,
4452- ..
4453- }) => {
4454- walk(body, out);
4455- for clause in elif_else_clauses {
4456- walk(&clause.body, out);
4457- }
4458- }
4459- ast::Stmt::For(ast::StmtFor { body, orelse, .. })
4460- | ast::Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
4461- walk(body, out);
4462- walk(orelse, out);
4463- }
4464- ast::Stmt::With(ast::StmtWith { body, .. }) => walk(body, out),
4465- ast::Stmt::Try(ast::StmtTry {
4466- body,
4467- handlers,
4468- orelse,
4469- finalbody,
4470- ..
4471- }) => {
4472- walk(body, out);
4473- for handler in handlers {
4474- let ast::ExceptHandler::ExceptHandler(
4475- ast::ExceptHandlerExceptHandler { body, .. },
4476- ) = handler;
4477- walk(body, out);
4478- }
4479- walk(orelse, out);
4480- walk(finalbody, out);
4481- }
4482- ast::Stmt::Match(ast::StmtMatch { cases, .. }) => {
4483- for case in cases {
4484- walk(&case.body, out);
4485- }
4486- }
4487- _ => {}
4455+ ast::Stmt::AnnAssign(ann_assign) => self.annotations.push(ann_assign),
4456+ ast::Stmt::ClassDef(_) | ast::Stmt::FunctionDef(_) => {}
4457+ _ => ast::visitor::walk_stmt(self, stmt),
44884458 }
44894459 }
44904460 }
4491- let mut annotations = Vec::new();
4492- walk(body, &mut annotations);
4493- annotations
4461+
4462+ let mut visitor = AnnotationsVisitor::default();
4463+ for stmt in body {
4464+ visitor.visit_stmt(stmt);
4465+ }
4466+ visitor.annotations
44944467 }
44954468
44964469 fn compile_annotation_for_symbol_cursor_only(
@@ -4509,12 +4482,11 @@ impl Compiler {
45094482 ) -> CompileResult<bool> {
45104483 let loc = loc.unwrap_or(self.current_source_range);
45114484 let annotations = Self::collect_annotations(body);
4512- let simple_annotation_count = annotations
4485+ let has_simple_annotation = annotations
45134486 .iter()
4514- .filter(|stmt| stmt.simple && matches!(stmt.target.as_ref(), ast::Expr::Name(_)))
4515- .count();
4487+ .any(|stmt| stmt.simple && matches!(stmt.target.as_ref(), ast::Expr::Name(_)));
45164488
4517- if simple_annotation_count == 0 {
4489+ if !has_simple_annotation {
45184490 return Ok(false);
45194491 }
45204492
0 commit comments