diff --git a/sway-core/src/asm_generation/fuel/functions.rs b/sway-core/src/asm_generation/fuel/functions.rs index 59748d6e7ff..c82a92a4524 100644 --- a/sway-core/src/asm_generation/fuel/functions.rs +++ b/sway-core/src/asm_generation/fuel/functions.rs @@ -17,6 +17,7 @@ use crate::{ use sway_ir::*; use either::Either; +use sway_types::Ident; /// A summary of the adopted calling convention: /// @@ -150,7 +151,11 @@ impl<'ir> FuelAsmBuilder<'ir> { let span = self.md_mgr.md_to_span(self.context, md); let test_decl_index = self.md_mgr.md_to_test_decl_index(self.context, md); let test_decl_id = match (&span, &test_decl_index) { - (Some(span), Some(decl_index)) => Some(DeclId::new(*decl_index, span.clone())), + (Some(span), Some(decl_index)) => Some(DeclId::new( + Ident::new(span.clone()), + *decl_index, + span.clone(), + )), _ => None, }; let comment = format!( diff --git a/sway-core/src/control_flow_analysis/analyze_return_paths.rs b/sway-core/src/control_flow_analysis/analyze_return_paths.rs index fff4a946b66..9fecd72f5e5 100644 --- a/sway-core/src/control_flow_analysis/analyze_return_paths.rs +++ b/sway-core/src/control_flow_analysis/analyze_return_paths.rs @@ -136,7 +136,7 @@ fn connect_node<'eng: 'cfg, 'cfg>( .. }) | ty::TyAstNodeContent::ImplicitReturnExpression(_) => { - let this_index = graph.add_node(engines, node.into()); + let this_index = graph.add_node(node.into()); for leaf_ix in leaves { graph.add_edge(*leaf_ix, this_index, "".into()); } @@ -149,14 +149,14 @@ fn connect_node<'eng: 'cfg, 'cfg>( // An abridged version of the dead code analysis for a while loop // since we don't really care about what the loop body contains when detecting // divergent paths - let node = graph.add_node(engines, node.into()); + let node = graph.add_node(node.into()); for leaf in leaves { graph.add_edge(*leaf, node, "while loop entry".into()); } Ok(NodeConnection::NextStep(vec![node])) } ty::TyAstNodeContent::Expression(ty::TyExpression { .. }) => { - let entry = graph.add_node(engines, node.into()); + let entry = graph.add_node(node.into()); // insert organizational dominator node // connected to all current leaves for leaf in leaves { @@ -189,7 +189,7 @@ fn connect_declaration<'eng: 'cfg, 'cfg>( | StorageDeclaration(_) | GenericTypeForFunctionScope { .. } => Ok(leaves.to_vec()), VariableDeclaration(_) | ConstantDeclaration(_) => { - let entry_node = graph.add_node(engines, node.into()); + let entry_node = graph.add_node(node.into()); for leaf in leaves { graph.add_edge(*leaf, entry_node, "".into()); } @@ -197,7 +197,7 @@ fn connect_declaration<'eng: 'cfg, 'cfg>( } FunctionDeclaration(decl_id) => { let fn_decl = decl_engine.get_function(decl_id.clone(), &decl.span())?; - let entry_node = graph.add_node(engines, node.into()); + let entry_node = graph.add_node(node.into()); for leaf in leaves { graph.add_edge(*leaf, entry_node, "".into()); } @@ -210,7 +210,7 @@ fn connect_declaration<'eng: 'cfg, 'cfg>( methods, .. } = decl_engine.get_impl_trait(decl_id.clone(), &span)?; - let entry_node = graph.add_node(engines, node.into()); + let entry_node = graph.add_node(node.into()); for leaf in leaves { graph.add_edge(*leaf, entry_node, "".into()); } @@ -239,15 +239,12 @@ fn connect_impl_trait<'eng: 'cfg, 'cfg>( // insert method declarations into the graph for method_decl_id in methods { let fn_decl = decl_engine.get_function(method_decl_id.clone(), &trait_name.span())?; - let fn_decl_entry_node = graph.add_node( + let fn_decl_entry_node = graph.add_node(ControlFlowGraphNode::MethodDeclaration { + span: fn_decl.span.clone(), + method_name: fn_decl.name.clone(), + method_decl_id: method_decl_id.clone(), engines, - ControlFlowGraphNode::MethodDeclaration { - span: fn_decl.span.clone(), - method_name: fn_decl.name.clone(), - method_decl_id: method_decl_id.clone(), - engines, - }, - ); + }); graph.add_edge(entry_node, fn_decl_entry_node, "".into()); // connect the impl declaration node to the functions themselves, as all trait functions are // public if the trait is in scope @@ -288,10 +285,7 @@ fn connect_typed_fn_decl<'eng: 'cfg, 'cfg>( _span: Span, ) -> Result<(), CompileError> { let type_engine = engines.te(); - let fn_exit_node = graph.add_node( - engines, - format!("\"{}\" fn exit", fn_decl.name.as_str()).into(), - ); + let fn_exit_node = graph.add_node(format!("\"{}\" fn exit", fn_decl.name.as_str()).into()); let return_nodes = depth_first_insertion_code_block(engines, &fn_decl.body, graph, &[entry_node])?; for node in return_nodes { diff --git a/sway-core/src/control_flow_analysis/dead_code_analysis.rs b/sway-core/src/control_flow_analysis/dead_code_analysis.rs index 5dec37df273..ac6deb547be 100644 --- a/sway-core/src/control_flow_analysis/dead_code_analysis.rs +++ b/sway-core/src/control_flow_analysis/dead_code_analysis.rs @@ -134,7 +134,7 @@ impl<'cfg> ControlFlowGraph<'cfg> { // do a depth first traversal and cover individual inner ast nodes let decl_engine = engines.de(); let mut leaves = vec![]; - let exit_node = Some(graph.add_node(engines, ("Program exit".to_string()).into())); + let exit_node = Some(graph.add_node(("Program exit".to_string()).into())); let mut entry_points = vec![]; let mut non_entry_points = vec![]; for ast_entrypoint in module_nodes { @@ -206,7 +206,7 @@ fn connect_node<'eng: 'cfg, 'cfg>( let span = node.span.clone(); Ok(match &node.content { ty::TyAstNodeContent::ImplicitReturnExpression(expr) => { - let this_index = graph.add_node(engines, node.into()); + let this_index = graph.add_node(node.into()); for leaf_ix in leaves { graph.add_edge(*leaf_ix, this_index, "".into()); } @@ -238,7 +238,7 @@ fn connect_node<'eng: 'cfg, 'cfg>( span, .. }) => { - let entry = graph.add_node(engines, node.into()); + let entry = graph.add_node(node.into()); // insert organizational dominator node // connected to all current leaves for leaf in leaves { @@ -265,9 +265,9 @@ fn connect_node<'eng: 'cfg, 'cfg>( // all leaves connect to this node, then this node is the singular leaf let cfg_node: ControlFlowGraphNode = node.into(); // check if node for this decl already exists - let decl_node = match graph.get_node_from_decl(engines, &cfg_node) { + let decl_node = match graph.get_node_from_decl(&cfg_node) { Some(node) => node, - None => graph.add_node(engines, cfg_node), + None => graph.add_node(cfg_node), }; for leaf in leaves { graph.add_edge(*leaf, decl_node, "".into()); @@ -346,12 +346,12 @@ fn connect_declaration<'eng: 'cfg, 'cfg>( } StructDeclaration(decl_id) => { let struct_decl = decl_engine.get_struct(decl_id.clone(), &span)?; - connect_struct_declaration(engines, &struct_decl, graph, entry_node, tree_type); + connect_struct_declaration(&struct_decl, graph, entry_node, tree_type); Ok(leaves.to_vec()) } EnumDeclaration(decl_id) => { let enum_decl = decl_engine.get_enum(decl_id.clone(), &span)?; - connect_enum_declaration(engines, &enum_decl, graph, entry_node); + connect_enum_declaration(&enum_decl, graph, entry_node); Ok(leaves.to_vec()) } ImplTrait(decl_id) => { @@ -374,7 +374,7 @@ fn connect_declaration<'eng: 'cfg, 'cfg>( } StorageDeclaration(decl_id) => { let storage = decl_engine.get_storage(decl_id.clone(), &span)?; - connect_storage_declaration(engines, &storage, graph, entry_node, tree_type); + connect_storage_declaration(&storage, graph, entry_node, tree_type); Ok(leaves.to_vec()) } ErrorRecovery(_) | GenericTypeForFunctionScope { .. } => Ok(leaves.to_vec()), @@ -384,7 +384,6 @@ fn connect_declaration<'eng: 'cfg, 'cfg>( /// Connect each individual struct field, and when that field is accessed in a subfield expression, /// connect that field. fn connect_struct_declaration<'eng: 'cfg, 'cfg>( - engines: Engines<'eng>, struct_decl: &ty::TyStructDeclaration, graph: &mut ControlFlowGraph<'cfg>, entry_node: NodeIndex, @@ -398,7 +397,7 @@ fn connect_struct_declaration<'eng: 'cfg, 'cfg>( } = struct_decl; let field_nodes = fields .iter() - .map(|field| (field.name.clone(), graph.add_node(engines, field.into()))) + .map(|field| (field.name.clone(), graph.add_node(field.into()))) .collect::>(); // If this is a library or smart contract, and if this is public, then we want to connect the // declaration node itself to the individual fields. @@ -440,7 +439,7 @@ fn connect_impl_trait<'eng: 'cfg, 'cfg>( let trait_decl_node = graph.namespace.find_trait(trait_name).cloned(); match trait_decl_node { None => { - let node_ix = graph.add_node(engines, "External trait".into()); + let node_ix = graph.add_node("External trait".into()); graph.add_edge(entry_node, node_ix, "".into()); } Some(trait_decl_node) => { @@ -452,15 +451,12 @@ fn connect_impl_trait<'eng: 'cfg, 'cfg>( // insert method declarations into the graph for method_decl_id in methods { let fn_decl = decl_engine.get_function(method_decl_id.clone(), &trait_name.span())?; - let fn_decl_entry_node = graph.add_node( + let fn_decl_entry_node = graph.add_node(ControlFlowGraphNode::MethodDeclaration { + span: fn_decl.span.clone(), + method_name: fn_decl.name.clone(), + method_decl_id: method_decl_id.clone(), engines, - ControlFlowGraphNode::MethodDeclaration { - span: fn_decl.span.clone(), - method_name: fn_decl.name.clone(), - method_decl_id: method_decl_id.clone(), - engines, - }, - ); + }); if matches!(tree_type, TreeType::Library { .. } | TreeType::Contract) { graph.add_edge(entry_node, fn_decl_entry_node, "".into()); } @@ -615,7 +611,6 @@ fn get_struct_type_info_from_type_id( /// variant. When a variant is constructed, we can point an edge at that variant. This way, /// we can see clearly, and thusly warn, when individual variants are not ever constructed. fn connect_enum_declaration<'eng: 'cfg, 'cfg>( - engines: Engines<'eng>, enum_decl: &ty::TyEnumDeclaration, graph: &mut ControlFlowGraph<'cfg>, entry_node: NodeIndex, @@ -626,13 +621,10 @@ fn connect_enum_declaration<'eng: 'cfg, 'cfg>( // keep a mapping of each variant for variant in enum_decl.variants.iter() { - let variant_index = graph.add_node( - engines, - ControlFlowGraphNode::from_enum_variant( - variant.name.clone(), - enum_decl.visibility != Visibility::Private, - ), - ); + let variant_index = graph.add_node(ControlFlowGraphNode::from_enum_variant( + variant.name.clone(), + enum_decl.visibility != Visibility::Private, + )); graph.namespace.insert_enum_variant( enum_decl.call_path.suffix.clone(), @@ -658,10 +650,7 @@ fn connect_typed_fn_decl<'eng: 'cfg, 'cfg>( options: NodeConnectionOptions, ) -> Result<(), CompileError> { let type_engine = engines.te(); - let fn_exit_node = graph.add_node( - engines, - format!("\"{}\" fn exit", fn_decl.name.as_str()).into(), - ); + let fn_exit_node = graph.add_node(format!("\"{}\" fn exit", fn_decl.name.as_str()).into()); let (_exit_nodes, _exit_node) = depth_first_insertion_code_block( engines, &fn_decl.body, @@ -710,20 +699,16 @@ fn connect_fn_params_struct_enums<'eng: 'cfg, 'cfg>( TypeInfo::Enum { call_path, .. } => { let ty_index = match graph.namespace.find_enum(&call_path.suffix) { Some(ix) => *ix, - None => graph.add_node( - engines, - format!("External enum {}", call_path.suffix.as_str()).into(), - ), + None => graph + .add_node(format!("External enum {}", call_path.suffix.as_str()).into()), }; graph.add_edge(fn_decl_entry_node, ty_index, "".into()); } TypeInfo::Struct { call_path, .. } => { let ty_index = match graph.namespace.find_struct_decl(call_path.suffix.as_str()) { Some(ix) => *ix, - None => graph.add_node( - engines, - format!("External struct {}", call_path.suffix.as_str()).into(), - ), + None => graph + .add_node(format!("External struct {}", call_path.suffix.as_str()).into()), }; graph.add_edge(fn_decl_entry_node, ty_index, "".into()); } @@ -870,17 +855,12 @@ fn connect_expression<'eng: 'cfg, 'cfg>( }| (entry_point, exit_point), ) .unwrap_or_else(|| { - let node_idx = graph.add_node( - engines, - format!("extern fn {}()", name.suffix.as_str()).into(), - ); + let node_idx = + graph.add_node(format!("extern fn {}()", name.suffix.as_str()).into()); is_external = true; ( node_idx, - graph.add_node( - engines, - format!("extern fn {} exit", name.suffix.as_str()).into(), - ), + graph.add_node(format!("extern fn {} exit", name.suffix.as_str()).into()), ) }); @@ -965,7 +945,7 @@ fn connect_expression<'eng: 'cfg, 'cfg>( Ok([lhs_expr, rhs_expr].concat()) } Literal(_) => { - let node = graph.add_node(engines, "Literal value".into()); + let node = graph.add_node("Literal value".into()); for leaf in leaves { graph.add_edge(*leaf, node, "".into()); } @@ -1060,13 +1040,10 @@ fn connect_expression<'eng: 'cfg, 'cfg>( } => { let decl = match graph.namespace.find_struct_decl(struct_name.as_str()) { Some(ix) => *ix, - None => graph.add_node( - engines, - format!("External struct {}", struct_name.as_str()).into(), - ), + None => graph.add_node(format!("External struct {}", struct_name.as_str()).into()), }; - let entry = graph.add_node(engines, "Struct declaration entry".into()); - let exit = graph.add_node(engines, "Struct declaration exit".into()); + let entry = graph.add_node("Struct declaration entry".into()); + let exit = graph.add_node("Struct declaration exit".into()); // connect current leaves to the beginning of this expr for leaf in leaves { graph.add_edge(*leaf, entry, label.into()); @@ -1140,11 +1117,10 @@ fn connect_expression<'eng: 'cfg, 'cfg>( .find_struct_field_idx(resolved_type_of_parent.suffix.as_str(), field_name.as_str()) { Some(ix) => *ix, - None => graph.add_node(engines, "external struct".into()), + None => graph.add_node("external struct".into()), }; let this_ix = graph.add_node( - engines, format!("Struct field access: {resolved_type_of_parent}.{field_name}").into(), ); for leaf in leaves { @@ -1154,8 +1130,8 @@ fn connect_expression<'eng: 'cfg, 'cfg>( Ok(vec![this_ix]) } AsmExpression { registers, .. } => { - let asm_node_entry = graph.add_node(engines, "Inline asm entry".into()); - let asm_node_exit = graph.add_node(engines, "Inline asm exit".into()); + let asm_node_entry = graph.add_node("Inline asm entry".into()); + let asm_node_exit = graph.add_node("Inline asm exit".into()); for leaf in leaves { graph.add_edge(*leaf, asm_node_entry, "".into()); } @@ -1186,8 +1162,8 @@ fn connect_expression<'eng: 'cfg, 'cfg>( Ok(vec![asm_node_exit]) } Tuple { fields } => { - let entry = graph.add_node(engines, "tuple entry".into()); - let exit = graph.add_node(engines, "tuple exit".into()); + let entry = graph.add_node("tuple entry".into()); + let exit = graph.add_node("tuple exit".into()); // connect current leaves to the beginning of this expr for leaf in leaves { graph.add_edge(*leaf, entry, label.into()); @@ -1290,10 +1266,8 @@ fn connect_expression<'eng: 'cfg, 'cfg>( .storage .get(&fields.storage_field_name()) .cloned(); - let this_ix = graph.add_node( - engines, - format!("storage field access: {}", fields.storage_field_name()).into(), - ); + let this_ix = graph + .add_node(format!("storage field access: {}", fields.storage_field_name()).into()); for leaf in leaves { storage_node.map(|x| graph.add_edge(*leaf, x, "".into())); graph.add_edge(*leaf, this_ix, "".into()); @@ -1350,7 +1324,7 @@ fn connect_expression<'eng: 'cfg, 'cfg>( let entry = leaves[0]; - let while_loop_exit = graph.add_node(engines, "while loop exit".to_string().into()); + let while_loop_exit = graph.add_node("while loop exit".to_string().into()); // it is possible for a whole while loop to be skipped so add edge from // beginning of while loop straight to exit @@ -1389,14 +1363,14 @@ fn connect_expression<'eng: 'cfg, 'cfg>( Ok(vec![while_loop_exit]) } Break => { - let break_node = graph.add_node(engines, "break".to_string().into()); + let break_node = graph.add_node("break".to_string().into()); for leaf in leaves { graph.add_edge(*leaf, break_node, "".into()); } Ok(vec![]) } Continue => { - let continue_node = graph.add_node(engines, "continue".to_string().into()); + let continue_node = graph.add_node("continue".to_string().into()); for leaf in leaves { graph.add_edge(*leaf, continue_node, "".into()); } @@ -1425,7 +1399,7 @@ fn connect_expression<'eng: 'cfg, 'cfg>( options, ), Return(exp) => { - let this_index = graph.add_node(engines, "return entry".into()); + let this_index = graph.add_node("return entry".into()); for leaf in leaves { graph.add_edge(*leaf, this_index, "".into()); } @@ -1463,7 +1437,7 @@ fn connect_intrinsic_function<'eng: 'cfg, 'cfg>( exit_node: Option, tree_type: &TreeType, ) -> Result, CompileError> { - let node = graph.add_node(engines, format!("Intrinsic {kind}").into()); + let node = graph.add_node(format!("Intrinsic {kind}").into()); for leaf in leaves { graph.add_edge(*leaf, node, "".into()); } @@ -1498,7 +1472,7 @@ fn connect_code_block<'eng: 'cfg, 'cfg>( options: NodeConnectionOptions, ) -> Result, CompileError> { let contents = &block.contents; - let block_entry = graph.add_node(engines, "Code block entry".into()); + let block_entry = graph.add_node("Code block entry".into()); for leaf in leaves { graph.add_edge(*leaf, block_entry, "".into()); } @@ -1516,7 +1490,7 @@ fn connect_code_block<'eng: 'cfg, 'cfg>( .0; } - let block_exit = graph.add_node(engines, "Code block exit".into()); + let block_exit = graph.add_node("Code block exit".into()); for leaf in current_leaf { graph.add_edge(leaf, block_exit, "".into()); } @@ -1541,7 +1515,6 @@ fn connect_enum_instantiation<'eng: 'cfg, 'cfg>( .find_enum_variant_index(&enum_call_path.suffix, variant_name) .unwrap_or_else(|| { let node_idx = graph.add_node( - engines, format!( "extern enum {}::{}", enum_call_path.suffix.as_str(), @@ -1553,8 +1526,8 @@ fn connect_enum_instantiation<'eng: 'cfg, 'cfg>( }); // insert organizational nodes for instantiation of enum - let enum_instantiation_entry_idx = graph.add_node(engines, "enum instantiation entry".into()); - let enum_instantiation_exit_idx = graph.add_node(engines, "enum instantiation exit".into()); + let enum_instantiation_entry_idx = graph.add_node("enum instantiation entry".into()); + let enum_instantiation_exit_idx = graph.add_node("enum instantiation exit".into()); // connect to declaration node itself to show that the declaration is used graph.add_edge(enum_instantiation_entry_idx, decl_ix, "".into()); @@ -1600,71 +1573,41 @@ fn construct_dead_code_warning_from_node( ty::TyAstNode { content: ty::TyAstNodeContent::Declaration(ty::TyDeclaration::FunctionDeclaration(decl_id)), - span, - } => { - let warning_span = match decl_engine.get_function(decl_id.clone(), span) { - Ok(ty::TyFunctionDeclaration { name, .. }) => name.span(), - Err(_) => span.clone(), - }; - CompileWarning { - span: warning_span, - warning_content: Warning::DeadFunctionDeclaration, - } - } + .. + } => CompileWarning { + span: decl_id.name.span(), + warning_content: Warning::DeadFunctionDeclaration, + }, ty::TyAstNode { content: ty::TyAstNodeContent::Declaration(ty::TyDeclaration::StructDeclaration(decl_id)), - span, - } => { - let warning_span = match decl_engine.get_struct(decl_id.clone(), span) { - Ok(ty::TyStructDeclaration { call_path, .. }) => call_path.span(), - Err(_) => span.clone(), - }; - CompileWarning { - span: warning_span, - warning_content: Warning::DeadStructDeclaration, - } - } + .. + } => CompileWarning { + span: decl_id.name.span(), + warning_content: Warning::DeadStructDeclaration, + }, ty::TyAstNode { content: ty::TyAstNodeContent::Declaration(ty::TyDeclaration::EnumDeclaration(decl_id)), - span, - } => { - let warning_span = match decl_engine.get_enum(decl_id.clone(), span) { - Ok(ty::TyEnumDeclaration { call_path, .. }) => call_path.span(), - Err(_) => span.clone(), - }; - CompileWarning { - span: warning_span, - warning_content: Warning::DeadEnumDeclaration, - } - } + .. + } => CompileWarning { + span: decl_id.name.span(), + warning_content: Warning::DeadEnumDeclaration, + }, ty::TyAstNode { content: ty::TyAstNodeContent::Declaration(ty::TyDeclaration::TraitDeclaration(decl_id)), - span, - } => { - let warning_span = match decl_engine.get_trait(decl_id.clone(), span) { - Ok(ty::TyTraitDeclaration { name, .. }) => name.span(), - Err(_) => span.clone(), - }; - CompileWarning { - span: warning_span, - warning_content: Warning::DeadTrait, - } - } + .. + } => CompileWarning { + span: decl_id.name.span(), + warning_content: Warning::DeadTrait, + }, ty::TyAstNode { content: ty::TyAstNodeContent::Declaration(ty::TyDeclaration::ConstantDeclaration(decl_id)), - span, - } => { - let warning_span = match decl_engine.get_constant(decl_id.clone(), span) { - Ok(ty::TyConstantDeclaration { name, .. }) => name.span(), - Err(_) => span.clone(), - }; - CompileWarning { - span: warning_span, - warning_content: Warning::DeadDeclaration, - } - } + .. + } => CompileWarning { + span: decl_id.name.span(), + warning_content: Warning::DeadDeclaration, + }, ty::TyAstNode { content: ty::TyAstNodeContent::Declaration(ty::TyDeclaration::VariableDeclaration(decl)), span, @@ -1731,7 +1674,6 @@ fn construct_dead_code_warning_from_node( } fn connect_storage_declaration<'eng: 'cfg, 'cfg>( - engines: Engines<'eng>, decl: &ty::TyStorageDeclaration, graph: &mut ControlFlowGraph<'cfg>, _entry_node: NodeIndex, @@ -1740,7 +1682,7 @@ fn connect_storage_declaration<'eng: 'cfg, 'cfg>( let ty::TyStorageDeclaration { fields, .. } = decl; let field_nodes = fields .iter() - .map(|field| (field.clone(), graph.add_node(engines, field.into()))) + .map(|field| (field.clone(), graph.add_node(field.into()))) .collect::>(); graph.namespace.insert_storage(field_nodes); diff --git a/sway-core/src/control_flow_analysis/flow_graph/mod.rs b/sway-core/src/control_flow_analysis/flow_graph/mod.rs index f2e7f59fcb3..32f6d96c1a1 100644 --- a/sway-core/src/control_flow_analysis/flow_graph/mod.rs +++ b/sway-core/src/control_flow_analysis/flow_graph/mod.rs @@ -75,10 +75,10 @@ pub enum ControlFlowGraphNode<'cfg> { } impl<'cfg> GetDeclIdent for ControlFlowGraphNode<'cfg> { - fn get_decl_ident(&self, decl_engine: &DeclEngine) -> Option { + fn get_decl_ident(&self) -> Option { match self { ControlFlowGraphNode::OrganizationalDominator(_) => None, - ControlFlowGraphNode::ProgramNode(node) => node.get_decl_ident(decl_engine), + ControlFlowGraphNode::ProgramNode(node) => node.get_decl_ident(), ControlFlowGraphNode::EnumVariant { variant_name, .. } => Some(variant_name.clone()), ControlFlowGraphNode::MethodDeclaration { method_name, .. } => { Some(method_name.clone()) @@ -170,13 +170,8 @@ impl<'cfg> ControlFlowGraph<'cfg> { pub(crate) fn add_edge_from_entry(&mut self, to: NodeIndex, label: ControlFlowGraphEdge) { self.pending_entry_points_edges.push((to, label)); } - pub(crate) fn add_node<'eng: 'cfg>( - &mut self, - engines: Engines<'eng>, - node: ControlFlowGraphNode<'cfg>, - ) -> NodeIndex { - let decl_engine = engines.de(); - let ident_opt = node.get_decl_ident(decl_engine); + pub(crate) fn add_node<'eng: 'cfg>(&mut self, node: ControlFlowGraphNode<'cfg>) -> NodeIndex { + let ident_opt = node.get_decl_ident(); let node_index = self.graph.add_node(node); if let Some(ident) = ident_opt { self.decls.insert(ident.into(), node_index); @@ -201,13 +196,8 @@ impl<'cfg> ControlFlowGraph<'cfg> { self.pending_entry_points_edges.clear(); } - pub(crate) fn get_node_from_decl( - &self, - engines: Engines<'_>, - cfg_node: &ControlFlowGraphNode, - ) -> Option { - let decl_engine = engines.de(); - if let Some(ident) = cfg_node.get_decl_ident(decl_engine) { + pub(crate) fn get_node_from_decl(&self, cfg_node: &ControlFlowGraphNode) -> Option { + if let Some(ident) = cfg_node.get_decl_ident() { self.decls.get(&ident.into()).cloned() } else { None diff --git a/sway-core/src/decl_engine/engine.rs b/sway-core/src/decl_engine/engine.rs index 34c3f8fbb9b..4b0417be1c0 100644 --- a/sway-core/src/decl_engine/engine.rs +++ b/sway-core/src/decl_engine/engine.rs @@ -5,7 +5,7 @@ use std::{ }; use sway_error::error::CompileError; -use sway_types::Span; +use sway_types::{Ident, Span}; use crate::{ concurrent_slab::{ConcurrentSlab, ListDisplay}, @@ -41,14 +41,19 @@ impl DeclEngine { pub(crate) fn insert(&self, decl: T) -> DeclId where - T: Into<(DeclWrapper, Span)>, + T: Into<(Ident, DeclWrapper, Span)>, { - let (decl_wrapper, span) = decl.into(); - DeclId::new(self.slab.insert(decl_wrapper), span) + let (ident, decl_wrapper, span) = decl.into(); + DeclId::new(ident, self.slab.insert(decl_wrapper), span) } - pub(crate) fn insert_wrapper(&self, decl_wrapper: DeclWrapper, span: Span) -> DeclId { - DeclId::new(self.slab.insert(decl_wrapper), span) + pub(crate) fn insert_wrapper( + &self, + ident: Ident, + decl_wrapper: DeclWrapper, + span: Span, + ) -> DeclId { + DeclId::new(ident, self.slab.insert(decl_wrapper), span) } /// Given a [DeclId] `index`, finds all the parents of `index` and all the diff --git a/sway-core/src/decl_engine/id.rs b/sway-core/src/decl_engine/id.rs index 17c5b5d92bf..23b7cf5bf93 100644 --- a/sway-core/src/decl_engine/id.rs +++ b/sway-core/src/decl_engine/id.rs @@ -1,21 +1,30 @@ -use sway_types::{Span, Spanned}; +use sway_types::{Ident, Span, Spanned}; -use crate::{ - engine_threading::*, - language::ty, - type_system::{SubstTypes, TypeSubstMap}, - ReplaceSelfType, TypeId, -}; +use crate::{engine_threading::*, language::ty, type_system::*}; use super::{DeclEngine, DeclMapping, ReplaceDecls, ReplaceFunctionImplementingType}; -/// An ID used to refer to an item in the [DeclarationEngine](super::decl_engine::DeclarationEngine) +/// An ID used to refer to an item in the [DeclEngine](super::decl_engine::DeclEngine) #[derive(Debug)] -pub struct DeclId(usize, Span); +pub struct DeclId { + /// The name of the declaration. + // NOTE: In the case of storage, the name is "storage". + pub name: Ident, + + /// The low-level index into the [DeclEngine]. + id: usize, + + /// The [Span] of the entire declaration. + decl_span: Span, +} impl Clone for DeclId { - fn clone(&self) -> Self { - Self(self.0, self.1.clone()) + fn clone(&self) -> DeclId { + DeclId { + name: self.name.clone(), + id: self.id, + decl_span: self.decl_span.clone(), + } } } @@ -35,20 +44,20 @@ impl PartialEqWithEngines for DeclId { impl std::ops::Deref for DeclId { type Target = usize; fn deref(&self) -> &Self::Target { - &self.0 + &self.id } } #[allow(clippy::from_over_into)] impl Into for DeclId { fn into(self) -> usize { - self.0 + self.id } } impl Spanned for DeclId { fn span(&self) -> Span { - self.1.clone() + self.decl_span.clone() } } @@ -74,13 +83,13 @@ impl ReplaceDecls for DeclId { fn replace_decls_inner(&mut self, decl_mapping: &DeclMapping, engines: Engines<'_>) { let decl_engine = engines.de(); if let Some(new_decl_id) = decl_mapping.find_match(self) { - self.0 = *new_decl_id; + self.id = *new_decl_id; return; } let all_parents = decl_engine.find_all_parents(engines, self.clone()); for parent in all_parents.into_iter() { if let Some(new_decl_id) = decl_mapping.find_match(&parent) { - self.0 = *new_decl_id; + self.id = *new_decl_id; return; } } @@ -101,8 +110,12 @@ impl ReplaceFunctionImplementingType for DeclId { } impl DeclId { - pub(crate) fn new(index: usize, span: Span) -> DeclId { - DeclId(index, span) + pub(crate) fn new(name: Ident, id: usize, decl_span: Span) -> DeclId { + DeclId { + name, + id, + decl_span, + } } pub(crate) fn with_parent(self, decl_engine: &DeclEngine, parent: DeclId) -> DeclId { @@ -111,7 +124,7 @@ impl DeclId { } pub(crate) fn replace_id(&mut self, index: usize) { - self.0 = index; + self.id = index; } pub(crate) fn subst_types_and_insert_new( @@ -123,7 +136,7 @@ impl DeclId { let mut decl = decl_engine.get(self.clone()); decl.subst(type_mapping, engines); decl_engine - .insert_wrapper(decl, self.1.clone()) + .insert_wrapper(self.name.clone(), decl, self.decl_span.clone()) .with_parent(decl_engine, self.clone()) } @@ -136,7 +149,7 @@ impl DeclId { let mut decl = decl_engine.get(self.clone()); decl.replace_self_type(engines, self_type); decl_engine - .insert_wrapper(decl, self.1.clone()) + .insert_wrapper(self.name.clone(), decl, self.decl_span.clone()) .with_parent(decl_engine, self.clone()) } @@ -149,7 +162,7 @@ impl DeclId { let mut decl = decl_engine.get(self.clone()); decl.replace_decls(decl_mapping, engines); decl_engine - .insert_wrapper(decl, self.1.clone()) + .insert_wrapper(self.name.clone(), decl, self.decl_span.clone()) .with_parent(decl_engine, self.clone()) } } diff --git a/sway-core/src/decl_engine/mapping.rs b/sway-core/src/decl_engine/mapping.rs index 160a7ef3098..e82ab4adc54 100644 --- a/sway-core/src/decl_engine/mapping.rs +++ b/sway-core/src/decl_engine/mapping.rs @@ -1,8 +1,6 @@ -use std::{collections::BTreeMap, fmt}; +use std::fmt; -use sway_types::Ident; - -use super::DeclId; +use super::{DeclId, MethodMap}; type SourceDecl = DeclId; type DestinationDecl = DeclId; @@ -47,8 +45,8 @@ impl DeclMapping { } pub(crate) fn from_stub_and_impld_decl_ids( - stub_decl_ids: BTreeMap, - impld_decl_ids: BTreeMap, + stub_decl_ids: MethodMap, + impld_decl_ids: MethodMap, ) -> DeclMapping { let mut mapping = vec![]; for (stub_decl_name, stub_decl_id) in stub_decl_ids.into_iter() { diff --git a/sway-core/src/decl_engine/mod.rs b/sway-core/src/decl_engine/mod.rs index 8e05651d7f8..aa670ec91e2 100644 --- a/sway-core/src/decl_engine/mod.rs +++ b/sway-core/src/decl_engine/mod.rs @@ -15,8 +15,13 @@ pub(crate) mod mapping; pub(crate) mod replace_decl_id; pub(crate) mod wrapper; +use std::collections::BTreeMap; + pub use engine::*; pub use id::*; pub(crate) use mapping::*; pub(crate) use replace_decl_id::*; +use sway_types::Ident; pub(crate) use wrapper::*; + +pub(crate) type MethodMap = BTreeMap; diff --git a/sway-core/src/decl_engine/wrapper.rs b/sway-core/src/decl_engine/wrapper.rs index 388bfa327da..c731cfd02f8 100644 --- a/sway-core/src/decl_engine/wrapper.rs +++ b/sway-core/src/decl_engine/wrapper.rs @@ -1,7 +1,7 @@ use std::fmt; use sway_error::error::CompileError; -use sway_types::{Span, Spanned}; +use sway_types::{Ident, Span, Spanned}; use crate::{ engine_threading::*, @@ -125,66 +125,86 @@ impl ReplaceFunctionImplementingType for DeclWrapper { } } -impl From for (DeclWrapper, Span) { +impl From for (Ident, DeclWrapper, Span) { fn from(value: ty::TyFunctionDeclaration) -> Self { let span = value.span(); - (DeclWrapper::Function(value), span) + (value.name.clone(), DeclWrapper::Function(value), span) } } -impl From for (DeclWrapper, Span) { +impl From for (Ident, DeclWrapper, Span) { fn from(value: ty::TyTraitDeclaration) -> Self { let span = value.name.span(); - (DeclWrapper::Trait(value), span) + (value.name.clone(), DeclWrapper::Trait(value), span) } } -impl From for (DeclWrapper, Span) { +impl From for (Ident, DeclWrapper, Span) { fn from(value: ty::TyTraitFn) -> Self { let span = value.name.span(); - (DeclWrapper::TraitFn(value), span) + (value.name.clone(), DeclWrapper::TraitFn(value), span) } } -impl From for (DeclWrapper, Span) { +impl From for (Ident, DeclWrapper, Span) { fn from(value: ty::TyImplTrait) -> Self { let span = value.span.clone(); - (DeclWrapper::ImplTrait(value), span) + ( + value.trait_name.suffix.clone(), + DeclWrapper::ImplTrait(value), + span, + ) } } -impl From for (DeclWrapper, Span) { +impl From for (Ident, DeclWrapper, Span) { fn from(value: ty::TyStructDeclaration) -> Self { let span = value.span(); - (DeclWrapper::Struct(value), span) + ( + value.call_path.suffix.clone(), + DeclWrapper::Struct(value), + span, + ) } } -impl From for (DeclWrapper, Span) { +impl From for (Ident, DeclWrapper, Span) { fn from(value: ty::TyStorageDeclaration) -> Self { let span = value.span(); - (DeclWrapper::Storage(value), span) + ( + Ident::new_with_override("storage", span.clone()), + DeclWrapper::Storage(value), + span, + ) } } -impl From for (DeclWrapper, Span) { +impl From for (Ident, DeclWrapper, Span) { fn from(value: ty::TyAbiDeclaration) -> Self { let span = value.span.clone(); - (DeclWrapper::Abi(value), span) + (value.name.clone(), DeclWrapper::Abi(value), span) } } -impl From for (DeclWrapper, Span) { +impl From for (Ident, DeclWrapper, Span) { fn from(value: ty::TyConstantDeclaration) -> Self { let span = value.name.span(); - (DeclWrapper::Constant(Box::new(value)), span) + ( + value.name.clone(), + DeclWrapper::Constant(Box::new(value)), + span, + ) } } -impl From for (DeclWrapper, Span) { +impl From for (Ident, DeclWrapper, Span) { fn from(value: ty::TyEnumDeclaration) -> Self { let span = value.span(); - (DeclWrapper::Enum(value), span) + ( + value.call_path.suffix.clone(), + DeclWrapper::Enum(value), + span, + ) } } diff --git a/sway-core/src/language/ty/ast_node.rs b/sway-core/src/language/ty/ast_node.rs index 298f209ec13..012b0cc45c7 100644 --- a/sway-core/src/language/ty/ast_node.rs +++ b/sway-core/src/language/ty/ast_node.rs @@ -14,7 +14,7 @@ use crate::{ }; pub trait GetDeclIdent { - fn get_decl_ident(&self, decl_engine: &DeclEngine) -> Option; + fn get_decl_ident(&self) -> Option; } pub trait GetDeclId { @@ -112,8 +112,8 @@ impl DeterministicallyAborts for TyAstNode { } impl GetDeclIdent for TyAstNode { - fn get_decl_ident(&self, decl_engine: &DeclEngine) -> Option { - self.content.get_decl_ident(decl_engine) + fn get_decl_ident(&self) -> Option { + self.content.get_decl_ident() } } @@ -322,9 +322,9 @@ impl CollectTypesMetadata for TyAstNodeContent { } impl GetDeclIdent for TyAstNodeContent { - fn get_decl_ident(&self, decl_engine: &DeclEngine) -> Option { + fn get_decl_ident(&self) -> Option { match self { - TyAstNodeContent::Declaration(decl) => decl.get_decl_ident(decl_engine), + TyAstNodeContent::Declaration(decl) => decl.get_decl_ident(), TyAstNodeContent::Expression(_expr) => None, //expr.get_decl_ident(), TyAstNodeContent::ImplicitReturnExpression(_expr) => None, //expr.get_decl_ident(), TyAstNodeContent::SideEffect(_) => None, diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 3e48af45a14..d65a301af92 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -119,7 +119,6 @@ impl Spanned for TyDeclaration { impl DisplayWithEngines for TyDeclaration { fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: Engines<'_>) -> std::fmt::Result { let type_engine = engines.te(); - let decl_engine = engines.de(); write!( f, "{} declaration ({})", @@ -150,32 +149,10 @@ impl DisplayWithEngines for TyDeclaration { builder.push_str(&engines.help_out(body).to_string()); builder } - TyDeclaration::FunctionDeclaration(decl_id) => { - match decl_engine.get_function(decl_id.clone(), &decl_id.span()) { - Ok(TyFunctionDeclaration { name, .. }) => name.as_str().into(), - Err(_) => "unknown function".into(), - } - } - TyDeclaration::TraitDeclaration(decl_id) => { - match decl_engine.get_trait(decl_id.clone(), &decl_id.span()) { - Ok(TyTraitDeclaration { name, .. }) => name.as_str().into(), - Err(_) => "unknown trait".into(), - } - } - TyDeclaration::StructDeclaration(decl_id) => { - match decl_engine.get_struct(decl_id.clone(), &decl_id.span()) { - Ok(TyStructDeclaration { call_path, .. }) => { - call_path.suffix.as_str().into() - } - Err(_) => "unknown struct".into(), - } - } - TyDeclaration::EnumDeclaration(decl_id) => { - match decl_engine.get_enum(decl_id.clone(), &decl_id.span()) { - Ok(TyEnumDeclaration { call_path, .. }) => call_path.suffix.as_str().into(), - Err(_) => "unknown enum".into(), - } - } + TyDeclaration::FunctionDeclaration(decl_id) => decl_id.name.as_str().into(), + TyDeclaration::TraitDeclaration(decl_id) => decl_id.name.as_str().into(), + TyDeclaration::StructDeclaration(decl_id) => decl_id.name.as_str().into(), + TyDeclaration::EnumDeclaration(decl_id) => decl_id.name.as_str().into(), _ => String::new(), } ) @@ -258,54 +235,16 @@ impl CollectTypesMetadata for TyDeclaration { } impl GetDeclIdent for TyDeclaration { - fn get_decl_ident(&self, decl_engine: &DeclEngine) -> Option { + fn get_decl_ident(&self) -> Option { match self { TyDeclaration::VariableDeclaration(decl) => Some(decl.name.clone()), - TyDeclaration::ConstantDeclaration(decl) => Some( - decl_engine - .get_constant(decl.clone(), &decl.span()) - .unwrap() - .name, - ), - TyDeclaration::FunctionDeclaration(decl) => Some( - decl_engine - .get_function(decl.clone(), &decl.span()) - .unwrap() - .name, - ), - TyDeclaration::TraitDeclaration(decl) => Some( - decl_engine - .get_trait(decl.clone(), &decl.span()) - .unwrap() - .name, - ), - TyDeclaration::StructDeclaration(decl) => Some( - decl_engine - .get_struct(decl.clone(), &decl.span()) - .unwrap() - .call_path - .suffix, - ), - TyDeclaration::EnumDeclaration(decl) => Some( - decl_engine - .get_enum(decl.clone(), &decl.span()) - .unwrap() - .call_path - .suffix, - ), - TyDeclaration::ImplTrait(decl) => Some( - decl_engine - .get_impl_trait(decl.clone(), &decl.span()) - .unwrap() - .trait_name - .suffix, - ), - TyDeclaration::AbiDeclaration(decl) => Some( - decl_engine - .get_abi(decl.clone(), &decl.span()) - .unwrap() - .name, - ), + TyDeclaration::ConstantDeclaration(decl_id) => Some(decl_id.name.clone()), + TyDeclaration::FunctionDeclaration(decl_id) => Some(decl_id.name.clone()), + TyDeclaration::TraitDeclaration(decl_id) => Some(decl_id.name.clone()), + TyDeclaration::StructDeclaration(decl_id) => Some(decl_id.name.clone()), + TyDeclaration::EnumDeclaration(decl_id) => Some(decl_id.name.clone()), + TyDeclaration::ImplTrait(decl_id) => Some(decl_id.name.clone()), + TyDeclaration::AbiDeclaration(decl_id) => Some(decl_id.name.clone()), TyDeclaration::GenericTypeForFunctionScope { name, .. } => Some(name.clone()), TyDeclaration::ErrorRecovery(_) => None, TyDeclaration::StorageDeclaration(_decl) => None, @@ -498,13 +437,13 @@ impl TyDeclaration { let implementing_for_type_id = type_engine.get(decl.implementing_for_type_id); format!( "{} for {}", - self.get_decl_ident(decl_engine) + self.get_decl_ident() .map_or(String::from(""), |f| f.as_str().to_string()), implementing_for_type_id.json_abi_str(type_engine) ) } _ => self - .get_decl_ident(decl_engine) + .get_decl_ident() .map_or(String::from(""), |f| f.as_str().to_string()), } } diff --git a/sway-core/src/language/ty/program.rs b/sway-core/src/language/ty/program.rs index 90eef223a65..6938d73ac66 100644 --- a/sway-core/src/language/ty/program.rs +++ b/sway-core/src/language/ty/program.rs @@ -156,15 +156,9 @@ impl TyProgram { .find(|decl| matches!(decl, TyDeclaration::StorageDeclaration(_))); if let Some(TyDeclaration::StorageDeclaration(decl_id)) = storage_decl { - let TyStorageDeclaration { span, .. } = check!( - CompileResult::from(decl_engine.get_storage(decl_id.clone(), &decl_id.span())), - return err(warnings, errors), - warnings, - errors - ); errors.push(CompileError::StorageDeclarationInNonContract { program_kind: format!("{kind}"), - span, + span: decl_id.span(), }); } } diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs b/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs index 4c8731d0fa5..85ae0d3b06a 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs @@ -634,11 +634,11 @@ fn type_check_trait_implementation( // This map keeps track of the stub declaration id's of the trait // definition. - let mut stub_method_ids: BTreeMap = BTreeMap::new(); + let mut stub_method_ids: MethodMap = BTreeMap::new(); // This map keeps track of the new declaration ids of the implemented // interface surface. - let mut impld_method_ids: BTreeMap = BTreeMap::new(); + let mut impld_method_ids: MethodMap = BTreeMap::new(); for decl_id in trait_interface_surface.iter() { let method = check!( @@ -744,7 +744,7 @@ fn type_check_impl_method( impl_method: &FunctionDeclaration, trait_name: &CallPath, is_contract: bool, - impld_method_ids: &BTreeMap, + impld_method_ids: &MethodMap, method_checklist: &BTreeMap, ) -> CompileResult { let mut warnings = vec![]; @@ -1071,14 +1071,14 @@ fn check_for_unconstrained_type_parameters( fn handle_supertraits( mut ctx: TypeCheckContext, supertraits: &[Supertrait], -) -> CompileResult<(BTreeMap, BTreeMap)> { +) -> CompileResult<(MethodMap, MethodMap)> { let mut warnings = Vec::new(); let mut errors = Vec::new(); let decl_engine = ctx.decl_engine; - let mut interface_surface_methods_ids: BTreeMap = BTreeMap::new(); - let mut impld_method_ids: BTreeMap = BTreeMap::new(); + let mut interface_surface_methods_ids: MethodMap = BTreeMap::new(); + let mut impld_method_ids: MethodMap = BTreeMap::new(); let self_type = ctx.self_type(); for supertrait in supertraits.iter() { @@ -1120,16 +1120,12 @@ fn handle_supertraits( // Retrieve the interface surface and implemented method ids for // this trait. - let (trait_interface_surface_methods_ids, trait_impld_method_ids) = check!( - trait_decl.retrieve_interface_surface_and_implemented_methods_for_type( + let (trait_interface_surface_methods_ids, trait_impld_method_ids) = trait_decl + .retrieve_interface_surface_and_implemented_methods_for_type( ctx.by_ref(), self_type, - &supertrait.name - ), - continue, - warnings, - errors - ); + &supertrait.name, + ); interface_surface_methods_ids.extend(trait_interface_surface_methods_ids); impld_method_ids.extend(trait_impld_method_ids); diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs b/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs index 052b27d93bf..db58c24d8fa 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs @@ -1,7 +1,7 @@ use std::collections::BTreeMap; use sway_error::warning::{CompileWarning, Warning}; -use sway_types::{style::is_upper_camel_case, Ident, Spanned}; +use sway_types::{style::is_upper_camel_case, Spanned}; use crate::{ decl_engine::*, @@ -11,8 +11,6 @@ use crate::{ type_system::*, }; -type MethodMap = BTreeMap; - impl ty::TyTraitDeclaration { pub(crate) fn type_check( ctx: TypeCheckContext, @@ -139,31 +137,19 @@ impl ty::TyTraitDeclaration { ctx: TypeCheckContext, type_id: TypeId, call_path: &CallPath, - ) -> CompileResult<(MethodMap, MethodMap)> { - let mut warnings = vec![]; - let mut errors = vec![]; - + ) -> (MethodMap, MethodMap) { let mut interface_surface_method_ids: MethodMap = BTreeMap::new(); let mut impld_method_ids: MethodMap = BTreeMap::new(); let ty::TyTraitDeclaration { - interface_surface, - name, - .. + interface_surface, .. } = self; - let decl_engine = ctx.decl_engine; let engines = ctx.engines(); // Retrieve the interface surface for this trait. for decl_id in interface_surface.iter() { - let method = check!( - CompileResult::from(decl_engine.get_trait_fn(decl_id.clone(), &call_path.span())), - return err(warnings, errors), - warnings, - errors - ); - interface_surface_method_ids.insert(method.name, decl_id.clone()); + interface_surface_method_ids.insert(decl_id.name.clone(), decl_id.clone()); } // Retrieve the implemented methods for this type. @@ -172,20 +158,10 @@ impl ty::TyTraitDeclaration { .get_methods_for_type_and_trait_name(engines, type_id, call_path) .into_iter() { - let method = check!( - CompileResult::from(decl_engine.get_function(decl_id.clone(), &name.span())), - return err(warnings, errors), - warnings, - errors - ); - impld_method_ids.insert(method.name, decl_id); + impld_method_ids.insert(decl_id.name.clone(), decl_id); } - ok( - (interface_surface_method_ids, impld_method_ids), - warnings, - errors, - ) + (interface_surface_method_ids, impld_method_ids) } /// Retrieves the interface surface, methods, and implemented methods for @@ -216,24 +192,12 @@ impl ty::TyTraitDeclaration { // Retrieve the interface surface for this trait. for decl_id in interface_surface.iter() { - let method = check!( - CompileResult::from(decl_engine.get_trait_fn(decl_id.clone(), &call_path.span())), - return err(warnings, errors), - warnings, - errors - ); - interface_surface_method_ids.insert(method.name, decl_id.clone()); + interface_surface_method_ids.insert(decl_id.name.clone(), decl_id.clone()); } // Retrieve the trait methods for this trait. for decl_id in methods.iter() { - let method = check!( - CompileResult::from(decl_engine.get_function(decl_id.clone(), &call_path.span())), - return err(warnings, errors), - warnings, - errors - ); - method_ids.insert(method.name, decl_id.clone()); + method_ids.insert(decl_id.name.clone(), decl_id.clone()); } // Retrieve the implemented methods for this type. diff --git a/sway-core/src/semantic_analysis/namespace/namespace.rs b/sway-core/src/semantic_analysis/namespace/namespace.rs index 89338d5145b..cd0c862f9bf 100644 --- a/sway-core/src/semantic_analysis/namespace/namespace.rs +++ b/sway-core/src/semantic_analysis/namespace/namespace.rs @@ -223,13 +223,7 @@ impl Namespace { let mut matching_method_decl_ids: Vec = vec![]; for decl_id in methods.into_iter() { - let method = check!( - CompileResult::from(decl_engine.get_function(decl_id.clone(), &decl_id.span())), - return err(warnings, errors), - warnings, - errors - ); - if &method.name == method_name { + if &decl_id.name == method_name { matching_method_decl_ids.push(decl_id); } } diff --git a/sway-core/src/semantic_analysis/namespace/trait_map.rs b/sway-core/src/semantic_analysis/namespace/trait_map.rs index d4035d2f404..faeec68f008 100644 --- a/sway-core/src/semantic_analysis/namespace/trait_map.rs +++ b/sway-core/src/semantic_analysis/namespace/trait_map.rs @@ -103,7 +103,7 @@ impl TraitMap { is_impl_self: bool, engines: Engines<'_>, ) -> CompileResult<()> { - let mut warnings = vec![]; + let warnings = vec![]; let mut errors = vec![]; let type_engine = engines.te(); @@ -111,13 +111,7 @@ impl TraitMap { let mut trait_methods: TraitMethods = im::HashMap::new(); for decl_id in methods.iter() { - let method = check!( - CompileResult::from(decl_engine.get_function(decl_id.clone(), impl_span)), - return err(warnings, errors), - warnings, - errors - ); - trait_methods.insert(method.name.to_string(), decl_id.clone()); + trait_methods.insert(decl_id.name.to_string(), decl_id.clone()); } // check to see if adding this trait will produce a conflicting definition @@ -193,18 +187,10 @@ impl TraitMap { } else if types_are_subset && (traits_are_subset || is_impl_self) { for (name, decl_id) in trait_methods.iter() { if map_trait_methods.get(name).is_some() { - let method = check!( - CompileResult::from( - decl_engine.get_function(decl_id.clone(), impl_span) - ), - return err(warnings, errors), - warnings, - errors - ); errors.push(CompileError::DuplicateMethodsDefinedForType { - func_name: method.name.to_string(), + func_name: decl_id.name.to_string(), type_implementing_for: engines.help_out(type_id).to_string(), - span: method.name.span(), + span: decl_id.name.span(), }); } } @@ -602,7 +588,7 @@ impl TraitMap { ( name, decl_engine - .insert_wrapper(decl, decl_id.span()) + .insert_wrapper(decl_id.name.clone(), decl, decl_id.span()) .with_parent(decl_engine, decl_id), ) }) diff --git a/sway-core/src/type_system/type_parameter.rs b/sway-core/src/type_system/type_parameter.rs index 626d026063c..1afdb68a330 100644 --- a/sway-core/src/type_system/type_parameter.rs +++ b/sway-core/src/type_system/type_parameter.rs @@ -165,8 +165,8 @@ impl TypeParameter { let mut warnings = vec![]; let mut errors = vec![]; - let mut original_method_ids: BTreeMap = BTreeMap::new(); - let mut impld_method_ids: BTreeMap = BTreeMap::new(); + let mut original_method_ids: MethodMap = BTreeMap::new(); + let mut impld_method_ids: MethodMap = BTreeMap::new(); for type_param in type_parameters.iter() { let TypeParameter { @@ -222,14 +222,14 @@ fn handle_trait( type_id: TypeId, trait_name: &CallPath, type_arguments: &[TypeArgument], -) -> CompileResult<(BTreeMap, BTreeMap)> { +) -> CompileResult<(MethodMap, MethodMap)> { let mut warnings = vec![]; let mut errors = vec![]; let decl_engine = ctx.decl_engine; - let mut original_method_ids: BTreeMap = BTreeMap::new(); - let mut impld_method_ids: BTreeMap = BTreeMap::new(); + let mut original_method_ids: MethodMap = BTreeMap::new(); + let mut impld_method_ids: MethodMap = BTreeMap::new(); match ctx .namespace diff --git a/sway-lsp/src/core/session.rs b/sway-lsp/src/core/session.rs index 0e7f4e4491d..852564dc119 100644 --- a/sway-lsp/src/core/session.rs +++ b/sway-lsp/src/core/session.rs @@ -204,7 +204,7 @@ impl Session { self.parse_ast_to_tokens(&parsed, |an| dependency.collect_parsed_declaration(an)); self.parse_ast_to_typed_tokens(typed_program, |node, _module| { - dependency.collect_typed_declaration(decl_engine, node) + dependency.collect_typed_declaration(node) }); } } diff --git a/sway-lsp/src/traverse/dependency.rs b/sway-lsp/src/traverse/dependency.rs index 4dd2a58ba11..d310cc97148 100644 --- a/sway-lsp/src/traverse/dependency.rs +++ b/sway-lsp/src/traverse/dependency.rs @@ -2,14 +2,10 @@ use crate::core::{ token::{self, AstToken, SymbolKind, Token, TypeDefinition, TypedAstToken}, token_map::TokenMap, }; -use sway_core::{ - decl_engine as de, - language::{ - parsed::{AstNode, AstNodeContent, Declaration}, - ty, - }, +use sway_core::language::{ + parsed::{AstNode, AstNodeContent, Declaration}, + ty, }; -use sway_types::Spanned; pub struct Dependency<'a> { tokens: &'a TokenMap, @@ -44,34 +40,23 @@ impl<'a> Dependency<'a> { } /// Insert TypedDeclaration tokens into the TokenMap. - pub fn collect_typed_declaration(&self, decl_engine: &de::DeclEngine, node: &ty::TyAstNode) { + pub fn collect_typed_declaration(&self, node: &ty::TyAstNode) { if let ty::TyAstNodeContent::Declaration(declaration) = &node.content { let typed_token = TypedAstToken::TypedDeclaration(declaration.clone()); - if let Ok(ident) = match declaration { - ty::TyDeclaration::VariableDeclaration(variable) => Ok(variable.name.clone()), - ty::TyDeclaration::StructDeclaration(decl_id) => decl_engine - .get_struct(decl_id.clone(), &declaration.span()) - .map(|decl| decl.call_path.suffix), - ty::TyDeclaration::TraitDeclaration(decl_id) => decl_engine - .get_trait(decl_id.clone(), &declaration.span()) - .map(|decl| decl.name), - ty::TyDeclaration::FunctionDeclaration(decl_id) => decl_engine - .get_function(decl_id.clone(), &declaration.span()) - .map(|decl| decl.name), - ty::TyDeclaration::ConstantDeclaration(decl_id) => decl_engine - .get_constant(decl_id.clone(), &declaration.span()) - .map(|decl| decl.name), - ty::TyDeclaration::EnumDeclaration(decl_id) => decl_engine - .get_enum(decl_id.clone(), &declaration.span()) - .map(|decl| decl.call_path.suffix), + let ident = match declaration { + ty::TyDeclaration::VariableDeclaration(variable) => variable.name.clone(), + ty::TyDeclaration::StructDeclaration(decl_id) + | ty::TyDeclaration::TraitDeclaration(decl_id) + | ty::TyDeclaration::FunctionDeclaration(decl_id) + | ty::TyDeclaration::ConstantDeclaration(decl_id) + | ty::TyDeclaration::EnumDeclaration(decl_id) => decl_id.name.clone(), _ => return, - } { - let ident = token::to_ident_key(&ident); - if let Some(mut token) = self.tokens.try_get_mut(&ident).try_unwrap() { - token.typed = Some(typed_token); - token.type_def = Some(TypeDefinition::Ident(ident.0)); - } + }; + let ident = token::to_ident_key(&ident); + if let Some(mut token) = self.tokens.try_get_mut(&ident).try_unwrap() { + token.typed = Some(typed_token); + token.type_def = Some(TypeDefinition::Ident(ident.0)); } } } diff --git a/sway-lsp/src/traverse/typed_tree.rs b/sway-lsp/src/traverse/typed_tree.rs index e5b9fef0ddb..0bd0f19e903 100644 --- a/sway-lsp/src/traverse/typed_tree.rs +++ b/sway-lsp/src/traverse/typed_tree.rs @@ -399,12 +399,10 @@ impl<'a> TypedTree<'a> { token.typed = Some(TypedAstToken::TypedUseStatement(use_statement.clone())); - let decl_engine = self.engines.de(); - if let Some(decl_ident) = namespace .submodule(call_path) .and_then(|module| module.symbols().get(item)) - .and_then(|decl| decl.get_decl_ident(decl_engine)) + .and_then(|decl| decl.get_decl_ident()) { token.type_def = Some(TypeDefinition::Ident(decl_ident)); } @@ -831,7 +829,7 @@ impl<'a> TypedTree<'a> { .ok() }) .and_then(|function_decl| function_decl.implementing_type) - .and_then(|impl_type| impl_type.get_decl_ident(decl_engine)); + .and_then(|impl_type| impl_type.get_decl_ident()); let prefixes = if let Some(impl_type_name) = implementing_type_name { // the last prefix of the call path is not a module but a type