about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDropDemBits <r3usrlnd@gmail.com>2024-03-08 21:45:01 -0500
committerDropDemBits <r3usrlnd@gmail.com>2024-06-02 11:10:39 -0400
commitbecd71f826f6f8c45376fa8ab9d3c60376758aa3 (patch)
treee93166c93cda7edf55a02d6fe10b44eae6ea2c1c
parent5fc5f63d09e5ff7fe3483d04bfdbb15bbf481f37 (diff)
downloadrust-becd71f826f6f8c45376fa8ab9d3c60376758aa3.tar.gz
rust-becd71f826f6f8c45376fa8ab9d3c60376758aa3.zip
minor: tidy up `Parse` a little bit
- Add doc comments to some `Parse` methods
- Uses `Parse::new` more
-rw-r--r--src/tools/rust-analyzer/crates/syntax/src/lib.rs21
1 files changed, 10 insertions, 11 deletions
diff --git a/src/tools/rust-analyzer/crates/syntax/src/lib.rs b/src/tools/rust-analyzer/crates/syntax/src/lib.rs
index 3a9ebafe87d..b8a8103cc65 100644
--- a/src/tools/rust-analyzer/crates/syntax/src/lib.rs
+++ b/src/tools/rust-analyzer/crates/syntax/src/lib.rs
@@ -107,14 +107,22 @@ impl<T> Parse<T> {
 }
 
 impl<T: AstNode> Parse<T> {
+    /// Converts this parse result into a parse result for an untyped syntax tree.
     pub fn to_syntax(self) -> Parse<SyntaxNode> {
         Parse { green: self.green, errors: self.errors, _ty: PhantomData }
     }
 
+    /// Gets the parsed syntax tree as a typed ast node.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the root node cannot be casted into the typed ast node
+    /// (e.g. if it's an `ERROR` node).
     pub fn tree(&self) -> T {
         T::cast(self.syntax_node()).unwrap()
     }
 
+    /// Converts from `Parse<T>` to [`Result<T, Vec<SyntaxError>>`].
     pub fn ok(self) -> Result<T, Vec<SyntaxError>> {
         match self.errors() {
             errors if !errors.is_empty() => Err(errors),
@@ -177,11 +185,7 @@ impl SourceFile {
         let root = SyntaxNode::new_root(green.clone());
 
         assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE);
-        Parse {
-            green,
-            errors: if errors.is_empty() { None } else { Some(errors.into()) },
-            _ty: PhantomData,
-        }
+        Parse::new(green, errors)
     }
 }
 
@@ -290,12 +294,7 @@ impl ast::TokenTree {
         }
 
         let (green, errors) = builder.finish_raw();
-
-        Parse {
-            green,
-            errors: if errors.is_empty() { None } else { Some(errors.into()) },
-            _ty: PhantomData,
-        }
+        Parse::new(green, errors)
     }
 }