If I keep AstPtrs to two nodes in a tree (appearing one after the other in source code), and make changes to the first one, the second AstPtr becomes invalid and will panic when to_node is called
In retrospect I should've seen this coming considering AstPtr only keeps a source code location reference to the node in the tree, and the tree source physically gets changed when mutations occur
Pseudocode to demonstrate the issue:
println!("Create Tree");
let mut tree = make_module("root");
println!("Add 2 children");
tree.append_child(make_module("child_1"));
tree.append_child(make_module("child_2"));
println!("Create child pointers");
let child_1_ptr = AstPtr::new(&tree.ast_children().nth(0).unwrap());
let child_2_ptr = AstPtr::new(&tree.ast_children().nth(1).unwrap());
println!("Resolve both pointers");
let mut child_1 = child_1_ptr.to_node(tree.syntax());
let child_2 = child_2_ptr.to_node(tree.syntax()); // All fine here, no mutations done
println!("Mutate tree under child 1");
child_1.append_child(make_module("child_1_1")).unwrap();
println!("Try to resolve pointer 2");
let child_2 = child_2_ptr.to_node(tree.syntax()); // Panic!
I'm fairly certain this is by design, but is there any way around this? If not, I could make a PR to add this as a note to the docs
If I keep
AstPtrs to two nodes in a tree (appearing one after the other in source code), and make changes to the first one, the secondAstPtrbecomes invalid and will panic whento_nodeis calledIn retrospect I should've seen this coming considering
AstPtronly keeps a source code location reference to the node in the tree, and the tree source physically gets changed when mutations occurPseudocode to demonstrate the issue:
I'm fairly certain this is by design, but is there any way around this? If not, I could make a PR to add this as a note to the docs