Skip to content

Commit

Permalink
Applies review changes.
Browse files Browse the repository at this point in the history
Replaces `IdentUnique` usage with `(Ident, TyFunctionSig)`.
Borrowed TyFunctionSig from 3878.
  • Loading branch information
esdrubal committed Jan 27, 2023
1 parent e17372a commit 174438f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
20 changes: 15 additions & 5 deletions sway-core/src/decl_engine/mapping.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{collections::BTreeMap, fmt};

use sway_types::{Ident, IdentUnique};
use sway_types::Ident;

use crate::language::ty::TyFunctionSig;

use super::DeclId;

Expand All @@ -10,7 +12,7 @@ type DestinationDecl = DeclId;
/// The [DeclMapping] is used to create a mapping between a [SourceDecl] (LHS)
/// and a [DestinationDecl] (RHS).
pub(crate) struct DeclMapping {
pub(crate) mapping: Vec<(SourceDecl, DestinationDecl)>,
mapping: Vec<(SourceDecl, DestinationDecl)>,
}

impl fmt::Display for DeclMapping {
Expand Down Expand Up @@ -46,6 +48,14 @@ impl DeclMapping {
self.mapping.is_empty()
}

pub(crate) fn mapping(&self) -> Vec<(SourceDecl, DestinationDecl)> {
self.mapping.clone()
}

pub(crate) fn from_mapping(mapping: Vec<(SourceDecl, DestinationDecl)>) -> DeclMapping {
DeclMapping { mapping }
}

pub(crate) fn from_stub_and_impld_decl_ids(
stub_decl_ids: BTreeMap<Ident, DeclId>,
impld_decl_ids: BTreeMap<Ident, DeclId>,
Expand All @@ -59,13 +69,13 @@ impl DeclMapping {
DeclMapping { mapping }
}

pub(crate) fn from_stub_and_impld_decl_ids_unique(
pub(crate) fn from_stub_and_impld_decl_ids_with_function_sig(
stub_decl_ids: BTreeMap<Ident, DeclId>,
new_decl_ids: BTreeMap<IdentUnique, DeclId>,
new_decl_ids: BTreeMap<(Ident, TyFunctionSig), DeclId>,
) -> DeclMapping {
let mut mapping = vec![];
for (stub_decl_name, stub_decl_id) in stub_decl_ids.into_iter() {
for (new_decl_name, new_decl_id) in new_decl_ids.iter() {
for ((new_decl_name, _), new_decl_id) in new_decl_ids.iter() {
if new_decl_name.as_str() != stub_decl_name.as_str() {
continue;
}
Expand Down
19 changes: 19 additions & 0 deletions sway-core/src/language/ty/declaration/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,22 @@ impl TyFunctionParameter {
self.name.as_str() == "self"
}
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TyFunctionSig {
pub return_type: TypeId,
pub parameters: Vec<TypeId>,
}

impl TyFunctionSig {
pub fn from_fn_decl(fn_decl: &TyFunctionDeclaration) -> Self {
Self {
return_type: fn_decl.return_type,
parameters: fn_decl
.parameters
.iter()
.map(|p| p.type_id)
.collect::<Vec<_>>(),
}
}
}
14 changes: 5 additions & 9 deletions sway-core/src/language/ty/expression/expression_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ impl ReplaceDecls for TyExpressionVariant {
// declaration to be replaced by two different implemented function
// declaration. The code below filters the correct implementation to use.
let mut decl_mapping_filtered = vec![];
'decls: for (orig_decl_id, dest_decl_id) in decl_mapping.mapping.clone() {
'decls: for (orig_decl_id, dest_decl_id) in decl_mapping.mapping() {
let all_parents = engines
.de()
.find_all_parents(engines, function_decl_id.clone());
Expand Down Expand Up @@ -663,17 +663,13 @@ impl ReplaceDecls for TyExpressionVariant {
}
}

// No function with same arguemnt type were found so we try to replace decls as usual
// No function with same argument type were found so we try to replace decls as usual
if decl_mapping_filtered.is_empty() {
decl_mapping_filtered = decl_mapping.mapping.clone();
decl_mapping_filtered = decl_mapping.mapping();
}

function_decl_id.replace_decls(
&DeclMapping {
mapping: decl_mapping_filtered,
},
engines,
);
function_decl_id
.replace_decls(&DeclMapping::from_mapping(decl_mapping_filtered), engines);
let new_decl_id = function_decl_id
.clone()
.replace_decls_and_insert_new(decl_mapping, engines);
Expand Down
16 changes: 11 additions & 5 deletions sway-core/src/type_system/type_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ use crate::{
decl_engine::*,
engine_threading::*,
error::*,
language::{ty, CallPath},
language::{
ty::{self, TyFunctionSig},
CallPath,
},
semantic_analysis::*,
type_system::*,
};

use sway_error::error::CompileError;
use sway_types::{ident::Ident, span::Span, IdentUnique, Spanned};
use sway_types::{ident::Ident, span::Span, Spanned};

use fuel_abi_types::program_abi;

Expand Down Expand Up @@ -191,7 +194,7 @@ impl TypeParameter {
let mut errors = vec![];

let mut original_method_ids: BTreeMap<Ident, DeclId> = BTreeMap::new();
let mut impld_method_ids: BTreeMap<IdentUnique, DeclId> = BTreeMap::new();
let mut impld_method_ids: BTreeMap<(Ident, TyFunctionSig), DeclId> = BTreeMap::new();

for type_param in type_parameters.iter() {
let TypeParameter {
Expand Down Expand Up @@ -229,13 +232,16 @@ impl TypeParameter {
);
original_method_ids.extend(trait_original_method_ids);
for (key, value) in trait_impld_method_ids {
impld_method_ids.insert(key.into(), value);
if let Ok(fn_decl) = ctx.decl_engine.get_function(value.clone(), access_span) {
impld_method_ids
.insert((key, TyFunctionSig::from_fn_decl(&fn_decl)), value);
}
}
}
}

if errors.is_empty() {
let decl_mapping = DeclMapping::from_stub_and_impld_decl_ids_unique(
let decl_mapping = DeclMapping::from_stub_and_impld_decl_ids_with_function_sig(
original_method_ids,
impld_method_ids,
);
Expand Down

0 comments on commit 174438f

Please sign in to comment.