Skip to content

Commit

Permalink
Implement EqWithEngines and PartialEqWithEngines for parsed decla…
Browse files Browse the repository at this point in the history
…rations (#6054)

## Description

As the title says. Will be used in a following PR.

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] 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.
  • Loading branch information
tritao authored May 27, 2024
1 parent 5389aa8 commit 96d4cb6
Show file tree
Hide file tree
Showing 25 changed files with 891 additions and 40 deletions.
45 changes: 44 additions & 1 deletion sway-core/src/decl_engine/interface_decl_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,47 @@
use crate::{decl_engine::*, language::ty};
use crate::{
decl_engine::*,
engine_threading::{EqWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext},
language::{
parsed::{AbiDeclaration, TraitDeclaration},
ty,
},
};

use super::{parsed_engine::ParsedDeclEngineGet, parsed_id::ParsedDeclId};

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub enum ParsedInterfaceDeclId {
Abi(ParsedDeclId<AbiDeclaration>),
Trait(ParsedDeclId<TraitDeclaration>),
}

impl EqWithEngines for ParsedInterfaceDeclId {}
impl PartialEqWithEngines for ParsedInterfaceDeclId {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
let decl_engine = ctx.engines().pe();
match (self, other) {
(ParsedInterfaceDeclId::Abi(lhs), ParsedInterfaceDeclId::Abi(rhs)) => {
decl_engine.get(lhs).eq(&decl_engine.get(rhs), ctx)
}
(ParsedInterfaceDeclId::Trait(lhs), ParsedInterfaceDeclId::Trait(rhs)) => {
decl_engine.get(lhs).eq(&decl_engine.get(rhs), ctx)
}
_ => false,
}
}
}

impl From<ParsedDeclId<AbiDeclaration>> for ParsedInterfaceDeclId {
fn from(id: ParsedDeclId<AbiDeclaration>) -> Self {
Self::Abi(id)
}
}

impl From<ParsedDeclId<TraitDeclaration>> for ParsedInterfaceDeclId {
fn from(id: ParsedDeclId<TraitDeclaration>) -> Self {
Self::Trait(id)
}
}

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub enum InterfaceDeclId {
Expand Down
21 changes: 16 additions & 5 deletions sway-core/src/decl_engine/parsed_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::hash::{DefaultHasher, Hasher};
use std::marker::PhantomData;
use std::{fmt, hash::Hash};

use crate::engine_threading::{EqWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext};

use super::DeclUniqueId;

pub type ParsedDeclIdIndexType = usize;
Expand Down Expand Up @@ -40,16 +42,25 @@ impl<T> Clone for ParsedDeclId<T> {
}

impl<T> Eq for ParsedDeclId<T> {}
impl<T> Hash for ParsedDeclId<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state)
}
}
impl<T> PartialEq for ParsedDeclId<T> {
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}

impl<T> EqWithEngines for ParsedDeclId<T> {}
impl<T> PartialEqWithEngines for ParsedDeclId<T> {
fn eq(&self, other: &Self, _ctx: &PartialEqWithEnginesContext) -> bool {
self.0 == other.0
}
}

impl<T> Hash for ParsedDeclId<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state)
}
}

impl<T> PartialOrd for ParsedDeclId<T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/language/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::hash::{Hash, Hasher};

use sway_types::{BaseIdent, Ident, Span};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Eq)]
pub struct AsmOp {
pub(crate) op_name: Ident,
pub(crate) op_args: Vec<Ident>,
Expand Down
30 changes: 29 additions & 1 deletion sway-core/src/language/call_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ impl PartialEqWithEngines for QualifiedCallPath {
call_path,
qualified_path_root,
} = self;
call_path.eq(&other.call_path) && qualified_path_root.eq(&other.qualified_path_root, ctx)
PartialEqWithEngines::eq(call_path, &other.call_path, ctx)
&& qualified_path_root.eq(&other.qualified_path_root, ctx)
}
}

Expand Down Expand Up @@ -187,6 +188,33 @@ pub struct CallPath<T = Ident> {
pub is_absolute: bool,
}

impl EqWithEngines for CallPath {}
impl PartialEqWithEngines for CallPath {
fn eq(&self, other: &Self, _ctx: &PartialEqWithEnginesContext) -> bool {
self.prefixes == other.prefixes
&& self.suffix == other.suffix
&& self.is_absolute == other.is_absolute
}
}

