Skip to content

Commit

Permalink
fix: LocalEnv::merge to also merge the bindings vector (#596)
Browse files Browse the repository at this point in the history
* Fix LocalEnv::merge and merge bindings

* added a unit test
  • Loading branch information
dotansimha committed Dec 14, 2023
1 parent 59fb310 commit a24c6d3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/compiler/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ impl LocalEnv {
for (ident, other_details) in other.bindings {
if let Some(self_details) = self.bindings.get_mut(&ident) {
*self_details = self_details.clone().merge(other_details);
} else {
self.bindings.insert(ident, other_details);
}
}
self
Expand Down
29 changes: 28 additions & 1 deletion src/compiler/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ mod value_target_impl {
mod tests {
#![allow(clippy::print_stdout)] // tests

use crate::owned_value_path;
use crate::{
compiler::{parser::Ident, state::LocalEnv, type_def::Details, TypeDef},
owned_value_path,
};

use super::*;
use crate::value;
Expand Down Expand Up @@ -530,4 +533,28 @@ mod tests {
);
}
}

#[test]
fn merge_multiple_local_env() {
let mut a = LocalEnv::default();
a.insert_variable(
Ident("foo".into()),
Details {
type_def: TypeDef::any(),
value: None,
},
);

let mut b = LocalEnv::default();
b.insert_variable(
Ident("bar".into()),
Details {
type_def: TypeDef::any(),
value: None,
},
);

let merged = a.merge(b);
assert_eq!(merged.bindings.len(), 2);
}
}

0 comments on commit a24c6d3

Please sign in to comment.