Skip to content

Commit 3acbaf1

Browse files
committed
Compare Function bodies by pointer equality
We're changing the `body` field to contain an `Rc<dyn FunctionBodyObject>`, which does not implement `PartialEq`. To compare two `Function` objects for equality, we will compare the `body` fields using pointer equality with `Rc::ptr_eq`.
1 parent 288d538 commit 3acbaf1

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

yash-env/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ A _private dependency_ is used internally and not visible to downstream users.
2020
variable name. This allows modules to check variable names without directly
2121
depending on the `yash-syntax` crate.
2222

23+
### Changed
24+
25+
- `impl PartialEq for function::Function`: Now compares the function bodies
26+
using pointer equality instead of deep equality.
27+
2328
### Removed
2429

2530
- `parser::is_name`: This re-export of `yash_syntax::parser::lex::is_name` has

yash-env/src/function.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<T: FunctionBody + ?Sized> FunctionBodyObject for T {
6464
}
6565

6666
/// Definition of a function.
67-
#[derive(Clone, Debug, Eq, PartialEq)]
67+
#[derive(Clone, Debug, Eq)]
6868
pub struct Function {
6969
/// String that identifies the function.
7070
pub name: String,
@@ -125,6 +125,19 @@ impl Function {
125125
}
126126
}
127127

128+
/// Compares two functions for equality.
129+
///
130+
/// Two functions are considered equal if all their members are equal.
131+
/// This includes comparing the `body` members by pointer equality.
132+
impl PartialEq for Function {
133+
fn eq(&self, other: &Self) -> bool {
134+
self.name == other.name
135+
&& Rc::ptr_eq(&self.body, &other.body)
136+
&& self.origin == other.origin
137+
&& self.read_only_location == other.read_only_location
138+
}
139+
}
140+
128141
/// Wrapper of [`Function`] for inserting into a hash set.
129142
///
130143
/// A `HashEntry` wraps a `Function` in `Rc` so that the `Function` object can

0 commit comments

Comments
 (0)