diff --git a/forc-plugins/forc-doc/src/doc/mod.rs b/forc-plugins/forc-doc/src/doc/mod.rs index b4f0ce3c7b4..07ebcc3d125 100644 --- a/forc-plugins/forc-doc/src/doc/mod.rs +++ b/forc-plugins/forc-doc/src/doc/mod.rs @@ -14,7 +14,7 @@ use sway_core::{ language::ty::{TyAstNodeContent, TyDecl, TyImplTrait, TyModule, TyProgram, TySubmodule}, Engines, }; -use sway_types::{BaseIdent, Spanned}; +use sway_types::{BaseIdent, Named, Spanned}; mod descriptor; pub mod module; @@ -77,10 +77,11 @@ impl Documentation { let mut impl_vec: Vec = Vec::new(); match doc.item_body.ty_decl { - TyDecl::StructDecl(ref struct_decl) => { + TyDecl::StructDecl(ref decl) => { for (impl_trait, module_info) in impl_traits.iter_mut() { - if struct_decl.name.as_str() == impl_trait.implementing_for.span.as_str() - && struct_decl.name.as_str() + let struct_decl = decl_engine.get_struct(&decl.decl_id); + if struct_decl.name().as_str() == impl_trait.implementing_for.span.as_str() + && struct_decl.name().as_str() != impl_trait.trait_name.suffix.span().as_str() { let module_info_override = if let Some(decl_module_info) = 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 29735addb68..6d5743749b4 100644 --- a/sway-core/src/control_flow_analysis/analyze_return_paths.rs +++ b/sway-core/src/control_flow_analysis/analyze_return_paths.rs @@ -21,7 +21,7 @@ impl<'cfg> ControlFlowGraph<'cfg> { ) -> Result> { let mut errors = vec![]; - let mut graph = ControlFlowGraph::default(); + let mut graph = ControlFlowGraph::new(engines.clone()); // do a depth first traversal and cover individual inner ast nodes let mut leaves = vec![]; for ast_entrypoint in module_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 64ef4f02154..4080895ae34 100644 --- a/sway-core/src/control_flow_analysis/dead_code_analysis.rs +++ b/sway-core/src/control_flow_analysis/dead_code_analysis.rs @@ -51,23 +51,14 @@ fn is_entry_point(node: &TyAstNode, decl_engine: &DeclEngine, tree_type: &TreeTy TreeType::Contract | TreeType::Library { .. } => match node { TyAstNode { content: - TyAstNodeContent::Declaration(TyDecl::FunctionDecl(FunctionDecl { - decl_id, - decl_span: _, - .. - })), + TyAstNodeContent::Declaration(TyDecl::FunctionDecl(FunctionDecl { decl_id })), .. } => { let decl = decl_engine.get_function(decl_id); decl.visibility == Visibility::Public || decl.is_test() || decl.is_fallback() } TyAstNode { - content: - TyAstNodeContent::Declaration(TyDecl::TraitDecl(TraitDecl { - decl_id, - decl_span: _, - .. - })), + content: TyAstNodeContent::Declaration(TyDecl::TraitDecl(TraitDecl { decl_id })), .. } => decl_engine.get_trait(decl_id).visibility.is_public(), TyAstNode { @@ -84,11 +75,7 @@ fn is_entry_point(node: &TyAstNode, decl_engine: &DeclEngine, tree_type: &TreeTy } => true, TyAstNode { content: - TyAstNodeContent::Declaration(TyDecl::ConstantDecl(ConstantDecl { - decl_id, - decl_span: _, - .. - })), + TyAstNodeContent::Declaration(TyDecl::ConstantDecl(ConstantDecl { decl_id })), .. } => { let decl = decl_engine.get_constant(decl_id); @@ -2234,49 +2221,45 @@ fn construct_dead_code_warning_from_node( ty::TyAstNode { content: ty::TyAstNodeContent::Declaration(ty::TyDecl::FunctionDecl(ty::FunctionDecl { - name, - .. + decl_id, })), .. } => CompileWarning { - span: name.span(), + span: decl_engine.get(decl_id).name.span(), warning_content: Warning::DeadFunctionDeclaration, }, ty::TyAstNode { content: - ty::TyAstNodeContent::Declaration(ty::TyDecl::StructDecl(ty::StructDecl { - name, .. - })), + ty::TyAstNodeContent::Declaration(ty::TyDecl::StructDecl(ty::StructDecl { decl_id })), .. } => CompileWarning { - span: name.span(), + span: decl_engine.get(decl_id).name().span(), warning_content: Warning::DeadStructDeclaration, }, ty::TyAstNode { content: - ty::TyAstNodeContent::Declaration(ty::TyDecl::EnumDecl(ty::EnumDecl { name, .. })), + ty::TyAstNodeContent::Declaration(ty::TyDecl::EnumDecl(ty::EnumDecl { decl_id })), .. } => CompileWarning { - span: name.span(), + span: decl_engine.get(decl_id).name().span(), warning_content: Warning::DeadEnumDeclaration, }, ty::TyAstNode { content: - ty::TyAstNodeContent::Declaration(ty::TyDecl::TraitDecl(ty::TraitDecl { name, .. })), + ty::TyAstNodeContent::Declaration(ty::TyDecl::TraitDecl(ty::TraitDecl { decl_id })), .. } => CompileWarning { - span: name.span(), + span: decl_engine.get(decl_id).name.span(), warning_content: Warning::DeadTrait, }, ty::TyAstNode { content: ty::TyAstNodeContent::Declaration(ty::TyDecl::ConstantDecl(ty::ConstantDecl { - name, - .. + decl_id, })), .. } => CompileWarning { - span: name.span(), + span: decl_engine.get_constant(decl_id).name().span(), warning_content: Warning::DeadDeclaration, }, ty::TyAstNode { 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 21b78995591..0e5b4e87ef4 100644 --- a/sway-core/src/control_flow_analysis/flow_graph/mod.rs +++ b/sway-core/src/control_flow_analysis/flow_graph/mod.rs @@ -23,7 +23,7 @@ pub(crate) use namespace::VariableNamespaceEntry; pub type EntryPoint = NodeIndex; pub type ExitPoint = NodeIndex; -#[derive(Clone, Default)] +#[derive(Clone)] /// A graph that can be used to model the control flow of a Sway program. /// This graph is used as the basis for all of the algorithms in the control flow analysis portion /// of the compiler. @@ -33,10 +33,24 @@ pub struct ControlFlowGraph<'cfg> { pub(crate) pending_entry_points_edges: Vec<(NodeIndex, ControlFlowGraphEdge)>, pub(crate) namespace: ControlFlowNamespace, pub(crate) decls: HashMap, + pub(crate) engines: Engines, } pub type Graph<'cfg> = petgraph::Graph, ControlFlowGraphEdge>; +impl<'cfg> ControlFlowGraph<'cfg> { + pub fn new(engines: Engines) -> Self { + Self { + graph: Default::default(), + entry_points: Default::default(), + pending_entry_points_edges: Default::default(), + namespace: Default::default(), + decls: Default::default(), + engines, + } + } +} + #[derive(Clone)] pub struct ControlFlowGraphEdge(String); @@ -88,10 +102,10 @@ pub enum ControlFlowGraphNode<'cfg> { } impl<'cfg> GetDeclIdent for ControlFlowGraphNode<'cfg> { - fn get_decl_ident(&self) -> Option { + fn get_decl_ident(&self, engines: &Engines) -> Option { match self { ControlFlowGraphNode::OrganizationalDominator(_) => None, - ControlFlowGraphNode::ProgramNode { node, .. } => node.get_decl_ident(), + ControlFlowGraphNode::ProgramNode { node, .. } => node.get_decl_ident(engines), ControlFlowGraphNode::EnumVariant { variant_name, .. } => Some(variant_name.clone()), ControlFlowGraphNode::MethodDeclaration { method_name, .. } => { Some(method_name.clone()) @@ -174,7 +188,7 @@ impl<'cfg> ControlFlowGraph<'cfg> { self.pending_entry_points_edges.push((to, label)); } pub(crate) fn add_node<'eng: 'cfg>(&mut self, node: ControlFlowGraphNode<'cfg>) -> NodeIndex { - let ident_opt = node.get_decl_ident(); + let ident_opt = node.get_decl_ident(&self.engines); let node_index = self.graph.add_node(node); if let Some(ident) = ident_opt { self.decls.insert(ident.into(), node_index); @@ -200,7 +214,7 @@ impl<'cfg> ControlFlowGraph<'cfg> { } pub(crate) fn get_node_from_decl(&self, cfg_node: &ControlFlowGraphNode) -> Option { - if let Some(ident) = cfg_node.get_decl_ident() { + if let Some(ident) = cfg_node.get_decl_ident(&self.engines) { if !ident.span().is_dummy() { self.decls.get(&ident.into()).cloned() } else { diff --git a/sway-core/src/decl_engine/mod.rs b/sway-core/src/decl_engine/mod.rs index 0d4b2c2ad25..94f6e7987f7 100644 --- a/sway-core/src/decl_engine/mod.rs +++ b/sway-core/src/decl_engine/mod.rs @@ -18,7 +18,6 @@ pub(crate) mod parsed_engine; pub mod parsed_id; pub(crate) mod r#ref; pub(crate) mod replace_decls; -pub(crate) mod template; use std::collections::BTreeMap; @@ -30,7 +29,6 @@ pub(crate) use mapping::*; pub use r#ref::*; pub(crate) use replace_decls::*; use sway_types::Ident; -pub(crate) use template::*; use crate::{ language::ty::{TyTraitInterfaceItem, TyTraitItem}, diff --git a/sway-core/src/decl_engine/ref.rs b/sway-core/src/decl_engine/ref.rs index 8429c212f42..9d5186d33f1 100644 --- a/sway-core/src/decl_engine/ref.rs +++ b/sway-core/src/decl_engine/ref.rs @@ -62,10 +62,6 @@ pub struct DeclRef { /// The index into the [DeclEngine]. id: I, - /// The type substitution list to apply to the `id` field for type - /// monomorphization. - subst_list: SubstList, - /// The [Span] of the entire declaration. decl_span: Span, } @@ -75,7 +71,6 @@ impl DeclRef { DeclRef { name, id, - subst_list: SubstList::new(), decl_span, } } @@ -88,10 +83,6 @@ impl DeclRef { &self.id } - pub(crate) fn subst_list(&self) -> &SubstList { - &self.subst_list - } - pub fn decl_span(&self) -> &Span { &self.decl_span } @@ -183,7 +174,6 @@ where // relevant/a reliable source of obj v. obj distinction decl_span: _, // temporarily omitted - subst_list: _, } = self; let DeclRef { name: rn, @@ -192,7 +182,6 @@ where // relevant/a reliable source of obj v. obj distinction decl_span: _, // temporarily omitted - subst_list: _, } = other; ln == rn && decl_engine.get(lid).eq(&decl_engine.get(rid), ctx) } @@ -211,8 +200,6 @@ where // these fields are not hashed because they aren't relevant/a // reliable source of obj v. obj distinction decl_span: _, - // temporarily omitted - subst_list: _, } = self; name.hash(state); decl_engine.get(id).hash(state, engines); diff --git a/sway-core/src/decl_engine/template.rs b/sway-core/src/decl_engine/template.rs deleted file mode 100644 index c4778b1f75c..00000000000 --- a/sway-core/src/decl_engine/template.rs +++ /dev/null @@ -1,37 +0,0 @@ -/// An object that is a template for copies from the template. -/// -/// This is predominantly used with [SubstList](crate::type_system::SubstList) -/// and [TyDecl](crate::language::ty::TyDecl). The various variants of -/// [TyDecl](crate::language::ty::TyDecl) contain fields -/// `subst_list: Template`. This type indicates that the -/// [SubstList](crate::type_system::SubstList) contained in this field is simply -/// a template for usages of the declaration declared in that particular -/// [TyDecl](crate::language::ty::TyDecl) node. -#[derive(Clone, Debug, Default)] -pub struct Template(T) -where - T: Clone; - -impl Template -where - T: Clone, -{ - pub(crate) fn new(value: T) -> Template { - Template(value) - } - - #[allow(dead_code)] - pub(crate) fn inner(&self) -> &T { - &self.0 - } - - #[allow(dead_code)] - pub(crate) fn into_inner(self) -> T { - self.0 - } - - #[allow(dead_code)] - pub(crate) fn fresh_copy(&self) -> T { - self.0.clone() - } -} diff --git a/sway-core/src/engine_threading.rs b/sway-core/src/engine_threading.rs index 191f6529f73..2a6f99bba28 100644 --- a/sway-core/src/engine_threading.rs +++ b/sway-core/src/engine_threading.rs @@ -8,7 +8,7 @@ use std::{ fmt, hash::{BuildHasher, Hash, Hasher}, }; -use sway_types::SourceEngine; +use sway_types::{SourceEngine, Span}; #[derive(Clone, Debug, Default)] pub struct Engines { @@ -377,3 +377,7 @@ where state.finish() } } + +pub trait SpannedWithEngines { + fn span(&self, engines: &Engines) -> Span; +} diff --git a/sway-core/src/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index 883c1f00ed2..6ecb76ebead 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -27,7 +27,7 @@ use sway_ir::{ value::Value, InstOp, Instruction, Type, TypeContent, }; -use sway_types::{ident::Ident, integer_bits::IntegerBits, span::Spanned, Span}; +use sway_types::{ident::Ident, integer_bits::IntegerBits, span::Spanned, Named, Span}; use sway_utils::mapped_stack::MappedStack; enum ConstEvalError { @@ -702,7 +702,7 @@ fn const_eval_codeblock( Ok(None) } else { Err(ConstEvalError::CannotBeEvaluatedToConst { - span: decl.span().clone(), + span: decl.span(lookup.engines).clone(), }) } } @@ -716,12 +716,12 @@ fn const_eval_codeblock( }) .flatten() { - known_consts.push(const_decl.name.clone(), constant); - bindings.push(const_decl.name.clone()); + known_consts.push(ty_const_decl.name().clone(), constant); + bindings.push(ty_const_decl.name().clone()); Ok(None) } else { Err(ConstEvalError::CannotBeEvaluatedToConst { - span: const_decl.decl_span.clone(), + span: ty_const_decl.span.clone(), }) } } @@ -1211,7 +1211,13 @@ mod tests { .declarations .iter() .find_map(|x| match x { - ty::TyDecl::FunctionDecl(x) if x.name.as_str() == "f" => Some(x), + ty::TyDecl::FunctionDecl(x) => { + if engines.de().get_function(&x.decl_id).name.as_str() == "f" { + Some(x) + } else { + None + } + } _ => None, }) .expect("An function named `f` was not found."); diff --git a/sway-core/src/language/ty/ast_node.rs b/sway-core/src/language/ty/ast_node.rs index 21563cdd33a..d43bc71bf97 100644 --- a/sway-core/src/language/ty/ast_node.rs +++ b/sway-core/src/language/ty/ast_node.rs @@ -20,7 +20,7 @@ use crate::{ }; pub trait GetDeclIdent { - fn get_decl_ident(&self) -> Option; + fn get_decl_ident(&self, engines: &Engines) -> Option; } #[derive(Clone, Debug)] @@ -135,8 +135,8 @@ impl CollectTypesMetadata for TyAstNode { } impl GetDeclIdent for TyAstNode { - fn get_decl_ident(&self) -> Option { - self.content.get_decl_ident() + fn get_decl_ident(&self, engines: &Engines) -> Option { + self.content.get_decl_ident(engines) } } @@ -418,9 +418,9 @@ impl CollectTypesMetadata for TyAstNodeContent { } impl GetDeclIdent for TyAstNodeContent { - fn get_decl_ident(&self) -> Option { + fn get_decl_ident(&self, engines: &Engines) -> Option { match self { - TyAstNodeContent::Declaration(decl) => decl.get_decl_ident(), + TyAstNodeContent::Declaration(decl) => decl.get_decl_ident(engines), TyAstNodeContent::Expression(_expr) => None, //expr.get_decl_ident(), TyAstNodeContent::SideEffect(_) => None, TyAstNodeContent::Error(_, _) => None, diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index d6c3d2045d7..b0633d19bd2 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -7,7 +7,7 @@ use sway_error::{ error::CompileError, handler::{ErrorEmitted, Handler}, }; -use sway_types::{Ident, Span, Spanned}; +use sway_types::{Ident, Named, Span, Spanned}; use crate::{ decl_engine::*, @@ -39,48 +39,32 @@ pub enum TyDecl { #[derive(Clone, Debug)] pub struct ConstantDecl { - pub name: Ident, pub decl_id: DeclId, - pub decl_span: Span, } #[derive(Clone, Debug)] pub struct TraitTypeDecl { - pub name: Ident, pub decl_id: DeclId, - pub decl_span: Span, } #[derive(Clone, Debug)] pub struct FunctionDecl { - pub name: Ident, pub decl_id: DeclId, - pub subst_list: Template, - pub decl_span: Span, } #[derive(Clone, Debug)] pub struct TraitDecl { - pub name: Ident, pub decl_id: DeclId, - pub subst_list: Template, - pub decl_span: Span, } #[derive(Clone, Debug)] pub struct StructDecl { - pub name: Ident, pub decl_id: DeclId, - pub subst_list: Template, - pub decl_span: Span, } #[derive(Clone, Debug)] pub struct EnumDecl { - pub name: Ident, pub decl_id: DeclId, - pub subst_list: Template, - pub decl_span: Span, } #[derive(Clone, Debug)] @@ -92,17 +76,12 @@ pub struct EnumVariantDecl { #[derive(Clone, Debug)] pub struct ImplTrait { - pub name: Ident, pub decl_id: DeclId, - pub subst_list: Template, - pub decl_span: Span, } #[derive(Clone, Debug)] pub struct AbiDecl { - pub name: Ident, pub decl_id: DeclId, - pub decl_span: Span, } #[derive(Clone, Debug)] @@ -114,14 +93,11 @@ pub struct GenericTypeForFunctionScope { #[derive(Clone, Debug)] pub struct StorageDecl { pub decl_id: DeclId, - pub decl_span: Span, } #[derive(Clone, Debug)] pub struct TypeAliasDecl { - pub name: Ident, pub decl_id: DeclId, - pub decl_span: Span, } impl EqWithEngines for TyDecl {} @@ -132,89 +108,33 @@ impl PartialEqWithEngines for TyDecl { match (self, other) { (TyDecl::VariableDecl(x), TyDecl::VariableDecl(y)) => x.eq(y, ctx), ( - TyDecl::ConstantDecl(ConstantDecl { - name: ln, - decl_id: lid, - .. - }), - TyDecl::ConstantDecl(ConstantDecl { - name: rn, - decl_id: rid, - .. - }), - ) => ln == rn && decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), + TyDecl::ConstantDecl(ConstantDecl { decl_id: lid, .. }), + TyDecl::ConstantDecl(ConstantDecl { decl_id: rid, .. }), + ) => decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), ( - TyDecl::FunctionDecl(FunctionDecl { - name: ln, - decl_id: lid, - .. - }), - TyDecl::FunctionDecl(FunctionDecl { - name: rn, - decl_id: rid, - .. - }), - ) => ln == rn && decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), + TyDecl::FunctionDecl(FunctionDecl { decl_id: lid, .. }), + TyDecl::FunctionDecl(FunctionDecl { decl_id: rid, .. }), + ) => decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), ( - TyDecl::TraitDecl(TraitDecl { - name: ln, - decl_id: lid, - .. - }), - TyDecl::TraitDecl(TraitDecl { - name: rn, - decl_id: rid, - .. - }), - ) => ln == rn && decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), + TyDecl::TraitDecl(TraitDecl { decl_id: lid, .. }), + TyDecl::TraitDecl(TraitDecl { decl_id: rid, .. }), + ) => decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), ( - TyDecl::StructDecl(StructDecl { - name: ln, - decl_id: lid, - .. - }), - TyDecl::StructDecl(StructDecl { - name: rn, - decl_id: rid, - .. - }), - ) => ln == rn && decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), + TyDecl::StructDecl(StructDecl { decl_id: lid, .. }), + TyDecl::StructDecl(StructDecl { decl_id: rid, .. }), + ) => decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), ( - TyDecl::EnumDecl(EnumDecl { - name: ln, - decl_id: lid, - .. - }), - TyDecl::EnumDecl(EnumDecl { - name: rn, - decl_id: rid, - .. - }), - ) => ln == rn && decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), + TyDecl::EnumDecl(EnumDecl { decl_id: lid, .. }), + TyDecl::EnumDecl(EnumDecl { decl_id: rid, .. }), + ) => decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), ( - TyDecl::ImplTrait(ImplTrait { - name: ln, - decl_id: lid, - .. - }), - TyDecl::ImplTrait(ImplTrait { - name: rn, - decl_id: rid, - .. - }), - ) => ln == rn && decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), + TyDecl::ImplTrait(ImplTrait { decl_id: lid, .. }), + TyDecl::ImplTrait(ImplTrait { decl_id: rid, .. }), + ) => decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), ( - TyDecl::AbiDecl(AbiDecl { - name: ln, - decl_id: lid, - .. - }), - TyDecl::AbiDecl(AbiDecl { - name: rn, - decl_id: rid, - .. - }), - ) => ln == rn && decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), + TyDecl::AbiDecl(AbiDecl { decl_id: lid, .. }), + TyDecl::AbiDecl(AbiDecl { decl_id: rid, .. }), + ) => decl_engine.get(lid).eq(&decl_engine.get(rid), ctx), ( TyDecl::StorageDecl(StorageDecl { decl_id: lid, .. }), TyDecl::StorageDecl(StorageDecl { decl_id: rid, .. }), @@ -333,76 +253,35 @@ impl SubstTypes for TyDecl { } } -impl TyDecl { - pub fn get_enum_decl_ref(&self) -> Option { - if let TyDecl::EnumDecl(EnumDecl { - name, - decl_id, - decl_span, - .. - }) = self - { - Some(DeclRef::new(name.clone(), *decl_id, decl_span.clone())) - } else { - None - } - } - - pub fn get_struct_decl_ref(&self) -> Option { - if let TyDecl::StructDecl(StructDecl { - name, - decl_id, - decl_span, - .. - }) = self - { - Some(DeclRef::new(name.clone(), *decl_id, decl_span.clone())) - } else { - None - } - } - - pub fn get_trait_decl_ref(&self) -> Option { - if let TyDecl::TraitDecl(decl) = self { - Some(DeclRef::new( - decl.name.clone(), - decl.decl_id, - decl.decl_span.clone(), - )) - } else { - None - } - } - - pub fn get_fun_decl_ref(&self) -> Option { - if let TyDecl::FunctionDecl(FunctionDecl { - name, - decl_id, - subst_list: _, - decl_span, - }) = self - { - Some(DeclRef::new(name.clone(), *decl_id, decl_span.clone())) - } else { - None - } - } -} - -impl Spanned for TyDecl { - fn span(&self) -> Span { +impl SpannedWithEngines for TyDecl { + fn span(&self, engines: &Engines) -> Span { match self { + TyDecl::ConstantDecl(ConstantDecl { decl_id, .. }) => { + let const_decl = engines.de().get(decl_id); + const_decl.span.clone() + } + TyDecl::TraitTypeDecl(TraitTypeDecl { decl_id }) => { + engines.de().get_type(decl_id).span.clone() + } + TyDecl::FunctionDecl(FunctionDecl { decl_id }) => { + engines.de().get_function(decl_id).span.clone() + } + TyDecl::TraitDecl(TraitDecl { decl_id }) => { + engines.de().get_trait(decl_id).span.clone() + } + TyDecl::StructDecl(StructDecl { decl_id }) => { + engines.de().get_struct(decl_id).span.clone() + } + TyDecl::EnumDecl(EnumDecl { decl_id }) => engines.de().get_enum(decl_id).span.clone(), + TyDecl::ImplTrait(ImplTrait { decl_id }) => { + engines.de().get_impl_trait(decl_id).span.clone() + } + TyDecl::AbiDecl(AbiDecl { decl_id }) => engines.de().get_abi(decl_id).span.clone(), TyDecl::VariableDecl(decl) => decl.name.span(), - TyDecl::FunctionDecl(FunctionDecl { decl_span, .. }) - | TyDecl::TraitDecl(TraitDecl { decl_span, .. }) - | TyDecl::ImplTrait(ImplTrait { decl_span, .. }) - | TyDecl::ConstantDecl(ConstantDecl { decl_span, .. }) - | TyDecl::TraitTypeDecl(TraitTypeDecl { decl_span, .. }) - | TyDecl::StorageDecl(StorageDecl { decl_span, .. }) - | TyDecl::TypeAliasDecl(TypeAliasDecl { decl_span, .. }) - | TyDecl::AbiDecl(AbiDecl { decl_span, .. }) - | TyDecl::StructDecl(StructDecl { decl_span, .. }) - | TyDecl::EnumDecl(EnumDecl { decl_span, .. }) => decl_span.clone(), + TyDecl::StorageDecl(StorageDecl { decl_id }) => engines.de().get(decl_id).span.clone(), + TyDecl::TypeAliasDecl(TypeAliasDecl { decl_id }) => { + engines.de().get(decl_id).span.clone() + } TyDecl::EnumVariantDecl(EnumVariantDecl { variant_decl_span, .. }) => variant_decl_span.clone(), @@ -447,12 +326,23 @@ impl DisplayWithEngines for TyDecl { builder.push_str(&engines.help_out(body).to_string()); builder } - TyDecl::FunctionDecl(FunctionDecl { name, .. }) - | TyDecl::TraitDecl(TraitDecl { name, .. }) - | TyDecl::StructDecl(StructDecl { name, .. }) - | TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) - | TyDecl::ImplTrait(ImplTrait { name, .. }) - | TyDecl::EnumDecl(EnumDecl { name, .. }) => name.as_str().into(), + TyDecl::FunctionDecl(FunctionDecl { decl_id }) => { + engines.de().get(decl_id).name.as_str().into() + } + TyDecl::TraitDecl(TraitDecl { decl_id }) => { + engines.de().get(decl_id).name.as_str().into() + } + TyDecl::StructDecl(StructDecl { decl_id }) => { + engines.de().get(decl_id).name().as_str().into() + } + TyDecl::EnumDecl(EnumDecl { decl_id }) => { + engines.de().get(decl_id).name().as_str().into() + } + TyDecl::ImplTrait(ImplTrait { decl_id }) => { + engines.de().get(decl_id).name().as_str().into() + } + TyDecl::TypeAliasDecl(TypeAliasDecl { decl_id }) => + engines.de().get(decl_id).name().as_str().into(), _ => String::new(), } ) @@ -494,10 +384,18 @@ impl DebugWithEngines for TyDecl { builder.push_str(format!("{:?}", engines.help_out(body)).as_str()); builder } - TyDecl::FunctionDecl(FunctionDecl { name, .. }) - | TyDecl::TraitDecl(TraitDecl { name, .. }) - | TyDecl::StructDecl(StructDecl { name, .. }) - | TyDecl::EnumDecl(EnumDecl { name, .. }) => name.as_str().into(), + TyDecl::FunctionDecl(FunctionDecl { decl_id }) => { + engines.de().get(decl_id).name.as_str().into() + } + TyDecl::TraitDecl(TraitDecl { decl_id }) => { + engines.de().get(decl_id).name.as_str().into() + } + TyDecl::StructDecl(StructDecl { decl_id }) => { + engines.de().get(decl_id).name().as_str().into() + } + TyDecl::EnumDecl(EnumDecl { decl_id }) => { + engines.de().get(decl_id).name().as_str().into() + } _ => String::new(), } ) @@ -553,19 +451,37 @@ impl CollectTypesMetadata for TyDecl { } impl GetDeclIdent for TyDecl { - fn get_decl_ident(&self) -> Option { + fn get_decl_ident(&self, engines: &Engines) -> Option { match self { + TyDecl::ConstantDecl(ConstantDecl { decl_id }) => { + Some(engines.de().get_constant(decl_id).name().clone()) + } + TyDecl::TraitTypeDecl(TraitTypeDecl { decl_id }) => { + Some(engines.de().get_type(decl_id).name().clone()) + } + TyDecl::FunctionDecl(FunctionDecl { decl_id }) => { + Some(engines.de().get(decl_id).name.clone()) + } + TyDecl::TraitDecl(TraitDecl { decl_id }) => { + Some(engines.de().get(decl_id).name.clone()) + } + TyDecl::StructDecl(StructDecl { decl_id }) => { + Some(engines.de().get(decl_id).name().clone()) + } + TyDecl::EnumDecl(EnumDecl { decl_id }) => { + Some(engines.de().get(decl_id).name().clone()) + } + TyDecl::ImplTrait(ImplTrait { decl_id }) => { + Some(engines.de().get(decl_id).name().clone()) + } + TyDecl::AbiDecl(AbiDecl { decl_id }) => Some(engines.de().get(decl_id).name().clone()), TyDecl::VariableDecl(decl) => Some(decl.name.clone()), - TyDecl::FunctionDecl(FunctionDecl { name, .. }) - | TyDecl::TraitDecl(TraitDecl { name, .. }) - | TyDecl::ConstantDecl(ConstantDecl { name, .. }) - | TyDecl::ImplTrait(ImplTrait { name, .. }) - | TyDecl::AbiDecl(AbiDecl { name, .. }) - | TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) - | TyDecl::TraitTypeDecl(TraitTypeDecl { name, .. }) - | TyDecl::GenericTypeForFunctionScope(GenericTypeForFunctionScope { name, .. }) - | TyDecl::StructDecl(StructDecl { name, .. }) - | TyDecl::EnumDecl(EnumDecl { name, .. }) => Some(name.clone()), + TyDecl::TypeAliasDecl(TypeAliasDecl { decl_id }) => { + Some(engines.de().get(decl_id).name().clone()) + } + TyDecl::GenericTypeForFunctionScope(GenericTypeForFunctionScope { name, .. }) => { + Some(name.clone()) + } TyDecl::EnumVariantDecl(EnumVariantDecl { variant_name, .. }) => { Some(variant_name.clone()) } @@ -585,12 +501,14 @@ impl TyDecl { engines: &Engines, ) -> Result { match self { - TyDecl::EnumDecl(EnumDecl { - name, - decl_id, - subst_list: _, - decl_span, - }) => Ok(DeclRef::new(name.clone(), *decl_id, decl_span.clone())), + TyDecl::EnumDecl(EnumDecl { decl_id }) => { + let enum_decl = engines.de().get_enum(decl_id); + Ok(DeclRef::new( + enum_decl.name().clone(), + *decl_id, + enum_decl.span.clone(), + )) + } TyDecl::TypeAliasDecl(TypeAliasDecl { decl_id, .. }) => { let alias_decl = engines.de().get_type_alias(decl_id); let TyTypeAliasDecl { ty, span, .. } = &*alias_decl; @@ -606,13 +524,13 @@ impl TyDecl { TypeInfo::Enum(r) => Ok(r.clone()), _ => Err(handler.emit_err(CompileError::DeclIsNotAnEnum { actually: self.friendly_type_name().to_string(), - span: self.span(), + span: self.span(engines), })), }, TyDecl::ErrorRecovery(_, err) => Err(*err), decl => Err(handler.emit_err(CompileError::DeclIsNotAnEnum { actually: decl.friendly_type_name().to_string(), - span: decl.span(), + span: decl.span(engines), })), } } @@ -626,12 +544,14 @@ impl TyDecl { engines: &Engines, ) -> Result { match self { - TyDecl::StructDecl(StructDecl { - name, - decl_id, - subst_list: _, - decl_span, - }) => Ok(DeclRef::new(name.clone(), *decl_id, decl_span.clone())), + TyDecl::StructDecl(StructDecl { decl_id }) => { + let struct_decl = engines.de().get_struct(decl_id); + Ok(DeclRef::new( + struct_decl.name().clone(), + *decl_id, + struct_decl.span.clone(), + )) + } TyDecl::TypeAliasDecl(TypeAliasDecl { decl_id, .. }) => { let alias_decl = engines.de().get_type_alias(decl_id); let TyTypeAliasDecl { ty, span, .. } = &*alias_decl; @@ -643,7 +563,7 @@ impl TyDecl { TyDecl::ErrorRecovery(_, err) => Err(*err), decl => Err(handler.emit_err(CompileError::DeclIsNotAStruct { actually: decl.friendly_type_name().to_string(), - span: decl.span(), + span: decl.span(engines), })), } } @@ -654,18 +574,17 @@ impl TyDecl { pub(crate) fn to_fn_ref( &self, handler: &Handler, - ) -> Result>, ErrorEmitted> { + engines: &Engines, + ) -> Result { match self { - TyDecl::FunctionDecl(FunctionDecl { - name, - decl_id, - subst_list: _, - decl_span, - }) => Ok(DeclRef::new(name.clone(), *decl_id, decl_span.clone())), + TyDecl::FunctionDecl(FunctionDecl { decl_id }) => { + let decl = engines.de().get(decl_id); + Ok(DeclRef::new(decl.name.clone(), *decl_id, decl.span.clone())) + } TyDecl::ErrorRecovery(_, err) => Err(*err), decl => Err(handler.emit_err(CompileError::DeclIsNotAFunction { actually: decl.friendly_type_name().to_string(), - span: decl.span(), + span: decl.span(engines), })), } } @@ -676,13 +595,14 @@ impl TyDecl { pub(crate) fn expect_variable( &self, handler: &Handler, + engines: &Engines, ) -> Result<&TyVariableDecl, ErrorEmitted> { match self { TyDecl::VariableDecl(decl) => Ok(decl), TyDecl::ErrorRecovery(_, err) => Err(*err), decl => Err(handler.emit_err(CompileError::DeclIsNotAVariable { actually: decl.friendly_type_name().to_string(), - span: decl.span(), + span: decl.span(engines), })), } } @@ -693,17 +613,21 @@ impl TyDecl { pub(crate) fn to_abi_ref( &self, handler: &Handler, + engines: &Engines, ) -> Result>, ErrorEmitted> { match self { - TyDecl::AbiDecl(AbiDecl { - name, - decl_id, - decl_span, - }) => Ok(DeclRef::new(name.clone(), *decl_id, decl_span.clone())), + TyDecl::AbiDecl(AbiDecl { decl_id }) => { + let abi_decl = engines.de().get_abi(decl_id); + Ok(DeclRef::new( + abi_decl.name().clone(), + *decl_id, + abi_decl.span.clone(), + )) + } TyDecl::ErrorRecovery(_, err) => Err(*err), decl => Err(handler.emit_err(CompileError::DeclIsNotAnAbi { actually: decl.friendly_type_name().to_string(), - span: decl.span(), + span: decl.span(engines), })), } } @@ -714,17 +638,21 @@ impl TyDecl { pub(crate) fn to_const_ref( &self, handler: &Handler, + engines: &Engines, ) -> Result>, ErrorEmitted> { match self { - TyDecl::ConstantDecl(ConstantDecl { - name, - decl_id, - decl_span, - }) => Ok(DeclRef::new(name.clone(), *decl_id, decl_span.clone())), + TyDecl::ConstantDecl(ConstantDecl { decl_id }) => { + let const_decl = engines.de().get_constant(decl_id); + Ok(DeclRef::new( + const_decl.name().clone(), + *decl_id, + const_decl.span.clone(), + )) + } TyDecl::ErrorRecovery(_, err) => Err(*err), decl => Err(handler.emit_err(CompileError::DeclIsNotAConstant { actually: decl.friendly_type_name().to_string(), - span: decl.span(), + span: decl.span(engines), })), } } @@ -741,13 +669,13 @@ impl TyDecl { let implementing_for_type_id = &*implementing_for_type_id_arc; format!( "{} for {:?}", - self.get_decl_ident() + self.get_decl_ident(engines) .map_or(String::from(""), |f| f.as_str().to_string()), engines.help_out(implementing_for_type_id) ) } _ => self - .get_decl_ident() + .get_decl_ident(engines) .map_or(String::from(""), |f| f.as_str().to_string()), } } @@ -817,26 +745,30 @@ impl TyDecl { let decl = decl_engine.get_function(decl_id); decl.return_type.type_id } - TyDecl::StructDecl(StructDecl { - name, - decl_id, - subst_list: _, - decl_span, - }) => type_engine.insert( - engines, - TypeInfo::Struct(DeclRef::new(name.clone(), *decl_id, decl_span.clone())), - name.span().source_id(), - ), - TyDecl::EnumDecl(EnumDecl { - name, - decl_id, - subst_list: _, - decl_span, - }) => type_engine.insert( - engines, - TypeInfo::Enum(DeclRef::new(name.clone(), *decl_id, decl_span.clone())), - name.span().source_id(), - ), + TyDecl::StructDecl(StructDecl { decl_id }) => { + let decl = decl_engine.get_struct(decl_id); + type_engine.insert( + engines, + TypeInfo::Struct(DeclRef::new( + decl.name().clone(), + *decl_id, + decl.span.clone(), + )), + decl.name().span().source_id(), + ) + } + TyDecl::EnumDecl(EnumDecl { decl_id }) => { + let decl = decl_engine.get_enum(decl_id); + type_engine.insert( + engines, + TypeInfo::Enum(DeclRef::new( + decl.name().clone(), + *decl_id, + decl.span.clone(), + )), + decl.name().span().source_id(), + ) + } TyDecl::StorageDecl(StorageDecl { decl_id, .. }) => { let storage_decl = decl_engine.get_storage(decl_id); type_engine.insert( @@ -856,7 +788,7 @@ impl TyDecl { }) => *type_id, decl => { return Err(handler.emit_err(CompileError::NotAType { - span: decl.span(), + span: decl.span(engines), name: engines.help_out(decl).to_string(), actually_is: decl.friendly_type_name(), })); @@ -900,9 +832,7 @@ impl TyDecl { impl From>> for TyDecl { fn from(decl_ref: DeclRef>) -> Self { TyDecl::TraitTypeDecl(TraitTypeDecl { - name: decl_ref.name().clone(), decl_id: *decl_ref.id(), - decl_span: decl_ref.decl_span().clone(), }) } } @@ -910,9 +840,7 @@ impl From>> for TyDecl { impl From>> for TyDecl { fn from(decl_ref: DeclRef>) -> Self { TyDecl::ConstantDecl(ConstantDecl { - name: decl_ref.name().clone(), decl_id: *decl_ref.id(), - decl_span: decl_ref.decl_span().clone(), }) } } @@ -920,10 +848,7 @@ impl From>> for TyDecl { impl From>> for TyDecl { fn from(decl_ref: DeclRef>) -> Self { TyDecl::EnumDecl(EnumDecl { - name: decl_ref.name().clone(), decl_id: *decl_ref.id(), - subst_list: Template::new(decl_ref.subst_list().clone()), - decl_span: decl_ref.decl_span().clone(), }) } } @@ -931,10 +856,7 @@ impl From>> for TyDecl { impl From>> for TyDecl { fn from(decl_ref: DeclRef>) -> Self { TyDecl::FunctionDecl(FunctionDecl { - name: decl_ref.name().clone(), decl_id: *decl_ref.id(), - subst_list: Template::new(decl_ref.subst_list().clone()), - decl_span: decl_ref.decl_span().clone(), }) } } @@ -942,10 +864,7 @@ impl From>> for TyDecl { impl From>> for TyDecl { fn from(decl_ref: DeclRef>) -> Self { TyDecl::TraitDecl(TraitDecl { - name: decl_ref.name().clone(), decl_id: *decl_ref.id(), - subst_list: Template::new(decl_ref.subst_list().clone()), - decl_span: decl_ref.decl_span().clone(), }) } } @@ -953,10 +872,7 @@ impl From>> for TyDecl { impl From>> for TyDecl { fn from(decl_ref: DeclRef>) -> Self { TyDecl::ImplTrait(ImplTrait { - name: decl_ref.name().clone(), decl_id: *decl_ref.id(), - subst_list: Template::new(decl_ref.subst_list().clone()), - decl_span: decl_ref.decl_span().clone(), }) } } @@ -964,10 +880,7 @@ impl From>> for TyDecl { impl From>> for TyDecl { fn from(decl_ref: DeclRef>) -> Self { TyDecl::StructDecl(StructDecl { - name: decl_ref.name().clone(), decl_id: *decl_ref.id(), - subst_list: Template::new(decl_ref.subst_list().clone()), - decl_span: decl_ref.decl_span().clone(), }) } } @@ -975,9 +888,7 @@ impl From>> for TyDecl { impl From>> for TyDecl { fn from(decl_ref: DeclRef>) -> Self { TyDecl::AbiDecl(AbiDecl { - name: decl_ref.name().clone(), decl_id: *decl_ref.id(), - decl_span: decl_ref.decl_span().clone(), }) } } @@ -986,16 +897,13 @@ impl From>> for TyDecl { fn from(decl_ref: DeclRef>) -> Self { TyDecl::StorageDecl(StorageDecl { decl_id: *decl_ref.id(), - decl_span: decl_ref.decl_span().clone(), }) } } impl From>> for TyDecl { fn from(decl_ref: DeclRef>) -> Self { TyDecl::TypeAliasDecl(TypeAliasDecl { - name: decl_ref.name().clone(), decl_id: *decl_ref.id(), - decl_span: decl_ref.decl_span().clone(), }) } } diff --git a/sway-core/src/language/ty/module.rs b/sway-core/src/language/ty/module.rs index b3b5c44d2f1..21bc45eb6cf 100644 --- a/sway-core/src/language/ty/module.rs +++ b/sway-core/src/language/ty/module.rs @@ -53,19 +53,14 @@ impl TyModule { decl_engine: &'a DeclEngine, ) -> impl '_ + Iterator, DeclRefFunction)> { self.all_nodes.iter().filter_map(|node| { - if let TyAstNodeContent::Declaration(TyDecl::FunctionDecl(FunctionDecl { - decl_id, - subst_list: _, - name, - decl_span, - })) = &node.content + if let TyAstNodeContent::Declaration(TyDecl::FunctionDecl(FunctionDecl { decl_id })) = + &node.content { let fn_decl = decl_engine.get_function(decl_id); + let name = fn_decl.name.clone(); + let span = fn_decl.span.clone(); if fn_decl.is_test() { - return Some(( - fn_decl, - DeclRef::new(name.clone(), *decl_id, decl_span.clone()), - )); + return Some((fn_decl, DeclRef::new(name, *decl_id, span))); } } None diff --git a/sway-core/src/language/ty/program.rs b/sway-core/src/language/ty/program.rs index 743c0956bf5..d801040702b 100644 --- a/sway-core/src/language/ty/program.rs +++ b/sway-core/src/language/ty/program.rs @@ -96,12 +96,7 @@ impl TyProgram { for node in &root.all_nodes { match &node.content { - TyAstNodeContent::Declaration(TyDecl::FunctionDecl(FunctionDecl { - name, - decl_id, - subst_list, - decl_span, - })) => { + TyAstNodeContent::Declaration(TyDecl::FunctionDecl(FunctionDecl { decl_id })) => { let func = decl_engine.get_function(decl_id); match func.kind { @@ -117,12 +112,7 @@ impl TyProgram { }); } - declarations.push(TyDecl::FunctionDecl(FunctionDecl { - name: name.clone(), - decl_id: *decl_id, - subst_list: subst_list.clone(), - decl_span: decl_span.clone(), - })); + declarations.push(TyDecl::FunctionDecl(FunctionDecl { decl_id: *decl_id })); } TyAstNodeContent::Declaration(TyDecl::ConstantDecl(ConstantDecl { decl_id, @@ -160,20 +150,14 @@ impl TyProgram { abi_entries.push(*method_ref.id()); } TyImplItem::Constant(const_ref) => { - let const_decl = decl_engine.get_constant(const_ref); declarations.push(TyDecl::ConstantDecl(ConstantDecl { - name: const_decl.name().clone(), decl_id: *const_ref.id(), - decl_span: const_decl.span.clone(), })); } TyImplItem::Type(type_ref) => { - let type_decl = decl_engine.get_type(type_ref); declarations.push(TyDecl::TraitTypeDecl( TraitTypeDecl { - name: type_decl.name().clone(), decl_id: *type_ref.id(), - decl_span: type_decl.span.clone(), }, )); } @@ -205,10 +189,10 @@ impl TyProgram { .iter() .find(|decl| matches!(decl, TyDecl::StorageDecl { .. })); - if let Some(TyDecl::StorageDecl(StorageDecl { decl_span, .. })) = storage_decl { + if let Some(TyDecl::StorageDecl(StorageDecl { decl_id })) = storage_decl { handler.emit_err(CompileError::StorageDeclarationInNonContract { program_kind: format!("{kind}"), - span: decl_span.clone(), + span: engines.de().get(decl_id).span.clone(), }); } } @@ -218,11 +202,7 @@ impl TyProgram { parsed::TreeType::Contract => { // Types containing raw_ptr are not allowed in storage (e.g Vec) for decl in declarations.iter() { - if let TyDecl::StorageDecl(StorageDecl { - decl_id, - decl_span: _, - }) = decl - { + if let TyDecl::StorageDecl(StorageDecl { decl_id }) = decl { let storage_decl = decl_engine.get_storage(decl_id); for field in storage_decl.fields.iter() { if let Some(error) = get_type_not_allowed_error( diff --git a/sway-core/src/lib.rs b/sway-core/src/lib.rs index 7068da4d8e5..c112e47e2a3 100644 --- a/sway-core/src/lib.rs +++ b/sway-core/src/lib.rs @@ -983,7 +983,7 @@ fn dead_code_analysis<'a>( program: &ty::TyProgram, ) -> Result, ErrorEmitted> { let decl_engine = engines.de(); - let mut dead_code_graph = ControlFlowGraph::default(); + let mut dead_code_graph = ControlFlowGraph::new(engines.clone()); let tree_type = program.kind.tree_type(); module_dead_code_analysis( handler, diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs b/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs index 1ce19b8c5a8..78bb2648b14 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs @@ -1,7 +1,7 @@ use std::collections::HashSet; use sway_error::error::CompileError; -use sway_types::{Ident, Span, Spanned}; +use sway_types::{Ident, Named, Span, Spanned}; use crate::{ decl_engine::{DeclEngineInsert, DeclEngineInsertArc, DeclId}, @@ -91,9 +91,10 @@ impl ty::TyAbiDecl { if let Some(ty::TyDecl::AbiDecl(abi_decl)) = &superabi_impl_method.implementing_type { + let abi_decl = engines.de().get_abi(&abi_decl.decl_id); handler.emit_err(CompileError::AbiShadowsSuperAbiMethod { span: method_name.span(), - superabi: abi_decl.name.clone(), + superabi: abi_decl.name().clone(), }); } } @@ -135,9 +136,7 @@ impl ty::TyAbiDecl { handler, const_name.clone(), ty::TyDecl::ConstantDecl(ty::ConstantDecl { - name: const_name.clone(), decl_id: *decl_ref.id(), - decl_span: const_decl.span.clone(), }), )?; @@ -275,11 +274,12 @@ impl ty::TyAbiDecl { // to place it into Bottom we will encounter // the same method from Top in both Left and Right if self_decl_id != abi_decl.decl_id { + let abi_decl = engines.de().get_abi(&abi_decl.decl_id); handler.emit_err( CompileError::ConflictingSuperAbiMethods { span: subabi_span.clone(), method_name: method.name.to_string(), - superabi1: abi_decl.name.to_string(), + superabi1: abi_decl.name().to_string(), superabi2: self.name.to_string(), }, ); @@ -306,11 +306,10 @@ impl ty::TyAbiDecl { let _ = ctx.namespace_mut().module_mut(engines).write(engines, |m| { m.current_items_mut().insert_symbol( handler, + engines, const_name.clone(), ty::TyDecl::ConstantDecl(ty::ConstantDecl { - name: const_name.clone(), decl_id: *decl_ref.id(), - decl_span: const_decl.span.clone(), }), const_shadowing_mode, generic_shadowing_mode, @@ -346,10 +345,11 @@ impl ty::TyAbiDecl { { // allow the diamond superABI hierarchy if self_decl_id != abi_decl.decl_id { + let abi_decl = engines.de().get_abi(&abi_decl.decl_id); handler.emit_err(CompileError::ConflictingSuperAbiMethods { span: subabi_span.clone(), method_name: method.name.to_string(), - superabi1: abi_decl.name.to_string(), + superabi1: abi_decl.name().to_string(), superabi2: self.name.to_string(), }); } diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs b/sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs index 0b1e7698967..fb305b6aeb1 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs @@ -1,6 +1,7 @@ use crate::{ asm_generation::fuel::compiler_constants::MISMATCHED_SELECTOR_REVERT_CODE, decl_engine::{DeclEngineGet, DeclId, DeclRef}, + engine_threading::SpannedWithEngines, language::{ parsed::{self, AstNodeContent, Declaration, FunctionDeclarationKind}, ty::{self, TyAstNode, TyDecl, TyEnumDecl, TyFunctionDecl, TyStructDecl}, @@ -344,7 +345,7 @@ where } else { *self.ctx.namespace = namespace; Ok(TyAstNode { - span: decl.span(), + span: decl.span(engines), content: ty::TyAstNodeContent::Declaration(decl), }) } @@ -391,7 +392,7 @@ where } else { *self.ctx.namespace = namespace; Ok(TyAstNode { - span: decl.span(), + span: decl.span(engines), content: ty::TyAstNodeContent::Declaration(decl), }) } @@ -408,7 +409,7 @@ where return Some((None, None)); } - let implementing_for_decl_ref = decl.get_struct_decl_ref().unwrap(); + let implementing_for_decl_ref = decl.to_struct_ref(&Handler::default(), engines).unwrap(); let struct_decl = self.ctx.engines().de().get(implementing_for_decl_ref.id()); let module_id = struct_decl.span().source_id().map(|sid| sid.module_id()); @@ -444,7 +445,7 @@ where return Some((None, None)); } - let enum_decl_ref = decl.get_enum_decl_ref().unwrap(); + let enum_decl_ref = decl.to_enum_ref(&Handler::default(), engines).unwrap(); let enum_decl = self.ctx.engines().de().get(enum_decl_ref.id()); let module_id = enum_decl.span().source_id().map(|sid| sid.module_id()); diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs index 5cde1a0f30b..4ca4a44f19c 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs @@ -2,9 +2,7 @@ use sway_error::handler::{ErrorEmitted, Handler}; use sway_types::{BaseIdent, Ident, Named, Spanned}; use crate::{ - decl_engine::{ - DeclEngineGet, DeclEngineInsert, DeclRef, ReplaceFunctionImplementingType, Template, - }, + decl_engine::{DeclEngineGet, DeclEngineInsert, DeclRef, ReplaceFunctionImplementingType}, language::{ parsed, ty::{self, FunctionDecl, TyDecl}, @@ -243,16 +241,13 @@ impl TyDecl { ) .map(|supertrait_decl| { if let ty::TyDecl::TraitDecl(ty::TraitDecl { - name: supertrait_name, decl_id: supertrait_decl_id, - subst_list: _, - decl_span: supertrait_decl_span, }) = supertrait_decl { supertrait.decl_ref = Some(DeclRef::new( - supertrait_name, + engines.de().get(&supertrait_decl_id).name.clone(), supertrait_decl_id, - supertrait_decl_span, + engines.de().get(&supertrait_decl_id).span.clone(), )); } }); @@ -289,16 +284,12 @@ impl TyDecl { let _ = ctx.namespace.module_mut(ctx.engines()).write(engines, |m| { m.current_items_mut().insert_symbol( handler, + engines, Ident::new_no_span(format!( "__contract_entry_{}", decl.name.clone() )), - TyDecl::FunctionDecl(FunctionDecl { - name: decl.name.clone(), - decl_id: *f.id(), - subst_list: Template::default(), - decl_span: f.span(), - }), + TyDecl::FunctionDecl(FunctionDecl { decl_id: *f.id() }), ConstShadowingMode::ItemStyle, GenericShadowingMode::Allow, ) @@ -414,16 +405,13 @@ impl TyDecl { ) .map(|supertrait_decl| { if let ty::TyDecl::TraitDecl(ty::TraitDecl { - name: supertrait_name, decl_id: supertrait_decl_id, - subst_list: _, - decl_span: supertrait_decl_span, }) = supertrait_decl { supertrait.decl_ref = Some(DeclRef::new( - supertrait_name, + engines.de().get(&supertrait_decl_id).name.clone(), supertrait_decl_id, - supertrait_decl_span, + engines.de().get(&supertrait_decl_id).span.clone(), )); } }); diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs b/sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs index 381bd96f586..127dc2d154a 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs @@ -110,6 +110,7 @@ impl ty::TyFunctionParameter { let _ = ctx.namespace_mut().module_mut(engines).write(engines, |m| { m.current_items_mut().insert_symbol( handler, + engines, self.name.clone(), ty::TyDecl::VariableDecl(Box::new(ty::TyVariableDecl { name: self.name.clone(), 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 4f1ee44fbad..a734a030d30 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 @@ -416,9 +416,7 @@ impl TyImplTrait { handler, decl_ref.name().clone(), ty::TyDecl::ConstantDecl(ty::ConstantDecl { - name: decl_ref.name().clone(), decl_id: *decl_ref.id(), - decl_span: decl_ref.span().clone(), }), )?; } 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 3d99e64924a..78818373702 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs @@ -154,9 +154,7 @@ impl TyTraitDecl { handler, const_name.clone(), ty::TyDecl::ConstantDecl(ty::ConstantDecl { - name: const_name.clone(), decl_id: *decl_ref.id(), - decl_span: const_decl.span.clone(), }), )?; @@ -451,11 +449,10 @@ impl TyTraitDecl { let _ = ctx.namespace_mut().module_mut(engines).write(engines, |m| { m.current_items_mut().insert_symbol( handler, + engines, const_name.clone(), ty::TyDecl::ConstantDecl(ty::ConstantDecl { - name: const_name.clone(), decl_id: *decl_ref.id(), - decl_span: const_decl.span.clone(), }), const_shadowing_mode, generic_shadowing_mode, diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/trait_fn.rs b/sway-core/src/semantic_analysis/ast_node/declaration/trait_fn.rs index 421a234b007..7cb1c7367b7 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/trait_fn.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/trait_fn.rs @@ -1,4 +1,4 @@ -use sway_types::{Span, Spanned}; +use sway_types::Spanned; use crate::{ decl_engine::DeclId, @@ -90,15 +90,13 @@ impl ty::TyTraitFn { body: ty::TyCodeBlock::default(), parameters: self.parameters.clone(), implementing_type: match abi_mode.clone() { - AbiMode::ImplAbiFn(abi_name, abi_decl_id) => { + AbiMode::ImplAbiFn(_abi_name, abi_decl_id) => { // ABI and their super-ABI methods cannot have the same names, // so in order to provide meaningful error messages if this condition // is violated, we need to keep track of ABI names before we can // provide type-checked `AbiDecl`s Some(ty::TyDecl::AbiDecl(ty::AbiDecl { - name: abi_name, decl_id: abi_decl_id.unwrap_or(DeclId::dummy()), - decl_span: Span::dummy(), })) } AbiMode::NonAbi => None, diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs index d96db5e9547..55322d3bce7 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs @@ -1626,11 +1626,10 @@ impl ty::TyExpression { ctx.self_type(), )?; let abi_ref = match abi { - ty::TyDecl::AbiDecl(ty::AbiDecl { - name, - decl_id, - decl_span, - }) => DeclRef::new(name, decl_id, decl_span), + ty::TyDecl::AbiDecl(ty::AbiDecl { decl_id }) => { + let abi_decl = engines.de().get(&decl_id); + DeclRef::new(abi_decl.name().clone(), decl_id, abi_decl.span.clone()) + } ty::TyDecl::VariableDecl(ref decl) => { let ty::TyVariableDecl { body: expr, .. } = &**decl; let ret_ty = type_engine.get(expr.return_type); @@ -1652,7 +1651,7 @@ impl ty::TyExpression { abi_name, ctx.self_type(), )?; - unknown_decl.to_abi_ref(handler)? + unknown_decl.to_abi_ref(handler, engines)? } AbiName::Deferred => { return Ok(ty::TyExpression { @@ -2143,7 +2142,7 @@ impl ty::TyExpression { decl => { return Err(handler.emit_err( CompileError::DeclAssignmentTargetCannotBeAssignedTo { - decl_name: decl.get_decl_ident(), + decl_name: decl.get_decl_ident(ctx.engines), decl_friendly_type_name: decl .friendly_type_name_with_acronym(), lhs_span, diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs index c6b3d563f69..70b536873af 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs @@ -297,7 +297,9 @@ pub(crate) fn type_check_method_application( let is_decl_mutable = match unknown_decl { ty::TyDecl::ConstantDecl { .. } => false, _ => { - let variable_decl = unknown_decl.expect_variable(handler).cloned()?; + let variable_decl = unknown_decl + .expect_variable(handler, ctx.engines()) + .cloned()?; variable_decl.mutability.is_mutable() } }; diff --git a/sway-core/src/semantic_analysis/module.rs b/sway-core/src/semantic_analysis/module.rs index e7287c57a1b..b75215a1099 100644 --- a/sway-core/src/semantic_analysis/module.rs +++ b/sway-core/src/semantic_analysis/module.rs @@ -320,7 +320,8 @@ impl ty::TyModule { if ctx.experimental.new_encoding { let main_decl = all_nodes.iter_mut().find_map(|x| match &mut x.content { ty::TyAstNodeContent::Declaration(ty::TyDecl::FunctionDecl(decl)) => { - (decl.name.as_str() == "main").then(|| engines.de().get(&decl.decl_id)) + let fn_decl = engines.de().get_function(&decl.decl_id); + (fn_decl.name.as_str() == "main").then_some(fn_decl) } _ => None, }); diff --git a/sway-core/src/semantic_analysis/namespace/lexical_scope.rs b/sway-core/src/semantic_analysis/namespace/lexical_scope.rs index e84288275ec..036affd234a 100644 --- a/sway-core/src/semantic_analysis/namespace/lexical_scope.rs +++ b/sway-core/src/semantic_analysis/namespace/lexical_scope.rs @@ -149,11 +149,13 @@ impl Items { pub(crate) fn insert_symbol( &mut self, handler: &Handler, + engines: &Engines, name: Ident, item: ty::TyDecl, const_shadowing_mode: ConstShadowingMode, generic_shadowing_mode: GenericShadowingMode, ) -> Result<(), ErrorEmitted> { + let decl_engine = engines.de(); let append_shadowing_error = |ident: &Ident, decl: &ty::TyDecl, @@ -186,7 +188,7 @@ impl Items { name: (&name).into(), constant_span: constant_ident.span(), constant_decl: if is_imported_constant { - constant_decl.decl_span.clone() + decl_engine.get(&constant_decl.decl_id).span.clone() } else { Span::dummy() }, @@ -208,7 +210,7 @@ impl Items { name: (&name).into(), constant_span: constant_ident.span(), constant_decl: if is_imported_constant { - constant_decl.decl_span.clone() + decl_engine.get(&constant_decl.decl_id).span.clone() } else { Span::dummy() }, diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index 917c46777d2..977ca36ef84 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -16,7 +16,7 @@ use sway_error::{ error::CompileError, handler::{ErrorEmitted, Handler}, }; -use sway_types::Spanned; +use sway_types::{Named, Spanned}; use sway_utils::iter_prefixes; pub enum ResolvedDeclaration { @@ -146,7 +146,7 @@ impl Root { ); } // if this is a trait, import its implementations - let decl_span = decl.span(); + let decl_span = decl.span(engines); if let TyDecl::TraitDecl(_) = &decl { // TODO: we only import local impls from the source namespace // this is okay for now but we'll need to device some mechanism to collect all available trait impls @@ -227,12 +227,7 @@ impl Root { }); } - if let TyDecl::EnumDecl(ty::EnumDecl { - decl_id, - subst_list: _, - .. - }) = decl - { + if let TyDecl::EnumDecl(ty::EnumDecl { decl_id, .. }) = decl { let enum_decl = decl_engine.get_enum(&decl_id); let enum_ref = DeclRef::new( enum_decl.call_path.suffix.clone(), @@ -333,12 +328,7 @@ impl Root { }); } - if let TyDecl::EnumDecl(ty::EnumDecl { - decl_id, - subst_list: _, - .. - }) = decl - { + if let TyDecl::EnumDecl(ty::EnumDecl { decl_id, .. }) = decl { let enum_decl = decl_engine.get_enum(&decl_id); let enum_ref = DeclRef::new( enum_decl.call_path.suffix.clone(), @@ -722,16 +712,22 @@ impl Root { match decl { ResolvedDeclaration::Parsed(_decl) => todo!(), ResolvedDeclaration::Typed(decl) => Ok(match decl.clone() { - ty::TyDecl::StructDecl(struct_decl) => TypeInfo::Struct(DeclRef::new( - struct_decl.name.clone(), - struct_decl.decl_id, - struct_decl.name.span(), - )), - ty::TyDecl::EnumDecl(enum_decl) => TypeInfo::Enum(DeclRef::new( - enum_decl.name.clone(), - enum_decl.decl_id, - enum_decl.name.span(), - )), + ty::TyDecl::StructDecl(struct_ty_decl) => { + let struct_decl = engines.de().get_struct(&struct_ty_decl.decl_id); + TypeInfo::Struct(DeclRef::new( + struct_decl.name().clone(), + struct_ty_decl.decl_id, + struct_decl.span().clone(), + )) + } + ty::TyDecl::EnumDecl(enum_ty_decl) => { + let enum_decl = engines.de().get_enum(&enum_ty_decl.decl_id); + TypeInfo::Enum(DeclRef::new( + enum_decl.name().clone(), + enum_ty_decl.decl_id, + enum_decl.span().clone(), + )) + } ty::TyDecl::TraitTypeDecl(type_decl) => { let type_decl = engines.de().get_type(&type_decl.decl_id); (*engines.te().get(type_decl.ty.clone().unwrap().type_id)).clone() diff --git a/sway-core/src/semantic_analysis/program.rs b/sway-core/src/semantic_analysis/program.rs index b457d4e7bc6..d6705717e6e 100644 --- a/sway-core/src/semantic_analysis/program.rs +++ b/sway-core/src/semantic_analysis/program.rs @@ -104,11 +104,7 @@ impl TyProgram { // Expecting at most a single storage declaration match storage_decl { - Some(ty::TyDecl::StorageDecl(ty::StorageDecl { - decl_id, - decl_span: _, - .. - })) => { + Some(ty::TyDecl::StorageDecl(ty::StorageDecl { decl_id, .. })) => { let decl = decl_engine.get_storage(decl_id); let mut storage_slots = decl.get_initialized_storage_slots( handler, engines, context, md_mgr, module, diff --git a/sway-core/src/semantic_analysis/type_check_analysis.rs b/sway-core/src/semantic_analysis/type_check_analysis.rs index 8180ccce2c8..41b260538de 100644 --- a/sway-core/src/semantic_analysis/type_check_analysis.rs +++ b/sway-core/src/semantic_analysis/type_check_analysis.rs @@ -9,6 +9,7 @@ use petgraph::stable_graph::NodeIndex; use petgraph::Graph; use sway_error::error::CompileError; use sway_error::handler::{ErrorEmitted, Handler}; +use sway_types::Named; use crate::decl_engine::{AssociatedItemDeclId, DeclId, DeclUniqueId}; use crate::engine_threading::DebugWithEngines; @@ -371,7 +372,8 @@ impl DebugWithEngines for TyNodeDepGraphNode { format!("{:?}", str) } TyNodeDepGraphNode::ImplTrait { node } => { - format!("{:?}", node.name.as_str()) + let decl = engines.de().get_impl_trait(&node.decl_id); + format!("{:?}", decl.name().as_str()) } TyNodeDepGraphNode::Fn { node } => { let fn_decl = engines.de().get_function(node); diff --git a/sway-core/src/semantic_analysis/type_check_context.rs b/sway-core/src/semantic_analysis/type_check_context.rs index db0280630b7..8aecf62b023 100644 --- a/sway-core/src/semantic_analysis/type_check_context.rs +++ b/sway-core/src/semantic_analysis/type_check_context.rs @@ -530,12 +530,13 @@ impl<'a> TypeCheckContext<'a> { ) -> Result<(), ErrorEmitted> { let const_shadowing_mode = self.const_shadowing_mode; let generic_shadowing_mode = self.generic_shadowing_mode; - let engines = self.engines; + let engines = self.engines(); self.namespace_mut() .module_mut(engines) .current_items_mut() .insert_symbol( handler, + engines, name, item, const_shadowing_mode, @@ -986,11 +987,7 @@ impl<'a> TypeCheckContext<'a> { type_id, .. })) => type_id, - Some(ty::TyDecl::TraitTypeDecl(ty::TraitTypeDecl { - decl_id, - name, - decl_span: _, - })) => { + Some(ty::TyDecl::TraitTypeDecl(ty::TraitTypeDecl { decl_id })) => { let decl_type = decl_engine.get_type(&decl_id); if let Some(ty) = &decl_type.ty { @@ -999,10 +996,10 @@ impl<'a> TypeCheckContext<'a> { type_engine.insert( self.engines, TypeInfo::TraitType { - name: name.clone(), + name: decl_type.name.clone(), trait_type_id: implementing_type, }, - name.span().source_id(), + decl_type.name.span().source_id(), ) } else { return Err(handler.emit_err(CompileError::Internal( diff --git a/sway-core/src/type_system/ast_elements/binding.rs b/sway-core/src/type_system/ast_elements/binding.rs index 24d778cee79..80b1dcb261b 100644 --- a/sway-core/src/type_system/ast_elements/binding.rs +++ b/sway-core/src/type_system/ast_elements/binding.rs @@ -238,7 +238,7 @@ impl TypeCheckTypeBinding for TypeBinding { // Grab the declaration. let unknown_decl = ctx.resolve_call_path_with_visibility_check(handler, &self.inner)?; // Check to see if this is a fn declaration. - let fn_ref = unknown_decl.to_fn_ref(handler)?; + let fn_ref = unknown_decl.to_fn_ref(handler, ctx.engines())?; // Get a new copy from the declaration engine. let mut new_copy = (*decl_engine.get_function(fn_ref.id())).clone(); match self.type_arguments { @@ -381,7 +381,7 @@ impl TypeBinding { ctx.resolve_qualified_call_path_with_visibility_check(handler, &self.inner)?; // Check to see if this is a const declaration. - let const_ref = unknown_decl.to_const_ref(handler)?; + let const_ref = unknown_decl.to_const_ref(handler, ctx.engines())?; Ok(const_ref) } @@ -404,7 +404,7 @@ impl TypeCheckTypeBinding for TypeBinding { let unknown_decl = ctx.resolve_call_path_with_visibility_check(handler, &self.inner)?; // Check to see if this is a const declaration. - let const_ref = unknown_decl.to_const_ref(handler)?; + let const_ref = unknown_decl.to_const_ref(handler, ctx.engines())?; Ok((const_ref, None, None)) } diff --git a/sway-core/src/type_system/ast_elements/type_parameter.rs b/sway-core/src/type_system/ast_elements/type_parameter.rs index 9f3035afef8..6a146fcb532 100644 --- a/sway-core/src/type_system/ast_elements/type_parameter.rs +++ b/sway-core/src/type_system/ast_elements/type_parameter.rs @@ -184,6 +184,7 @@ impl TypeParameter { .current_items_mut() .insert_symbol( handler, + engines, name_a, type_parameter_decl.clone(), const_shadowing_mode, @@ -195,6 +196,7 @@ impl TypeParameter { .current_items_mut() .insert_symbol( handler, + engines, name_b, type_parameter_decl, const_shadowing_mode, diff --git a/sway-core/src/type_system/priv_prelude.rs b/sway-core/src/type_system/priv_prelude.rs index db124603ce1..57f8b6ceefb 100644 --- a/sway-core/src/type_system/priv_prelude.rs +++ b/sway-core/src/type_system/priv_prelude.rs @@ -6,10 +6,7 @@ pub(crate) use super::{ create_type_id::CreateTypeId, }, info::VecSet, - substitute::{ - subst_list::SubstList, subst_map::TypeSubstMap, subst_types::HasChanges, - subst_types::SubstTypes, - }, + substitute::{subst_map::TypeSubstMap, subst_types::HasChanges, subst_types::SubstTypes}, unify::unify_check::UnifyCheck, }; diff --git a/sway-core/src/type_system/substitute/mod.rs b/sway-core/src/type_system/substitute/mod.rs index 373971ca5e6..bf3bbc61968 100644 --- a/sway-core/src/type_system/substitute/mod.rs +++ b/sway-core/src/type_system/substitute/mod.rs @@ -1,3 +1,2 @@ -pub(crate) mod subst_list; pub(crate) mod subst_map; pub(crate) mod subst_types; diff --git a/sway-lsp/benches/lsp_benchmarks/token_map.rs b/sway-lsp/benches/lsp_benchmarks/token_map.rs index cfd4d9286b1..79182e239a2 100644 --- a/sway-lsp/benches/lsp_benchmarks/token_map.rs +++ b/sway-lsp/benches/lsp_benchmarks/token_map.rs @@ -27,7 +27,7 @@ fn benchmarks(c: &mut Criterion) { b.iter(|| { session .token_map() - .tokens_at_position(engines.se(), &uri, position, None) + .tokens_at_position(&engines, &uri, position, None) }) }); @@ -39,7 +39,7 @@ fn benchmarks(c: &mut Criterion) { b.iter(|| { session .token_map() - .parent_decl_at_position(engines.se(), &uri, position) + .parent_decl_at_position(&engines, &uri, position) }) }); } diff --git a/sway-lsp/src/capabilities/hover/hover_link_contents.rs b/sway-lsp/src/capabilities/hover/hover_link_contents.rs index a24c95a618e..f0dc74f8c39 100644 --- a/sway-lsp/src/capabilities/hover/hover_link_contents.rs +++ b/sway-lsp/src/capabilities/hover/hover_link_contents.rs @@ -4,6 +4,7 @@ use crate::{ }; use std::sync::Arc; use sway_core::{ + engine_threading::SpannedWithEngines, language::{ ty::{TyDecl, TyTraitDecl}, CallPath, @@ -107,7 +108,7 @@ impl<'a> HoverLinkContents<'a> { .module(self.engines) .current_items() .get_impl_spans_for_decl(self.engines, ty_decl); - self.add_implementations(&ty_decl.span(), impl_spans); + self.add_implementations(&ty_decl.span(self.engines), impl_spans); } } diff --git a/sway-lsp/src/capabilities/rename.rs b/sway-lsp/src/capabilities/rename.rs index b1c414ac74b..693d6977708 100644 --- a/sway-lsp/src/capabilities/rename.rs +++ b/sway-lsp/src/capabilities/rename.rs @@ -191,7 +191,7 @@ fn find_all_methods_for_decl<'a>( // Find the parent declaration let t = session .token_map() - .parent_decl_at_position(engines.se(), url, position) + .parent_decl_at_position(engines, url, position) .ok_or(RenameError::TokenNotFound)?; let decl_token = t.value(); diff --git a/sway-lsp/src/core/session.rs b/sway-lsp/src/core/session.rs index caf63aadc1c..feccd818199 100644 --- a/sway-lsp/src/core/session.rs +++ b/sway-lsp/src/core/session.rs @@ -180,7 +180,7 @@ impl Session { let engines = self.engines.read(); let fn_tokens = self.token_map - .tokens_at_position(engines.se(), uri, shifted_position, Some(true)); + .tokens_at_position(&engines, uri, shifted_position, Some(true)); let fn_token = fn_tokens.first()?.value(); let compiled_program = &*self.compiled_program.read(); if let Some(TypedAstToken::TypedFunctionDeclaration(fn_decl)) = fn_token.typed.clone() { diff --git a/sway-lsp/src/core/token_map.rs b/sway-lsp/src/core/token_map.rs index 9177a9bf6fb..c827220f8bf 100644 --- a/sway-lsp/src/core/token_map.rs +++ b/sway-lsp/src/core/token_map.rs @@ -9,8 +9,8 @@ use dashmap::{ }; use lsp_types::{Position, Url}; use std::{thread, time::Duration}; -use sway_core::{language::ty, type_system::TypeId, Engines}; -use sway_types::{Ident, SourceEngine, Spanned}; +use sway_core::{engine_threading::SpannedWithEngines, language::ty, type_system::TypeId, Engines}; +use sway_types::Ident; // Re-export the TokenMapExt trait. pub use crate::core::token_map_ext::TokenMapExt; @@ -129,11 +129,11 @@ impl<'a> TokenMap { /// For example, if the cursor is inside a function body, this function returns the function declaration. pub fn parent_decl_at_position<'s>( &'s self, - source_engine: &'s SourceEngine, + engines: &'s Engines, uri: &'s Url, position: Position, ) -> Option> { - self.tokens_at_position(source_engine, uri, position, None) + self.tokens_at_position(engines, uri, position, None) .into_iter() .find_map(|entry| { let (_, token) = entry.pair(); @@ -166,11 +166,12 @@ impl<'a> TokenMap { /// of the function declaration (the function name). pub fn tokens_at_position<'s>( &'s self, - source_engine: &'s SourceEngine, + engines: &'s Engines, uri: &'s Url, position: Position, functions_only: Option, ) -> Vec> { + let source_engine = engines.se(); self.tokens_for_file(uri) .filter_map(move |entry| { let (ident, token) = entry.pair(); @@ -181,7 +182,7 @@ impl<'a> TokenMap { TokenIdent::new(&Ident::new(decl.span.clone()), source_engine) } Some(TypedAstToken::TypedDeclaration(decl)) => { - TokenIdent::new(&Ident::new(decl.span()), source_engine) + TokenIdent::new(&Ident::new(decl.span(engines)), source_engine) } _ => ident.clone(), }; diff --git a/sway-lsp/src/traverse/dependency.rs b/sway-lsp/src/traverse/dependency.rs index dc123f4f003..d270f04f9ef 100644 --- a/sway-lsp/src/traverse/dependency.rs +++ b/sway-lsp/src/traverse/dependency.rs @@ -6,6 +6,7 @@ use sway_core::language::{ parsed::{AstNode, AstNodeContent, Declaration}, ty, }; +use sway_types::Named; /// Insert Declaration tokens into the TokenMap. pub fn collect_parsed_declaration(node: &AstNode, ctx: &ParseContext) { @@ -51,12 +52,22 @@ pub fn collect_typed_declaration(node: &ty::TyAstNode, ctx: &ParseContext) { let typed_token = TypedAstToken::TypedDeclaration(declaration.clone()); let ident = match declaration { + ty::TyDecl::ConstantDecl(ty::ConstantDecl { decl_id }) => { + ctx.engines.de().get_constant(decl_id).name().clone() + } + ty::TyDecl::FunctionDecl(ty::FunctionDecl { decl_id }) => { + ctx.engines.de().get_function(decl_id).name().clone() + } + ty::TyDecl::TraitDecl(ty::TraitDecl { decl_id }) => { + ctx.engines.de().get_trait(decl_id).name().clone() + } + ty::TyDecl::StructDecl(ty::StructDecl { decl_id }) => { + ctx.engines.de().get_struct(decl_id).name().clone() + } + ty::TyDecl::EnumDecl(ty::EnumDecl { decl_id }) => { + ctx.engines.de().get_enum(decl_id).name().clone() + } ty::TyDecl::VariableDecl(variable) => variable.name.clone(), - ty::TyDecl::StructDecl(ty::StructDecl { name, .. }) - | ty::TyDecl::EnumDecl(ty::EnumDecl { name, .. }) - | ty::TyDecl::TraitDecl(ty::TraitDecl { name, .. }) - | ty::TyDecl::FunctionDecl(ty::FunctionDecl { name, .. }) - | ty::TyDecl::ConstantDecl(ty::ConstantDecl { name, .. }) => name.clone(), _ => return, }; diff --git a/sway-lsp/src/traverse/typed_tree.rs b/sway-lsp/src/traverse/typed_tree.rs index 2000bba4e62..abd8fd10926 100644 --- a/sway-lsp/src/traverse/typed_tree.rs +++ b/sway-lsp/src/traverse/typed_tree.rs @@ -130,7 +130,7 @@ impl Parse for ty::TySideEffect { .namespace .submodule(ctx.engines, call_path) .and_then(|module| module.current_items().symbols().get(item)) - .and_then(GetDeclIdent::get_decl_ident) + .and_then(|decl| decl.get_decl_ident(ctx.engines)) { // Update the symbol kind to match the declarations symbol kind if let Some(decl) = @@ -227,7 +227,7 @@ impl Parse for ty::TyExpression { let implementing_type_name = (*ctx.engines.de().get_function(fn_ref)) .clone() .implementing_type - .and_then(|impl_type| impl_type.get_decl_ident()); + .and_then(|impl_type| impl_type.get_decl_ident(ctx.engines)); 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 if let Some((last, prefixes)) = call_path.prefixes.split_last() { @@ -446,7 +446,7 @@ impl Parse for ty::TyExpression { .namespace .submodule(ctx.engines, &abi_name.prefixes) .and_then(|module| module.current_items().symbols().get(&abi_name.suffix)) - .and_then(GetDeclIdent::get_decl_ident) + .and_then(|decl| decl.get_decl_ident(ctx.engines)) { token.type_def = Some(TypeDefinition::Ident(abi_def_ident)); } @@ -594,7 +594,7 @@ impl Parse for ty::ConstantDecl { impl Parse for ty::TraitTypeDecl { fn parse(&self, ctx: &ParseContext) { let type_decl = ctx.engines.de().get_type(&self.decl_id); - collect_trait_type_decl(ctx, &type_decl, &self.decl_span); + collect_trait_type_decl(ctx, &type_decl, &type_decl.span); } } @@ -1164,7 +1164,7 @@ fn collect_call_path_tree(ctx: &ParseContext, tree: &CallPathTree, type_arg: &Ty .symbols() .get(&abi_call_path.call_path.suffix) }) - .and_then(GetDeclIdent::get_decl_ident) + .and_then(|decl| decl.get_decl_ident(ctx.engines)) { token.type_def = Some(TypeDefinition::Ident(abi_def_ident)); } @@ -1342,7 +1342,7 @@ fn collect_trait_constraint( .namespace .submodule(ctx.engines, &trait_name.prefixes) .and_then(|module| module.current_items().symbols().get(&trait_name.suffix)) - .and_then(GetDeclIdent::get_decl_ident) + .and_then(|decl| decl.get_decl_ident(ctx.engines)) { token.type_def = Some(TypeDefinition::Ident(trait_def_ident)); }