Skip to content

Move syntax editor initialization invariants to its constructor#21960

Merged
ChayimFriedman2 merged 9 commits intorust-lang:masterfrom
Shourya742:2026-04-04-move-syntax-tree-invariants-to-constructor
Apr 6, 2026
Merged

Move syntax editor initialization invariants to its constructor#21960
ChayimFriedman2 merged 9 commits intorust-lang:masterfrom
Shourya742:2026-04-04-move-syntax-tree-invariants-to-constructor

Conversation

@Shourya742
Copy link
Copy Markdown
Member

@Shourya742 Shourya742 commented Apr 5, 2026

Currently, initializing SyntaxEditor requires the caller to maintain certain invariants. Specifically, the node used for initialization must be the root (i.e., it must have no parent) and must not be mutable. To ensure this, call sites either detach the subtree using clone_subtree or pass the root of the tree directly. This PR moves these invariants into the SyntaxEditor constructor. If the node passed to the constructor is not a root node or is mutable, the constructor will internally call clone_subtree to detach it and ensure a fresh, immutable tree. This removes the burden from the caller to handle detachment or mutability concerns. Additionally, the constructor’s return type has been updated. Along with SyntaxEditor, it now also returns a detached, immutable snapshot of the node, which can be used to issue mutation queries to the editor.

part of #15710 and #18285

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 5, 2026
@Shourya742 Shourya742 changed the title Move syntax tree initialization invariants to its constructor Move syntax editor initialization invariants to its constructor Apr 5, 2026
Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that in a few places we cast the returned node and unwrap, I prefer there to be a generic version of the constructor that is typed.

View changes since this review

@Shourya742
Copy link
Copy Markdown
Member Author

Given that in a few places we cast the returned node and unwrap, I prefer there to be a generic version of the constructor that is typed.

View changes since this review

Ah!!! thats a nice idea. Updated.

Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Apr 6, 2026
Merged via the queue into rust-lang:master with commit 1041087 Apr 6, 2026
17 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 6, 2026
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