From 6759b60a4e6f863500458a4efe97fe96de03386b Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Fri, 3 May 2024 10:59:27 +0100 Subject: [PATCH] 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 45d5401a119..131df035a9c 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).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, };