From 4fea5c50369e2c9531209b14ea83847170523798 Mon Sep 17 00:00:00 2001 From: Joshua Batty Date: Wed, 20 Mar 2024 10:52:15 +1100 Subject: [PATCH 1/8] add adaptive_iter to lexed and parsed ast traversal --- sway-lsp/src/traverse/lexed_tree.rs | 86 ++--- sway-lsp/src/traverse/mod.rs | 16 + sway-lsp/src/traverse/parsed_tree.rs | 516 +++++++++++++++++---------- 3 files changed, 370 insertions(+), 248 deletions(-) diff --git a/sway-lsp/src/traverse/lexed_tree.rs b/sway-lsp/src/traverse/lexed_tree.rs index 80a991f9bf8..37c6befab3c 100644 --- a/sway-lsp/src/traverse/lexed_tree.rs +++ b/sway-lsp/src/traverse/lexed_tree.rs @@ -1,6 +1,6 @@ use crate::{ core::token::{AstToken, SymbolKind, Token}, - traverse::{Parse, ParseContext}, + traverse::{Parse, ParseContext, adaptive_iter}, }; use rayon::iter::{IntoParallelRefIterator, ParallelBridge, ParallelIterator}; use sway_ast::{ @@ -14,25 +14,22 @@ use sway_ast::{ use sway_core::language::{lexed::LexedProgram, HasSubmodules}; use sway_types::{Ident, Span, Spanned}; + + pub fn parse(lexed_program: &LexedProgram, ctx: &ParseContext) { insert_module_kind(ctx, &lexed_program.root.tree.kind); - lexed_program - .root - .tree - .items - .par_iter() - .for_each(|item| item.value.parse(ctx)); + adaptive_iter(&lexed_program.root.tree.items, |item| { + item.value.parse(ctx); + }); lexed_program .root .submodules_recursive() .for_each(|(_, dep)| { insert_module_kind(ctx, &dep.module.tree.kind); - dep.module - .tree - .items - .par_iter() - .for_each(|item| item.value.parse(ctx)); + adaptive_iter(&dep.module.tree.items, |item| { + item.value.parse(ctx); + }); }); } @@ -157,10 +154,14 @@ impl Parse for Expr { } => { insert_keyword(ctx, match_token.span()); value.parse(ctx); - branches.get().iter().par_bridge().for_each(|branch| { + adaptive_iter(branches.get(), |branch| { branch.pattern.parse(ctx); branch.kind.parse(ctx); }); + // branches.get().iter().par_bridge().for_each(|branch| { + // branch.pattern.parse(ctx); + // branch.kind.parse(ctx); + // }); } Expr::While { while_token, @@ -326,21 +327,15 @@ impl Parse for ItemTrait { insert_keyword(ctx, where_clause_opt.where_token.span()); } - self.trait_items - .get() - .par_iter() - .for_each(|annotated| match &annotated.value { - sway_ast::ItemTraitItem::Fn(fn_sig, _) => fn_sig.parse(ctx), - sway_ast::ItemTraitItem::Const(item_const, _) => item_const.parse(ctx), - sway_ast::ItemTraitItem::Type(item_type, _) => item_type.parse(ctx), - sway_ast::ItemTraitItem::Error(_, _) => {} - }); + adaptive_iter(self.trait_items.get(), |annotated| match &annotated.value { + sway_ast::ItemTraitItem::Fn(fn_sig, _) => fn_sig.parse(ctx), + sway_ast::ItemTraitItem::Const(item_const, _) => item_const.parse(ctx), + sway_ast::ItemTraitItem::Type(item_type, _) => item_type.parse(ctx), + sway_ast::ItemTraitItem::Error(_, _) => {} + }); if let Some(trait_defs_opt) = &self.trait_defs_opt { - trait_defs_opt - .get() - .par_iter() - .for_each(|item| item.value.parse(ctx)); + adaptive_iter(trait_defs_opt.get(), |item| item.value.parse(ctx)); } } } @@ -359,14 +354,11 @@ impl Parse for ItemImpl { insert_keyword(ctx, where_clause_opt.where_token.span()); } - self.contents - .get() - .par_iter() - .for_each(|item| match &item.value { - ItemImplItem::Fn(fn_decl) => fn_decl.parse(ctx), - ItemImplItem::Const(const_decl) => const_decl.parse(ctx), - ItemImplItem::Type(type_decl) => type_decl.parse(ctx), - }); + adaptive_iter(self.contents.get(), |item| match &item.value { + ItemImplItem::Fn(fn_decl) => fn_decl.parse(ctx), + ItemImplItem::Const(const_decl) => const_decl.parse(ctx), + ItemImplItem::Type(type_decl) => type_decl.parse(ctx), + }); } } @@ -374,21 +366,15 @@ impl Parse for ItemAbi { fn parse(&self, ctx: &ParseContext) { insert_keyword(ctx, self.abi_token.span()); - self.abi_items - .get() - .par_iter() - .for_each(|annotated| match &annotated.value { - sway_ast::ItemTraitItem::Fn(fn_sig, _) => fn_sig.parse(ctx), - sway_ast::ItemTraitItem::Const(item_const, _) => item_const.parse(ctx), - sway_ast::ItemTraitItem::Type(item_type, _) => item_type.parse(ctx), - sway_ast::ItemTraitItem::Error(_, _) => {} - }); + adaptive_iter(self.abi_items.get(), |annotated| match &annotated.value { + sway_ast::ItemTraitItem::Fn(fn_sig, _) => fn_sig.parse(ctx), + sway_ast::ItemTraitItem::Const(item_const, _) => item_const.parse(ctx), + sway_ast::ItemTraitItem::Type(item_type, _) => item_type.parse(ctx), + sway_ast::ItemTraitItem::Error(_, _) => {} + }); if let Some(abi_defs_opt) = self.abi_defs_opt.as_ref() { - abi_defs_opt - .get() - .par_iter() - .for_each(|item| item.value.parse(ctx)); + adaptive_iter(abi_defs_opt.get(), |item| item.value.parse(ctx)); } } } @@ -576,9 +562,9 @@ impl Parse for FnArg { impl Parse for CodeBlockContents { fn parse(&self, ctx: &ParseContext) { - self.statements - .par_iter() - .for_each(|statement| statement.parse(ctx)); + adaptive_iter(&self.statements, |statement| { + statement.parse(ctx); + }); if let Some(expr) = self.final_expr_opt.as_ref() { expr.parse(ctx); } diff --git a/sway-lsp/src/traverse/mod.rs b/sway-lsp/src/traverse/mod.rs index e2f3121ef13..fe69a06a2c7 100644 --- a/sway-lsp/src/traverse/mod.rs +++ b/sway-lsp/src/traverse/mod.rs @@ -1,4 +1,5 @@ use crate::core::{token::TokenIdent, token_map::TokenMap}; +use rayon_cond::CondIterator; use sway_core::{namespace::Module, Engines}; pub(crate) mod dependency; @@ -30,3 +31,18 @@ impl<'a> ParseContext<'a> { pub trait Parse { fn parse(&self, ctx: &ParseContext); } + +const PARALLEL_THRESHOLD: usize = 10; + +/// Iterates over items, choosing parallel or sequential execution based on size. +pub fn adaptive_iter(items: &[T], action: F) +where + T: Sync + Send, // Required for parallel processing + F: Fn(&T) + Sync + Send, // Action to be applied to each item +{ + // Determine if the length meets the parallel threshold + let use_parallel = items.len() >= PARALLEL_THRESHOLD; + + // Create a conditional iterator based on the use_parallel flag + CondIterator::new(items, use_parallel).for_each(action); +} \ No newline at end of file diff --git a/sway-lsp/src/traverse/parsed_tree.rs b/sway-lsp/src/traverse/parsed_tree.rs index b2da3de1c0b..2d63e33f0f8 100644 --- a/sway-lsp/src/traverse/parsed_tree.rs +++ b/sway-lsp/src/traverse/parsed_tree.rs @@ -7,7 +7,7 @@ use crate::{ }, token_map::TokenMap, }, - traverse::{Parse, ParseContext}, + traverse::{Parse, ParseContext, adaptive_iter}, }; use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use sway_core::{ @@ -143,7 +143,7 @@ impl Parse for UseStatement { Token::from_parsed(AstToken::UseStatement(self.clone()), SymbolKind::Unknown), ); } - self.call_path.par_iter().for_each(|prefix| { + adaptive_iter(&self.call_path, |prefix| { ctx.tokens.insert( ctx.ident(prefix), Token::from_parsed(AstToken::UseStatement(self.clone()), SymbolKind::Module), @@ -183,7 +183,7 @@ impl Parse for Expression { fn parse(&self, ctx: &ParseContext) { match &self.kind { ExpressionKind::Error(part_spans, _) => { - part_spans.par_iter().for_each(|span| { + adaptive_iter(part_spans, |span| { ctx.tokens.insert( ctx.ident(&Ident::new(span.clone())), Token::from_parsed( @@ -192,6 +192,15 @@ impl Parse for Expression { ), ); }); + // part_spans.par_iter().for_each(|span| { + // ctx.tokens.insert( + // ctx.ident(&Ident::new(span.clone())), + // Token::from_parsed( + // AstToken::ErrorRecovery(span.clone()), + // SymbolKind::Unknown, + // ), + // ); + // }); } ExpressionKind::Literal(value) => { let symbol_kind = literal_to_symbol_kind(value); @@ -225,7 +234,8 @@ impl Parse for Expression { } } ExpressionKind::Tuple(fields) => { - fields.par_iter().for_each(|field| field.parse(ctx)); + adaptive_iter(fields, |field| field.parse(ctx)); + // fields.par_iter().for_each(|field| field.parse(ctx)); } ExpressionKind::TupleIndex(TupleIndexExpression { prefix, index_span, .. @@ -246,10 +256,11 @@ impl Parse for Expression { struct_expression.parse(ctx); } ExpressionKind::CodeBlock(code_block) => { - code_block - .contents - .par_iter() - .for_each(|node| node.parse(ctx)); + adaptive_iter(&code_block.contents, |node| node.parse(ctx)); + // code_block + // .contents + // .par_iter() + // .for_each(|node| node.parse(ctx)); } ExpressionKind::If(IfExpression { condition, @@ -267,17 +278,26 @@ impl Parse for Expression { value, branches, .. }) => { value.parse(ctx); - branches.par_iter().for_each(|branch| { + adaptive_iter(branches, |branch| { branch.scrutinee.parse(ctx); branch.result.parse(ctx); }); + // branches.par_iter().for_each(|branch| { + // branch.scrutinee.parse(ctx); + // branch.result.parse(ctx); + // }); } ExpressionKind::Asm(asm) => { - asm.registers.par_iter().for_each(|register| { + adaptive_iter(&asm.registers, |register| { if let Some(initializer) = ®ister.initializer { initializer.parse(ctx); } }); + // asm.registers.par_iter().for_each(|register| { + // if let Some(initializer) = ®ister.initializer { + // initializer.parse(ctx); + // } + // }); } ExpressionKind::MethodApplication(method_application_expression) => { method_application_expression.parse(ctx); @@ -321,8 +341,7 @@ impl Parse for Expression { ctx.ident(&storage_ident), Token::from_parsed(AstToken::Ident(storage_ident), SymbolKind::Unknown), ); - - field_names.par_iter().for_each(|field_name| { + adaptive_iter(field_names, |field_name| { ctx.tokens.insert( ctx.ident(field_name), Token::from_parsed(AstToken::Ident(field_name.clone()), SymbolKind::Field), @@ -335,7 +354,7 @@ impl Parse for Expression { ExpressionKind::WhileLoop(WhileLoopExpression { body, condition, .. }) => { - body.contents.par_iter().for_each(|node| node.parse(ctx)); + adaptive_iter(&body.contents, |node| node.parse(ctx)); condition.parse(ctx); } ExpressionKind::ForLoop(ForLoopExpression { desugared }) => { @@ -377,18 +396,14 @@ impl Parse for IntrinsicFunctionExpression { SymbolKind::Intrinsic, ), ); - self.arguments.par_iter().for_each(|arg| arg.parse(ctx)); - self.kind_binding - .type_arguments - .to_vec() - .par_iter() - .for_each(|type_arg| type_arg.parse(ctx)); + adaptive_iter(&self.arguments, |arg| arg.parse(ctx)); + adaptive_iter(&self.kind_binding.type_arguments.to_vec(), |type_arg| type_arg.parse(ctx)); } } impl Parse for AbiCastExpression { fn parse(&self, ctx: &ParseContext) { - self.abi_name.prefixes.par_iter().for_each(|ident| { + adaptive_iter(&self.abi_name.prefixes, |ident| { ctx.tokens.insert( ctx.ident(ident), Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Module), @@ -408,17 +423,23 @@ impl Parse for DelineatedPathExpression { call_path_binding, args, } = self; - call_path_binding - .inner - .call_path - .prefixes - .par_iter() - .for_each(|ident| { - ctx.tokens.insert( - ctx.ident(ident), - Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Enum), - ); - }); + adaptive_iter(&call_path_binding.inner.call_path.prefixes, |ident| { + ctx.tokens.insert( + ctx.ident(ident), + Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Enum), + ); + }); + // call_path_binding + // .inner + // .call_path + // .prefixes + // .par_iter() + // .for_each(|ident| { + // ctx.tokens.insert( + // ctx.ident(ident), + // Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Enum), + // ); + // }); ctx.tokens.insert( ctx.ident(&call_path_binding.inner.call_path.suffix), Token::from_parsed( @@ -426,17 +447,19 @@ impl Parse for DelineatedPathExpression { SymbolKind::Variant, ), ); - call_path_binding - .type_arguments - .to_vec() - .par_iter() - .for_each(|type_arg| { - type_arg.parse(ctx); - }); + adaptive_iter(&call_path_binding.type_arguments.to_vec(), |type_arg| type_arg.parse(ctx)); + // call_path_binding + // .type_arguments + // .to_vec() + // .par_iter() + // .for_each(|type_arg| { + // type_arg.parse(ctx); + // }); if let Some(args_vec) = args.as_ref() { - args_vec.par_iter().for_each(|exp| { - exp.parse(ctx); - }); + adaptive_iter(args_vec, |exp| exp.parse(ctx)); + // args_vec.par_iter().for_each(|exp| { + // exp.parse(ctx); + // }); } collect_qualified_path_root(ctx, call_path_binding.inner.qualified_path_root.clone()); } @@ -469,14 +492,16 @@ impl Parse for AmbiguousPathExpression { SymbolKind::Variant, ), ); - call_path_binding - .type_arguments - .to_vec() - .par_iter() - .for_each(|type_arg| { - type_arg.parse(ctx); - }); - args.par_iter().for_each(|arg| arg.parse(ctx)); + adaptive_iter(&call_path_binding.type_arguments.to_vec(), |type_arg| type_arg.parse(ctx)); + // call_path_binding + // .type_arguments + // .to_vec() + // .par_iter() + // .for_each(|type_arg| { + // type_arg.parse(ctx); + // }); + adaptive_iter(args, |exp| exp.parse(ctx)); + // args.par_iter().for_each(|arg| arg.parse(ctx)); collect_qualified_path_root(ctx, qualified_path_root.clone().map(Box::new)); } } @@ -497,13 +522,16 @@ impl Parse for MethodApplicationExpression { let (type_info, ident) = &call_path_binding.inner.suffix; collect_type_info_token(ctx, type_info, Some(&ident.span())); } - self.method_name_binding - .type_arguments - .to_vec() - .par_iter() - .for_each(|type_arg| { - type_arg.parse(ctx); - }); + adaptive_iter(&self.method_name_binding.type_arguments.to_vec(), |type_arg| { + type_arg.parse(ctx); + }); + // self.method_name_binding + // .type_arguments + // .to_vec() + // .par_iter() + // .for_each(|type_arg| { + // type_arg.parse(ctx); + // }); // Don't collect applications of desugared operators due to mismatched ident lengths. if !desugared_op(&prefixes) { ctx.tokens.insert( @@ -514,10 +542,12 @@ impl Parse for MethodApplicationExpression { ), ); } - self.arguments.par_iter().for_each(|arg| arg.parse(ctx)); - self.contract_call_params - .par_iter() - .for_each(|param| param.parse(ctx)); + adaptive_iter(&self.arguments, |arg| arg.parse(ctx)); + // self.arguments.par_iter().for_each(|arg| arg.parse(ctx)); + adaptive_iter(&self.contract_call_params, |param| param.parse(ctx)); + // self.contract_call_params + // .par_iter() + // .for_each(|param| param.parse(ctx)); } } @@ -545,26 +575,38 @@ impl Parse for Scrutinee { fields, .. } => { - struct_name.prefixes.par_iter().for_each(|ident| { + adaptive_iter(&struct_name.prefixes, |ident| { let token = Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Struct); ctx.tokens.insert(ctx.ident(ident), token); }); + // struct_name.prefixes.par_iter().for_each(|ident| { + // let token = + // Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Struct); + // ctx.tokens.insert(ctx.ident(ident), token); + // }); ctx.tokens.insert( ctx.ident(&struct_name.suffix), Token::from_parsed(AstToken::Scrutinee(self.clone()), SymbolKind::Struct), ); - fields.par_iter().for_each(|field| field.parse(ctx)); + adaptive_iter(fields, |field| field.parse(ctx)); + // fields.par_iter().for_each(|field| field.parse(ctx)); } Scrutinee::EnumScrutinee { call_path, value, .. } => { - call_path.prefixes.par_iter().for_each(|ident| { + adaptive_iter(&call_path.prefixes, |ident| { ctx.tokens.insert( ctx.ident(ident), Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Enum), ); }); + // call_path.prefixes.par_iter().for_each(|ident| { + // ctx.tokens.insert( + // ctx.ident(ident), + // Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Enum), + // ); + // }); let token = Token::from_parsed(AstToken::Scrutinee(self.clone()), SymbolKind::Variant); ctx.tokens.insert(ctx.ident(&call_path.suffix), token); @@ -575,7 +617,8 @@ impl Parse for Scrutinee { ctx.tokens.insert(ctx.ident(ident), token); } Scrutinee::Tuple { elems, .. } | Scrutinee::Or { elems, .. } => { - elems.par_iter().for_each(|elem| elem.parse(ctx)); + adaptive_iter(elems, |elem| elem.parse(ctx)); + // elems.par_iter().for_each(|elem| elem.parse(ctx)); } Scrutinee::Error { .. } => { // FIXME: Left for @JoshuaBatty to use. @@ -604,16 +647,22 @@ impl Parse for StructScrutineeField { impl Parse for StructExpression { fn parse(&self, ctx: &ParseContext) { - self.call_path_binding - .inner - .prefixes - .par_iter() - .for_each(|ident| { - ctx.tokens.insert( - ctx.ident(ident), - Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Struct), - ); - }); + adaptive_iter(&self.call_path_binding.inner.prefixes, |ident| { + ctx.tokens.insert( + ctx.ident(ident), + Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Struct), + ); + }); + // self.call_path_binding + // .inner + // .prefixes + // .par_iter() + // .for_each(|ident| { + // ctx.tokens.insert( + // ctx.ident(ident), + // Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Struct), + // ); + // }); let name = &self.call_path_binding.inner.suffix; let symbol_kind = if name.as_str() == "Self" { SymbolKind::SelfKeyword @@ -625,10 +674,12 @@ impl Parse for StructExpression { Token::from_parsed(AstToken::StructExpression(self.clone()), symbol_kind), ); let type_arguments = &self.call_path_binding.type_arguments.to_vec(); - type_arguments.par_iter().for_each(|type_arg| { - type_arg.parse(ctx); - }); - self.fields.par_iter().for_each(|field| field.parse(ctx)); + adaptive_iter(type_arguments, |type_arg| type_arg.parse(ctx)); + // type_arguments.par_iter().for_each(|type_arg| { + // type_arg.parse(ctx); + // }); + adaptive_iter(&self.fields, |field| field.parse(ctx)); + // self.fields.par_iter().for_each(|field| field.parse(ctx)); } } @@ -647,7 +698,8 @@ impl Parse for StructExpressionField { impl Parse for ArrayExpression { fn parse(&self, ctx: &ParseContext) { - self.contents.par_iter().for_each(|exp| exp.parse(ctx)); + adaptive_iter(&self.contents, |exp| exp.parse(ctx)); + // self.contents.par_iter().for_each(|exp| exp.parse(ctx)); if let Some(length_span) = &self.length_span { let ident = Ident::new(length_span.clone()); ctx.tokens.insert( @@ -662,16 +714,22 @@ impl Parse for FunctionApplicationExpression { fn parse(&self, ctx: &ParseContext) { // Don't collect applications of desugared operators due to mismatched ident lengths. if !desugared_op(&self.call_path_binding.inner.prefixes) { - self.call_path_binding - .inner - .prefixes - .par_iter() - .for_each(|ident| { - ctx.tokens.insert( - ctx.ident(ident), - Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Module), - ); - }); + adaptive_iter(&self.call_path_binding.inner.prefixes, |ident| { + ctx.tokens.insert( + ctx.ident(ident), + Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Module), + ); + }); + // self.call_path_binding + // .inner + // .prefixes + // .par_iter() + // .for_each(|ident| { + // ctx.tokens.insert( + // ctx.ident(ident), + // Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Module), + // ); + // }); ctx.tokens.insert( ctx.ident(&self.call_path_binding.inner.suffix), Token::from_parsed( @@ -679,17 +737,21 @@ impl Parse for FunctionApplicationExpression { SymbolKind::Function, ), ); - self.call_path_binding - .type_arguments - .to_vec() - .par_iter() - .for_each(|type_arg| { - type_arg.parse(ctx); - }); + adaptive_iter(&self.call_path_binding.type_arguments.to_vec(), |type_arg| { + type_arg.parse(ctx); + }); + // self.call_path_binding + // .type_arguments + // .to_vec() + // .par_iter() + // .for_each(|type_arg| { + // type_arg.parse(ctx); + // }); } - self.arguments.par_iter().for_each(|exp| { - exp.parse(ctx); - }); + adaptive_iter(&self.arguments, |exp| exp.parse(ctx)); + // self.arguments.par_iter().for_each(|exp| { + // exp.parse(ctx); + // }); } } @@ -737,20 +799,24 @@ impl Parse for ParsedDeclId { let fn_decl = ctx.engines.pe().get_function(self); ctx.tokens.insert(ctx.ident(&fn_decl.name), token.clone()); - fn_decl.body.contents.par_iter().for_each(|node| { - node.parse(ctx); - }); - fn_decl.parameters.par_iter().for_each(|param| { - param.parse(ctx); - }); - fn_decl.type_parameters.par_iter().for_each(|type_param| { - type_param.parse(ctx); - }); + adaptive_iter(&fn_decl.body.contents, |node| node.parse(ctx)); + // fn_decl.body.contents.par_iter().for_each(|node| { + // node.parse(ctx); + // }); + adaptive_iter(&fn_decl.parameters, |param| param.parse(ctx)); + // fn_decl.parameters.par_iter().for_each(|param| { + // param.parse(ctx); + // }); + adaptive_iter(&fn_decl.type_parameters, |type_param| type_param.parse(ctx)); + // fn_decl.type_parameters.par_iter().for_each(|type_param| { + // type_param.parse(ctx); + // }); for (ident, constraints) in &fn_decl.where_clause { ctx.tokens.insert(ctx.ident(ident), token.clone()); - constraints.par_iter().for_each(|constraint| { - constraint.parse(ctx); - }); + adaptive_iter(constraints, |constraint| constraint.parse(ctx)); + // constraints.par_iter().for_each(|constraint| { + // constraint.parse(ctx); + // }); } fn_decl.return_type.parse(ctx); fn_decl.attributes.parse(ctx); @@ -767,21 +833,29 @@ impl Parse for ParsedDeclId { SymbolKind::Trait, ), ); - trait_decl - .interface_surface - .par_iter() - .for_each(|item| match item { - TraitItem::TraitFn(trait_fn) => trait_fn.parse(ctx), - TraitItem::Constant(const_decl) => const_decl.parse(ctx), - TraitItem::Type(trait_type) => trait_type.parse(ctx), - TraitItem::Error(_, _) => {} - }); - trait_decl.methods.par_iter().for_each(|func_dec| { - func_dec.parse(ctx); - }); - trait_decl.supertraits.par_iter().for_each(|supertrait| { - supertrait.parse(ctx); + adaptive_iter(&trait_decl.interface_surface, |item| match item { + TraitItem::TraitFn(trait_fn) => trait_fn.parse(ctx), + TraitItem::Constant(const_decl) => const_decl.parse(ctx), + TraitItem::Type(trait_type) => trait_type.parse(ctx), + TraitItem::Error(_, _) => {} }); + // trait_decl + // .interface_surface + // .par_iter() + // .for_each(|item| match item { + // TraitItem::TraitFn(trait_fn) => trait_fn.parse(ctx), + // TraitItem::Constant(const_decl) => const_decl.parse(ctx), + // TraitItem::Type(trait_type) => trait_type.parse(ctx), + // TraitItem::Error(_, _) => {} + // }); + adaptive_iter(&trait_decl.methods, |func_dec| func_dec.parse(ctx)); + // trait_decl.methods.par_iter().for_each(|func_dec| { + // func_dec.parse(ctx); + // }); + adaptive_iter(&trait_decl.supertraits, |supertrait| supertrait.parse(ctx)); + // trait_decl.supertraits.par_iter().for_each(|supertrait| { + // supertrait.parse(ctx); + // }); } } @@ -795,15 +869,17 @@ impl Parse for ParsedDeclId { SymbolKind::Struct, ), ); - struct_decl.fields.par_iter().for_each(|field| { - field.parse(ctx); - }); - struct_decl - .type_parameters - .par_iter() - .for_each(|type_param| { - type_param.parse(ctx); - }); + adaptive_iter(&struct_decl.fields, |field| field.parse(ctx)); + // struct_decl.fields.par_iter().for_each(|field| { + // field.parse(ctx); + // }); + adaptive_iter(&struct_decl.type_parameters, |type_param| type_param.parse(ctx)); + // struct_decl + // .type_parameters + // .par_iter() + // .for_each(|type_param| { + // type_param.parse(ctx); + // }); struct_decl.attributes.parse(ctx); } } @@ -818,12 +894,14 @@ impl Parse for ParsedDeclId { SymbolKind::Enum, ), ); - enum_decl.type_parameters.par_iter().for_each(|type_param| { - type_param.parse(ctx); - }); - enum_decl.variants.par_iter().for_each(|variant| { - variant.parse(ctx); - }); + adaptive_iter(&enum_decl.type_parameters, |type_param| type_param.parse(ctx)); + // enum_decl.type_parameters.par_iter().for_each(|type_param| { + // type_param.parse(ctx); + // }); + adaptive_iter(&enum_decl.variants, |variant| variant.parse(ctx)); + // enum_decl.variants.par_iter().for_each(|variant| { + // variant.parse(ctx); + // }); enum_decl.attributes.parse(ctx); } } @@ -831,12 +909,18 @@ impl Parse for ParsedDeclId { impl Parse for ParsedDeclId { fn parse(&self, ctx: &ParseContext) { let impl_trait = ctx.engines.pe().get_impl_trait(self); - impl_trait.trait_name.prefixes.par_iter().for_each(|ident| { + adaptive_iter(&impl_trait.trait_name.prefixes, |ident| { ctx.tokens.insert( ctx.ident(ident), Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Module), ); }); + // impl_trait.trait_name.prefixes.par_iter().for_each(|ident| { + // ctx.tokens.insert( + // ctx.ident(ident), + // Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Module), + // ); + // }); ctx.tokens.insert( ctx.ident(&impl_trait.trait_name.suffix), Token::from_parsed( @@ -845,17 +929,23 @@ impl Parse for ParsedDeclId { ), ); impl_trait.implementing_for.parse(ctx); - impl_trait - .impl_type_parameters - .par_iter() - .for_each(|type_param| { - type_param.parse(ctx); - }); - impl_trait.items.par_iter().for_each(|item| match item { + adaptive_iter(&impl_trait.impl_type_parameters, |type_param| type_param.parse(ctx)); + // impl_trait + // .impl_type_parameters + // .par_iter() + // .for_each(|type_param| { + // type_param.parse(ctx); + // }); + adaptive_iter(&impl_trait.items, |item| match item { ImplItem::Fn(fn_decl) => fn_decl.parse(ctx), ImplItem::Constant(const_decl) => const_decl.parse(ctx), ImplItem::Type(type_decl) => type_decl.parse(ctx), }); + // impl_trait.items.par_iter().for_each(|item| match item { + // ImplItem::Fn(fn_decl) => fn_decl.parse(ctx), + // ImplItem::Constant(const_decl) => const_decl.parse(ctx), + // ImplItem::Type(type_decl) => type_decl.parse(ctx), + // }); } } @@ -876,22 +966,29 @@ impl Parse for ParsedDeclId { ), ); if let Some(type_arguments) = type_arguments { - type_arguments.par_iter().for_each(|type_arg| { - type_arg.parse(ctx); - }); + adaptive_iter(type_arguments, |type_arg| type_arg.parse(ctx)); + // type_arguments.par_iter().for_each(|type_arg| { + // type_arg.parse(ctx); + // }); } } - impl_self - .impl_type_parameters - .par_iter() - .for_each(|type_param| { - type_param.parse(ctx); - }); - impl_self.items.par_iter().for_each(|item| match item { + adaptive_iter(&impl_self.impl_type_parameters, |type_param| type_param.parse(ctx)); + // impl_self + // .impl_type_parameters + // .par_iter() + // .for_each(|type_param| { + // type_param.parse(ctx); + // }); + adaptive_iter(&impl_self.items, |item| match item { ImplItem::Fn(fn_decl) => fn_decl.parse(ctx), ImplItem::Constant(const_decl) => const_decl.parse(ctx), ImplItem::Type(type_decl) => type_decl.parse(ctx), }); + // impl_self.items.par_iter().for_each(|item| match item { + // ImplItem::Fn(fn_decl) => fn_decl.parse(ctx), + // ImplItem::Constant(const_decl) => const_decl.parse(ctx), + // ImplItem::Type(type_decl) => type_decl.parse(ctx), + // }); } } @@ -905,18 +1002,25 @@ impl Parse for ParsedDeclId { SymbolKind::Trait, ), ); - abi_decl - .interface_surface - .par_iter() - .for_each(|item| match item { - TraitItem::TraitFn(trait_fn) => trait_fn.parse(ctx), - TraitItem::Constant(const_decl) => const_decl.parse(ctx), - TraitItem::Type(type_decl) => type_decl.parse(ctx), - TraitItem::Error(_, _) => {} - }); - abi_decl.supertraits.par_iter().for_each(|supertrait| { - supertrait.parse(ctx); + adaptive_iter(&abi_decl.interface_surface, |item| match item { + TraitItem::TraitFn(trait_fn) => trait_fn.parse(ctx), + TraitItem::Constant(const_decl) => const_decl.parse(ctx), + TraitItem::Type(type_decl) => type_decl.parse(ctx), + TraitItem::Error(_, _) => {} }); + // abi_decl + // .interface_surface + // .par_iter() + // .for_each(|item| match item { + // TraitItem::TraitFn(trait_fn) => trait_fn.parse(ctx), + // TraitItem::Constant(const_decl) => const_decl.parse(ctx), + // TraitItem::Type(type_decl) => type_decl.parse(ctx), + // TraitItem::Error(_, _) => {} + // }); + adaptive_iter(&abi_decl.supertraits, |supertrait| supertrait.parse(ctx)); + // abi_decl.supertraits.par_iter().for_each(|supertrait| { + // supertrait.parse(ctx); + // }); abi_decl.attributes.parse(ctx); } } @@ -959,9 +1063,10 @@ impl Parse for ParsedDeclId { impl Parse for ParsedDeclId { fn parse(&self, ctx: &ParseContext) { let storage_decl = ctx.engines.pe().get_storage(self); - storage_decl.fields.par_iter().for_each(|field| { - field.parse(ctx); - }); + adaptive_iter(&storage_decl.fields, |field| field.parse(ctx)); + // storage_decl.fields.par_iter().for_each(|field| { + // field.parse(ctx); + // }); storage_decl.attributes.parse(ctx); } } @@ -994,9 +1099,10 @@ impl Parse for ParsedDeclId { ctx.ident(&trait_fn.name), Token::from_parsed(AstToken::TraitFn(*self), SymbolKind::Function), ); - trait_fn.parameters.par_iter().for_each(|param| { - param.parse(ctx); - }); + adaptive_iter(&trait_fn.parameters, |param| param.parse(ctx)); + // trait_fn.parameters.par_iter().for_each(|param| { + // param.parse(ctx); + // }); trait_fn.return_type.parse(ctx); trait_fn.attributes.parse(ctx); } @@ -1004,12 +1110,18 @@ impl Parse for ParsedDeclId { impl Parse for TraitConstraint { fn parse(&self, ctx: &ParseContext) { - self.trait_name.prefixes.par_iter().for_each(|prefix| { + adaptive_iter(&self.trait_name.prefixes, |prefix| { ctx.tokens.insert( ctx.ident(prefix), Token::from_parsed(AstToken::Ident(prefix.clone()), SymbolKind::Function), ); }); + // self.trait_name.prefixes.par_iter().for_each(|prefix| { + // ctx.tokens.insert( + // ctx.ident(prefix), + // Token::from_parsed(AstToken::Ident(prefix.clone()), SymbolKind::Function), + // ); + // }); ctx.tokens.insert( ctx.ident(&self.trait_name.suffix), Token::from_parsed( @@ -1017,9 +1129,10 @@ impl Parse for TraitConstraint { SymbolKind::Function, ), ); - self.type_arguments.par_iter().for_each(|type_arg| { - type_arg.parse(ctx); - }); + adaptive_iter(&self.type_arguments, |type_arg| type_arg.parse(ctx)); + // self.type_arguments.par_iter().for_each(|type_arg| { + // type_arg.parse(ctx); + // }); } } @@ -1083,9 +1196,10 @@ impl Parse for TypeArgument { type_arg.parse(ctx); } TypeInfo::Tuple(type_arguments) => { - type_arguments.par_iter().for_each(|type_arg| { - type_arg.parse(ctx); - }); + adaptive_iter(type_arguments, |type_arg| type_arg.parse(ctx)); + // type_arguments.par_iter().for_each(|type_arg| { + // type_arg.parse(ctx); + // }); } _ => { let symbol_kind = type_info_to_symbol_kind(ctx.engines.te(), &type_info, None); @@ -1133,9 +1247,10 @@ fn collect_type_info_token(ctx: &ParseContext, type_info: &TypeInfo, type_span: type_arg.parse(ctx); } TypeInfo::Tuple(type_arguments) => { - type_arguments.par_iter().for_each(|type_arg| { - type_arg.parse(ctx); - }); + adaptive_iter(type_arguments, |type_arg| type_arg.parse(ctx)); + // type_arguments.par_iter().for_each(|type_arg| { + // type_arg.parse(ctx); + // }); } TypeInfo::Custom { qualified_call_path, @@ -1149,9 +1264,10 @@ fn collect_type_info_token(ctx: &ParseContext, type_info: &TypeInfo, type_span: token.type_def = Some(TypeDefinition::Ident(ident.clone())); ctx.tokens.insert(ctx.ident(&ident), token); if let Some(type_arguments) = type_arguments { - type_arguments.par_iter().for_each(|type_arg| { - type_arg.parse(ctx); - }); + adaptive_iter(type_arguments, |type_arg| type_arg.parse(ctx)); + // type_arguments.par_iter().for_each(|type_arg| { + // type_arg.parse(ctx); + // }); } } _ => { @@ -1174,20 +1290,24 @@ fn collect_call_path_tree( ) { collect_qualified_path_root(ctx, tree.qualified_call_path.qualified_path_root.clone()); - tree.qualified_call_path - .call_path - .prefixes - .par_iter() - .for_each(|ident| { - tokens.insert(ctx.ident(ident), token.clone()); - }); + adaptive_iter(&tree.qualified_call_path.call_path.prefixes, |ident| { + tokens.insert(ctx.ident(ident), token.clone()); + }); + // tree.qualified_call_path + // .call_path + // .prefixes + // .par_iter() + // .for_each(|ident| { + // tokens.insert(ctx.ident(ident), token.clone()); + // }); tokens.insert( ctx.ident(&tree.qualified_call_path.call_path.suffix), token.clone(), ); - tree.children.par_iter().for_each(|child| { - collect_call_path_tree(ctx, child, token, tokens); - }); + adaptive_iter(&tree.children, |child| collect_call_path_tree(ctx, child, token, tokens)); + // tree.children.par_iter().for_each(|child| { + // collect_call_path_tree(ctx, child, token, tokens); + // }); } fn collect_qualified_path_root( From 0dd54124fba12059afc8ad194a0dc7f15b1c26de Mon Sep 17 00:00:00 2001 From: Joshua Batty Date: Wed, 20 Mar 2024 12:21:15 +1100 Subject: [PATCH 2/8] add adaptive_iter ti typed ast traversal --- sway-lsp/src/traverse/typed_tree.rs | 613 ++++++++++++++++++++-------- 1 file changed, 439 insertions(+), 174 deletions(-) diff --git a/sway-lsp/src/traverse/typed_tree.rs b/sway-lsp/src/traverse/typed_tree.rs index a0d43315788..c0467be76cd 100644 --- a/sway-lsp/src/traverse/typed_tree.rs +++ b/sway-lsp/src/traverse/typed_tree.rs @@ -3,7 +3,7 @@ use crate::{ core::token::{ type_info_to_symbol_kind, SymbolKind, Token, TokenIdent, TypeDefinition, TypedAstToken, }, - traverse::{Parse, ParseContext}, + traverse::{Parse, ParseContext, adaptive_iter}, }; use dashmap::mapref::one::RefMut; use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator}; @@ -220,13 +220,16 @@ impl Parse for ty::TyExpression { .. } => { if let Some(type_binding) = type_binding { - type_binding - .type_arguments - .to_vec() - .par_iter() - .for_each(|type_arg| { - collect_type_argument(ctx, type_arg); - }); + adaptive_iter(&type_binding.type_arguments.to_vec(), |type_arg| { + collect_type_argument(ctx, type_arg); + }); + // type_binding + // .type_arguments + // .to_vec() + // .par_iter() + // .for_each(|type_arg| { + // collect_type_argument(ctx, type_arg); + // }); } let implementing_type_name = (*ctx.engines.de().get_function(fn_ref)) .clone() @@ -259,18 +262,25 @@ impl Parse for ty::TyExpression { token.type_def = Some(TypeDefinition::Ident(function_decl.name.clone())); } contract_call_params.values().for_each(|exp| exp.parse(ctx)); - arguments.par_iter().for_each(|(ident, exp)| { + adaptive_iter(arguments, |(ident, exp)| { if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { token.typed = Some(TypedAstToken::Ident(ident.clone())); } exp.parse(ctx); }); + // arguments.par_iter().for_each(|(ident, exp)| { + // if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { + // token.typed = Some(TypedAstToken::Ident(ident.clone())); + // } + // exp.parse(ctx); + // }); let function_decl = ctx.engines.de().get_function(fn_ref); - function_decl - .body - .contents - .par_iter() - .for_each(|node| node.parse(ctx)); + adaptive_iter(&function_decl.body.contents, |node| node.parse(ctx)); + // function_decl + // .body + // .contents + // .par_iter() + // .for_each(|node| node.parse(ctx)); } ty::TyExpressionVariant::LazyOperator { lhs, rhs, .. } => { lhs.parse(ctx); @@ -305,13 +315,15 @@ impl Parse for ty::TyExpression { } } ty::TyExpressionVariant::Tuple { fields } => { - fields.par_iter().for_each(|field| field.parse(ctx)); + adaptive_iter(fields, |field| field.parse(ctx)); + // fields.par_iter().for_each(|field| field.parse(ctx)); } ty::TyExpressionVariant::Array { elem_type: _, contents, } => { - contents.par_iter().for_each(|exp| exp.parse(ctx)); + adaptive_iter(contents, |exp| exp.parse(ctx)); + // contents.par_iter().for_each(|exp| exp.parse(ctx)); } ty::TyExpressionVariant::ArrayIndex { prefix, index } => { prefix.parse(ctx); @@ -329,15 +341,18 @@ impl Parse for ty::TyExpression { token.typed = Some(TypedAstToken::TypedExpression(self.clone())); token.type_def = Some(TypeDefinition::TypeId(self.return_type)); } - call_path_binding - .type_arguments - .to_vec() - .par_iter() - .for_each(|type_arg| { - collect_type_argument(ctx, type_arg); - }); + adaptive_iter(&call_path_binding.type_arguments.to_vec(), |type_arg| { + collect_type_argument(ctx, type_arg); + }); + // call_path_binding + // .type_arguments + // .to_vec() + // .par_iter() + // .for_each(|type_arg| { + // collect_type_argument(ctx, type_arg); + // }); collect_call_path_prefixes(ctx, &call_path_binding.inner.prefixes); - fields.par_iter().for_each(|field| { + adaptive_iter(fields, |field| { if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(&field.name)) { @@ -357,12 +372,33 @@ impl Parse for ty::TyExpression { } field.value.parse(ctx); }); + // fields.par_iter().for_each(|field| { + // if let Some(mut token) = + // ctx.tokens.try_get_mut_with_retry(&ctx.ident(&field.name)) + // { + // token.typed = Some(TypedAstToken::TypedExpression(field.value.clone())); + + // if let Some(struct_decl) = &ctx + // .tokens + // .struct_declaration_of_type_id(ctx.engines, &self.return_type) + // { + // struct_decl.fields.iter().for_each(|decl_field| { + // if decl_field.name == field.name { + // token.type_def = + // Some(TypeDefinition::Ident(decl_field.name.clone())); + // } + // }); + // } + // } + // field.value.parse(ctx); + // }); } ty::TyExpressionVariant::CodeBlock(code_block) => { - code_block - .contents - .par_iter() - .for_each(|node| node.parse(ctx)); + adaptive_iter(&code_block.contents, |node| node.parse(ctx)); + // code_block + // .contents + // .par_iter() + // .for_each(|node| node.parse(ctx)); } ty::TyExpressionVariant::FunctionParameter { .. } => {} ty::TyExpressionVariant::MatchExp { @@ -373,7 +409,8 @@ impl Parse for ty::TyExpression { // scrutinee information will get overwritten by processing the underlying tree of // conditions desugared.parse(ctx); - scrutinees.par_iter().for_each(|s| s.parse(ctx)); + adaptive_iter(scrutinees, |s| s.parse(ctx)); + // scrutinees.par_iter().for_each(|s| s.parse(ctx)); } ty::TyExpressionVariant::IfExp { condition, @@ -387,11 +424,16 @@ impl Parse for ty::TyExpression { } } ty::TyExpressionVariant::AsmExpression { registers, .. } => { - registers.par_iter().for_each(|r| { + adaptive_iter(registers, |r| { if let Some(initializer) = &r.initializer { initializer.parse(ctx); } }); + // registers.par_iter().for_each(|r| { + // if let Some(initializer) = &r.initializer { + // initializer.parse(ctx); + // } + // }); } ty::TyExpressionVariant::StructFieldAccess { prefix, @@ -435,13 +477,16 @@ impl Parse for ty::TyExpression { token.typed = Some(TypedAstToken::TypedExpression(self.clone())); token.type_def = Some(TypeDefinition::Ident(enum_ref.name().clone())); } - call_path_binding - .type_arguments - .to_vec() - .par_iter() - .for_each(|type_arg| { - collect_type_argument(ctx, type_arg); - }); + adaptive_iter(&call_path_binding.type_arguments.to_vec(), |type_arg| { + collect_type_argument(ctx, type_arg); + }); + // call_path_binding + // .type_arguments + // .to_vec() + // .par_iter() + // .for_each(|type_arg| { + // collect_type_argument(ctx, type_arg); + // }); collect_call_path_prefixes(ctx, &call_path_binding.inner.prefixes); if let Some(mut token) = ctx.tokens.try_get_mut_with_retry( &ctx.ident(&Ident::new(variant_instantiation_span.clone())), @@ -570,7 +615,8 @@ impl Parse for ty::TyExpression { body, condition, .. } => { condition.parse(ctx); - body.contents.par_iter().for_each(|node| node.parse(ctx)); + adaptive_iter(&body.contents, |node| node.parse(ctx)); + // body.contents.par_iter().for_each(|node| node.parse(ctx)); } ty::TyExpressionVariant::ForLoop { desugared, .. } => { desugared.parse(ctx); @@ -630,16 +676,18 @@ impl Parse for ty::FunctionDecl { token.typed = Some(typed_token.clone()); token.type_def = Some(TypeDefinition::Ident(func_decl.name.clone())); } - func_decl - .body - .contents - .par_iter() - .for_each(|node| node.parse(ctx)); - func_decl - .parameters - .par_iter() - .for_each(|param| param.parse(ctx)); - func_decl.type_parameters.par_iter().for_each(|type_param| { + adaptive_iter(&func_decl.body.contents, |node| node.parse(ctx)); + // func_decl + // .body + // .contents + // .par_iter() + // .for_each(|node| node.parse(ctx)); + adaptive_iter(&func_decl.parameters, |param| param.parse(ctx)); + // func_decl + // .parameters + // .par_iter() + // .for_each(|param| param.parse(ctx)); + adaptive_iter(&func_decl.type_parameters, |type_param| { collect_type_id( ctx, type_param.type_id, @@ -647,27 +695,55 @@ impl Parse for ty::FunctionDecl { type_param.name_ident.span(), ); }); + // func_decl.type_parameters.par_iter().for_each(|type_param| { + // collect_type_id( + // ctx, + // type_param.type_id, + // &typed_token, + // type_param.name_ident.span(), + // ); + // }); collect_type_argument(ctx, &func_decl.return_type); - func_decl - .where_clause - .par_iter() - .for_each(|(ident, trait_constraints)| { - trait_constraints.par_iter().for_each(|constraint| { - collect_trait_constraint(ctx, constraint); - }); - if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { - token.typed = Some(typed_token.clone()); - if let Some(param_decl_ident) = func_decl - .type_parameters - .par_iter() - .find_any(|type_param| type_param.name_ident.as_str() == ident.as_str()) - .map(|type_param| type_param.name_ident.clone()) - { - token.type_def = Some(TypeDefinition::Ident(param_decl_ident)); - } - } + adaptive_iter(&func_decl.where_clause, |(ident, trait_constraints)| { + adaptive_iter(trait_constraints, |constraint| { + collect_trait_constraint(ctx, constraint); }); + // trait_constraints.par_iter().for_each(|constraint| { + // collect_trait_constraint(ctx, constraint); + // }); + if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { + token.typed = Some(typed_token.clone()); + if let Some(param_decl_ident) = func_decl + .type_parameters + .par_iter() + .find_any(|type_param| type_param.name_ident.as_str() == ident.as_str()) + .map(|type_param| type_param.name_ident.clone()) + { + token.type_def = Some(TypeDefinition::Ident(param_decl_ident)); + } + } + }); + + // func_decl + // .where_clause + // .par_iter() + // .for_each(|(ident, trait_constraints)| { + // adaptive_iter(trait_constraints, |constraint| { + // collect_trait_constraint(ctx, constraint); + // }); + // if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { + // token.typed = Some(typed_token.clone()); + // if let Some(param_decl_ident) = func_decl + // .type_parameters + // .par_iter() + // .find_any(|type_param| type_param.name_ident.as_str() == ident.as_str()) + // .map(|type_param| type_param.name_ident.clone()) + // { + // token.type_def = Some(TypeDefinition::Ident(param_decl_ident)); + // } + // } + // }); } } @@ -683,26 +759,43 @@ impl Parse for ty::TraitDecl { ))); token.type_def = Some(TypeDefinition::Ident(trait_decl.name.clone())); } - trait_decl - .interface_surface - .par_iter() - .for_each(|item| match item { - ty::TyTraitInterfaceItem::TraitFn(trait_fn_decl_ref) => { - let trait_fn = ctx.engines.de().get_trait_fn(trait_fn_decl_ref); - trait_fn.parse(ctx); - } - ty::TyTraitInterfaceItem::Constant(decl_ref) => { - let constant = ctx.engines.de().get_constant(decl_ref); - collect_const_decl(ctx, &constant, None); - } - ty::TyTraitInterfaceItem::Type(decl_ref) => { - let trait_type = ctx.engines.de().get_type(decl_ref); - collect_trait_type_decl(ctx, &trait_type, &decl_ref.span()); - } - }); - trait_decl.supertraits.par_iter().for_each(|supertrait| { + adaptive_iter(&trait_decl.interface_surface, |item| match item { + ty::TyTraitInterfaceItem::TraitFn(trait_fn_decl_ref) => { + let trait_fn = ctx.engines.de().get_trait_fn(trait_fn_decl_ref); + trait_fn.parse(ctx); + } + ty::TyTraitInterfaceItem::Constant(decl_ref) => { + let constant = ctx.engines.de().get_constant(decl_ref); + collect_const_decl(ctx, &constant, None); + } + ty::TyTraitInterfaceItem::Type(decl_ref) => { + let trait_type = ctx.engines.de().get_type(decl_ref); + collect_trait_type_decl(ctx, &trait_type, &decl_ref.span()); + } + }); + // trait_decl + // .interface_surface + // .par_iter() + // .for_each(|item| match item { + // ty::TyTraitInterfaceItem::TraitFn(trait_fn_decl_ref) => { + // let trait_fn = ctx.engines.de().get_trait_fn(trait_fn_decl_ref); + // trait_fn.parse(ctx); + // } + // ty::TyTraitInterfaceItem::Constant(decl_ref) => { + // let constant = ctx.engines.de().get_constant(decl_ref); + // collect_const_decl(ctx, &constant, None); + // } + // ty::TyTraitInterfaceItem::Type(decl_ref) => { + // let trait_type = ctx.engines.de().get_type(decl_ref); + // collect_trait_type_decl(ctx, &trait_type, &decl_ref.span()); + // } + // }); + adaptive_iter(&trait_decl.supertraits, |supertrait| { collect_supertrait(ctx, supertrait); }); + // trait_decl.supertraits.par_iter().for_each(|supertrait| { + // collect_supertrait(ctx, supertrait); + // }); } } @@ -718,21 +811,33 @@ impl Parse for ty::StructDecl { ))); token.type_def = Some(TypeDefinition::Ident(struct_decl.call_path.suffix.clone())); } - struct_decl.fields.par_iter().for_each(|field| { + adaptive_iter(&struct_decl.fields, |field| { field.parse(ctx); }); - struct_decl - .type_parameters - .par_iter() - .for_each(|type_param| { - if let Some(mut token) = ctx - .tokens - .try_get_mut_with_retry(&ctx.ident(&type_param.name_ident)) - { - token.typed = Some(TypedAstToken::TypedParameter(type_param.clone())); - token.type_def = Some(TypeDefinition::TypeId(type_param.type_id)); - } - }); + // struct_decl.fields.par_iter().for_each(|field| { + // field.parse(ctx); + // }); + adaptive_iter(&struct_decl.type_parameters, |type_param| { + if let Some(mut token) = ctx + .tokens + .try_get_mut_with_retry(&ctx.ident(&type_param.name_ident)) + { + token.typed = Some(TypedAstToken::TypedParameter(type_param.clone())); + token.type_def = Some(TypeDefinition::TypeId(type_param.type_id)); + } + }); + // struct_decl + // .type_parameters + // .par_iter() + // .for_each(|type_param| { + // if let Some(mut token) = ctx + // .tokens + // .try_get_mut_with_retry(&ctx.ident(&type_param.name_ident)) + // { + // token.typed = Some(TypedAstToken::TypedParameter(type_param.clone())); + // token.type_def = Some(TypeDefinition::TypeId(type_param.type_id)); + // } + // }); } } @@ -748,7 +853,7 @@ impl Parse for ty::ImplTrait { implementing_for, .. } = &*impl_trait_decl; - impl_type_parameters.par_iter().for_each(|param| { + adaptive_iter(impl_type_parameters, |param| { collect_type_id( ctx, param.type_id, @@ -756,11 +861,24 @@ impl Parse for ty::ImplTrait { param.name_ident.span(), ); }); - trait_name.prefixes.par_iter().for_each(|ident| { + // impl_type_parameters.par_iter().for_each(|param| { + // collect_type_id( + // ctx, + // param.type_id, + // &TypedAstToken::TypedParameter(param.clone()), + // param.name_ident.span(), + // ); + // }); + adaptive_iter(&trait_name.prefixes, |ident| { if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { token.typed = Some(TypedAstToken::Ident(ident.clone())); } }); + // trait_name.prefixes.par_iter().for_each(|ident| { + // if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { + // token.typed = Some(TypedAstToken::Ident(ident.clone())); + // } + // }); // Which typed token should be used for collect_type_id // if trait_decl_ref is some, then our ImplTrait is for an ABI or Trait. In this instance, @@ -792,23 +910,42 @@ impl Parse for ty::ImplTrait { Some(TypeDefinition::TypeId(implementing_for.type_id)) }; } - trait_type_arguments.par_iter().for_each(|type_arg| { + adaptive_iter(trait_type_arguments, |type_arg| { collect_type_argument(ctx, type_arg); }); - items.par_iter().for_each(|item| match item { - ty::TyTraitItem::Fn(method_ref) => { - let method = ctx.engines.de().get_function(method_ref); - method.parse(ctx); - } - ty::TyTraitItem::Constant(const_ref) => { - let constant = ctx.engines.de().get_constant(const_ref); - collect_const_decl(ctx, &constant, None); - } - ty::TyTraitItem::Type(type_ref) => { - let trait_type = ctx.engines.de().get_type(type_ref); - collect_trait_type_decl(ctx, &trait_type, &type_ref.span()); + // trait_type_arguments.par_iter().for_each(|type_arg| { + // collect_type_argument(ctx, type_arg); + // }); + adaptive_iter(items, |item| { + match item { + ty::TyTraitItem::Fn(method_ref) => { + let method = ctx.engines.de().get_function(method_ref); + method.parse(ctx); + } + ty::TyTraitItem::Constant(const_ref) => { + let constant = ctx.engines.de().get_constant(const_ref); + collect_const_decl(ctx, &constant, None); + } + ty::TyTraitItem::Type(type_ref) => { + let trait_type = ctx.engines.de().get_type(type_ref); + collect_trait_type_decl(ctx, &trait_type, &type_ref.span()); + } } }); + // items.par_iter().for_each(|item| match item { + // ty::TyTraitItem::Fn(method_ref) => { + // let method = ctx.engines.de().get_function(method_ref); + // method.parse(ctx); + // } + // ty::TyTraitItem::Constant(const_ref) => { + // let constant = ctx.engines.de().get_constant(const_ref); + // collect_const_decl(ctx, &constant, None); + // } + // ty::TyTraitItem::Type(type_ref) => { + // let trait_type = ctx.engines.de().get_type(type_ref); + // collect_trait_type_decl(ctx, &trait_type, &type_ref.span()); + // } + // }); collect_type_argument(ctx, implementing_for); // collect the root type argument again with declaration info this time so the @@ -840,26 +977,43 @@ impl Parse for ty::AbiDecl { ))); token.type_def = Some(TypeDefinition::Ident(abi_decl.name.clone())); } - abi_decl - .interface_surface - .par_iter() - .for_each(|item| match item { - ty::TyTraitInterfaceItem::TraitFn(trait_fn_decl_ref) => { - let trait_fn = ctx.engines.de().get_trait_fn(trait_fn_decl_ref); - trait_fn.parse(ctx); - } - ty::TyTraitInterfaceItem::Constant(const_ref) => { - let constant = ctx.engines.de().get_constant(const_ref); - collect_const_decl(ctx, &constant, None); - } - ty::TyTraitInterfaceItem::Type(type_ref) => { - let trait_type = ctx.engines.de().get_type(type_ref); - collect_trait_type_decl(ctx, &trait_type, &type_ref.span()); - } - }); - abi_decl.supertraits.par_iter().for_each(|supertrait| { + adaptive_iter(&abi_decl.interface_surface, |item| match item { + ty::TyTraitInterfaceItem::TraitFn(trait_fn_decl_ref) => { + let trait_fn = ctx.engines.de().get_trait_fn(trait_fn_decl_ref); + trait_fn.parse(ctx); + } + ty::TyTraitInterfaceItem::Constant(const_ref) => { + let constant = ctx.engines.de().get_constant(const_ref); + collect_const_decl(ctx, &constant, None); + } + ty::TyTraitInterfaceItem::Type(type_ref) => { + let trait_type = ctx.engines.de().get_type(type_ref); + collect_trait_type_decl(ctx, &trait_type, &type_ref.span()); + } + }); + // abi_decl + // .interface_surface + // .par_iter() + // .for_each(|item| match item { + // ty::TyTraitInterfaceItem::TraitFn(trait_fn_decl_ref) => { + // let trait_fn = ctx.engines.de().get_trait_fn(trait_fn_decl_ref); + // trait_fn.parse(ctx); + // } + // ty::TyTraitInterfaceItem::Constant(const_ref) => { + // let constant = ctx.engines.de().get_constant(const_ref); + // collect_const_decl(ctx, &constant, None); + // } + // ty::TyTraitInterfaceItem::Type(type_ref) => { + // let trait_type = ctx.engines.de().get_type(type_ref); + // collect_trait_type_decl(ctx, &trait_type, &type_ref.span()); + // } + // }); + adaptive_iter(&abi_decl.supertraits, |supertrait| { supertrait.parse(ctx); }); + // abi_decl.supertraits.par_iter().for_each(|supertrait| { + // supertrait.parse(ctx); + // }); } } @@ -877,7 +1031,7 @@ impl Parse for ty::GenericTypeForFunctionScope { impl Parse for ty::StorageDecl { fn parse(&self, ctx: &ParseContext) { let storage_decl = ctx.engines.de().get_storage(&self.decl_id); - storage_decl.fields.par_iter().for_each(|field| { + adaptive_iter(&storage_decl.fields, |field| { if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(&field.name)) { token.typed = Some(TypedAstToken::TypedStorageField(field.clone())); token.type_def = Some(TypeDefinition::Ident(field.name.clone())); @@ -885,6 +1039,14 @@ impl Parse for ty::StorageDecl { collect_type_argument(ctx, &field.type_argument); field.initializer.parse(ctx); }); + // storage_decl.fields.par_iter().for_each(|field| { + // if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(&field.name)) { + // token.typed = Some(TypedAstToken::TypedStorageField(field.clone())); + // token.type_def = Some(TypeDefinition::Ident(field.name.clone())); + // } + // collect_type_argument(ctx, &field.type_argument); + // field.initializer.parse(ctx); + // }); } } @@ -912,9 +1074,10 @@ impl Parse for ty::TyTraitFn { token.typed = Some(TypedAstToken::TypedTraitFn(self.clone())); token.type_def = Some(TypeDefinition::Ident(self.name.clone())); } - self.parameters - .par_iter() - .for_each(|param| param.parse(ctx)); + adaptive_iter(&self.parameters, |param| param.parse(ctx)); + // self.parameters + // .par_iter() + // .for_each(|param| param.parse(ctx)); let return_ident = Ident::new(self.return_type.span.clone()); if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(&return_ident)) { token.typed = Some(TypedAstToken::TypedTraitFn(self.clone())); @@ -951,14 +1114,16 @@ impl Parse for ty::TyFunctionDecl { token.typed = Some(typed_token.clone()); token.type_def = Some(TypeDefinition::Ident(self.name.clone())); } - self.body - .contents - .par_iter() - .for_each(|node| node.parse(ctx)); - self.parameters - .par_iter() - .for_each(|param| param.parse(ctx)); - self.type_parameters.par_iter().for_each(|type_param| { + adaptive_iter(&self.body.contents, |node| node.parse(ctx)); + // self.body + // .contents + // .par_iter() + // .for_each(|node| node.parse(ctx)); + adaptive_iter(&self.parameters, |param| param.parse(ctx)); + // self.parameters + // .par_iter() + // .for_each(|param| param.parse(ctx)); + adaptive_iter(&self.type_parameters, |type_param| { collect_type_id( ctx, type_param.type_id, @@ -966,25 +1131,53 @@ impl Parse for ty::TyFunctionDecl { type_param.name_ident.span(), ); }); + // self.type_parameters.par_iter().for_each(|type_param| { + // collect_type_id( + // ctx, + // type_param.type_id, + // &typed_token, + // type_param.name_ident.span(), + // ); + // }); collect_type_argument(ctx, &self.return_type); - self.where_clause - .par_iter() - .for_each(|(ident, trait_constraints)| { - trait_constraints.par_iter().for_each(|constraint| { - collect_trait_constraint(ctx, constraint); - }); - if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { - token.typed = Some(typed_token.clone()); - if let Some(param_decl_ident) = self - .type_parameters - .par_iter() - .find_any(|type_param| type_param.name_ident.as_str() == ident.as_str()) - .map(|type_param| type_param.name_ident.clone()) - { - token.type_def = Some(TypeDefinition::Ident(param_decl_ident)); - } - } + + adaptive_iter(&self.where_clause, |(ident, trait_constraints)| { + adaptive_iter(trait_constraints, |constraint| { + collect_trait_constraint(ctx, constraint); }); + // trait_constraints.par_iter().for_each(|constraint| { + // collect_trait_constraint(ctx, constraint); + // }); + if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { + token.typed = Some(typed_token.clone()); + if let Some(param_decl_ident) = self + .type_parameters + .par_iter() + .find_any(|type_param| type_param.name_ident.as_str() == ident.as_str()) + .map(|type_param| type_param.name_ident.clone()) + { + token.type_def = Some(TypeDefinition::Ident(param_decl_ident)); + } + } + }); + // self.where_clause + // .par_iter() + // .for_each(|(ident, trait_constraints)| { + // adaptive_iter(trait_constraints, |constraint| { + // collect_trait_constraint(ctx, constraint); + // }); + // if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { + // token.typed = Some(typed_token.clone()); + // if let Some(param_decl_ident) = self + // .type_parameters + // .par_iter() + // .find_any(|type_param| type_param.name_ident.as_str() == ident.as_str()) + // .map(|type_param| type_param.name_ident.clone()) + // { + // token.type_def = Some(TypeDefinition::Ident(param_decl_ident)); + // } + // } + // }); } } @@ -1000,12 +1193,18 @@ impl Parse for ty::TyTypeAliasDecl { impl Parse for ty::TyIntrinsicFunctionKind { fn parse(&self, ctx: &ParseContext) { - self.type_arguments.par_iter().for_each(|type_arg| { + adaptive_iter(&self.type_arguments, |type_arg| { collect_type_argument(ctx, type_arg); }); - self.arguments.par_iter().for_each(|arg| { + // self.type_arguments.par_iter().for_each(|type_arg| { + // collect_type_argument(ctx, type_arg); + // }); + adaptive_iter(&self.arguments, |arg| { arg.parse(ctx); }); + // self.arguments.par_iter().for_each(|arg| { + // arg.parse(ctx); + // }); } } @@ -1046,7 +1245,8 @@ impl Parse for ty::TyScrutinee { token.typed = Some(TypedAstToken::TypedScrutinee(self.clone())); token.type_def = Some(TypeDefinition::Ident(struct_ref.name().clone())); } - fields.par_iter().for_each(|field| field.parse(ctx)); + adaptive_iter(fields, |field| field.parse(ctx)); + // fields.par_iter().for_each(|field| field.parse(ctx)); } EnumScrutinee { enum_ref, @@ -1078,7 +1278,8 @@ impl Parse for ty::TyScrutinee { value.parse(ctx); } Tuple(scrutinees) | Or(scrutinees) => { - scrutinees.par_iter().for_each(|s| s.parse(ctx)); + adaptive_iter(scrutinees, |s| s.parse(ctx)); + // scrutinees.par_iter().for_each(|s| s.parse(ctx)); } } } @@ -1105,7 +1306,7 @@ impl Parse for ty::TyReassignment { { token.typed = Some(TypedAstToken::TypedReassignment(self.clone())); } - self.lhs_indices.par_iter().for_each(|proj_kind| { + adaptive_iter(&self.lhs_indices, |proj_kind| { if let ty::ProjectionKind::StructField { name } = proj_kind { if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(name)) { token.typed = Some(TypedAstToken::TypedReassignment(self.clone())); @@ -1123,6 +1324,24 @@ impl Parse for ty::TyReassignment { } } }); + // self.lhs_indices.par_iter().for_each(|proj_kind| { + // if let ty::ProjectionKind::StructField { name } = proj_kind { + // if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(name)) { + // token.typed = Some(TypedAstToken::TypedReassignment(self.clone())); + // if let Some(struct_decl) = &ctx + // .tokens + // .struct_declaration_of_type_id(ctx.engines, &self.lhs_type) + // { + // struct_decl.fields.iter().for_each(|decl_field| { + // if &decl_field.name == name { + // token.type_def = + // Some(TypeDefinition::Ident(decl_field.name.clone())); + // } + // }); + // } + // } + // } + // }); } } @@ -1275,9 +1494,12 @@ fn collect_type_id( collect_type_argument(ctx, type_arg); } TypeInfo::Tuple(type_arguments) => { - type_arguments.par_iter().for_each(|type_arg| { + adaptive_iter(type_arguments, |type_arg| { collect_type_argument(ctx, type_arg); }); + // type_arguments.par_iter().for_each(|type_arg| { + // collect_type_argument(ctx, type_arg); + // }); } TypeInfo::Enum(decl_ref) => { let decl = ctx.engines.de().get_enum(decl_ref); @@ -1287,7 +1509,7 @@ fn collect_type_id( { assign_type_to_token(token, symbol_kind, typed_token.clone(), type_id); } - decl.type_parameters.par_iter().for_each(|param| { + adaptive_iter(&decl.type_parameters, |param| { collect_type_id( ctx, param.type_id, @@ -1295,9 +1517,20 @@ fn collect_type_id( param.name_ident.span(), ); }); - decl.variants.par_iter().for_each(|variant| { + // decl.type_parameters.par_iter().for_each(|param| { + // collect_type_id( + // ctx, + // param.type_id, + // &TypedAstToken::TypedParameter(param.clone()), + // param.name_ident.span(), + // ); + // }); + adaptive_iter(&decl.variants, |variant| { variant.parse(ctx); }); + // decl.variants.par_iter().for_each(|variant| { + // variant.parse(ctx); + // }); } TypeInfo::Struct(decl_ref) => { let decl = ctx.engines.de().get_struct(decl_ref); @@ -1307,7 +1540,7 @@ fn collect_type_id( { assign_type_to_token(token, symbol_kind, typed_token.clone(), type_id); } - decl.type_parameters.par_iter().for_each(|param| { + adaptive_iter(&decl.type_parameters, |param| { collect_type_id( ctx, param.type_id, @@ -1315,9 +1548,20 @@ fn collect_type_id( param.name_ident.span(), ); }); - decl.fields.par_iter().for_each(|field| { + // decl.type_parameters.par_iter().for_each(|param| { + // collect_type_id( + // ctx, + // param.type_id, + // &TypedAstToken::TypedParameter(param.clone()), + // param.name_ident.span(), + // ); + // }); + adaptive_iter(&decl.fields, |field| { field.parse(ctx); }); + // decl.fields.par_iter().for_each(|field| { + // field.parse(ctx); + // }); } TypeInfo::Custom { type_arguments, @@ -1332,15 +1576,21 @@ fn collect_type_id( assign_type_to_token(token, symbol_kind, typed_token.clone(), type_id); } if let Some(type_arguments) = type_arguments { - type_arguments.par_iter().for_each(|type_arg| { + adaptive_iter(type_arguments, |type_arg| { collect_type_argument(ctx, type_arg); }); + // type_arguments.par_iter().for_each(|type_arg| { + // collect_type_argument(ctx, type_arg); + // }); } } TypeInfo::Storage { fields } => { - fields.par_iter().for_each(|field| { + adaptive_iter(fields, |field| { field.parse(ctx); }); + // fields.par_iter().for_each(|field| { + // field.parse(ctx); + // }); } _ => { if let Some(token) = ctx @@ -1390,9 +1640,12 @@ fn collect_trait_constraint( token.type_def = Some(TypeDefinition::Ident(trait_def_ident)); } } - type_arguments.par_iter().for_each(|type_arg| { + adaptive_iter(type_arguments, |type_arg| { collect_type_argument(ctx, type_arg); }); + // type_arguments.par_iter().for_each(|type_arg| { + // collect_type_argument(ctx, type_arg); + // }); } fn collect_supertrait(ctx: &ParseContext, supertrait: &Supertrait) { @@ -1419,7 +1672,7 @@ fn collect_enum(ctx: &ParseContext, decl_id: &DeclId, declaratio token.typed = Some(TypedAstToken::TypedDeclaration(declaration.clone())); token.type_def = Some(TypeDefinition::Ident(enum_decl.call_path.suffix.clone())); } - enum_decl.type_parameters.par_iter().for_each(|type_param| { + adaptive_iter(&enum_decl.type_parameters, |type_param| { if let Some(mut token) = ctx .tokens .try_get_mut_with_retry(&ctx.ident(&type_param.name_ident)) @@ -1428,9 +1681,21 @@ fn collect_enum(ctx: &ParseContext, decl_id: &DeclId, declaratio token.type_def = Some(TypeDefinition::TypeId(type_param.type_id)); } }); - enum_decl.variants.par_iter().for_each(|variant| { + // enum_decl.type_parameters.par_iter().for_each(|type_param| { + // if let Some(mut token) = ctx + // .tokens + // .try_get_mut_with_retry(&ctx.ident(&type_param.name_ident)) + // { + // token.typed = Some(TypedAstToken::TypedParameter(type_param.clone())); + // token.type_def = Some(TypeDefinition::TypeId(type_param.type_id)); + // } + // }); + adaptive_iter(&enum_decl.variants, |variant| { variant.parse(ctx); }); + // enum_decl.variants.par_iter().for_each(|variant| { + // variant.parse(ctx); + // }); } fn collect_qualified_path_root( From a7719bb3eb0f4b75c02d6334708163348edabde2 Mon Sep 17 00:00:00 2001 From: Joshua Batty Date: Wed, 20 Mar 2024 13:15:06 +1100 Subject: [PATCH 3/8] remove comments --- sway-lsp/src/traverse/lexed_tree.rs | 2 +- sway-lsp/src/traverse/parsed_tree.rs | 233 -------------------- sway-lsp/src/traverse/typed_tree.rs | 314 --------------------------- 3 files changed, 1 insertion(+), 548 deletions(-) diff --git a/sway-lsp/src/traverse/lexed_tree.rs b/sway-lsp/src/traverse/lexed_tree.rs index 37c6befab3c..1ff00e9c16b 100644 --- a/sway-lsp/src/traverse/lexed_tree.rs +++ b/sway-lsp/src/traverse/lexed_tree.rs @@ -2,7 +2,7 @@ use crate::{ core::token::{AstToken, SymbolKind, Token}, traverse::{Parse, ParseContext, adaptive_iter}, }; -use rayon::iter::{IntoParallelRefIterator, ParallelBridge, ParallelIterator}; +use rayon::iter::{ParallelBridge, ParallelIterator}; use sway_ast::{ expr::LoopControlFlow, ty::TyTupleDescriptor, Assignable, CodeBlockContents, ConfigurableField, Expr, ExprArrayDescriptor, ExprStructField, ExprTupleDescriptor, FnArg, FnArgs, FnSignature, diff --git a/sway-lsp/src/traverse/parsed_tree.rs b/sway-lsp/src/traverse/parsed_tree.rs index 2d63e33f0f8..9a2e6cc06cb 100644 --- a/sway-lsp/src/traverse/parsed_tree.rs +++ b/sway-lsp/src/traverse/parsed_tree.rs @@ -192,15 +192,6 @@ impl Parse for Expression { ), ); }); - // part_spans.par_iter().for_each(|span| { - // ctx.tokens.insert( - // ctx.ident(&Ident::new(span.clone())), - // Token::from_parsed( - // AstToken::ErrorRecovery(span.clone()), - // SymbolKind::Unknown, - // ), - // ); - // }); } ExpressionKind::Literal(value) => { let symbol_kind = literal_to_symbol_kind(value); @@ -235,7 +226,6 @@ impl Parse for Expression { } ExpressionKind::Tuple(fields) => { adaptive_iter(fields, |field| field.parse(ctx)); - // fields.par_iter().for_each(|field| field.parse(ctx)); } ExpressionKind::TupleIndex(TupleIndexExpression { prefix, index_span, .. @@ -257,10 +247,6 @@ impl Parse for Expression { } ExpressionKind::CodeBlock(code_block) => { adaptive_iter(&code_block.contents, |node| node.parse(ctx)); - // code_block - // .contents - // .par_iter() - // .for_each(|node| node.parse(ctx)); } ExpressionKind::If(IfExpression { condition, @@ -282,10 +268,6 @@ impl Parse for Expression { branch.scrutinee.parse(ctx); branch.result.parse(ctx); }); - // branches.par_iter().for_each(|branch| { - // branch.scrutinee.parse(ctx); - // branch.result.parse(ctx); - // }); } ExpressionKind::Asm(asm) => { adaptive_iter(&asm.registers, |register| { @@ -293,11 +275,6 @@ impl Parse for Expression { initializer.parse(ctx); } }); - // asm.registers.par_iter().for_each(|register| { - // if let Some(initializer) = ®ister.initializer { - // initializer.parse(ctx); - // } - // }); } ExpressionKind::MethodApplication(method_application_expression) => { method_application_expression.parse(ctx); @@ -429,17 +406,6 @@ impl Parse for DelineatedPathExpression { Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Enum), ); }); - // call_path_binding - // .inner - // .call_path - // .prefixes - // .par_iter() - // .for_each(|ident| { - // ctx.tokens.insert( - // ctx.ident(ident), - // Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Enum), - // ); - // }); ctx.tokens.insert( ctx.ident(&call_path_binding.inner.call_path.suffix), Token::from_parsed( @@ -448,18 +414,8 @@ impl Parse for DelineatedPathExpression { ), ); adaptive_iter(&call_path_binding.type_arguments.to_vec(), |type_arg| type_arg.parse(ctx)); - // call_path_binding - // .type_arguments - // .to_vec() - // .par_iter() - // .for_each(|type_arg| { - // type_arg.parse(ctx); - // }); if let Some(args_vec) = args.as_ref() { adaptive_iter(args_vec, |exp| exp.parse(ctx)); - // args_vec.par_iter().for_each(|exp| { - // exp.parse(ctx); - // }); } collect_qualified_path_root(ctx, call_path_binding.inner.qualified_path_root.clone()); } @@ -493,15 +449,7 @@ impl Parse for AmbiguousPathExpression { ), ); adaptive_iter(&call_path_binding.type_arguments.to_vec(), |type_arg| type_arg.parse(ctx)); - // call_path_binding - // .type_arguments - // .to_vec() - // .par_iter() - // .for_each(|type_arg| { - // type_arg.parse(ctx); - // }); adaptive_iter(args, |exp| exp.parse(ctx)); - // args.par_iter().for_each(|arg| arg.parse(ctx)); collect_qualified_path_root(ctx, qualified_path_root.clone().map(Box::new)); } } @@ -525,13 +473,6 @@ impl Parse for MethodApplicationExpression { adaptive_iter(&self.method_name_binding.type_arguments.to_vec(), |type_arg| { type_arg.parse(ctx); }); - // self.method_name_binding - // .type_arguments - // .to_vec() - // .par_iter() - // .for_each(|type_arg| { - // type_arg.parse(ctx); - // }); // Don't collect applications of desugared operators due to mismatched ident lengths. if !desugared_op(&prefixes) { ctx.tokens.insert( @@ -543,11 +484,7 @@ impl Parse for MethodApplicationExpression { ); } adaptive_iter(&self.arguments, |arg| arg.parse(ctx)); - // self.arguments.par_iter().for_each(|arg| arg.parse(ctx)); adaptive_iter(&self.contract_call_params, |param| param.parse(ctx)); - // self.contract_call_params - // .par_iter() - // .for_each(|param| param.parse(ctx)); } } @@ -580,17 +517,11 @@ impl Parse for Scrutinee { Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Struct); ctx.tokens.insert(ctx.ident(ident), token); }); - // struct_name.prefixes.par_iter().for_each(|ident| { - // let token = - // Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Struct); - // ctx.tokens.insert(ctx.ident(ident), token); - // }); ctx.tokens.insert( ctx.ident(&struct_name.suffix), Token::from_parsed(AstToken::Scrutinee(self.clone()), SymbolKind::Struct), ); adaptive_iter(fields, |field| field.parse(ctx)); - // fields.par_iter().for_each(|field| field.parse(ctx)); } Scrutinee::EnumScrutinee { call_path, value, .. @@ -601,12 +532,6 @@ impl Parse for Scrutinee { Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Enum), ); }); - // call_path.prefixes.par_iter().for_each(|ident| { - // ctx.tokens.insert( - // ctx.ident(ident), - // Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Enum), - // ); - // }); let token = Token::from_parsed(AstToken::Scrutinee(self.clone()), SymbolKind::Variant); ctx.tokens.insert(ctx.ident(&call_path.suffix), token); @@ -618,7 +543,6 @@ impl Parse for Scrutinee { } Scrutinee::Tuple { elems, .. } | Scrutinee::Or { elems, .. } => { adaptive_iter(elems, |elem| elem.parse(ctx)); - // elems.par_iter().for_each(|elem| elem.parse(ctx)); } Scrutinee::Error { .. } => { // FIXME: Left for @JoshuaBatty to use. @@ -653,16 +577,6 @@ impl Parse for StructExpression { Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Struct), ); }); - // self.call_path_binding - // .inner - // .prefixes - // .par_iter() - // .for_each(|ident| { - // ctx.tokens.insert( - // ctx.ident(ident), - // Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Struct), - // ); - // }); let name = &self.call_path_binding.inner.suffix; let symbol_kind = if name.as_str() == "Self" { SymbolKind::SelfKeyword @@ -675,11 +589,7 @@ impl Parse for StructExpression { ); let type_arguments = &self.call_path_binding.type_arguments.to_vec(); adaptive_iter(type_arguments, |type_arg| type_arg.parse(ctx)); - // type_arguments.par_iter().for_each(|type_arg| { - // type_arg.parse(ctx); - // }); adaptive_iter(&self.fields, |field| field.parse(ctx)); - // self.fields.par_iter().for_each(|field| field.parse(ctx)); } } @@ -699,7 +609,6 @@ impl Parse for StructExpressionField { impl Parse for ArrayExpression { fn parse(&self, ctx: &ParseContext) { adaptive_iter(&self.contents, |exp| exp.parse(ctx)); - // self.contents.par_iter().for_each(|exp| exp.parse(ctx)); if let Some(length_span) = &self.length_span { let ident = Ident::new(length_span.clone()); ctx.tokens.insert( @@ -720,16 +629,6 @@ impl Parse for FunctionApplicationExpression { Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Module), ); }); - // self.call_path_binding - // .inner - // .prefixes - // .par_iter() - // .for_each(|ident| { - // ctx.tokens.insert( - // ctx.ident(ident), - // Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Module), - // ); - // }); ctx.tokens.insert( ctx.ident(&self.call_path_binding.inner.suffix), Token::from_parsed( @@ -740,25 +639,14 @@ impl Parse for FunctionApplicationExpression { adaptive_iter(&self.call_path_binding.type_arguments.to_vec(), |type_arg| { type_arg.parse(ctx); }); - // self.call_path_binding - // .type_arguments - // .to_vec() - // .par_iter() - // .for_each(|type_arg| { - // type_arg.parse(ctx); - // }); } adaptive_iter(&self.arguments, |exp| exp.parse(ctx)); - // self.arguments.par_iter().for_each(|exp| { - // exp.parse(ctx); - // }); } } impl Parse for ParsedDeclId { fn parse(&self, ctx: &ParseContext) { let var_decl = ctx.engines.pe().get_variable(self); - // Don't collect tokens if the idents are generated tuple or match desugaring names. // The individual elements are handled in the subsequent VariableDeclaration's. if !(is_generated_tuple_var_name(var_decl.name.as_str()) @@ -797,26 +685,13 @@ impl Parse for ParsedDeclId { SymbolKind::Function, ); let fn_decl = ctx.engines.pe().get_function(self); - ctx.tokens.insert(ctx.ident(&fn_decl.name), token.clone()); adaptive_iter(&fn_decl.body.contents, |node| node.parse(ctx)); - // fn_decl.body.contents.par_iter().for_each(|node| { - // node.parse(ctx); - // }); adaptive_iter(&fn_decl.parameters, |param| param.parse(ctx)); - // fn_decl.parameters.par_iter().for_each(|param| { - // param.parse(ctx); - // }); adaptive_iter(&fn_decl.type_parameters, |type_param| type_param.parse(ctx)); - // fn_decl.type_parameters.par_iter().for_each(|type_param| { - // type_param.parse(ctx); - // }); for (ident, constraints) in &fn_decl.where_clause { ctx.tokens.insert(ctx.ident(ident), token.clone()); adaptive_iter(constraints, |constraint| constraint.parse(ctx)); - // constraints.par_iter().for_each(|constraint| { - // constraint.parse(ctx); - // }); } fn_decl.return_type.parse(ctx); fn_decl.attributes.parse(ctx); @@ -839,23 +714,8 @@ impl Parse for ParsedDeclId { TraitItem::Type(trait_type) => trait_type.parse(ctx), TraitItem::Error(_, _) => {} }); - // trait_decl - // .interface_surface - // .par_iter() - // .for_each(|item| match item { - // TraitItem::TraitFn(trait_fn) => trait_fn.parse(ctx), - // TraitItem::Constant(const_decl) => const_decl.parse(ctx), - // TraitItem::Type(trait_type) => trait_type.parse(ctx), - // TraitItem::Error(_, _) => {} - // }); adaptive_iter(&trait_decl.methods, |func_dec| func_dec.parse(ctx)); - // trait_decl.methods.par_iter().for_each(|func_dec| { - // func_dec.parse(ctx); - // }); adaptive_iter(&trait_decl.supertraits, |supertrait| supertrait.parse(ctx)); - // trait_decl.supertraits.par_iter().for_each(|supertrait| { - // supertrait.parse(ctx); - // }); } } @@ -870,16 +730,7 @@ impl Parse for ParsedDeclId { ), ); adaptive_iter(&struct_decl.fields, |field| field.parse(ctx)); - // struct_decl.fields.par_iter().for_each(|field| { - // field.parse(ctx); - // }); adaptive_iter(&struct_decl.type_parameters, |type_param| type_param.parse(ctx)); - // struct_decl - // .type_parameters - // .par_iter() - // .for_each(|type_param| { - // type_param.parse(ctx); - // }); struct_decl.attributes.parse(ctx); } } @@ -895,13 +746,7 @@ impl Parse for ParsedDeclId { ), ); adaptive_iter(&enum_decl.type_parameters, |type_param| type_param.parse(ctx)); - // enum_decl.type_parameters.par_iter().for_each(|type_param| { - // type_param.parse(ctx); - // }); adaptive_iter(&enum_decl.variants, |variant| variant.parse(ctx)); - // enum_decl.variants.par_iter().for_each(|variant| { - // variant.parse(ctx); - // }); enum_decl.attributes.parse(ctx); } } @@ -915,12 +760,6 @@ impl Parse for ParsedDeclId { Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Module), ); }); - // impl_trait.trait_name.prefixes.par_iter().for_each(|ident| { - // ctx.tokens.insert( - // ctx.ident(ident), - // Token::from_parsed(AstToken::Ident(ident.clone()), SymbolKind::Module), - // ); - // }); ctx.tokens.insert( ctx.ident(&impl_trait.trait_name.suffix), Token::from_parsed( @@ -930,22 +769,11 @@ impl Parse for ParsedDeclId { ); impl_trait.implementing_for.parse(ctx); adaptive_iter(&impl_trait.impl_type_parameters, |type_param| type_param.parse(ctx)); - // impl_trait - // .impl_type_parameters - // .par_iter() - // .for_each(|type_param| { - // type_param.parse(ctx); - // }); adaptive_iter(&impl_trait.items, |item| match item { ImplItem::Fn(fn_decl) => fn_decl.parse(ctx), ImplItem::Constant(const_decl) => const_decl.parse(ctx), ImplItem::Type(type_decl) => type_decl.parse(ctx), }); - // impl_trait.items.par_iter().for_each(|item| match item { - // ImplItem::Fn(fn_decl) => fn_decl.parse(ctx), - // ImplItem::Constant(const_decl) => const_decl.parse(ctx), - // ImplItem::Type(type_decl) => type_decl.parse(ctx), - // }); } } @@ -967,28 +795,14 @@ impl Parse for ParsedDeclId { ); if let Some(type_arguments) = type_arguments { adaptive_iter(type_arguments, |type_arg| type_arg.parse(ctx)); - // type_arguments.par_iter().for_each(|type_arg| { - // type_arg.parse(ctx); - // }); } } adaptive_iter(&impl_self.impl_type_parameters, |type_param| type_param.parse(ctx)); - // impl_self - // .impl_type_parameters - // .par_iter() - // .for_each(|type_param| { - // type_param.parse(ctx); - // }); adaptive_iter(&impl_self.items, |item| match item { ImplItem::Fn(fn_decl) => fn_decl.parse(ctx), ImplItem::Constant(const_decl) => const_decl.parse(ctx), ImplItem::Type(type_decl) => type_decl.parse(ctx), }); - // impl_self.items.par_iter().for_each(|item| match item { - // ImplItem::Fn(fn_decl) => fn_decl.parse(ctx), - // ImplItem::Constant(const_decl) => const_decl.parse(ctx), - // ImplItem::Type(type_decl) => type_decl.parse(ctx), - // }); } } @@ -1008,19 +822,7 @@ impl Parse for ParsedDeclId { TraitItem::Type(type_decl) => type_decl.parse(ctx), TraitItem::Error(_, _) => {} }); - // abi_decl - // .interface_surface - // .par_iter() - // .for_each(|item| match item { - // TraitItem::TraitFn(trait_fn) => trait_fn.parse(ctx), - // TraitItem::Constant(const_decl) => const_decl.parse(ctx), - // TraitItem::Type(type_decl) => type_decl.parse(ctx), - // TraitItem::Error(_, _) => {} - // }); adaptive_iter(&abi_decl.supertraits, |supertrait| supertrait.parse(ctx)); - // abi_decl.supertraits.par_iter().for_each(|supertrait| { - // supertrait.parse(ctx); - // }); abi_decl.attributes.parse(ctx); } } @@ -1064,9 +866,6 @@ impl Parse for ParsedDeclId { fn parse(&self, ctx: &ParseContext) { let storage_decl = ctx.engines.pe().get_storage(self); adaptive_iter(&storage_decl.fields, |field| field.parse(ctx)); - // storage_decl.fields.par_iter().for_each(|field| { - // field.parse(ctx); - // }); storage_decl.attributes.parse(ctx); } } @@ -1100,9 +899,6 @@ impl Parse for ParsedDeclId { Token::from_parsed(AstToken::TraitFn(*self), SymbolKind::Function), ); adaptive_iter(&trait_fn.parameters, |param| param.parse(ctx)); - // trait_fn.parameters.par_iter().for_each(|param| { - // param.parse(ctx); - // }); trait_fn.return_type.parse(ctx); trait_fn.attributes.parse(ctx); } @@ -1116,12 +912,6 @@ impl Parse for TraitConstraint { Token::from_parsed(AstToken::Ident(prefix.clone()), SymbolKind::Function), ); }); - // self.trait_name.prefixes.par_iter().for_each(|prefix| { - // ctx.tokens.insert( - // ctx.ident(prefix), - // Token::from_parsed(AstToken::Ident(prefix.clone()), SymbolKind::Function), - // ); - // }); ctx.tokens.insert( ctx.ident(&self.trait_name.suffix), Token::from_parsed( @@ -1130,9 +920,6 @@ impl Parse for TraitConstraint { ), ); adaptive_iter(&self.type_arguments, |type_arg| type_arg.parse(ctx)); - // self.type_arguments.par_iter().for_each(|type_arg| { - // type_arg.parse(ctx); - // }); } } @@ -1197,9 +984,6 @@ impl Parse for TypeArgument { } TypeInfo::Tuple(type_arguments) => { adaptive_iter(type_arguments, |type_arg| type_arg.parse(ctx)); - // type_arguments.par_iter().for_each(|type_arg| { - // type_arg.parse(ctx); - // }); } _ => { let symbol_kind = type_info_to_symbol_kind(ctx.engines.te(), &type_info, None); @@ -1248,9 +1032,6 @@ fn collect_type_info_token(ctx: &ParseContext, type_info: &TypeInfo, type_span: } TypeInfo::Tuple(type_arguments) => { adaptive_iter(type_arguments, |type_arg| type_arg.parse(ctx)); - // type_arguments.par_iter().for_each(|type_arg| { - // type_arg.parse(ctx); - // }); } TypeInfo::Custom { qualified_call_path, @@ -1258,16 +1039,12 @@ fn collect_type_info_token(ctx: &ParseContext, type_info: &TypeInfo, type_span: root_type_id: _, } => { collect_qualified_path_root(ctx, qualified_call_path.qualified_path_root.clone()); - let ident = qualified_call_path.call_path.suffix.clone(); let mut token = Token::from_parsed(AstToken::Ident(ident.clone()), symbol_kind); token.type_def = Some(TypeDefinition::Ident(ident.clone())); ctx.tokens.insert(ctx.ident(&ident), token); if let Some(type_arguments) = type_arguments { adaptive_iter(type_arguments, |type_arg| type_arg.parse(ctx)); - // type_arguments.par_iter().for_each(|type_arg| { - // type_arg.parse(ctx); - // }); } } _ => { @@ -1293,21 +1070,11 @@ fn collect_call_path_tree( adaptive_iter(&tree.qualified_call_path.call_path.prefixes, |ident| { tokens.insert(ctx.ident(ident), token.clone()); }); - // tree.qualified_call_path - // .call_path - // .prefixes - // .par_iter() - // .for_each(|ident| { - // tokens.insert(ctx.ident(ident), token.clone()); - // }); tokens.insert( ctx.ident(&tree.qualified_call_path.call_path.suffix), token.clone(), ); adaptive_iter(&tree.children, |child| collect_call_path_tree(ctx, child, token, tokens)); - // tree.children.par_iter().for_each(|child| { - // collect_call_path_tree(ctx, child, token, tokens); - // }); } fn collect_qualified_path_root( diff --git a/sway-lsp/src/traverse/typed_tree.rs b/sway-lsp/src/traverse/typed_tree.rs index c0467be76cd..7fc37ae31cd 100644 --- a/sway-lsp/src/traverse/typed_tree.rs +++ b/sway-lsp/src/traverse/typed_tree.rs @@ -223,13 +223,6 @@ impl Parse for ty::TyExpression { adaptive_iter(&type_binding.type_arguments.to_vec(), |type_arg| { collect_type_argument(ctx, type_arg); }); - // type_binding - // .type_arguments - // .to_vec() - // .par_iter() - // .for_each(|type_arg| { - // collect_type_argument(ctx, type_arg); - // }); } let implementing_type_name = (*ctx.engines.de().get_function(fn_ref)) .clone() @@ -268,19 +261,8 @@ impl Parse for ty::TyExpression { } exp.parse(ctx); }); - // arguments.par_iter().for_each(|(ident, exp)| { - // if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { - // token.typed = Some(TypedAstToken::Ident(ident.clone())); - // } - // exp.parse(ctx); - // }); let function_decl = ctx.engines.de().get_function(fn_ref); adaptive_iter(&function_decl.body.contents, |node| node.parse(ctx)); - // function_decl - // .body - // .contents - // .par_iter() - // .for_each(|node| node.parse(ctx)); } ty::TyExpressionVariant::LazyOperator { lhs, rhs, .. } => { lhs.parse(ctx); @@ -316,14 +298,12 @@ impl Parse for ty::TyExpression { } ty::TyExpressionVariant::Tuple { fields } => { adaptive_iter(fields, |field| field.parse(ctx)); - // fields.par_iter().for_each(|field| field.parse(ctx)); } ty::TyExpressionVariant::Array { elem_type: _, contents, } => { adaptive_iter(contents, |exp| exp.parse(ctx)); - // contents.par_iter().for_each(|exp| exp.parse(ctx)); } ty::TyExpressionVariant::ArrayIndex { prefix, index } => { prefix.parse(ctx); @@ -344,20 +324,12 @@ impl Parse for ty::TyExpression { adaptive_iter(&call_path_binding.type_arguments.to_vec(), |type_arg| { collect_type_argument(ctx, type_arg); }); - // call_path_binding - // .type_arguments - // .to_vec() - // .par_iter() - // .for_each(|type_arg| { - // collect_type_argument(ctx, type_arg); - // }); collect_call_path_prefixes(ctx, &call_path_binding.inner.prefixes); adaptive_iter(fields, |field| { if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(&field.name)) { token.typed = Some(TypedAstToken::TypedExpression(field.value.clone())); - if let Some(struct_decl) = &ctx .tokens .struct_declaration_of_type_id(ctx.engines, &self.return_type) @@ -372,33 +344,9 @@ impl Parse for ty::TyExpression { } field.value.parse(ctx); }); - // fields.par_iter().for_each(|field| { - // if let Some(mut token) = - // ctx.tokens.try_get_mut_with_retry(&ctx.ident(&field.name)) - // { - // token.typed = Some(TypedAstToken::TypedExpression(field.value.clone())); - - // if let Some(struct_decl) = &ctx - // .tokens - // .struct_declaration_of_type_id(ctx.engines, &self.return_type) - // { - // struct_decl.fields.iter().for_each(|decl_field| { - // if decl_field.name == field.name { - // token.type_def = - // Some(TypeDefinition::Ident(decl_field.name.clone())); - // } - // }); - // } - // } - // field.value.parse(ctx); - // }); } ty::TyExpressionVariant::CodeBlock(code_block) => { adaptive_iter(&code_block.contents, |node| node.parse(ctx)); - // code_block - // .contents - // .par_iter() - // .for_each(|node| node.parse(ctx)); } ty::TyExpressionVariant::FunctionParameter { .. } => {} ty::TyExpressionVariant::MatchExp { @@ -410,7 +358,6 @@ impl Parse for ty::TyExpression { // conditions desugared.parse(ctx); adaptive_iter(scrutinees, |s| s.parse(ctx)); - // scrutinees.par_iter().for_each(|s| s.parse(ctx)); } ty::TyExpressionVariant::IfExp { condition, @@ -429,11 +376,6 @@ impl Parse for ty::TyExpression { initializer.parse(ctx); } }); - // registers.par_iter().for_each(|r| { - // if let Some(initializer) = &r.initializer { - // initializer.parse(ctx); - // } - // }); } ty::TyExpressionVariant::StructFieldAccess { prefix, @@ -480,13 +422,6 @@ impl Parse for ty::TyExpression { adaptive_iter(&call_path_binding.type_arguments.to_vec(), |type_arg| { collect_type_argument(ctx, type_arg); }); - // call_path_binding - // .type_arguments - // .to_vec() - // .par_iter() - // .for_each(|type_arg| { - // collect_type_argument(ctx, type_arg); - // }); collect_call_path_prefixes(ctx, &call_path_binding.inner.prefixes); if let Some(mut token) = ctx.tokens.try_get_mut_with_retry( &ctx.ident(&Ident::new(variant_instantiation_span.clone())), @@ -616,7 +551,6 @@ impl Parse for ty::TyExpression { } => { condition.parse(ctx); adaptive_iter(&body.contents, |node| node.parse(ctx)); - // body.contents.par_iter().for_each(|node| node.parse(ctx)); } ty::TyExpressionVariant::ForLoop { desugared, .. } => { desugared.parse(ctx); @@ -677,16 +611,7 @@ impl Parse for ty::FunctionDecl { token.type_def = Some(TypeDefinition::Ident(func_decl.name.clone())); } adaptive_iter(&func_decl.body.contents, |node| node.parse(ctx)); - // func_decl - // .body - // .contents - // .par_iter() - // .for_each(|node| node.parse(ctx)); adaptive_iter(&func_decl.parameters, |param| param.parse(ctx)); - // func_decl - // .parameters - // .par_iter() - // .for_each(|param| param.parse(ctx)); adaptive_iter(&func_decl.type_parameters, |type_param| { collect_type_id( ctx, @@ -695,23 +620,11 @@ impl Parse for ty::FunctionDecl { type_param.name_ident.span(), ); }); - // func_decl.type_parameters.par_iter().for_each(|type_param| { - // collect_type_id( - // ctx, - // type_param.type_id, - // &typed_token, - // type_param.name_ident.span(), - // ); - // }); collect_type_argument(ctx, &func_decl.return_type); - adaptive_iter(&func_decl.where_clause, |(ident, trait_constraints)| { adaptive_iter(trait_constraints, |constraint| { collect_trait_constraint(ctx, constraint); }); - // trait_constraints.par_iter().for_each(|constraint| { - // collect_trait_constraint(ctx, constraint); - // }); if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { token.typed = Some(typed_token.clone()); if let Some(param_decl_ident) = func_decl @@ -724,26 +637,6 @@ impl Parse for ty::FunctionDecl { } } }); - - // func_decl - // .where_clause - // .par_iter() - // .for_each(|(ident, trait_constraints)| { - // adaptive_iter(trait_constraints, |constraint| { - // collect_trait_constraint(ctx, constraint); - // }); - // if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { - // token.typed = Some(typed_token.clone()); - // if let Some(param_decl_ident) = func_decl - // .type_parameters - // .par_iter() - // .find_any(|type_param| type_param.name_ident.as_str() == ident.as_str()) - // .map(|type_param| type_param.name_ident.clone()) - // { - // token.type_def = Some(TypeDefinition::Ident(param_decl_ident)); - // } - // } - // }); } } @@ -773,29 +666,9 @@ impl Parse for ty::TraitDecl { collect_trait_type_decl(ctx, &trait_type, &decl_ref.span()); } }); - // trait_decl - // .interface_surface - // .par_iter() - // .for_each(|item| match item { - // ty::TyTraitInterfaceItem::TraitFn(trait_fn_decl_ref) => { - // let trait_fn = ctx.engines.de().get_trait_fn(trait_fn_decl_ref); - // trait_fn.parse(ctx); - // } - // ty::TyTraitInterfaceItem::Constant(decl_ref) => { - // let constant = ctx.engines.de().get_constant(decl_ref); - // collect_const_decl(ctx, &constant, None); - // } - // ty::TyTraitInterfaceItem::Type(decl_ref) => { - // let trait_type = ctx.engines.de().get_type(decl_ref); - // collect_trait_type_decl(ctx, &trait_type, &decl_ref.span()); - // } - // }); adaptive_iter(&trait_decl.supertraits, |supertrait| { collect_supertrait(ctx, supertrait); }); - // trait_decl.supertraits.par_iter().for_each(|supertrait| { - // collect_supertrait(ctx, supertrait); - // }); } } @@ -814,9 +687,6 @@ impl Parse for ty::StructDecl { adaptive_iter(&struct_decl.fields, |field| { field.parse(ctx); }); - // struct_decl.fields.par_iter().for_each(|field| { - // field.parse(ctx); - // }); adaptive_iter(&struct_decl.type_parameters, |type_param| { if let Some(mut token) = ctx .tokens @@ -826,18 +696,6 @@ impl Parse for ty::StructDecl { token.type_def = Some(TypeDefinition::TypeId(type_param.type_id)); } }); - // struct_decl - // .type_parameters - // .par_iter() - // .for_each(|type_param| { - // if let Some(mut token) = ctx - // .tokens - // .try_get_mut_with_retry(&ctx.ident(&type_param.name_ident)) - // { - // token.typed = Some(TypedAstToken::TypedParameter(type_param.clone())); - // token.type_def = Some(TypeDefinition::TypeId(type_param.type_id)); - // } - // }); } } @@ -861,25 +719,11 @@ impl Parse for ty::ImplTrait { param.name_ident.span(), ); }); - // impl_type_parameters.par_iter().for_each(|param| { - // collect_type_id( - // ctx, - // param.type_id, - // &TypedAstToken::TypedParameter(param.clone()), - // param.name_ident.span(), - // ); - // }); adaptive_iter(&trait_name.prefixes, |ident| { if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { token.typed = Some(TypedAstToken::Ident(ident.clone())); } }); - // trait_name.prefixes.par_iter().for_each(|ident| { - // if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { - // token.typed = Some(TypedAstToken::Ident(ident.clone())); - // } - // }); - // Which typed token should be used for collect_type_id // if trait_decl_ref is some, then our ImplTrait is for an ABI or Trait. In this instance, // we want to use the TypedArgument(implementing_for) type as the typed token. @@ -913,9 +757,6 @@ impl Parse for ty::ImplTrait { adaptive_iter(trait_type_arguments, |type_arg| { collect_type_argument(ctx, type_arg); }); - // trait_type_arguments.par_iter().for_each(|type_arg| { - // collect_type_argument(ctx, type_arg); - // }); adaptive_iter(items, |item| { match item { ty::TyTraitItem::Fn(method_ref) => { @@ -932,22 +773,7 @@ impl Parse for ty::ImplTrait { } } }); - // items.par_iter().for_each(|item| match item { - // ty::TyTraitItem::Fn(method_ref) => { - // let method = ctx.engines.de().get_function(method_ref); - // method.parse(ctx); - // } - // ty::TyTraitItem::Constant(const_ref) => { - // let constant = ctx.engines.de().get_constant(const_ref); - // collect_const_decl(ctx, &constant, None); - // } - // ty::TyTraitItem::Type(type_ref) => { - // let trait_type = ctx.engines.de().get_type(type_ref); - // collect_trait_type_decl(ctx, &trait_type, &type_ref.span()); - // } - // }); collect_type_argument(ctx, implementing_for); - // collect the root type argument again with declaration info this time so the // impl is registered if let Some(typed_token) = typed_token { @@ -991,29 +817,9 @@ impl Parse for ty::AbiDecl { collect_trait_type_decl(ctx, &trait_type, &type_ref.span()); } }); - // abi_decl - // .interface_surface - // .par_iter() - // .for_each(|item| match item { - // ty::TyTraitInterfaceItem::TraitFn(trait_fn_decl_ref) => { - // let trait_fn = ctx.engines.de().get_trait_fn(trait_fn_decl_ref); - // trait_fn.parse(ctx); - // } - // ty::TyTraitInterfaceItem::Constant(const_ref) => { - // let constant = ctx.engines.de().get_constant(const_ref); - // collect_const_decl(ctx, &constant, None); - // } - // ty::TyTraitInterfaceItem::Type(type_ref) => { - // let trait_type = ctx.engines.de().get_type(type_ref); - // collect_trait_type_decl(ctx, &trait_type, &type_ref.span()); - // } - // }); adaptive_iter(&abi_decl.supertraits, |supertrait| { supertrait.parse(ctx); }); - // abi_decl.supertraits.par_iter().for_each(|supertrait| { - // supertrait.parse(ctx); - // }); } } @@ -1039,14 +845,6 @@ impl Parse for ty::StorageDecl { collect_type_argument(ctx, &field.type_argument); field.initializer.parse(ctx); }); - // storage_decl.fields.par_iter().for_each(|field| { - // if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(&field.name)) { - // token.typed = Some(TypedAstToken::TypedStorageField(field.clone())); - // token.type_def = Some(TypeDefinition::Ident(field.name.clone())); - // } - // collect_type_argument(ctx, &field.type_argument); - // field.initializer.parse(ctx); - // }); } } @@ -1075,9 +873,6 @@ impl Parse for ty::TyTraitFn { token.type_def = Some(TypeDefinition::Ident(self.name.clone())); } adaptive_iter(&self.parameters, |param| param.parse(ctx)); - // self.parameters - // .par_iter() - // .for_each(|param| param.parse(ctx)); let return_ident = Ident::new(self.return_type.span.clone()); if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(&return_ident)) { token.typed = Some(TypedAstToken::TypedTraitFn(self.clone())); @@ -1115,14 +910,7 @@ impl Parse for ty::TyFunctionDecl { token.type_def = Some(TypeDefinition::Ident(self.name.clone())); } adaptive_iter(&self.body.contents, |node| node.parse(ctx)); - // self.body - // .contents - // .par_iter() - // .for_each(|node| node.parse(ctx)); adaptive_iter(&self.parameters, |param| param.parse(ctx)); - // self.parameters - // .par_iter() - // .for_each(|param| param.parse(ctx)); adaptive_iter(&self.type_parameters, |type_param| { collect_type_id( ctx, @@ -1131,23 +919,11 @@ impl Parse for ty::TyFunctionDecl { type_param.name_ident.span(), ); }); - // self.type_parameters.par_iter().for_each(|type_param| { - // collect_type_id( - // ctx, - // type_param.type_id, - // &typed_token, - // type_param.name_ident.span(), - // ); - // }); collect_type_argument(ctx, &self.return_type); - adaptive_iter(&self.where_clause, |(ident, trait_constraints)| { adaptive_iter(trait_constraints, |constraint| { collect_trait_constraint(ctx, constraint); }); - // trait_constraints.par_iter().for_each(|constraint| { - // collect_trait_constraint(ctx, constraint); - // }); if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { token.typed = Some(typed_token.clone()); if let Some(param_decl_ident) = self @@ -1160,24 +936,6 @@ impl Parse for ty::TyFunctionDecl { } } }); - // self.where_clause - // .par_iter() - // .for_each(|(ident, trait_constraints)| { - // adaptive_iter(trait_constraints, |constraint| { - // collect_trait_constraint(ctx, constraint); - // }); - // if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(ident)) { - // token.typed = Some(typed_token.clone()); - // if let Some(param_decl_ident) = self - // .type_parameters - // .par_iter() - // .find_any(|type_param| type_param.name_ident.as_str() == ident.as_str()) - // .map(|type_param| type_param.name_ident.clone()) - // { - // token.type_def = Some(TypeDefinition::Ident(param_decl_ident)); - // } - // } - // }); } } @@ -1196,15 +954,9 @@ impl Parse for ty::TyIntrinsicFunctionKind { adaptive_iter(&self.type_arguments, |type_arg| { collect_type_argument(ctx, type_arg); }); - // self.type_arguments.par_iter().for_each(|type_arg| { - // collect_type_argument(ctx, type_arg); - // }); adaptive_iter(&self.arguments, |arg| { arg.parse(ctx); }); - // self.arguments.par_iter().for_each(|arg| { - // arg.parse(ctx); - // }); } } @@ -1246,7 +998,6 @@ impl Parse for ty::TyScrutinee { token.type_def = Some(TypeDefinition::Ident(struct_ref.name().clone())); } adaptive_iter(fields, |field| field.parse(ctx)); - // fields.par_iter().for_each(|field| field.parse(ctx)); } EnumScrutinee { enum_ref, @@ -1279,7 +1030,6 @@ impl Parse for ty::TyScrutinee { } Tuple(scrutinees) | Or(scrutinees) => { adaptive_iter(scrutinees, |s| s.parse(ctx)); - // scrutinees.par_iter().for_each(|s| s.parse(ctx)); } } } @@ -1324,24 +1074,6 @@ impl Parse for ty::TyReassignment { } } }); - // self.lhs_indices.par_iter().for_each(|proj_kind| { - // if let ty::ProjectionKind::StructField { name } = proj_kind { - // if let Some(mut token) = ctx.tokens.try_get_mut_with_retry(&ctx.ident(name)) { - // token.typed = Some(TypedAstToken::TypedReassignment(self.clone())); - // if let Some(struct_decl) = &ctx - // .tokens - // .struct_declaration_of_type_id(ctx.engines, &self.lhs_type) - // { - // struct_decl.fields.iter().for_each(|decl_field| { - // if &decl_field.name == name { - // token.type_def = - // Some(TypeDefinition::Ident(decl_field.name.clone())); - // } - // }); - // } - // } - // } - // }); } } @@ -1497,9 +1229,6 @@ fn collect_type_id( adaptive_iter(type_arguments, |type_arg| { collect_type_argument(ctx, type_arg); }); - // type_arguments.par_iter().for_each(|type_arg| { - // collect_type_argument(ctx, type_arg); - // }); } TypeInfo::Enum(decl_ref) => { let decl = ctx.engines.de().get_enum(decl_ref); @@ -1517,20 +1246,9 @@ fn collect_type_id( param.name_ident.span(), ); }); - // decl.type_parameters.par_iter().for_each(|param| { - // collect_type_id( - // ctx, - // param.type_id, - // &TypedAstToken::TypedParameter(param.clone()), - // param.name_ident.span(), - // ); - // }); adaptive_iter(&decl.variants, |variant| { variant.parse(ctx); }); - // decl.variants.par_iter().for_each(|variant| { - // variant.parse(ctx); - // }); } TypeInfo::Struct(decl_ref) => { let decl = ctx.engines.de().get_struct(decl_ref); @@ -1548,20 +1266,9 @@ fn collect_type_id( param.name_ident.span(), ); }); - // decl.type_parameters.par_iter().for_each(|param| { - // collect_type_id( - // ctx, - // param.type_id, - // &TypedAstToken::TypedParameter(param.clone()), - // param.name_ident.span(), - // ); - // }); adaptive_iter(&decl.fields, |field| { field.parse(ctx); }); - // decl.fields.par_iter().for_each(|field| { - // field.parse(ctx); - // }); } TypeInfo::Custom { type_arguments, @@ -1579,18 +1286,12 @@ fn collect_type_id( adaptive_iter(type_arguments, |type_arg| { collect_type_argument(ctx, type_arg); }); - // type_arguments.par_iter().for_each(|type_arg| { - // collect_type_argument(ctx, type_arg); - // }); } } TypeInfo::Storage { fields } => { adaptive_iter(fields, |field| { field.parse(ctx); }); - // fields.par_iter().for_each(|field| { - // field.parse(ctx); - // }); } _ => { if let Some(token) = ctx @@ -1643,9 +1344,6 @@ fn collect_trait_constraint( adaptive_iter(type_arguments, |type_arg| { collect_type_argument(ctx, type_arg); }); - // type_arguments.par_iter().for_each(|type_arg| { - // collect_type_argument(ctx, type_arg); - // }); } fn collect_supertrait(ctx: &ParseContext, supertrait: &Supertrait) { @@ -1681,21 +1379,9 @@ fn collect_enum(ctx: &ParseContext, decl_id: &DeclId, declaratio token.type_def = Some(TypeDefinition::TypeId(type_param.type_id)); } }); - // enum_decl.type_parameters.par_iter().for_each(|type_param| { - // if let Some(mut token) = ctx - // .tokens - // .try_get_mut_with_retry(&ctx.ident(&type_param.name_ident)) - // { - // token.typed = Some(TypedAstToken::TypedParameter(type_param.clone())); - // token.type_def = Some(TypeDefinition::TypeId(type_param.type_id)); - // } - // }); adaptive_iter(&enum_decl.variants, |variant| { variant.parse(ctx); }); - // enum_decl.variants.par_iter().for_each(|variant| { - // variant.parse(ctx); - // }); } fn collect_qualified_path_root( From 57ed63ac05cff74bacfa3367505c945aa47b6289 Mon Sep 17 00:00:00 2001 From: Joshua Batty Date: Wed, 20 Mar 2024 13:15:32 +1100 Subject: [PATCH 4/8] fmt --- sway-lsp/src/traverse/lexed_tree.rs | 4 +-- sway-lsp/src/traverse/mod.rs | 2 +- sway-lsp/src/traverse/parsed_tree.rs | 52 ++++++++++++++++++++-------- sway-lsp/src/traverse/typed_tree.rs | 28 +++++++-------- 4 files changed, 52 insertions(+), 34 deletions(-) diff --git a/sway-lsp/src/traverse/lexed_tree.rs b/sway-lsp/src/traverse/lexed_tree.rs index 1ff00e9c16b..b8070143738 100644 --- a/sway-lsp/src/traverse/lexed_tree.rs +++ b/sway-lsp/src/traverse/lexed_tree.rs @@ -1,6 +1,6 @@ use crate::{ core::token::{AstToken, SymbolKind, Token}, - traverse::{Parse, ParseContext, adaptive_iter}, + traverse::{adaptive_iter, Parse, ParseContext}, }; use rayon::iter::{ParallelBridge, ParallelIterator}; use sway_ast::{ @@ -14,8 +14,6 @@ use sway_ast::{ use sway_core::language::{lexed::LexedProgram, HasSubmodules}; use sway_types::{Ident, Span, Spanned}; - - pub fn parse(lexed_program: &LexedProgram, ctx: &ParseContext) { insert_module_kind(ctx, &lexed_program.root.tree.kind); adaptive_iter(&lexed_program.root.tree.items, |item| { diff --git a/sway-lsp/src/traverse/mod.rs b/sway-lsp/src/traverse/mod.rs index fe69a06a2c7..1e6077605f2 100644 --- a/sway-lsp/src/traverse/mod.rs +++ b/sway-lsp/src/traverse/mod.rs @@ -45,4 +45,4 @@ where // Create a conditional iterator based on the use_parallel flag CondIterator::new(items, use_parallel).for_each(action); -} \ No newline at end of file +} diff --git a/sway-lsp/src/traverse/parsed_tree.rs b/sway-lsp/src/traverse/parsed_tree.rs index 9a2e6cc06cb..434081e13cf 100644 --- a/sway-lsp/src/traverse/parsed_tree.rs +++ b/sway-lsp/src/traverse/parsed_tree.rs @@ -7,7 +7,7 @@ use crate::{ }, token_map::TokenMap, }, - traverse::{Parse, ParseContext, adaptive_iter}, + traverse::{adaptive_iter, Parse, ParseContext}, }; use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use sway_core::{ @@ -374,7 +374,9 @@ impl Parse for IntrinsicFunctionExpression { ), ); adaptive_iter(&self.arguments, |arg| arg.parse(ctx)); - adaptive_iter(&self.kind_binding.type_arguments.to_vec(), |type_arg| type_arg.parse(ctx)); + adaptive_iter(&self.kind_binding.type_arguments.to_vec(), |type_arg| { + type_arg.parse(ctx) + }); } } @@ -413,7 +415,9 @@ impl Parse for DelineatedPathExpression { SymbolKind::Variant, ), ); - adaptive_iter(&call_path_binding.type_arguments.to_vec(), |type_arg| type_arg.parse(ctx)); + adaptive_iter(&call_path_binding.type_arguments.to_vec(), |type_arg| { + type_arg.parse(ctx) + }); if let Some(args_vec) = args.as_ref() { adaptive_iter(args_vec, |exp| exp.parse(ctx)); } @@ -448,7 +452,9 @@ impl Parse for AmbiguousPathExpression { SymbolKind::Variant, ), ); - adaptive_iter(&call_path_binding.type_arguments.to_vec(), |type_arg| type_arg.parse(ctx)); + adaptive_iter(&call_path_binding.type_arguments.to_vec(), |type_arg| { + type_arg.parse(ctx) + }); adaptive_iter(args, |exp| exp.parse(ctx)); collect_qualified_path_root(ctx, qualified_path_root.clone().map(Box::new)); } @@ -470,9 +476,12 @@ impl Parse for MethodApplicationExpression { let (type_info, ident) = &call_path_binding.inner.suffix; collect_type_info_token(ctx, type_info, Some(&ident.span())); } - adaptive_iter(&self.method_name_binding.type_arguments.to_vec(), |type_arg| { - type_arg.parse(ctx); - }); + adaptive_iter( + &self.method_name_binding.type_arguments.to_vec(), + |type_arg| { + type_arg.parse(ctx); + }, + ); // Don't collect applications of desugared operators due to mismatched ident lengths. if !desugared_op(&prefixes) { ctx.tokens.insert( @@ -636,9 +645,12 @@ impl Parse for FunctionApplicationExpression { SymbolKind::Function, ), ); - adaptive_iter(&self.call_path_binding.type_arguments.to_vec(), |type_arg| { - type_arg.parse(ctx); - }); + adaptive_iter( + &self.call_path_binding.type_arguments.to_vec(), + |type_arg| { + type_arg.parse(ctx); + }, + ); } adaptive_iter(&self.arguments, |exp| exp.parse(ctx)); } @@ -730,7 +742,9 @@ impl Parse for ParsedDeclId { ), ); adaptive_iter(&struct_decl.fields, |field| field.parse(ctx)); - adaptive_iter(&struct_decl.type_parameters, |type_param| type_param.parse(ctx)); + adaptive_iter(&struct_decl.type_parameters, |type_param| { + type_param.parse(ctx) + }); struct_decl.attributes.parse(ctx); } } @@ -745,7 +759,9 @@ impl Parse for ParsedDeclId { SymbolKind::Enum, ), ); - adaptive_iter(&enum_decl.type_parameters, |type_param| type_param.parse(ctx)); + adaptive_iter(&enum_decl.type_parameters, |type_param| { + type_param.parse(ctx) + }); adaptive_iter(&enum_decl.variants, |variant| variant.parse(ctx)); enum_decl.attributes.parse(ctx); } @@ -768,7 +784,9 @@ impl Parse for ParsedDeclId { ), ); impl_trait.implementing_for.parse(ctx); - adaptive_iter(&impl_trait.impl_type_parameters, |type_param| type_param.parse(ctx)); + adaptive_iter(&impl_trait.impl_type_parameters, |type_param| { + type_param.parse(ctx) + }); adaptive_iter(&impl_trait.items, |item| match item { ImplItem::Fn(fn_decl) => fn_decl.parse(ctx), ImplItem::Constant(const_decl) => const_decl.parse(ctx), @@ -797,7 +815,9 @@ impl Parse for ParsedDeclId { adaptive_iter(type_arguments, |type_arg| type_arg.parse(ctx)); } } - adaptive_iter(&impl_self.impl_type_parameters, |type_param| type_param.parse(ctx)); + adaptive_iter(&impl_self.impl_type_parameters, |type_param| { + type_param.parse(ctx) + }); adaptive_iter(&impl_self.items, |item| match item { ImplItem::Fn(fn_decl) => fn_decl.parse(ctx), ImplItem::Constant(const_decl) => const_decl.parse(ctx), @@ -1074,7 +1094,9 @@ fn collect_call_path_tree( ctx.ident(&tree.qualified_call_path.call_path.suffix), token.clone(), ); - adaptive_iter(&tree.children, |child| collect_call_path_tree(ctx, child, token, tokens)); + adaptive_iter(&tree.children, |child| { + collect_call_path_tree(ctx, child, token, tokens) + }); } fn collect_qualified_path_root( diff --git a/sway-lsp/src/traverse/typed_tree.rs b/sway-lsp/src/traverse/typed_tree.rs index 7fc37ae31cd..fb8bf01809d 100644 --- a/sway-lsp/src/traverse/typed_tree.rs +++ b/sway-lsp/src/traverse/typed_tree.rs @@ -3,7 +3,7 @@ use crate::{ core::token::{ type_info_to_symbol_kind, SymbolKind, Token, TokenIdent, TypeDefinition, TypedAstToken, }, - traverse::{Parse, ParseContext, adaptive_iter}, + traverse::{adaptive_iter, Parse, ParseContext}, }; use dashmap::mapref::one::RefMut; use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator}; @@ -757,20 +757,18 @@ impl Parse for ty::ImplTrait { adaptive_iter(trait_type_arguments, |type_arg| { collect_type_argument(ctx, type_arg); }); - adaptive_iter(items, |item| { - match item { - ty::TyTraitItem::Fn(method_ref) => { - let method = ctx.engines.de().get_function(method_ref); - method.parse(ctx); - } - ty::TyTraitItem::Constant(const_ref) => { - let constant = ctx.engines.de().get_constant(const_ref); - collect_const_decl(ctx, &constant, None); - } - ty::TyTraitItem::Type(type_ref) => { - let trait_type = ctx.engines.de().get_type(type_ref); - collect_trait_type_decl(ctx, &trait_type, &type_ref.span()); - } + adaptive_iter(items, |item| match item { + ty::TyTraitItem::Fn(method_ref) => { + let method = ctx.engines.de().get_function(method_ref); + method.parse(ctx); + } + ty::TyTraitItem::Constant(const_ref) => { + let constant = ctx.engines.de().get_constant(const_ref); + collect_const_decl(ctx, &constant, None); + } + ty::TyTraitItem::Type(type_ref) => { + let trait_type = ctx.engines.de().get_type(type_ref); + collect_trait_type_decl(ctx, &trait_type, &type_ref.span()); } }); collect_type_argument(ctx, implementing_for); From 65433ab08b5e433fb731b1436017fefe4e9d3293 Mon Sep 17 00:00:00 2001 From: Joshua Batty Date: Wed, 20 Mar 2024 13:38:17 +1100 Subject: [PATCH 5/8] use 8 for par threshold --- sway-lsp/Cargo.toml | 1 + sway-lsp/src/traverse/lexed_tree.rs | 4 ---- sway-lsp/src/traverse/mod.rs | 2 +- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/sway-lsp/Cargo.toml b/sway-lsp/Cargo.toml index 2f317451c27..1d7ca554bcc 100644 --- a/sway-lsp/Cargo.toml +++ b/sway-lsp/Cargo.toml @@ -24,6 +24,7 @@ parking_lot = "0.12.1" proc-macro2 = "1.0.5" quote = "1.0.9" rayon = "1.5.0" +rayon-cond = "0.3" ropey = "1.2" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.60" diff --git a/sway-lsp/src/traverse/lexed_tree.rs b/sway-lsp/src/traverse/lexed_tree.rs index b8070143738..3f30fd41caa 100644 --- a/sway-lsp/src/traverse/lexed_tree.rs +++ b/sway-lsp/src/traverse/lexed_tree.rs @@ -156,10 +156,6 @@ impl Parse for Expr { branch.pattern.parse(ctx); branch.kind.parse(ctx); }); - // branches.get().iter().par_bridge().for_each(|branch| { - // branch.pattern.parse(ctx); - // branch.kind.parse(ctx); - // }); } Expr::While { while_token, diff --git a/sway-lsp/src/traverse/mod.rs b/sway-lsp/src/traverse/mod.rs index 1e6077605f2..31bb55650c3 100644 --- a/sway-lsp/src/traverse/mod.rs +++ b/sway-lsp/src/traverse/mod.rs @@ -32,7 +32,7 @@ pub trait Parse { fn parse(&self, ctx: &ParseContext); } -const PARALLEL_THRESHOLD: usize = 10; +const PARALLEL_THRESHOLD: usize = 8; /// Iterates over items, choosing parallel or sequential execution based on size. pub fn adaptive_iter(items: &[T], action: F) From d8df8738750d3dcc7116710334d4a72b5aaae1b2 Mon Sep 17 00:00:00 2001 From: Joshua Batty Date: Wed, 20 Mar 2024 13:38:37 +1100 Subject: [PATCH 6/8] add Cargo.lock --- Cargo.lock | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index b2429e15243..7feacf58aec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3668,6 +3668,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.12.1" @@ -5323,6 +5332,17 @@ dependencies = [ "rayon-core", ] +[[package]] +name = "rayon-cond" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "059f538b55efd2309c9794130bc149c6a553db90e9d99c2030785c82f0bd7df9" +dependencies = [ + "either", + "itertools 0.11.0", + "rayon", +] + [[package]] name = "rayon-core" version = "1.12.1" @@ -6591,6 +6611,7 @@ dependencies = [ "quote", "rand", "rayon", + "rayon-cond", "regex", "ropey", "serde", From b7af6ec558d63d3650e01941346a7392172058dc Mon Sep 17 00:00:00 2001 From: JoshuaBatty Date: Thu, 21 Mar 2024 12:36:27 +1100 Subject: [PATCH 7/8] doc comment --- sway-lsp/src/traverse/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/sway-lsp/src/traverse/mod.rs b/sway-lsp/src/traverse/mod.rs index 31bb55650c3..30b891990fe 100644 --- a/sway-lsp/src/traverse/mod.rs +++ b/sway-lsp/src/traverse/mod.rs @@ -32,6 +32,7 @@ pub trait Parse { fn parse(&self, ctx: &ParseContext); } +/// Determines the threshold a collection must meet to be processed in parallel. const PARALLEL_THRESHOLD: usize = 8; /// Iterates over items, choosing parallel or sequential execution based on size. From b20a6675029c9041dd343295d2f9692a4bbfc9e2 Mon Sep 17 00:00:00 2001 From: JoshuaBatty Date: Thu, 21 Mar 2024 12:37:17 +1100 Subject: [PATCH 8/8] rebase master