Skip to content

Commit

Permalink
fix rewrites of ordered_hash_map so the key is also rewritten (#6945)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerStarkware authored Dec 29, 2024
1 parent 62add07 commit e133839
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
7 changes: 4 additions & 3 deletions crates/cairo-lang-semantic/src/expr/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ use crate::items::imp::{
GeneratedImplId, GeneratedImplItems, GeneratedImplLongId, ImplId, ImplImplId, ImplLongId,
ImplLookupContext, UninferredGeneratedImplId, UninferredGeneratedImplLongId, UninferredImpl,
};
use crate::items::trt::{ConcreteTraitGenericFunctionId, ConcreteTraitGenericFunctionLongId};
use crate::items::trt::{
ConcreteTraitGenericFunctionId, ConcreteTraitGenericFunctionLongId, ConcreteTraitTypeId,
ConcreteTraitTypeLongId,
};
use crate::substitution::{HasDb, RewriteResult, SemanticRewriter, SubstitutionRewriter};
use crate::types::{
ClosureTypeLongId, ConcreteEnumLongId, ConcreteExternTypeLongId, ConcreteStructLongId,
Expand Down Expand Up @@ -933,14 +936,12 @@ impl<'db> Inference<'db> {
}
_ => {}
};

let (canonical_trait, canonicalizer) = CanonicalTrait::canonicalize(
self.db,
self.inference_id,
concrete_trait_id,
impl_var_trait_item_mappings,
);

// impl_type_bounds order is deterimend by the generic params of the function and therefore
// is consistent.
let solution_set = match self.db.canonic_trait_solutions(
Expand Down
5 changes: 4 additions & 1 deletion crates/cairo-lang-semantic/src/expr/inference/canonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ use crate::items::imp::{
GeneratedImplId, GeneratedImplItems, GeneratedImplLongId, ImplId, ImplImplId, ImplLongId,
UninferredGeneratedImplId, UninferredGeneratedImplLongId, UninferredImpl,
};
use crate::items::trt::{ConcreteTraitGenericFunctionId, ConcreteTraitGenericFunctionLongId};
use crate::items::trt::{
ConcreteTraitGenericFunctionId, ConcreteTraitGenericFunctionLongId, ConcreteTraitTypeId,
ConcreteTraitTypeLongId,
};
use crate::substitution::{HasDb, RewriteResult, SemanticObject, SemanticRewriter};
use crate::types::{
ClosureTypeLongId, ConcreteEnumLongId, ConcreteExternTypeLongId, ConcreteStructLongId,
Expand Down
27 changes: 23 additions & 4 deletions crates/cairo-lang-semantic/src/substitution.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::VecDeque;
use std::hash::Hash;
use std::ops::{Deref, DerefMut};

use cairo_lang_defs::ids::{
Expand Down Expand Up @@ -28,7 +29,10 @@ use crate::items::imp::{
GeneratedImplId, GeneratedImplItems, GeneratedImplLongId, ImplId, ImplImplId, ImplLongId,
UninferredGeneratedImplId, UninferredGeneratedImplLongId, UninferredImpl,
};
use crate::items::trt::{ConcreteTraitGenericFunctionId, ConcreteTraitGenericFunctionLongId};
use crate::items::trt::{
ConcreteTraitGenericFunctionId, ConcreteTraitGenericFunctionLongId, ConcreteTraitTypeId,
ConcreteTraitTypeLongId,
};
use crate::types::{
ClosureTypeLongId, ConcreteEnumLongId, ConcreteExternTypeLongId, ConcreteStructLongId,
ImplTypeId,
Expand Down Expand Up @@ -198,19 +202,32 @@ impl<T, E, TRewriter: SemanticRewriter<T, E>> SemanticRewriter<Box<T>, E> for TR
}
}

impl<T: Clone, V: Clone, E, TRewriter: SemanticRewriter<V, E>>
impl<T: Clone + Hash + Eq, V: Clone, E, TRewriter: SemanticRewriter<V, E> + SemanticRewriter<T, E>>
SemanticRewriter<OrderedHashMap<T, V>, E> for TRewriter
{
fn internal_rewrite(&mut self, value: &mut OrderedHashMap<T, V>) -> Result<RewriteResult, E> {
let mut result = RewriteResult::NoChange;
for el in value.iter_mut() {
match self.internal_rewrite(el.1)? {
let mut changed_key = Vec::new();
for (k, v) in value.iter_mut() {
let mut temp_key = k.clone();
match self.internal_rewrite(&mut temp_key)? {
RewriteResult::Modified => {
changed_key.push((k.clone(), temp_key));
result = RewriteResult::Modified;
}
RewriteResult::NoChange => {}
}
match self.internal_rewrite(v)? {
RewriteResult::Modified => {
result = RewriteResult::Modified;
}
RewriteResult::NoChange => {}
}
}
for (old_key, new_key) in changed_key {
let v = value.swap_remove(&old_key).unwrap();
value.insert(new_key, v);
}

Ok(result)
}
Expand Down Expand Up @@ -344,6 +361,8 @@ macro_rules! add_basic_rewrites {
$crate::prune_single!(__regular_helper, ConcreteExternTypeLongId, $($exclude)*);
$crate::prune_single!(__regular_helper, ConcreteTraitId, $($exclude)*);
$crate::prune_single!(__regular_helper, ConcreteTraitLongId, $($exclude)*);
$crate::prune_single!(__regular_helper, ConcreteTraitTypeId, $($exclude)*);
$crate::prune_single!(__regular_helper, ConcreteTraitTypeLongId, $($exclude)*);
$crate::prune_single!(__regular_helper, ConcreteImplId, $($exclude)*);
$crate::prune_single!(__regular_helper, ConcreteImplLongId, $($exclude)*);
$crate::prune_single!(__regular_helper, ConcreteTraitGenericFunctionLongId, $($exclude)*);
Expand Down

0 comments on commit e133839

Please sign in to comment.