about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVictor Song <vsong1618@gmail.com>2024-02-12 23:57:42 -0600
committerVictor Song <vsong1618@gmail.com>2024-02-13 00:00:02 -0600
commit4923b8a74bdffb0a8d1ce3760d00da8959a711c2 (patch)
tree5411b4ebbcd4e79d71f0754f07efca13e5021f08
parent1918f9b9e0fe2e5b090491596a8d41b5ed2ed3bd (diff)
downloadrust-4923b8a74bdffb0a8d1ce3760d00da8959a711c2.tar.gz
rust-4923b8a74bdffb0a8d1ce3760d00da8959a711c2.zip
Return `Option<Parse<ast::Literal>>` from `ast::Literal::parse`
-rw-r--r--crates/proc-macro-srv/src/server/rust_analyzer_span.rs2
-rw-r--r--crates/proc-macro-srv/src/server/token_id.rs2
-rw-r--r--crates/syntax/src/lib.rs15
3 files changed, 11 insertions, 8 deletions
diff --git a/crates/proc-macro-srv/src/server/rust_analyzer_span.rs b/crates/proc-macro-srv/src/server/rust_analyzer_span.rs
index 7313e99bb12..cf6e816d599 100644
--- a/crates/proc-macro-srv/src/server/rust_analyzer_span.rs
+++ b/crates/proc-macro-srv/src/server/rust_analyzer_span.rs
@@ -71,7 +71,7 @@ impl server::FreeFunctions for RaSpanServer {
         &mut self,
         s: &str,
     ) -> Result<bridge::Literal<Self::Span, Self::Symbol>, ()> {
-        let literal = ast::Literal::parse(s);
+        let literal = ast::Literal::parse(s).ok_or(())?;
         let literal = literal.tree();
 
         let kind = literal_to_external(literal.kind()).ok_or(())?;
diff --git a/crates/proc-macro-srv/src/server/token_id.rs b/crates/proc-macro-srv/src/server/token_id.rs
index d16e9d25105..70e577f576f 100644
--- a/crates/proc-macro-srv/src/server/token_id.rs
+++ b/crates/proc-macro-srv/src/server/token_id.rs
@@ -63,7 +63,7 @@ impl server::FreeFunctions for TokenIdServer {
         &mut self,
         s: &str,
     ) -> Result<bridge::Literal<Self::Span, Self::Symbol>, ()> {
-        let literal = ast::Literal::parse(s);
+        let literal = ast::Literal::parse(s).ok_or(())?;
         let literal = literal.tree();
 
         let kind = literal_to_external(literal.kind()).ok_or(())?;
diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs
index 2390cacda28..61d9c65e4ff 100644
--- a/crates/syntax/src/lib.rs
+++ b/crates/syntax/src/lib.rs
@@ -188,7 +188,7 @@ impl SourceFile {
 }
 
 impl ast::Literal {
-    pub fn parse(text: &str) -> Parse<ast::Literal> {
+    pub fn parse(text: &str) -> Option<Parse<ast::Literal>> {
         let lexed = parser::LexedStr::new(text);
         let parser_input = lexed.to_input();
         let parser_output = parser::TopEntryPoint::Expr.parse(&parser_input);
@@ -197,11 +197,14 @@ impl ast::Literal {
 
         errors.extend(validation::validate(&root));
 
-        assert_eq!(root.kind(), SyntaxKind::LITERAL);
-        Parse {
-            green,
-            errors: if errors.is_empty() { None } else { Some(errors.into()) },
-            _ty: PhantomData,
+        if root.kind() == SyntaxKind::LITERAL {
+            Some(Parse {
+                green,
+                errors: if errors.is_empty() { None } else { Some(errors.into()) },
+                _ty: PhantomData,
+            })
+        } else {
+            None
         }
     }
 }