Skip to content

Commit

Permalink
Use DeclId directly for TyDeclaration::StructDeclaration and `TyD…
Browse files Browse the repository at this point in the history
…eclaration::EnumDeclaration`. (FuelLabs#4251)

## Description

This PR fixes a regression to `TyDeclaration::StructDeclaration` and
`TyDeclaration::EnumDeclaration` and removes the `DeclRef`s that they
were holding.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: emilyaherbert <[email protected]>
  • Loading branch information
emilyaherbert and emilyaherbert authored Mar 10, 2023
1 parent b48cabd commit 4c1c1d8
Show file tree
Hide file tree
Showing 18 changed files with 210 additions and 104 deletions.
8 changes: 4 additions & 4 deletions forc-plugins/forc-doc/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ impl Descriptor {
use swayfmt::parse;
use TyDeclaration::*;
match ty_decl {
StructDeclaration(decl_ref) => {
let struct_decl = decl_engine.get_struct(decl_ref);
StructDeclaration { decl_id, .. } => {
let struct_decl = decl_engine.get_struct(decl_id);
if !document_private_items && struct_decl.visibility.is_private() {
Ok(Descriptor::NonDocumentable)
} else {
Expand Down Expand Up @@ -75,8 +75,8 @@ impl Descriptor {
}))
}
}
EnumDeclaration(decl_ref) => {
let enum_decl = decl_engine.get_enum(decl_ref);
EnumDeclaration { decl_id, .. } => {
let enum_decl = decl_engine.get_enum(decl_id);
if !document_private_items && enum_decl.visibility.is_private() {
Ok(Descriptor::NonDocumentable)
} else {
Expand Down
4 changes: 2 additions & 2 deletions forc-plugins/forc-doc/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,8 +1158,8 @@ impl BlockTitle {
impl DocBlockTitle for TyDeclaration {
fn as_block_title(&self) -> BlockTitle {
match self {
TyDeclaration::StructDeclaration(_) => BlockTitle::Structs,
TyDeclaration::EnumDeclaration(_) => BlockTitle::Enums,
TyDeclaration::StructDeclaration { .. } => BlockTitle::Structs,
TyDeclaration::EnumDeclaration { .. } => BlockTitle::Enums,
TyDeclaration::TraitDeclaration { .. } => BlockTitle::Traits,
TyDeclaration::AbiDeclaration { .. } => BlockTitle::Abi,
TyDeclaration::StorageDeclaration { .. } => BlockTitle::ContractStorage,
Expand Down
33 changes: 17 additions & 16 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,14 @@ fn connect_declaration<'eng: 'cfg, 'cfg>(
connect_abi_declaration(engines, &abi_decl, graph, entry_node)?;
Ok(leaves.to_vec())
}
StructDeclaration(decl_ref) => {
let struct_decl = decl_engine.get_struct(decl_ref);
connect_struct_declaration(&struct_decl, decl_ref.id, graph, entry_node, tree_type);
StructDeclaration { decl_id, .. } => {
let struct_decl = decl_engine.get_struct(decl_id);
connect_struct_declaration(&struct_decl, *decl_id, graph, entry_node, tree_type);
Ok(leaves.to_vec())
}
EnumDeclaration(decl_ref) => {
let enum_decl = decl_engine.get_enum(decl_ref);
connect_enum_declaration(&enum_decl, decl_ref.id, graph, entry_node);
EnumDeclaration { decl_id, .. } => {
let enum_decl = decl_engine.get_enum(decl_id);
connect_enum_declaration(&enum_decl, *decl_id, graph, entry_node);
Ok(leaves.to_vec())
}
ImplTrait { decl_id, .. } => {
Expand Down Expand Up @@ -830,8 +830,8 @@ fn get_trait_fn_node_index<'a>(
.namespace
.find_trait_method(&trait_decl.name.into(), &fn_decl.name))
}
ty::TyDeclaration::StructDeclaration(decl_ref) => {
let struct_decl = decl_engine.get_struct(&decl_ref);
ty::TyDeclaration::StructDeclaration { decl_id, .. } => {
let struct_decl = decl_engine.get_struct(&decl_id);
Ok(graph
.namespace
.find_trait_method(&struct_decl.call_path.suffix.into(), &fn_decl.name))
Expand Down Expand Up @@ -1687,17 +1687,18 @@ fn construct_dead_code_warning_from_node(
},
ty::TyAstNode {
content:
ty::TyAstNodeContent::Declaration(ty::TyDeclaration::StructDeclaration(decl_ref)),
ty::TyAstNodeContent::Declaration(ty::TyDeclaration::StructDeclaration { name, .. }),
..
} => CompileWarning {
span: decl_ref.name.span(),
span: name.span(),
warning_content: Warning::DeadStructDeclaration,
},
ty::TyAstNode {
content: ty::TyAstNodeContent::Declaration(ty::TyDeclaration::EnumDeclaration(decl_ref)),
content:
ty::TyAstNodeContent::Declaration(ty::TyDeclaration::EnumDeclaration { name, .. }),
..
} => CompileWarning {
span: decl_ref.name.span(),
span: name.span(),
warning_content: Warning::DeadEnumDeclaration,
},
ty::TyAstNode {
Expand Down Expand Up @@ -1834,11 +1835,11 @@ fn allow_dead_code_ast_node(decl_engine: &DeclEngine, node: &ty::TyAstNode) -> b
ty::TyDeclaration::TraitDeclaration { decl_id, .. } => {
allow_dead_code(decl_engine.get_trait(decl_id).attributes)
}
ty::TyDeclaration::StructDeclaration(decl_ref) => {
allow_dead_code(decl_engine.get_struct(decl_ref).attributes)
ty::TyDeclaration::StructDeclaration { decl_id, .. } => {
allow_dead_code(decl_engine.get_struct(decl_id).attributes)
}
ty::TyDeclaration::EnumDeclaration(decl_ref) => {
allow_dead_code(decl_engine.get_enum(decl_ref).attributes)
ty::TyDeclaration::EnumDeclaration { decl_id, .. } => {
allow_dead_code(decl_engine.get_enum(decl_id).attributes)
}
ty::TyDeclaration::ImplTrait { .. } => false,
ty::TyDeclaration::AbiDeclaration { .. } => false,
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ impl<'eng> FnCompiler<'eng> {
span: ast_node.span.clone(),
})
}
ty::TyDeclaration::EnumDeclaration(decl_ref) => {
let ted = self.decl_engine.get_enum(decl_ref);
ty::TyDeclaration::EnumDeclaration { decl_id, .. } => {
let ted = self.decl_engine.get_enum(decl_id);
create_enum_aggregate(
self.type_engine,
self.decl_engine,
Expand Down
6 changes: 4 additions & 2 deletions sway-core/src/language/ty/ast_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,12 @@ impl TyAstNode {
} => decl_engine.get_trait(decl_id).visibility.is_public(),
TyAstNode {
content:
TyAstNodeContent::Declaration(TyDeclaration::StructDeclaration(decl_ref)),
TyAstNodeContent::Declaration(TyDeclaration::StructDeclaration {
decl_id, ..
}),
..
} => {
let struct_decl = decl_engine.get_struct(decl_ref);
let struct_decl = decl_engine.get_struct(decl_id);
struct_decl.visibility == Visibility::Public
}
TyAstNode {
Expand Down
132 changes: 97 additions & 35 deletions sway-core/src/language/ty/declaration/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ pub enum TyDeclaration {
decl_id: DeclId<TyTraitDeclaration>,
decl_span: Span,
},
StructDeclaration(DeclRefStruct),
EnumDeclaration(DeclRefEnum),
StructDeclaration {
name: Ident,
decl_id: DeclId<TyStructDeclaration>,
decl_span: Span,
},
EnumDeclaration {
name: Ident,
decl_id: DeclId<TyEnumDeclaration>,
decl_span: Span,
},
ImplTrait {
name: Ident,
decl_id: DeclId<TyImplTrait>,
Expand Down Expand Up @@ -102,10 +110,30 @@ impl PartialEqWithEngines for TyDeclaration {
..
},
) => ln == rn && decl_engine.get(*lid).eq(&decl_engine.get(*rid), engines),
(Self::StructDeclaration(lref), Self::StructDeclaration(rref)) => {
lref.eq(rref, engines)
}
(Self::EnumDeclaration(lref), Self::EnumDeclaration(rref)) => lref.eq(rref, engines),
(
Self::StructDeclaration {
name: ln,
decl_id: lid,
..
},
Self::StructDeclaration {
name: rn,
decl_id: rid,
..
},
) => ln == rn && decl_engine.get(*lid).eq(&decl_engine.get(*rid), engines),
(
Self::EnumDeclaration {
name: ln,
decl_id: lid,
..
},
Self::EnumDeclaration {
name: rn,
decl_id: rid,
..
},
) => ln == rn && decl_engine.get(*lid).eq(&decl_engine.get(*rid), engines),
(
Self::ImplTrait {
name: ln,
Expand Down Expand Up @@ -170,11 +198,11 @@ impl HashWithEngines for TyDeclaration {
TraitDeclaration { decl_id, .. } => {
decl_engine.get(*decl_id).hash(state, engines);
}
StructDeclaration(decl_ref) => {
decl_engine.get_struct(decl_ref).hash(state, engines);
StructDeclaration { decl_id, .. } => {
decl_engine.get(*decl_id).hash(state, engines);
}
EnumDeclaration(decl_ref) => {
decl_engine.get_enum(decl_ref).hash(state, engines);
EnumDeclaration { decl_id, .. } => {
decl_engine.get(*decl_id).hash(state, engines);
}
ImplTrait { decl_id, .. } => {
decl_engine.get(*decl_id).hash(state, engines);
Expand Down Expand Up @@ -205,8 +233,12 @@ impl SubstTypes for TyDeclaration {
TraitDeclaration {
ref mut decl_id, ..
} => decl_id.subst(type_mapping, engines),
StructDeclaration(ref mut decl_ref) => decl_ref.subst(type_mapping, engines),
EnumDeclaration(ref mut decl_ref) => decl_ref.subst(type_mapping, engines),
StructDeclaration {
ref mut decl_id, ..
} => decl_id.subst(type_mapping, engines),
EnumDeclaration {
ref mut decl_id, ..
} => decl_id.subst(type_mapping, engines),
ImplTrait {
ref mut decl_id, ..
} => decl_id.subst(type_mapping, engines),
Expand All @@ -231,8 +263,12 @@ impl ReplaceSelfType for TyDeclaration {
TraitDeclaration {
ref mut decl_id, ..
} => decl_id.replace_self_type(engines, self_type),
StructDeclaration(ref mut decl_ref) => decl_ref.replace_self_type(engines, self_type),
EnumDeclaration(ref mut decl_ref) => decl_ref.replace_self_type(engines, self_type),
StructDeclaration {
ref mut decl_id, ..
} => decl_id.replace_self_type(engines, self_type),
EnumDeclaration {
ref mut decl_id, ..
} => decl_id.replace_self_type(engines, self_type),
ImplTrait {
ref mut decl_id, ..
} => decl_id.replace_self_type(engines, self_type),
Expand Down Expand Up @@ -275,9 +311,9 @@ impl Spanned for TyDeclaration {
| ImplTrait { decl_span, .. }
| ConstantDeclaration { decl_span, .. }
| StorageDeclaration { decl_span, .. }
| AbiDeclaration { decl_span, .. } => decl_span.clone(),
StructDeclaration(decl_ref) => decl_ref.span(),
EnumDeclaration(decl_ref) => decl_ref.span(),
| AbiDeclaration { decl_span, .. }
| StructDeclaration { decl_span, .. }
| EnumDeclaration { decl_span, .. } => decl_span.clone(),
GenericTypeForFunctionScope { name, .. } => name.span(),
ErrorRecovery(span) => span.clone(),
}
Expand Down Expand Up @@ -318,9 +354,9 @@ impl DisplayWithEngines for TyDeclaration {
builder
}
TyDeclaration::FunctionDeclaration { name, .. }
| TyDeclaration::TraitDeclaration { name, .. } => name.as_str().into(),
TyDeclaration::StructDeclaration(decl_ref) => decl_ref.name.as_str().into(),
TyDeclaration::EnumDeclaration(decl_ref) => decl_ref.name.as_str().into(),
| TyDeclaration::TraitDeclaration { name, .. }
| TyDeclaration::StructDeclaration { name, .. }
| TyDeclaration::EnumDeclaration { name, .. } => name.as_str().into(),
_ => String::new(),
}
)
Expand Down Expand Up @@ -401,9 +437,9 @@ impl GetDeclIdent for TyDeclaration {
| TyDeclaration::ConstantDeclaration { name, .. }
| TyDeclaration::ImplTrait { name, .. }
| TyDeclaration::AbiDeclaration { name, .. }
| TyDeclaration::GenericTypeForFunctionScope { name, .. } => Some(name.clone()),
TyDeclaration::StructDeclaration(decl_ref) => Some(decl_ref.name.clone()),
TyDeclaration::EnumDeclaration(decl_ref) => Some(decl_ref.name.clone()),
| TyDeclaration::GenericTypeForFunctionScope { name, .. }
| TyDeclaration::StructDeclaration { name, .. }
| TyDeclaration::EnumDeclaration { name, .. } => Some(name.clone()),
TyDeclaration::ErrorRecovery(_) => None,
TyDeclaration::StorageDeclaration { .. } => None,
}
Expand All @@ -416,7 +452,15 @@ impl TyDeclaration {
/// Returns an error if `self` is not a [TyEnumDeclaration].
pub(crate) fn expect_enum(&self) -> CompileResult<DeclRefEnum> {
match self {
TyDeclaration::EnumDeclaration(decl_ref) => ok(decl_ref.clone(), vec![], vec![]),
TyDeclaration::EnumDeclaration {
name,
decl_id,
decl_span,
} => ok(
DeclRef::new(name.clone(), *decl_id, decl_span.clone()),
vec![],
vec![],
),
TyDeclaration::ErrorRecovery(_) => err(vec![], vec![]),
decl => err(
vec![],
Expand All @@ -435,7 +479,15 @@ impl TyDeclaration {
let warnings = vec![];
let mut errors = vec![];
match self {
TyDeclaration::StructDeclaration(decl_ref) => ok(decl_ref.clone(), warnings, errors),
TyDeclaration::StructDeclaration {
name,
decl_id,
decl_span,
} => ok(
DeclRef::new(name.clone(), *decl_id, decl_span.clone()),
vec![],
vec![],
),
TyDeclaration::ErrorRecovery(_) => err(vec![], vec![]),
decl => {
errors.push(CompileError::DeclIsNotAStruct {
Expand Down Expand Up @@ -603,12 +655,22 @@ impl TyDeclaration {
let decl = decl_engine.get_function(decl_id);
decl.return_type.type_id
}
TyDeclaration::StructDeclaration(decl_ref) => {
type_engine.insert(decl_engine, TypeInfo::Struct(decl_ref.clone()))
}
TyDeclaration::EnumDeclaration(decl_ref) => {
type_engine.insert(decl_engine, TypeInfo::Enum(decl_ref.clone()))
}
TyDeclaration::StructDeclaration {
name,
decl_id,
decl_span,
} => type_engine.insert(
decl_engine,
TypeInfo::Struct(DeclRef::new(name.clone(), *decl_id, decl_span.clone())),
),
TyDeclaration::EnumDeclaration {
name,
decl_id,
decl_span,
} => type_engine.insert(
decl_engine,
TypeInfo::Enum(DeclRef::new(name.clone(), *decl_id, decl_span.clone())),
),
TyDeclaration::StorageDeclaration { decl_id, .. } => {
let storage_decl = decl_engine.get_storage(decl_id);
type_engine.insert(
Expand Down Expand Up @@ -642,12 +704,12 @@ impl TyDeclaration {
let TyConstantDeclaration { visibility, .. } = decl_engine.get_constant(decl_id);
visibility
}
StructDeclaration(decl_ref) => {
let TyStructDeclaration { visibility, .. } = decl_engine.get_struct(decl_ref);
StructDeclaration { decl_id, .. } => {
let TyStructDeclaration { visibility, .. } = decl_engine.get_struct(decl_id);
visibility
}
EnumDeclaration(decl_ref) => {
let TyEnumDeclaration { visibility, .. } = decl_engine.get_enum(decl_ref);
EnumDeclaration { decl_id, .. } => {
let TyEnumDeclaration { visibility, .. } = decl_engine.get_enum(decl_id);
visibility
}
FunctionDeclaration { decl_id, .. } => {
Expand Down
19 changes: 13 additions & 6 deletions sway-core/src/semantic_analysis/ast_node/code_block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::*;
use crate::language::{parsed::CodeBlock, ty};
use crate::{
decl_engine::DeclRef,
language::{parsed::CodeBlock, ty},
};

impl ty::TyCodeBlock {
pub(crate) fn type_check(
Expand Down Expand Up @@ -68,12 +71,16 @@ impl ty::TyCodeBlock {
.resolve_symbol(&never_mod_path, &never_ident)
.value;

if let Some(ty::TyDeclaration::EnumDeclaration(never_decl_ref)) = never_decl_opt
if let Some(ty::TyDeclaration::EnumDeclaration {
name,
decl_id,
decl_span,
}) = never_decl_opt
{
return ctx
.engines()
.te()
.insert(decl_engine, TypeInfo::Enum(never_decl_ref.clone()));
return ctx.engines().te().insert(
decl_engine,
TypeInfo::Enum(DeclRef::new(name.clone(), *decl_id, decl_span.clone())),
);
}

ctx.type_engine.insert(decl_engine, TypeInfo::Unknown)
Expand Down
Loading

0 comments on commit 4c1c1d8

Please sign in to comment.