Skip to content

Commit

Permalink
Simplify TyDecl::EnumDecl.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed May 3, 2024
1 parent 2b9e221 commit 6759b60
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 44 deletions.
4 changes: 2 additions & 2 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
74 changes: 38 additions & 36 deletions sway-core/src/language/ty/declaration/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ pub struct StructDecl {

#[derive(Clone, Debug)]
pub struct EnumDecl {
pub name: Ident,
pub decl_id: DeclId<TyEnumDecl>,
pub decl_span: Span,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
}
)
Expand Down Expand Up @@ -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(),
}
)
Expand Down Expand Up @@ -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())
}
Expand All @@ -515,11 +513,14 @@ impl TyDecl {
engines: &Engines,
) -> Result<DeclRefEnum, ErrorEmitted> {
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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -853,9 +857,7 @@ impl From<DeclRef<DeclId<TyConstantDecl>>> for TyDecl {
impl From<DeclRef<DeclId<TyEnumDecl>>> for TyDecl {
fn from(decl_ref: DeclRef<DeclId<TyEnumDecl>>) -> Self {
TyDecl::EnumDecl(EnumDecl {
name: decl_ref.name().clone(),
decl_id: *decl_ref.id(),
decl_span: decl_ref.decl_span().clone(),
})
}
}
Expand Down
13 changes: 8 additions & 5 deletions sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion sway-lsp/src/traverse/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down

0 comments on commit 6759b60

Please sign in to comment.