impl<T: EqWithEngines> EqWithEngines for CallPath<T> {}
impl<T: PartialEqWithEngines> PartialEqWithEngines for CallPath<T> {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
self.prefixes == other.prefixes
&& self.suffix.eq(&other.suffix, ctx)
&& self.is_absolute == other.is_absolute
}
}

impl<T: OrdWithEngines> OrdWithEngines for CallPath<T> {
fn cmp(&self, other: &Self, ctx: &OrdWithEnginesContext) -> Ordering {
self.prefixes
.cmp(&other.prefixes)
.then_with(|| self.suffix.cmp(&other.suffix, ctx))
.then_with(|| self.is_absolute.cmp(&other.is_absolute))
}
}

impl std::convert::From<Ident> for CallPath {
fn from(other: Ident) -> Self {
CallPath {
Expand Down
12 changes: 11 additions & 1 deletion sway-core/src/language/parsed/code_block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::language::parsed::AstNode;
use crate::{
engine_threading::{EqWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext},
language::parsed::AstNode,
};

use sway_types::{span::Span, Spanned};

Expand All @@ -8,6 +11,13 @@ pub struct CodeBlock {
pub(crate) whole_block_span: Span,
}

impl EqWithEngines for CodeBlock {}
impl PartialEqWithEngines for CodeBlock {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
self.contents.eq(&other.contents, ctx)
}
}

impl Spanned for CodeBlock {
fn span(&self) -> Span {
self.whole_block_span.clone()
Expand Down
51 changes: 50 additions & 1 deletion sway-core/src/language/parsed/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ pub use variable::*;

use crate::{
decl_engine::{parsed_engine::ParsedDeclEngineGet, parsed_id::ParsedDeclId},
engine_threading::{DebugWithEngines, DisplayWithEngines},
engine_threading::{
DebugWithEngines, DisplayWithEngines, EqWithEngines, PartialEqWithEngines,
PartialEqWithEnginesContext,
},
Engines,
};

Expand Down Expand Up @@ -142,3 +145,49 @@ impl DebugWithEngines for Declaration {
DisplayWithEngines::fmt(&self, f, engines)
}
}

impl EqWithEngines for Declaration {}
impl PartialEqWithEngines for Declaration {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
let decl_engine = ctx.engines().pe();
match (self, other) {
(Declaration::VariableDeclaration(lid), Declaration::VariableDeclaration(rid)) => {
decl_engine.get(lid).eq(&decl_engine.get(rid), ctx)
}
(Declaration::FunctionDeclaration(lid), Declaration::FunctionDeclaration(rid)) => {
decl_engine.get(lid).eq(&decl_engine.get(rid), ctx)
}
(Declaration::TraitDeclaration(lid), Declaration::TraitDeclaration(rid)) => {
decl_engine.get(lid).eq(&decl_engine.get(rid), ctx)
}
(Declaration::StructDeclaration(lid), Declaration::StructDeclaration(rid)) => {
decl_engine.get(lid).eq(&decl_engine.get(rid), ctx)
}
(Declaration::EnumDeclaration(lid), Declaration::EnumDeclaration(rid)) => {
decl_engine.get(lid).eq(&decl_engine.get(rid), ctx)
}
(Declaration::ImplTrait(lid), Declaration::ImplTrait(rid)) => {
decl_engine.get(lid).eq(&decl_engine.get(rid), ctx)
}
(Declaration::ImplSelf(lid), Declaration::ImplSelf(rid)) => {
decl_engine.get(lid).eq(&decl_engine.get(rid), ctx)
}
(Declaration::AbiDeclaration(lid), Declaration::AbiDeclaration(rid)) => {
decl_engine.get(lid).eq(&decl_engine.get(rid), ctx)
}
(Declaration::ConstantDeclaration(lid), Declaration::ConstantDeclaration(rid)) => {
decl_engine.get(lid).eq(&decl_engine.get(rid), ctx)
}
(Declaration::StorageDeclaration(lid), Declaration::StorageDeclaration(rid)) => {
decl_engine.get(lid).eq(&decl_engine.get(rid), ctx)
}
(Declaration::TypeAliasDeclaration(lid), Declaration::TypeAliasDeclaration(rid)) => {
decl_engine.get(lid).eq(&decl_engine.get(rid), ctx)
}
(Declaration::TraitTypeDeclaration(lid), Declaration::TraitTypeDeclaration(rid)) => {
decl_engine.get(lid).eq(&decl_engine.get(rid), ctx)
}
_ => false,
}
}
}
18 changes: 17 additions & 1 deletion sway-core/src/language/parsed/declaration/abi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{decl_engine::parsed_id::ParsedDeclId, transform};
use crate::{
decl_engine::parsed_id::ParsedDeclId,
engine_threading::{EqWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext},
transform,
};

use super::{FunctionDeclaration, Supertrait, TraitItem};

Expand All @@ -19,6 +23,18 @@ pub struct AbiDeclaration {
pub attributes: transform::AttributesMap,
}

impl EqWithEngines for AbiDeclaration {}
impl PartialEqWithEngines for AbiDeclaration {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
self.name == other.name
&& self.interface_surface.eq(&other.interface_surface, ctx)
&& self.supertraits.eq(&other.supertraits, ctx)
&& PartialEqWithEngines::eq(&self.methods, &other.methods, ctx)
&& self.span == other.span
&& self.attributes == other.attributes
}
}

impl Named for AbiDeclaration {
fn name(&self) -> &sway_types::BaseIdent {
&self.name
Expand Down
17 changes: 16 additions & 1 deletion sway-core/src/language/parsed/declaration/constant.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{
engine_threading::DebugWithEngines,
engine_threading::{
DebugWithEngines, EqWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext,
},
language::{parsed::Expression, Visibility},
transform, Engines, TypeArgument,
};
Expand All @@ -16,6 +18,19 @@ pub struct ConstantDeclaration {
pub span: Span,
}

impl EqWithEngines for ConstantDeclaration {}
impl PartialEqWithEngines for ConstantDeclaration {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
self.name == other.name
&& self.attributes == other.attributes
&& self.type_ascription.eq(&other.type_ascription, ctx)
&& self.value.eq(&other.value, ctx)
&& self.visibility == other.visibility
&& self.is_configurable == other.is_configurable
&& self.span == other.span
}
}

impl Named for ConstantDeclaration {
fn name(&self) -> &sway_types::BaseIdent {
&self.name
Expand Down
30 changes: 29 additions & 1 deletion sway-core/src/language/parsed/declaration/enum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::{language::Visibility, transform, type_system::*};
use crate::{
engine_threading::{EqWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext},
language::Visibility,
transform,
type_system::*,
};
use sway_types::{ident::Ident, span::Span, Named, Spanned};

#[derive(Debug, Clone)]
Expand All @@ -11,6 +16,18 @@ pub struct EnumDeclaration {
pub visibility: Visibility,
}

impl EqWithEngines for EnumDeclaration {}
impl PartialEqWithEngines for EnumDeclaration {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
self.name == other.name
&& self.attributes == other.attributes
&& self.type_parameters.eq(&other.type_parameters, ctx)
&& self.variants.eq(&other.variants, ctx)
&& self.visibility == other.visibility
&& self.span == other.span
}
}

impl Named for EnumDeclaration {
fn name(&self) -> &sway_types::BaseIdent {
&self.name
Expand All @@ -31,3 +48,14 @@ pub struct EnumVariant {
pub(crate) tag: usize,
pub(crate) span: Span,
}

impl EqWithEngines for EnumVariant {}
impl PartialEqWithEngines for EnumVariant {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
self.name == other.name
&& self.attributes == other.attributes
&& self.type_argument.eq(&other.type_argument, ctx)
&& self.tag == other.tag
&& self.span == other.span
}
}
14 changes: 14 additions & 0 deletions sway-core/src/language/parsed/declaration/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ pub struct FunctionDeclaration {
pub kind: FunctionDeclarationKind,
}

impl EqWithEngines for FunctionDeclaration {}
impl PartialEqWithEngines for FunctionDeclaration {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
self.purity == other.purity
&& self.attributes == other.attributes
&& self.name == other.name
&& self.visibility == other.visibility
&& self.body.eq(&other.body, ctx)
&& self.parameters.eq(&other.parameters, ctx)
&& self.return_type.eq(&other.return_type, ctx)
&& self.type_parameters.eq(&other.type_parameters, ctx)
}
}

impl DebugWithEngines for FunctionDeclaration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, _engines: &Engines) -> std::fmt::Result {
f.write_fmt(format_args!("{}", self.name))
Expand Down
Loading

0 comments on commit 96d4cb6

Please sign in to comment.