Skip to content

Commit 4ea0c12

Browse files
committed
Make sure that newly created nodes are the root of the tree
1 parent c1a0649 commit 4ea0c12

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

crates/ra_syntax/src/ast/make.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! of smaller pieces.
33
use itertools::Itertools;
44

5-
use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxToken};
5+
use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken};
66

77
pub fn name(text: &str) -> ast::Name {
88
ast_from_text(&format!("mod {};", text))
@@ -179,7 +179,16 @@ pub fn token(kind: SyntaxKind) -> SyntaxToken {
179179

180180
fn ast_from_text<N: AstNode>(text: &str) -> N {
181181
let parse = SourceFile::parse(text);
182-
parse.tree().syntax().descendants().find_map(N::cast).unwrap()
182+
let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap();
183+
let node = node.syntax().clone();
184+
let node = unroot(node);
185+
let node = N::cast(node).unwrap();
186+
assert_eq!(node.syntax().text_range().start(), 0.into());
187+
node
188+
}
189+
190+
fn unroot(n: SyntaxNode) -> SyntaxNode {
191+
SyntaxNode::new_root(n.green().clone())
183192
}
184193

185194
pub mod tokens {

0 commit comments

Comments
 (0)