Skip to content
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

review: feature: fail when element is added twice #2009

Merged
merged 4 commits into from
May 30, 2018

Conversation

pvojtechovsky
Copy link
Collaborator

There is basic spoon concept: each element of AST has 0 or 1 parent and is in children collection of exactly one parent.

When some element which is already part of AST is added into another part of AST, then this rule concept is not true and Spoon can behave unpredictable.

This PR assures that element add will fail in such situation.

@monperrus
Copy link
Collaborator

There is basic spoon concept: each element of AST has 0 or 1 parent and is in children collection of exactly one parent.

I agree with this contract.

This PR is not the only solution for this.

This other solution to maintain this contract is to automatically do call delete before any call to setParent. This would be much more backward compatible.

WDYT?

@pvojtechovsky
Copy link
Collaborator Author

it is backward compatible, but it doesn't notifies client that s/he is probably doing something bad. It is much better to fail with exception, comparing to silient delete of node from origin tree and adding into new tree. Therefore we can make it configurable. By default fail and optionally it can delete the node from the origin tree automatically.

Another correct solution is to clone the node.... but I still think that throwing exception is best for new code - client is notified and can quickly understand what is wrong. For the old code we can have switch which will automatically remove node from the origin tree.

Note: thanks to this check this PR found real bug in Spoon sources, where element was added into new subtree but we forgot to clone it before, so the origin AST was broken.

@pvojtechovsky pvojtechovsky changed the title WIP: feature: fail when element is added twice review: feature: fail when element is added twice May 27, 2018
@ashutosh1598
Copy link
Contributor

ashutosh1598 commented May 28, 2018

I think it is better to throw an exception rather than silently deleting the node or cloning it. Two days ago I had to spend some time to debug because I forgot to clone a CtBlock.

@monperrus monperrus merged commit 7a96e81 into INRIA:master May 30, 2018
@monperrus
Copy link
Collaborator

thanks Pavel!

monperrus added a commit to monperrus/spoon that referenced this pull request Jun 6, 2018
pvojtechovsky pushed a commit that referenced this pull request Jun 6, 2018
@surli surli mentioned this pull request Jun 7, 2018
@pvojtechovsky pvojtechovsky deleted the feaFailOnInvalidAdd branch September 1, 2018 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants