about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2023-02-07 15:21:37 +0100
committerLukas Wirth <lukastw97@gmail.com>2023-02-07 15:21:37 +0100
commitc6e7917d6ed2ae36534b064a51697b54f497e02e (patch)
tree935e9504ac5ff200aef72b833844edaf41f92587
parent9053bcc65c41707e2272757fdccf3a97e167217d (diff)
downloadrust-c6e7917d6ed2ae36534b064a51697b54f497e02e.tar.gz
rust-c6e7917d6ed2ae36534b064a51697b54f497e02e.zip
Fix up token_tree_to_syntax_node float split handling
-rw-r--r--crates/hir-def/src/item_tree.rs5
-rw-r--r--crates/hir-def/src/macro_expansion_tests/proc_macros.rs7
-rw-r--r--crates/mbe/src/syntax_bridge.rs37
-rw-r--r--crates/mbe/src/tt_iter.rs35
-rw-r--r--crates/parser/src/lib.rs4
5 files changed, 76 insertions, 12 deletions
diff --git a/crates/hir-def/src/item_tree.rs b/crates/hir-def/src/item_tree.rs
index 3e1f7d44460..19d01630ef0 100644
--- a/crates/hir-def/src/item_tree.rs
+++ b/crates/hir-def/src/item_tree.rs
@@ -111,7 +111,8 @@ impl ItemTree {
             Some(node) => node,
             None => return Default::default(),
         };
-        if never!(syntax.kind() == SyntaxKind::ERROR) {
+        if never!(syntax.kind() == SyntaxKind::ERROR, "{:?} from {:?} {}", file_id, syntax, syntax)
+        {
             // FIXME: not 100% sure why these crop up, but return an empty tree to avoid a panic
             return Default::default();
         }
@@ -133,7 +134,7 @@ impl ItemTree {
                     ctx.lower_macro_stmts(stmts)
                 },
                 _ => {
-                    panic!("cannot create item tree from {syntax:?} {syntax}");
+                    panic!("cannot create item tree for file {file_id:?} from {syntax:?} {syntax}");
                 },
             }
         };
diff --git a/crates/hir-def/src/macro_expansion_tests/proc_macros.rs b/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
index 118c14ed843..822bdcc122d 100644
--- a/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
+++ b/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
@@ -104,7 +104,7 @@ macro_rules! id {
         $($t)*
     };
 }
-id /*+errors*/! {
+id! {
     #[proc_macros::identity]
     impl Foo for WrapBj {
         async fn foo(&self) {
@@ -113,18 +113,17 @@ id /*+errors*/! {
     }
 }
 "#,
-        expect![[r##"
+        expect![[r#"
 macro_rules! id {
     ($($t:tt)*) => {
         $($t)*
     };
 }
-/* parse error: expected SEMICOLON */
 #[proc_macros::identity] impl Foo for WrapBj {
     async fn foo(&self ) {
         self .0.id().await ;
     }
 }
-"##]],
+"#]],
     );
 }
diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs
index 7fe4fcfc68e..8b9a3bca021 100644
--- a/crates/mbe/src/syntax_bridge.rs
+++ b/crates/mbe/src/syntax_bridge.rs
@@ -95,7 +95,7 @@ pub fn token_tree_to_syntax_node(
             parser::Step::Token { kind, n_input_tokens: n_raw_tokens } => {
                 tree_sink.token(kind, n_raw_tokens)
             }
-            parser::Step::FloatSplit { .. } => tree_sink.token(SyntaxKind::FLOAT_NUMBER, 1),
+            parser::Step::FloatSplit { has_pseudo_dot } => tree_sink.float_split(has_pseudo_dot),
             parser::Step::Enter { kind } => tree_sink.start_node(kind),
             parser::Step::Exit => tree_sink.finish_node(),
             parser::Step::Error { msg } => tree_sink.error(msg.to_string()),
@@ -797,6 +797,41 @@ fn delim_to_str(d: tt::DelimiterKind, closing: bool) -> Option<&'static str> {
 }
 
 impl<'a> TtTreeSink<'a> {
+    fn float_split(&mut self, has_pseudo_dot: bool) {
+        let (text, _span) = match self.cursor.token_tree() {
+            Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Literal(lit), _)) => {
+                (lit.text.as_str(), lit.span)
+            }
+            _ => unreachable!(),
+        };
+        match text.split_once('.') {
+            Some((left, right)) => {
+                assert!(!left.is_empty());
+                self.inner.start_node(SyntaxKind::NAME_REF);
+                self.inner.token(SyntaxKind::INT_NUMBER, left);
+                self.inner.finish_node();
+
+                // here we move the exit up, the original exit has been deleted in process
+                self.inner.finish_node();
+
+                self.inner.token(SyntaxKind::DOT, ".");
+
+                if has_pseudo_dot {
+                    assert!(right.is_empty());
+                } else {
+                    self.inner.start_node(SyntaxKind::NAME_REF);
+                    self.inner.token(SyntaxKind::INT_NUMBER, right);
+                    self.inner.finish_node();
+
+                    // the parser creates an unbalanced start node, we are required to close it here
+                    self.inner.finish_node();
+                }
+            }
+            None => unreachable!(),
+        }
+        self.cursor = self.cursor.bump();
+    }
+
     fn token(&mut self, kind: SyntaxKind, mut n_tokens: u8) {
         if kind == LIFETIME_IDENT {
             n_tokens = 2;
diff --git a/crates/mbe/src/tt_iter.rs b/crates/mbe/src/tt_iter.rs
index b38243caeef..c05a2ca1502 100644
--- a/crates/mbe/src/tt_iter.rs
+++ b/crates/mbe/src/tt_iter.rs
@@ -170,11 +170,38 @@ impl<'a> TtIter<'a> {
         let mut res = vec![];
 
         if cursor.is_root() {
-            while curr != cursor {
-                if let Some(token) = curr.token_tree() {
-                    res.push(token.cloned());
+            if float_splits.is_empty() {
+                while curr != cursor {
+                    if let Some(token) = curr.token_tree() {
+                        res.push(token.cloned());
+                    }
+                    curr = curr.bump();
+                }
+            } else {
+                // let mut float_splits = float_splits.into_iter().peekable();
+                // while let Some(tt) = curr.token_tree() {
+                //     let mut tt = tt.cloned();
+                //     let mut tt_mut_ref = &mut tt;
+                //     if let Some(fs) = float_splits.peek() {
+                //         loop {
+                //             curr = curr.bump_subtree();
+                //             if curr == *fs {
+                //                 float_splits.next();
+                //             }
+                //             if curr.is_root() {
+                //                 break;
+                //             }
+                //         }
+                //     }
+                //     res.push(tt);
+                // }
+
+                while curr != cursor {
+                    if let Some(token) = curr.token_tree() {
+                        res.push(token.cloned());
+                    }
+                    curr = curr.bump();
                 }
-                curr = curr.bump();
             }
         }
         self.inner = self.inner.as_slice()[res.len()..].iter();
diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs
index f20d32d6cf8..6c72b5994b5 100644
--- a/crates/parser/src/lib.rs
+++ b/crates/parser/src/lib.rs
@@ -102,10 +102,12 @@ impl TopEntryPoint {
                 match step {
                     Step::Enter { .. } => depth += 1,
                     Step::Exit => depth -= 1,
-                    Step::FloatSplit { .. } | Step::Token { .. } | Step::Error { .. } => (),
+                    Step::FloatSplit { .. } => depth -= 1,
+                    Step::Token { .. } | Step::Error { .. } => (),
                 }
             }
             assert!(!first, "no tree at all");
+            assert_eq!(depth, 0, "unbalanced tree");
         }
 
         res