From 06a6e94819a62e59098c48ab30f68ceeadf722fa Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 19 Mar 2024 09:00:09 +0000 Subject: [PATCH 01/19] Remove duplicate/unused methods from `TyDecl`. --- .../language/ty/declaration/declaration.rs | 61 +------------------ .../ast_node/declaration/auto_impl.rs | 4 +- 2 files changed, 3 insertions(+), 62 deletions(-) diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index d6c3d2045d7..d1bf1519989 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -333,62 +333,6 @@ 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 { match self { @@ -651,10 +595,7 @@ impl TyDecl { /// Retrieves the declaration as a `DeclRef>`. /// /// Returns an error if `self` is not the [TyDecl][FunctionDecl] variant. - pub(crate) fn to_fn_ref( - &self, - handler: &Handler, - ) -> Result>, ErrorEmitted> { + pub(crate) fn to_fn_ref(&self, handler: &Handler) -> Result { match self { TyDecl::FunctionDecl(FunctionDecl { name, 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..90594ef3125 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 @@ -408,7 +408,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 +444,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()); From 14df23f1484fa9bb9c034b8ec9cf4f4ceefbd5df Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 19 Mar 2024 09:01:34 +0000 Subject: [PATCH 02/19] Remove unused `subst_list` from `TraitTypeDecl`. --- sway-core/src/language/ty/declaration/declaration.rs | 3 --- sway-core/src/language/ty/module.rs | 1 - sway-core/src/language/ty/program.rs | 2 -- .../semantic_analysis/ast_node/declaration/declaration.rs | 5 +---- 4 files changed, 1 insertion(+), 10 deletions(-) diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index d1bf1519989..c7ec657f622 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -55,7 +55,6 @@ pub struct TraitTypeDecl { pub struct FunctionDecl { pub name: Ident, pub decl_id: DeclId, - pub subst_list: Template, pub decl_span: Span, } @@ -600,7 +599,6 @@ impl TyDecl { TyDecl::FunctionDecl(FunctionDecl { name, decl_id, - subst_list: _, decl_span, }) => Ok(DeclRef::new(name.clone(), *decl_id, decl_span.clone())), TyDecl::ErrorRecovery(_, err) => Err(*err), @@ -874,7 +872,6 @@ impl From>> for TyDecl { 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(), }) } diff --git a/sway-core/src/language/ty/module.rs b/sway-core/src/language/ty/module.rs index b3b5c44d2f1..8eb2870b39c 100644 --- a/sway-core/src/language/ty/module.rs +++ b/sway-core/src/language/ty/module.rs @@ -55,7 +55,6 @@ impl TyModule { self.all_nodes.iter().filter_map(|node| { if let TyAstNodeContent::Declaration(TyDecl::FunctionDecl(FunctionDecl { decl_id, - subst_list: _, name, decl_span, })) = &node.content diff --git a/sway-core/src/language/ty/program.rs b/sway-core/src/language/ty/program.rs index 743c0956bf5..caafe436813 100644 --- a/sway-core/src/language/ty/program.rs +++ b/sway-core/src/language/ty/program.rs @@ -99,7 +99,6 @@ impl TyProgram { TyAstNodeContent::Declaration(TyDecl::FunctionDecl(FunctionDecl { name, decl_id, - subst_list, decl_span, })) => { let func = decl_engine.get_function(decl_id); @@ -120,7 +119,6 @@ impl TyProgram { declarations.push(TyDecl::FunctionDecl(FunctionDecl { name: name.clone(), decl_id: *decl_id, - subst_list: subst_list.clone(), decl_span: decl_span.clone(), })); } 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 764c2b74299..bec4824383f 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}, @@ -297,7 +295,6 @@ impl TyDecl { TyDecl::FunctionDecl(FunctionDecl { name: decl.name.clone(), decl_id: *f.id(), - subst_list: Template::default(), decl_span: f.span(), }), ConstShadowingMode::ItemStyle, From 646dbb81cc78841e826807750774f02bfde7b488 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 19 Mar 2024 09:03:02 +0000 Subject: [PATCH 03/19] Remove unused `subst_list` from `TraitDecl`. --- sway-core/src/language/ty/declaration/declaration.rs | 2 -- .../src/semantic_analysis/ast_node/declaration/declaration.rs | 2 -- 2 files changed, 4 deletions(-) diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index c7ec657f622..c58fa8b893a 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -62,7 +62,6 @@ pub struct FunctionDecl { pub struct TraitDecl { pub name: Ident, pub decl_id: DeclId, - pub subst_list: Template, pub decl_span: Span, } @@ -882,7 +881,6 @@ impl From>> for TyDecl { 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(), }) } 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 bec4824383f..b6680fd9864 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs @@ -244,7 +244,6 @@ impl TyDecl { if let ty::TyDecl::TraitDecl(ty::TraitDecl { name: supertrait_name, decl_id: supertrait_decl_id, - subst_list: _, decl_span: supertrait_decl_span, }) = supertrait_decl { @@ -414,7 +413,6 @@ impl TyDecl { if let ty::TyDecl::TraitDecl(ty::TraitDecl { name: supertrait_name, decl_id: supertrait_decl_id, - subst_list: _, decl_span: supertrait_decl_span, }) = supertrait_decl { From 6cd9d88eb3fbb9447c62e64d5619fd205449dcb2 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 19 Mar 2024 09:04:30 +0000 Subject: [PATCH 04/19] Remove unused `subst_list` from `ImplTrait`. --- sway-core/src/language/ty/declaration/declaration.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index c58fa8b893a..b7a2ea6bda7 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -92,7 +92,6 @@ pub struct EnumVariantDecl { pub struct ImplTrait { pub name: Ident, pub decl_id: DeclId, - pub subst_list: Template, pub decl_span: Span, } @@ -891,7 +890,6 @@ impl From>> for TyDecl { 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(), }) } From 924514f8761135a92159b5bb98e93369c4f12158 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 19 Mar 2024 09:05:40 +0000 Subject: [PATCH 05/19] Remove unused `subst_list` from `EnumDecl`. --- .../src/language/ty/declaration/declaration.rs | 4 ---- sway-core/src/semantic_analysis/namespace/root.rs | 14 ++------------ 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index b7a2ea6bda7..f696b5b3da3 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -77,7 +77,6 @@ pub struct StructDecl { pub struct EnumDecl { pub name: Ident, pub decl_id: DeclId, - pub subst_list: Template, pub decl_span: Span, } @@ -529,7 +528,6 @@ impl TyDecl { TyDecl::EnumDecl(EnumDecl { name, decl_id, - subst_list: _, decl_span, }) => Ok(DeclRef::new(name.clone(), *decl_id, decl_span.clone())), TyDecl::TypeAliasDecl(TypeAliasDecl { decl_id, .. }) => { @@ -767,7 +765,6 @@ impl TyDecl { TyDecl::EnumDecl(EnumDecl { name, decl_id, - subst_list: _, decl_span, }) => type_engine.insert( engines, @@ -859,7 +856,6 @@ impl From>> for TyDecl { 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(), }) } diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index 917c46777d2..8d5847e2299 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -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(), From 0e8d58a112bf3f737cc20bbdf9e3a5b91743a37b Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 19 Mar 2024 09:06:36 +0000 Subject: [PATCH 06/19] Remove unused `subst_list` from `StructDecl`. --- sway-core/src/language/ty/declaration/declaration.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index f696b5b3da3..8980c63506c 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -69,7 +69,6 @@ pub struct TraitDecl { pub struct StructDecl { pub name: Ident, pub decl_id: DeclId, - pub subst_list: Template, pub decl_span: Span, } @@ -568,7 +567,6 @@ impl TyDecl { TyDecl::StructDecl(StructDecl { name, decl_id, - subst_list: _, decl_span, }) => Ok(DeclRef::new(name.clone(), *decl_id, decl_span.clone())), TyDecl::TypeAliasDecl(TypeAliasDecl { decl_id, .. }) => { @@ -755,7 +753,6 @@ impl TyDecl { TyDecl::StructDecl(StructDecl { name, decl_id, - subst_list: _, decl_span, }) => type_engine.insert( engines, @@ -896,7 +893,6 @@ impl From>> for TyDecl { 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(), }) } From 7f51813c56a57e148a6ba659937be274233f1a26 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Thu, 2 May 2024 18:21:30 +0100 Subject: [PATCH 07/19] Remove subst list. --- sway-core/src/decl_engine/mod.rs | 2 -- sway-core/src/decl_engine/ref.rs | 13 -------- sway-core/src/decl_engine/template.rs | 37 --------------------- sway-core/src/type_system/priv_prelude.rs | 5 +-- sway-core/src/type_system/substitute/mod.rs | 1 - 5 files changed, 1 insertion(+), 57 deletions(-) delete mode 100644 sway-core/src/decl_engine/template.rs 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/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; From 26b1f1dffcfb1e9d3ddcdda89904be14d9822d6c Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Thu, 2 May 2024 18:23:57 +0100 Subject: [PATCH 08/19] Refactor `GetDeclIdent` to take engines. --- .../analyze_return_paths.rs | 2 +- .../control_flow_analysis/flow_graph/mod.rs | 24 +++++++++++++++---- sway-core/src/language/ty/ast_node.rs | 10 ++++---- .../language/ty/declaration/declaration.rs | 6 ++--- sway-core/src/lib.rs | 2 +- .../ast_node/expression/typed_expression.rs | 2 +- sway-lsp/src/traverse/typed_tree.rs | 10 ++++---- 7 files changed, 35 insertions(+), 21 deletions(-) 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/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/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 8980c63506c..049b694599d 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -492,7 +492,7 @@ 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::VariableDecl(decl) => Some(decl.name.clone()), TyDecl::FunctionDecl(FunctionDecl { name, .. }) @@ -674,13 +674,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()), } } diff --git a/sway-core/src/lib.rs b/sway-core/src/lib.rs index 1cb652ed746..e8f50b9e764 100644 --- a/sway-core/src/lib.rs +++ b/sway-core/src/lib.rs @@ -991,7 +991,7 @@ fn dead_code_analysis<'a>( program: &ty::TyProgram, ) -> Result, ErrorEmitted> { let decl_engine = engines.de(); - let mut dead_code_graph = Default::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/expression/typed_expression.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs index 7b4ecfb2b58..0a410985cf0 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 @@ -2145,7 +2145,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-lsp/src/traverse/typed_tree.rs b/sway-lsp/src/traverse/typed_tree.rs index 2000bba4e62..f68e8bdc6aa 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)); } @@ -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)); } From 4758927a369ce4d07edde21dacb40c9f89daf204 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Thu, 2 May 2024 18:40:14 +0100 Subject: [PATCH 09/19] Introduce a `SpannedWithEngines` trait. --- sway-core/src/engine_threading.rs | 6 +++- sway-core/src/ir_generation/const_eval.rs | 2 +- .../language/ty/declaration/declaration.rs | 29 ++++++++++++------- .../ast_node/declaration/auto_impl.rs | 5 ++-- .../ast_node/expression/typed_expression.rs | 2 +- .../typed_expression/method_application.rs | 4 ++- .../src/semantic_analysis/namespace/root.rs | 2 +- .../semantic_analysis/type_check_context.rs | 2 +- .../src/type_system/ast_elements/binding.rs | 6 ++-- sway-lsp/benches/lsp_benchmarks/token_map.rs | 4 +-- .../capabilities/hover/hover_link_contents.rs | 3 +- sway-lsp/src/capabilities/rename.rs | 2 +- sway-lsp/src/core/session.rs | 2 +- sway-lsp/src/core/token_map.rs | 13 +++++---- 14 files changed, 49 insertions(+), 33 deletions(-) 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 9cf0661a103..93e272a9868 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -702,7 +702,7 @@ fn const_eval_codeblock( Ok(None) } else { Err(ConstEvalError::CannotBeEvaluatedToConst { - span: decl.span().clone(), + span: decl.span(lookup.engines).clone(), }) } } diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 049b694599d..4bad3b50f8d 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -328,8 +328,8 @@ impl SubstTypes for TyDecl { } } -impl Spanned for TyDecl { - fn span(&self) -> Span { +impl SpannedWithEngines for TyDecl { + fn span(&self, _engines: &Engines) -> Span { match self { TyDecl::VariableDecl(decl) => decl.name.span(), TyDecl::FunctionDecl(FunctionDecl { decl_span, .. }) @@ -544,13 +544,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), })), } } @@ -580,7 +580,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), })), } } @@ -588,7 +588,11 @@ impl TyDecl { /// Retrieves the declaration as a `DeclRef>`. /// /// Returns an error if `self` is not the [TyDecl][FunctionDecl] variant. - pub(crate) fn to_fn_ref(&self, handler: &Handler) -> Result { + pub(crate) fn to_fn_ref( + &self, + handler: &Handler, + engines: &Engines, + ) -> Result { match self { TyDecl::FunctionDecl(FunctionDecl { name, @@ -598,7 +602,7 @@ impl TyDecl { 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), })), } } @@ -609,13 +613,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), })), } } @@ -626,6 +631,7 @@ impl TyDecl { pub(crate) fn to_abi_ref( &self, handler: &Handler, + engines: &Engines, ) -> Result>, ErrorEmitted> { match self { TyDecl::AbiDecl(AbiDecl { @@ -636,7 +642,7 @@ impl TyDecl { 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), })), } } @@ -647,6 +653,7 @@ impl TyDecl { pub(crate) fn to_const_ref( &self, handler: &Handler, + engines: &Engines, ) -> Result>, ErrorEmitted> { match self { TyDecl::ConstantDecl(ConstantDecl { @@ -657,7 +664,7 @@ impl TyDecl { 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), })), } } @@ -787,7 +794,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(), })); 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 90594ef3125..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), }) } 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 0a410985cf0..b601a76dfcd 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 @@ -1654,7 +1654,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 { 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 8573a12fa1f..a8b4b5d6c4d 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/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index 8d5847e2299..da5700ebbfb 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -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 diff --git a/sway-core/src/semantic_analysis/type_check_context.rs b/sway-core/src/semantic_analysis/type_check_context.rs index b7d71376e01..e3680533e0c 100644 --- a/sway-core/src/semantic_analysis/type_check_context.rs +++ b/sway-core/src/semantic_analysis/type_check_context.rs @@ -530,7 +530,7 @@ 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() diff --git a/sway-core/src/type_system/ast_elements/binding.rs b/sway-core/src/type_system/ast_elements/binding.rs index a61946eb6c9..9fe764f52f9 100644 --- a/sway-core/src/type_system/ast_elements/binding.rs +++ b/sway-core/src/type_system/ast_elements/binding.rs @@ -232,7 +232,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 { @@ -375,7 +375,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) } @@ -398,7 +398,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-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 98360e0bc3f..6f63466c469 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(), }; From c06bfb0c27cd2f36690aaf8dd42477963d7660a0 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Thu, 2 May 2024 18:42:21 +0100 Subject: [PATCH 10/19] Simplify `TyDecl::ConstantDecl`. --- .../dead_code_analysis.rs | 11 ++--- sway-core/src/ir_generation/const_eval.rs | 8 ++-- .../language/ty/declaration/declaration.rs | 46 +++++++++---------- sway-core/src/language/ty/program.rs | 3 -- .../ast_node/declaration/abi.rs | 5 +- .../ast_node/declaration/declaration.rs | 1 + .../function/function_parameter.rs | 1 + .../ast_node/declaration/impl_trait.rs | 2 - .../ast_node/declaration/trait.rs | 5 +- .../namespace/lexical_scope.rs | 6 ++- .../semantic_analysis/type_check_context.rs | 1 + .../ast_elements/type_parameter.rs | 2 + sway-lsp/src/traverse/dependency.rs | 8 +++- 13 files changed, 45 insertions(+), 54 deletions(-) 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..4047101776b 100644 --- a/sway-core/src/control_flow_analysis/dead_code_analysis.rs +++ b/sway-core/src/control_flow_analysis/dead_code_analysis.rs @@ -84,11 +84,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); @@ -2271,12 +2267,11 @@ fn construct_dead_code_warning_from_node( 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/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index 93e272a9868..430a64c039e 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 { @@ -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(), }) } } diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 4bad3b50f8d..0192fa832ed 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,9 +39,7 @@ pub enum TyDecl { #[derive(Clone, Debug)] pub struct ConstantDecl { - pub name: Ident, pub decl_id: DeclId, - pub decl_span: Span, } #[derive(Clone, Debug)] @@ -127,17 +125,9 @@ 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, @@ -329,13 +319,16 @@ impl SubstTypes for TyDecl { } impl SpannedWithEngines for TyDecl { - fn span(&self, _engines: &Engines) -> Span { + 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::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, .. }) @@ -492,12 +485,14 @@ impl CollectTypesMetadata for TyDecl { } impl GetDeclIdent for TyDecl { - fn get_decl_ident(&self, _engines: &Engines) -> 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::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, .. }) @@ -656,11 +651,14 @@ impl TyDecl { 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(), @@ -848,9 +846,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(), }) } } diff --git a/sway-core/src/language/ty/program.rs b/sway-core/src/language/ty/program.rs index caafe436813..d5c5d16d8b2 100644 --- a/sway-core/src/language/ty/program.rs +++ b/sway-core/src/language/ty/program.rs @@ -158,11 +158,8 @@ 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) => { 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..d5494f6101f 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs @@ -135,9 +135,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(), }), )?; @@ -306,11 +304,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, 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 b6680fd9864..368cf3b7ab8 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs @@ -287,6 +287,7 @@ 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() 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 8142504b83b..a4364af8d2a 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 e169171401a..9450e9892a2 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/namespace/lexical_scope.rs b/sway-core/src/semantic_analysis/namespace/lexical_scope.rs index acef75a9404..d21f79ce9da 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/type_check_context.rs b/sway-core/src/semantic_analysis/type_check_context.rs index e3680533e0c..4bc2cdd35e5 100644 --- a/sway-core/src/semantic_analysis/type_check_context.rs +++ b/sway-core/src/semantic_analysis/type_check_context.rs @@ -536,6 +536,7 @@ impl<'a> TypeCheckContext<'a> { .current_items_mut() .insert_symbol( handler, + engines, name, item, const_shadowing_mode, 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 95f9ef15f6d..77a921c7486 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-lsp/src/traverse/dependency.rs b/sway-lsp/src/traverse/dependency.rs index dc123f4f003..942c40f77b7 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,15 @@ 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 }) => { + let const_decl = ctx.engines.de().get_constant(decl_id); + const_decl.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(), + | ty::TyDecl::FunctionDecl(ty::FunctionDecl { name, .. }) => name.clone(), _ => return, }; From 1baad0b408d4ba37a18d1cdd8f0239e5dbbe94f9 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Thu, 2 May 2024 19:05:41 +0100 Subject: [PATCH 11/19] Simplify `TyDecl::TraitTypeDecl`. --- sway-core/src/language/ty/declaration/declaration.rs | 12 ++++++------ sway-core/src/language/ty/program.rs | 3 --- .../src/semantic_analysis/type_check_context.rs | 10 +++------- sway-lsp/src/traverse/typed_tree.rs | 2 +- 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 0192fa832ed..3d8169278fc 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -44,9 +44,7 @@ pub struct ConstantDecl { #[derive(Clone, Debug)] pub struct TraitTypeDecl { - pub name: Ident, pub decl_id: DeclId, - pub decl_span: Span, } #[derive(Clone, Debug)] @@ -325,11 +323,13 @@ impl SpannedWithEngines for TyDecl { 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::VariableDecl(decl) => decl.name.span(), TyDecl::FunctionDecl(FunctionDecl { decl_span, .. }) | TyDecl::TraitDecl(TraitDecl { decl_span, .. }) | TyDecl::ImplTrait(ImplTrait { decl_span, .. }) - | TyDecl::TraitTypeDecl(TraitTypeDecl { decl_span, .. }) | TyDecl::StorageDecl(StorageDecl { decl_span, .. }) | TyDecl::TypeAliasDecl(TypeAliasDecl { decl_span, .. }) | TyDecl::AbiDecl(AbiDecl { decl_span, .. }) @@ -490,13 +490,15 @@ impl GetDeclIdent for TyDecl { 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::VariableDecl(decl) => Some(decl.name.clone()), TyDecl::FunctionDecl(FunctionDecl { name, .. }) | TyDecl::TraitDecl(TraitDecl { 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()), @@ -836,9 +838,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(), }) } } diff --git a/sway-core/src/language/ty/program.rs b/sway-core/src/language/ty/program.rs index d5c5d16d8b2..56b81f710d0 100644 --- a/sway-core/src/language/ty/program.rs +++ b/sway-core/src/language/ty/program.rs @@ -163,12 +163,9 @@ impl TyProgram { })); } 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(), }, )); } diff --git a/sway-core/src/semantic_analysis/type_check_context.rs b/sway-core/src/semantic_analysis/type_check_context.rs index 4bc2cdd35e5..6fde2d401f1 100644 --- a/sway-core/src/semantic_analysis/type_check_context.rs +++ b/sway-core/src/semantic_analysis/type_check_context.rs @@ -987,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 { @@ -1000,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-lsp/src/traverse/typed_tree.rs b/sway-lsp/src/traverse/typed_tree.rs index f68e8bdc6aa..abd8fd10926 100644 --- a/sway-lsp/src/traverse/typed_tree.rs +++ b/sway-lsp/src/traverse/typed_tree.rs @@ -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); } } From b66b717085861be9bc9523ec4fe1f31998d20514 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Thu, 2 May 2024 18:43:01 +0100 Subject: [PATCH 12/19] Simplify `TyDecl::FunctionDecl`. --- .../dead_code_analysis.rs | 11 ++-- sway-core/src/ir_generation/const_eval.rs | 8 ++- .../language/ty/declaration/declaration.rs | 51 +++++++++---------- sway-core/src/language/ty/module.rs | 14 ++--- sway-core/src/language/ty/program.rs | 12 +---- .../ast_node/declaration/declaration.rs | 6 +-- sway-core/src/semantic_analysis/module.rs | 3 +- sway-lsp/src/traverse/dependency.rs | 9 ++-- 8 files changed, 48 insertions(+), 66 deletions(-) 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 4047101776b..73beef15a09 100644 --- a/sway-core/src/control_flow_analysis/dead_code_analysis.rs +++ b/sway-core/src/control_flow_analysis/dead_code_analysis.rs @@ -51,11 +51,7 @@ 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); @@ -2230,12 +2226,11 @@ 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 { diff --git a/sway-core/src/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index 430a64c039e..fc9e1cc8a30 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -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/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 3d8169278fc..76b9f8d9c51 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -49,9 +49,7 @@ pub struct TraitTypeDecl { #[derive(Clone, Debug)] pub struct FunctionDecl { - pub name: Ident, pub decl_id: DeclId, - pub decl_span: Span, } #[derive(Clone, Debug)] @@ -127,17 +125,9 @@ impl PartialEqWithEngines for TyDecl { 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, @@ -326,9 +316,11 @@ impl SpannedWithEngines for TyDecl { 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::VariableDecl(decl) => decl.name.span(), - TyDecl::FunctionDecl(FunctionDecl { decl_span, .. }) - | TyDecl::TraitDecl(TraitDecl { decl_span, .. }) + TyDecl::TraitDecl(TraitDecl { decl_span, .. }) | TyDecl::ImplTrait(ImplTrait { decl_span, .. }) | TyDecl::StorageDecl(StorageDecl { decl_span, .. }) | TyDecl::TypeAliasDecl(TypeAliasDecl { decl_span, .. }) @@ -379,8 +371,10 @@ impl DisplayWithEngines for TyDecl { builder.push_str(&engines.help_out(body).to_string()); builder } - TyDecl::FunctionDecl(FunctionDecl { name, .. }) - | TyDecl::TraitDecl(TraitDecl { name, .. }) + TyDecl::FunctionDecl(FunctionDecl { decl_id }) => { + engines.de().get(decl_id).name.as_str().into() + } + TyDecl::TraitDecl(TraitDecl { name, .. }) | TyDecl::StructDecl(StructDecl { name, .. }) | TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) | TyDecl::ImplTrait(ImplTrait { name, .. }) @@ -426,8 +420,10 @@ impl DebugWithEngines for TyDecl { builder.push_str(format!("{:?}", engines.help_out(body)).as_str()); builder } - TyDecl::FunctionDecl(FunctionDecl { name, .. }) - | TyDecl::TraitDecl(TraitDecl { name, .. }) + TyDecl::FunctionDecl(FunctionDecl { decl_id }) => { + engines.de().get(decl_id).name.as_str().into() + } + TyDecl::TraitDecl(TraitDecl { name, .. }) | TyDecl::StructDecl(StructDecl { name, .. }) | TyDecl::EnumDecl(EnumDecl { name, .. }) => name.as_str().into(), _ => String::new(), @@ -493,9 +489,11 @@ impl GetDeclIdent for TyDecl { 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::VariableDecl(decl) => Some(decl.name.clone()), - TyDecl::FunctionDecl(FunctionDecl { name, .. }) - | TyDecl::TraitDecl(TraitDecl { name, .. }) + TyDecl::TraitDecl(TraitDecl { name, .. }) | TyDecl::ImplTrait(ImplTrait { name, .. }) | TyDecl::AbiDecl(AbiDecl { name, .. }) | TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) @@ -591,11 +589,10 @@ impl TyDecl { engines: &Engines, ) -> Result { match self { - TyDecl::FunctionDecl(FunctionDecl { - name, - decl_id, - 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(), @@ -864,9 +861,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(), - 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 8eb2870b39c..21bc45eb6cf 100644 --- a/sway-core/src/language/ty/module.rs +++ b/sway-core/src/language/ty/module.rs @@ -53,18 +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, - 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 56b81f710d0..4ef4ee3a048 100644 --- a/sway-core/src/language/ty/program.rs +++ b/sway-core/src/language/ty/program.rs @@ -96,11 +96,7 @@ impl TyProgram { for node in &root.all_nodes { match &node.content { - TyAstNodeContent::Declaration(TyDecl::FunctionDecl(FunctionDecl { - name, - decl_id, - decl_span, - })) => { + TyAstNodeContent::Declaration(TyDecl::FunctionDecl(FunctionDecl { decl_id })) => { let func = decl_engine.get_function(decl_id); match func.kind { @@ -116,11 +112,7 @@ impl TyProgram { }); } - declarations.push(TyDecl::FunctionDecl(FunctionDecl { - name: name.clone(), - decl_id: *decl_id, - decl_span: decl_span.clone(), - })); + declarations.push(TyDecl::FunctionDecl(FunctionDecl { decl_id: *decl_id })); } TyAstNodeContent::Declaration(TyDecl::ConstantDecl(ConstantDecl { decl_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 368cf3b7ab8..fc018aedba2 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs @@ -292,11 +292,7 @@ impl TyDecl { "__contract_entry_{}", decl.name.clone() )), - TyDecl::FunctionDecl(FunctionDecl { - name: decl.name.clone(), - decl_id: *f.id(), - decl_span: f.span(), - }), + TyDecl::FunctionDecl(FunctionDecl { decl_id: *f.id() }), ConstShadowingMode::ItemStyle, GenericShadowingMode::Allow, ) diff --git a/sway-core/src/semantic_analysis/module.rs b/sway-core/src/semantic_analysis/module.rs index 63cf4631c81..9bbf70b3c27 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-lsp/src/traverse/dependency.rs b/sway-lsp/src/traverse/dependency.rs index 942c40f77b7..58a2727f196 100644 --- a/sway-lsp/src/traverse/dependency.rs +++ b/sway-lsp/src/traverse/dependency.rs @@ -53,14 +53,15 @@ pub fn collect_typed_declaration(node: &ty::TyAstNode, ctx: &ParseContext) { let ident = match declaration { ty::TyDecl::ConstantDecl(ty::ConstantDecl { decl_id }) => { - let const_decl = ctx.engines.de().get_constant(decl_id); - const_decl.name().clone() + 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::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, .. }) => name.clone(), + | ty::TyDecl::TraitDecl(ty::TraitDecl { name, .. }) => name.clone(), _ => return, }; From c997f647b7f11315ce5bc8b604691689e43e18c8 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Thu, 2 May 2024 18:43:15 +0100 Subject: [PATCH 13/19] Simplify `TyDecl::TraitDecl`. --- .../dead_code_analysis.rs | 11 ++--- .../language/ty/declaration/declaration.rs | 42 +++++++++---------- .../ast_node/declaration/declaration.rs | 12 ++---- sway-lsp/src/traverse/dependency.rs | 6 ++- 4 files changed, 30 insertions(+), 41 deletions(-) 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 73beef15a09..1beb5f735b9 100644 --- a/sway-core/src/control_flow_analysis/dead_code_analysis.rs +++ b/sway-core/src/control_flow_analysis/dead_code_analysis.rs @@ -58,12 +58,7 @@ fn is_entry_point(node: &TyAstNode, decl_engine: &DeclEngine, tree_type: &TreeTy 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 { @@ -2253,10 +2248,10 @@ fn construct_dead_code_warning_from_node( }, 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 { diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 76b9f8d9c51..c6dc003b486 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -54,9 +54,7 @@ pub struct FunctionDecl { #[derive(Clone, Debug)] pub struct TraitDecl { - pub name: Ident, pub decl_id: DeclId, - pub decl_span: Span, } #[derive(Clone, Debug)] @@ -129,17 +127,9 @@ impl PartialEqWithEngines for TyDecl { 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, @@ -319,9 +309,11 @@ impl SpannedWithEngines for TyDecl { 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::VariableDecl(decl) => decl.name.span(), - TyDecl::TraitDecl(TraitDecl { decl_span, .. }) - | TyDecl::ImplTrait(ImplTrait { decl_span, .. }) + TyDecl::ImplTrait(ImplTrait { decl_span, .. }) | TyDecl::StorageDecl(StorageDecl { decl_span, .. }) | TyDecl::TypeAliasDecl(TypeAliasDecl { decl_span, .. }) | TyDecl::AbiDecl(AbiDecl { decl_span, .. }) @@ -374,8 +366,10 @@ impl DisplayWithEngines for TyDecl { TyDecl::FunctionDecl(FunctionDecl { decl_id }) => { engines.de().get(decl_id).name.as_str().into() } - TyDecl::TraitDecl(TraitDecl { name, .. }) - | TyDecl::StructDecl(StructDecl { name, .. }) + TyDecl::TraitDecl(TraitDecl { decl_id }) => { + engines.de().get(decl_id).name.as_str().into() + } + TyDecl::StructDecl(StructDecl { name, .. }) | TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) | TyDecl::ImplTrait(ImplTrait { name, .. }) | TyDecl::EnumDecl(EnumDecl { name, .. }) => name.as_str().into(), @@ -423,8 +417,10 @@ impl DebugWithEngines for TyDecl { TyDecl::FunctionDecl(FunctionDecl { decl_id }) => { engines.de().get(decl_id).name.as_str().into() } - TyDecl::TraitDecl(TraitDecl { name, .. }) - | TyDecl::StructDecl(StructDecl { name, .. }) + TyDecl::TraitDecl(TraitDecl { decl_id }) => { + engines.de().get(decl_id).name.as_str().into() + } + TyDecl::StructDecl(StructDecl { name, .. }) | TyDecl::EnumDecl(EnumDecl { name, .. }) => name.as_str().into(), _ => String::new(), } @@ -492,9 +488,11 @@ impl GetDeclIdent for TyDecl { 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::VariableDecl(decl) => Some(decl.name.clone()), - TyDecl::TraitDecl(TraitDecl { name, .. }) - | TyDecl::ImplTrait(ImplTrait { name, .. }) + TyDecl::ImplTrait(ImplTrait { name, .. }) | TyDecl::AbiDecl(AbiDecl { name, .. }) | TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) | TyDecl::GenericTypeForFunctionScope(GenericTypeForFunctionScope { name, .. }) @@ -869,9 +867,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(), - decl_span: decl_ref.decl_span().clone(), }) } } 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 fc018aedba2..2f50f900178 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs @@ -242,15 +242,13 @@ impl TyDecl { ) .map(|supertrait_decl| { if let ty::TyDecl::TraitDecl(ty::TraitDecl { - name: supertrait_name, decl_id: supertrait_decl_id, - 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(), )); } }); @@ -408,15 +406,13 @@ impl TyDecl { ) .map(|supertrait_decl| { if let ty::TyDecl::TraitDecl(ty::TraitDecl { - name: supertrait_name, decl_id: supertrait_decl_id, - 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-lsp/src/traverse/dependency.rs b/sway-lsp/src/traverse/dependency.rs index 58a2727f196..eb231cd0ce7 100644 --- a/sway-lsp/src/traverse/dependency.rs +++ b/sway-lsp/src/traverse/dependency.rs @@ -58,10 +58,12 @@ pub fn collect_typed_declaration(node: &ty::TyAstNode, ctx: &ParseContext) { 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::VariableDecl(variable) => variable.name.clone(), ty::TyDecl::StructDecl(ty::StructDecl { name, .. }) - | ty::TyDecl::EnumDecl(ty::EnumDecl { name, .. }) - | ty::TyDecl::TraitDecl(ty::TraitDecl { name, .. }) => name.clone(), + | ty::TyDecl::EnumDecl(ty::EnumDecl { name, .. }) => name.clone(), _ => return, }; From cf6fba5e547130bdd1132e932930e349c87c63e7 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Thu, 2 May 2024 18:43:44 +0100 Subject: [PATCH 14/19] Simplify `TyDecl::StructDecl`. --- forc-plugins/forc-doc/src/doc/mod.rs | 9 +-- .../dead_code_analysis.rs | 6 +- .../language/ty/declaration/declaration.rs | 72 ++++++++++--------- .../src/semantic_analysis/namespace/root.rs | 15 ++-- sway-lsp/src/traverse/dependency.rs | 6 +- 5 files changed, 57 insertions(+), 51 deletions(-) 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/dead_code_analysis.rs b/sway-core/src/control_flow_analysis/dead_code_analysis.rs index 1beb5f735b9..6c7419db62a 100644 --- a/sway-core/src/control_flow_analysis/dead_code_analysis.rs +++ b/sway-core/src/control_flow_analysis/dead_code_analysis.rs @@ -2230,12 +2230,10 @@ fn construct_dead_code_warning_from_node( }, 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 { diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index c6dc003b486..5a3b9317063 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -59,9 +59,7 @@ pub struct TraitDecl { #[derive(Clone, Debug)] pub struct StructDecl { - pub name: Ident, pub decl_id: DeclId, - pub decl_span: Span, } #[derive(Clone, Debug)] @@ -131,17 +129,9 @@ impl PartialEqWithEngines for TyDecl { 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, @@ -312,12 +302,14 @@ impl SpannedWithEngines for TyDecl { 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::VariableDecl(decl) => decl.name.span(), TyDecl::ImplTrait(ImplTrait { 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::EnumVariantDecl(EnumVariantDecl { variant_decl_span, .. @@ -369,8 +361,10 @@ impl DisplayWithEngines for TyDecl { TyDecl::TraitDecl(TraitDecl { decl_id }) => { engines.de().get(decl_id).name.as_str().into() } - TyDecl::StructDecl(StructDecl { name, .. }) - | TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) + TyDecl::StructDecl(StructDecl { decl_id }) => { + engines.de().get(decl_id).name().as_str().into() + } + TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) | TyDecl::ImplTrait(ImplTrait { name, .. }) | TyDecl::EnumDecl(EnumDecl { name, .. }) => name.as_str().into(), _ => String::new(), @@ -420,8 +414,10 @@ impl DebugWithEngines for TyDecl { TyDecl::TraitDecl(TraitDecl { decl_id }) => { engines.de().get(decl_id).name.as_str().into() } - TyDecl::StructDecl(StructDecl { name, .. }) - | TyDecl::EnumDecl(EnumDecl { name, .. }) => name.as_str().into(), + TyDecl::StructDecl(StructDecl { decl_id }) => { + engines.de().get(decl_id).name().as_str().into() + } + TyDecl::EnumDecl(EnumDecl { name, .. }) => name.as_str().into(), _ => String::new(), } ) @@ -491,12 +487,14 @@ impl GetDeclIdent for TyDecl { 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::VariableDecl(decl) => Some(decl.name.clone()), TyDecl::ImplTrait(ImplTrait { name, .. }) | TyDecl::AbiDecl(AbiDecl { name, .. }) | TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) | TyDecl::GenericTypeForFunctionScope(GenericTypeForFunctionScope { name, .. }) - | TyDecl::StructDecl(StructDecl { name, .. }) | TyDecl::EnumDecl(EnumDecl { name, .. }) => Some(name.clone()), TyDecl::EnumVariantDecl(EnumVariantDecl { variant_name, .. }) => { Some(variant_name.clone()) @@ -557,11 +555,14 @@ impl TyDecl { engines: &Engines, ) -> Result { match self { - TyDecl::StructDecl(StructDecl { - name, - decl_id, - 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; @@ -752,15 +753,18 @@ impl TyDecl { let decl = decl_engine.get_function(decl_id); decl.return_type.type_id } - TyDecl::StructDecl(StructDecl { - name, - decl_id, - decl_span, - }) => type_engine.insert( - engines, - TypeInfo::Struct(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 { name, decl_id, @@ -885,9 +889,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(), - decl_span: decl_ref.decl_span().clone(), }) } } diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index da5700ebbfb..447b9566572 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 { @@ -712,11 +712,14 @@ 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::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_decl) => TypeInfo::Enum(DeclRef::new( enum_decl.name.clone(), enum_decl.decl_id, diff --git a/sway-lsp/src/traverse/dependency.rs b/sway-lsp/src/traverse/dependency.rs index eb231cd0ce7..12c7f659f18 100644 --- a/sway-lsp/src/traverse/dependency.rs +++ b/sway-lsp/src/traverse/dependency.rs @@ -61,9 +61,11 @@ pub fn collect_typed_declaration(node: &ty::TyAstNode, ctx: &ParseContext) { 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::VariableDecl(variable) => variable.name.clone(), - ty::TyDecl::StructDecl(ty::StructDecl { name, .. }) - | ty::TyDecl::EnumDecl(ty::EnumDecl { name, .. }) => name.clone(), + ty::TyDecl::EnumDecl(ty::EnumDecl { name, .. }) => name.clone(), _ => return, }; From 82a3791db87f9e42841455c986893362dc4f9b94 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Fri, 3 May 2024 10:59:27 +0100 Subject: [PATCH 15/19] Simplify `TyDecl::EnumDecl`. --- .../dead_code_analysis.rs | 4 +- .../language/ty/declaration/declaration.rs | 74 ++++++++++--------- .../src/semantic_analysis/namespace/root.rs | 13 ++-- sway-lsp/src/traverse/dependency.rs | 4 +- 4 files changed, 51 insertions(+), 44 deletions(-) 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 6c7419db62a..4080895ae34 100644 --- a/sway-core/src/control_flow_analysis/dead_code_analysis.rs +++ b/sway-core/src/control_flow_analysis/dead_code_analysis.rs @@ -2238,10 +2238,10 @@ fn construct_dead_code_warning_from_node( }, 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 { diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 5a3b9317063..89d7ac76e14 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -64,9 +64,7 @@ pub struct StructDecl { #[derive(Clone, Debug)] pub struct EnumDecl { - pub name: Ident, pub decl_id: DeclId, - pub decl_span: Span, } #[derive(Clone, Debug)] @@ -133,17 +131,9 @@ impl PartialEqWithEngines for TyDecl { 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, @@ -305,12 +295,12 @@ impl SpannedWithEngines for TyDecl { 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::VariableDecl(decl) => decl.name.span(), TyDecl::ImplTrait(ImplTrait { decl_span, .. }) | TyDecl::StorageDecl(StorageDecl { decl_span, .. }) | TyDecl::TypeAliasDecl(TypeAliasDecl { decl_span, .. }) - | TyDecl::AbiDecl(AbiDecl { decl_span, .. }) - | TyDecl::EnumDecl(EnumDecl { decl_span, .. }) => decl_span.clone(), + | TyDecl::AbiDecl(AbiDecl { decl_span, .. }) => decl_span.clone(), TyDecl::EnumVariantDecl(EnumVariantDecl { variant_decl_span, .. }) => variant_decl_span.clone(), @@ -364,9 +354,11 @@ impl DisplayWithEngines for TyDecl { 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::TypeAliasDecl(TypeAliasDecl { name, .. }) - | TyDecl::ImplTrait(ImplTrait { name, .. }) - | TyDecl::EnumDecl(EnumDecl { name, .. }) => name.as_str().into(), + | TyDecl::ImplTrait(ImplTrait { name, .. }) => name.as_str().into(), _ => String::new(), } ) @@ -417,7 +409,9 @@ impl DebugWithEngines for TyDecl { TyDecl::StructDecl(StructDecl { decl_id }) => { engines.de().get(decl_id).name().as_str().into() } - TyDecl::EnumDecl(EnumDecl { name, .. }) => name.as_str().into(), + TyDecl::EnumDecl(EnumDecl { decl_id }) => { + engines.de().get(decl_id).name().as_str().into() + } _ => String::new(), } ) @@ -490,12 +484,16 @@ impl GetDeclIdent for TyDecl { 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::VariableDecl(decl) => Some(decl.name.clone()), TyDecl::ImplTrait(ImplTrait { name, .. }) | TyDecl::AbiDecl(AbiDecl { name, .. }) | TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) - | TyDecl::GenericTypeForFunctionScope(GenericTypeForFunctionScope { name, .. }) - | TyDecl::EnumDecl(EnumDecl { name, .. }) => Some(name.clone()), + | TyDecl::GenericTypeForFunctionScope(GenericTypeForFunctionScope { name, .. }) => { + Some(name.clone()) + } TyDecl::EnumVariantDecl(EnumVariantDecl { variant_name, .. }) => { Some(variant_name.clone()) } @@ -515,11 +513,14 @@ impl TyDecl { engines: &Engines, ) -> Result { match self { - TyDecl::EnumDecl(EnumDecl { - name, - decl_id, - 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; @@ -765,15 +766,18 @@ impl TyDecl { decl.name().span().source_id(), ) } - TyDecl::EnumDecl(EnumDecl { - name, - decl_id, - decl_span, - }) => type_engine.insert( - engines, - TypeInfo::Enum(DeclRef::new(name.clone(), *decl_id, decl_span.clone())), - 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( @@ -853,9 +857,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(), - decl_span: decl_ref.decl_span().clone(), }) } } diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index 447b9566572..977ca36ef84 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -720,11 +720,14 @@ impl Root { struct_decl.span().clone(), )) } - ty::TyDecl::EnumDecl(enum_decl) => TypeInfo::Enum(DeclRef::new( - enum_decl.name.clone(), - enum_decl.decl_id, - enum_decl.name.span(), - )), + 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-lsp/src/traverse/dependency.rs b/sway-lsp/src/traverse/dependency.rs index 12c7f659f18..d270f04f9ef 100644 --- a/sway-lsp/src/traverse/dependency.rs +++ b/sway-lsp/src/traverse/dependency.rs @@ -64,8 +64,10 @@ pub fn collect_typed_declaration(node: &ty::TyAstNode, ctx: &ParseContext) { 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::EnumDecl(ty::EnumDecl { name, .. }) => name.clone(), _ => return, }; From 57ac5a2a33e3cba9ae572b1173ec3c5384f5f38a Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Thu, 2 May 2024 18:44:19 +0100 Subject: [PATCH 16/19] Simplify `TyDecl::ImplTrait`. --- .../language/ty/declaration/declaration.rs | 36 ++++++++----------- .../semantic_analysis/type_check_analysis.rs | 4 ++- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 89d7ac76e14..d60bb1cb9c9 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -76,9 +76,7 @@ pub struct EnumVariantDecl { #[derive(Clone, Debug)] pub struct ImplTrait { - pub name: Ident, pub decl_id: DeclId, - pub decl_span: Span, } #[derive(Clone, Debug)] @@ -135,17 +133,9 @@ impl PartialEqWithEngines for TyDecl { 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, @@ -296,9 +286,11 @@ impl SpannedWithEngines for TyDecl { 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::VariableDecl(decl) => decl.name.span(), - TyDecl::ImplTrait(ImplTrait { decl_span, .. }) - | TyDecl::StorageDecl(StorageDecl { decl_span, .. }) + TyDecl::StorageDecl(StorageDecl { decl_span, .. }) | TyDecl::TypeAliasDecl(TypeAliasDecl { decl_span, .. }) | TyDecl::AbiDecl(AbiDecl { decl_span, .. }) => decl_span.clone(), TyDecl::EnumVariantDecl(EnumVariantDecl { @@ -357,8 +349,10 @@ impl DisplayWithEngines for TyDecl { TyDecl::EnumDecl(EnumDecl { decl_id }) => { engines.de().get(decl_id).name().as_str().into() } - TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) - | TyDecl::ImplTrait(ImplTrait { name, .. }) => name.as_str().into(), + TyDecl::ImplTrait(ImplTrait { decl_id }) => { + engines.de().get(decl_id).name().as_str().into() + } + TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) => name.as_str().into(), _ => String::new(), } ) @@ -487,9 +481,11 @@ impl GetDeclIdent for TyDecl { 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::VariableDecl(decl) => Some(decl.name.clone()), - TyDecl::ImplTrait(ImplTrait { name, .. }) - | TyDecl::AbiDecl(AbiDecl { name, .. }) + TyDecl::AbiDecl(AbiDecl { name, .. }) | TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) | TyDecl::GenericTypeForFunctionScope(GenericTypeForFunctionScope { name, .. }) => { Some(name.clone()) @@ -881,9 +877,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(), - decl_span: decl_ref.decl_span().clone(), }) } } 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); From a1036bf63d1a53e8e59260839e7eef48fc9b11bc Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Thu, 2 May 2024 18:44:36 +0100 Subject: [PATCH 17/19] Simplify `TyDecl::AbiDecl`. --- .../language/ty/declaration/declaration.rs | 39 +++++++------------ .../ast_node/declaration/abi.rs | 11 ++++-- .../ast_node/declaration/trait_fn.rs | 6 +-- .../ast_node/expression/typed_expression.rs | 9 ++--- 4 files changed, 28 insertions(+), 37 deletions(-) diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index d60bb1cb9c9..9f3c7b7af66 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -81,9 +81,7 @@ pub struct ImplTrait { #[derive(Clone, Debug)] pub struct AbiDecl { - pub name: Ident, pub decl_id: DeclId, - pub decl_span: Span, } #[derive(Clone, Debug)] @@ -137,17 +135,9 @@ impl PartialEqWithEngines for TyDecl { 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, .. }), @@ -289,10 +279,10 @@ impl SpannedWithEngines for TyDecl { 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::StorageDecl(StorageDecl { decl_span, .. }) - | TyDecl::TypeAliasDecl(TypeAliasDecl { decl_span, .. }) - | TyDecl::AbiDecl(AbiDecl { decl_span, .. }) => decl_span.clone(), + | TyDecl::TypeAliasDecl(TypeAliasDecl { decl_span, .. }) => decl_span.clone(), TyDecl::EnumVariantDecl(EnumVariantDecl { variant_decl_span, .. }) => variant_decl_span.clone(), @@ -484,9 +474,9 @@ impl GetDeclIdent for TyDecl { 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::AbiDecl(AbiDecl { name, .. }) - | TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) + TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) | TyDecl::GenericTypeForFunctionScope(GenericTypeForFunctionScope { name, .. }) => { Some(name.clone()) } @@ -624,11 +614,14 @@ impl TyDecl { 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(), @@ -893,9 +886,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(), }) } } 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 d5494f6101f..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(), }); } } @@ -273,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(), }, ); @@ -343,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/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 b601a76dfcd..4d206f97495 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 @@ -1628,11 +1628,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); From 7581a7a816032897fa6184f9f0029609c585e905 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Thu, 21 Mar 2024 11:52:21 +0000 Subject: [PATCH 18/19] Simplify `TyDecl::StorageDecl`. --- sway-core/src/language/ty/declaration/declaration.rs | 6 ++---- sway-core/src/language/ty/program.rs | 10 +++------- sway-core/src/semantic_analysis/program.rs | 6 +----- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 9f3c7b7af66..8287e6bdd61 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -93,7 +93,6 @@ pub struct GenericTypeForFunctionScope { #[derive(Clone, Debug)] pub struct StorageDecl { pub decl_id: DeclId, - pub decl_span: Span, } #[derive(Clone, Debug)] @@ -281,8 +280,8 @@ impl SpannedWithEngines for TyDecl { } TyDecl::AbiDecl(AbiDecl { decl_id }) => engines.de().get_abi(decl_id).span.clone(), TyDecl::VariableDecl(decl) => decl.name.span(), - TyDecl::StorageDecl(StorageDecl { decl_span, .. }) - | TyDecl::TypeAliasDecl(TypeAliasDecl { decl_span, .. }) => decl_span.clone(), + TyDecl::StorageDecl(StorageDecl { decl_id }) => engines.de().get(decl_id).span.clone(), + TyDecl::TypeAliasDecl(TypeAliasDecl { decl_span, .. }) => decl_span.clone(), TyDecl::EnumVariantDecl(EnumVariantDecl { variant_decl_span, .. }) => variant_decl_span.clone(), @@ -895,7 +894,6 @@ 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(), }) } } diff --git a/sway-core/src/language/ty/program.rs b/sway-core/src/language/ty/program.rs index 4ef4ee3a048..d801040702b 100644 --- a/sway-core/src/language/ty/program.rs +++ b/sway-core/src/language/ty/program.rs @@ -189,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(), }); } } @@ -202,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/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, From c50f765bb1e191f434f8d32888316eb5a6637213 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Thu, 21 Mar 2024 12:05:12 +0000 Subject: [PATCH 19/19] Simplify `TyDecl::TypeAliasDecl`. --- .../src/language/ty/declaration/declaration.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/sway-core/src/language/ty/declaration/declaration.rs b/sway-core/src/language/ty/declaration/declaration.rs index 8287e6bdd61..b0633d19bd2 100644 --- a/sway-core/src/language/ty/declaration/declaration.rs +++ b/sway-core/src/language/ty/declaration/declaration.rs @@ -97,9 +97,7 @@ pub struct StorageDecl { #[derive(Clone, Debug)] pub struct TypeAliasDecl { - pub name: Ident, pub decl_id: DeclId, - pub decl_span: Span, } impl EqWithEngines for TyDecl {} @@ -281,7 +279,9 @@ impl SpannedWithEngines for TyDecl { TyDecl::AbiDecl(AbiDecl { decl_id }) => engines.de().get_abi(decl_id).span.clone(), TyDecl::VariableDecl(decl) => decl.name.span(), TyDecl::StorageDecl(StorageDecl { decl_id }) => engines.de().get(decl_id).span.clone(), - TyDecl::TypeAliasDecl(TypeAliasDecl { decl_span, .. }) => decl_span.clone(), + TyDecl::TypeAliasDecl(TypeAliasDecl { decl_id }) => { + engines.de().get(decl_id).span.clone() + } TyDecl::EnumVariantDecl(EnumVariantDecl { variant_decl_span, .. }) => variant_decl_span.clone(), @@ -341,7 +341,8 @@ impl DisplayWithEngines for TyDecl { TyDecl::ImplTrait(ImplTrait { decl_id }) => { engines.de().get(decl_id).name().as_str().into() } - TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) => name.as_str().into(), + TyDecl::TypeAliasDecl(TypeAliasDecl { decl_id }) => + engines.de().get(decl_id).name().as_str().into(), _ => String::new(), } ) @@ -475,8 +476,10 @@ impl GetDeclIdent for TyDecl { } TyDecl::AbiDecl(AbiDecl { decl_id }) => Some(engines.de().get(decl_id).name().clone()), TyDecl::VariableDecl(decl) => Some(decl.name.clone()), - TyDecl::TypeAliasDecl(TypeAliasDecl { name, .. }) - | TyDecl::GenericTypeForFunctionScope(GenericTypeForFunctionScope { name, .. }) => { + 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, .. }) => { @@ -900,9 +903,7 @@ impl From>> for TyDecl { 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(), }) } }