-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more functions for adding nodes to MastForest #1412
Add more functions for adding nodes to MastForest #1412
Conversation
@bobbinth - following on from previous PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Thank you! I left one comment inline about how we can make validation even more robust.
core/src/mast/mod.rs
Outdated
pub fn add_join( | ||
&mut self, | ||
left_child: MastNodeId, | ||
right_child: MastNodeId, | ||
) -> Result<MastNodeId, MastForestError> { | ||
self.add_node(MastNode::new_join(left_child, right_child, self)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One potentially nice thing to validate here would be that left_child
and right_child
have smaller values than the newly created node ID. We could create a new error variant in MastForestError
for this.
This is also relevant for add_split()
, add_loop()
, add_call()
, and add_syscall()
methods below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
LMK if you would like the error string to be more descriptive and contain references to the type of node and other IDs.
964ddda
to
eb551eb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All looks good! Thank you!
@bobbinth I added tests in this commit. Just want to double check you weren't expecting that logic to be captured by my changes - because what happens instead is that the overflow causes a panic. Understood you may not have intended my logic to avoid this overflow behaviour, just wanted to check.
I have put the PR into draft to avoid merging the currently failing test. |
Ah - good catch! I think we should fix this and there are two was to do it:
/// Adds a join node to the forest, and returns the [`MastNodeId`] associated with it.
pub fn add_join(
&mut self,
left_child: MastNodeId,
right_child: MastNodeId,
) -> Result<MastNodeId, MastForestError> {
if left_child >= self.nodes.len() {
Err(MastForestError::InvalidNodeId(left_child))
} else if right_child >= self.nodes.len() {
Err(MastForestError::InvalidNodeId(right_child))
} else {
self.add_node(MastNode::new_join(left_child, right_child, self))
}
} The first option may be a more intrusive change, but the second option would require duplication of logic (i.e., we'd need something similar in |
@bobbinth I have implemented option 1 as you suggested. LMK if anything needs to be changed. I preferred option 1 on the basis that it felt more in line with the "parse, don't validate" idiom by enforcing the invariant in the node constructors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Thank you! I left just one non-blocking comment inline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All looks good! Thank you again!
Add methods to
MastForest
to allow adding nodes with fewer LOC.Follow on from #1404 which added similar methods to
MastForestBuilder
.As per comment.