diff options
Diffstat (limited to 'src/yellow/syntax.rs')
-rw-r--r-- | src/yellow/syntax.rs | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/src/yellow/syntax.rs b/src/yellow/syntax.rs index 53d8c82b9..fa51e8d13 100644 --- a/src/yellow/syntax.rs +++ b/src/yellow/syntax.rs | |||
@@ -1,18 +1,20 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | fmt, | 2 | fmt, |
3 | sync::Arc, | 3 | sync::Arc, |
4 | ptr | ||
4 | }; | 5 | }; |
5 | 6 | ||
6 | use { | 7 | use { |
7 | TextRange, TextUnit, | 8 | TextRange, TextUnit, |
8 | SyntaxKind::{self, *}, | 9 | SyntaxKind::{self, *}, |
9 | yellow::{Ptr, RedNode, GreenNode}, | 10 | yellow::{RedNode, GreenNode}, |
10 | }; | 11 | }; |
11 | 12 | ||
12 | #[derive(Clone)] | 13 | #[derive(Clone)] |
13 | pub struct SyntaxNode { | 14 | pub struct SyntaxNode { |
14 | pub(crate) root: SyntaxRoot, | 15 | pub(crate) root: SyntaxRoot, |
15 | red: Ptr<RedNode>, | 16 | // guaranteed to be alive bc SyntaxRoot holds a strong ref |
17 | red: ptr::NonNull<RedNode>, | ||
16 | } | 18 | } |
17 | 19 | ||
18 | #[derive(Clone)] | 20 | #[derive(Clone)] |
@@ -29,10 +31,10 @@ pub(crate) struct SyntaxError { | |||
29 | 31 | ||
30 | impl SyntaxNode { | 32 | impl SyntaxNode { |
31 | pub(crate) fn new(root: GreenNode, errors: Vec<SyntaxError>) -> SyntaxNode { | 33 | pub(crate) fn new(root: GreenNode, errors: Vec<SyntaxError>) -> SyntaxNode { |
32 | let root = Arc::new(RedNode::new_root(root)); | 34 | let red = Arc::new(RedNode::new_root(root)); |
33 | let red = Ptr::new(&root); | 35 | let red_weak: ptr::NonNull<RedNode> = (&*red).into(); |
34 | let root = SyntaxRoot { red: root, errors: Arc::new(errors) }; | 36 | let root = SyntaxRoot { red, errors: Arc::new(errors) }; |
35 | SyntaxNode { root, red } | 37 | SyntaxNode { root, red: red_weak } |
36 | } | 38 | } |
37 | 39 | ||
38 | pub fn kind(&self) -> SyntaxKind { | 40 | pub fn kind(&self) -> SyntaxKind { |
@@ -58,15 +60,14 @@ impl SyntaxNode { | |||
58 | for i in 0..n_children { | 60 | for i in 0..n_children { |
59 | res.push(SyntaxNode { | 61 | res.push(SyntaxNode { |
60 | root: self.root.clone(), | 62 | root: self.root.clone(), |
61 | red: Ptr::new(&red.nth_child(Ptr::clone(&self.red), i)), | 63 | red: (&*red.nth_child(i)).into(), |
62 | }); | 64 | }); |
63 | } | 65 | } |
64 | res | 66 | res |
65 | } | 67 | } |
66 | 68 | ||
67 | fn red(&self) -> &RedNode { | 69 | fn red(&self) -> &RedNode { |
68 | // Safe b/c root ptr keeps red alive | 70 | unsafe { self.red.as_ref() } |
69 | unsafe { self.red.get() } | ||
70 | } | 71 | } |
71 | } | 72 | } |
72 | 73 | ||