Skip to content

Commit c6e7917

Browse files
committed
Fix up token_tree_to_syntax_node float split handling
1 parent 9053bcc commit c6e7917

File tree

5 files changed

+76
-12
lines changed

5 files changed

+76
-12
lines changed

crates/hir-def/src/item_tree.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ impl ItemTree {
111111
Some(node) => node,
112112
None => return Default::default(),
113113
};
114-
if never!(syntax.kind() == SyntaxKind::ERROR) {
114+
if never!(syntax.kind() == SyntaxKind::ERROR, "{:?} from {:?} {}", file_id, syntax, syntax)
115+
{
115116
// FIXME: not 100% sure why these crop up, but return an empty tree to avoid a panic
116117
return Default::default();
117118
}
@@ -133,7 +134,7 @@ impl ItemTree {
133134
ctx.lower_macro_stmts(stmts)
134135
},
135136
_ => {
136-
panic!("cannot create item tree from {syntax:?} {syntax}");
137+
panic!("cannot create item tree for file {file_id:?} from {syntax:?} {syntax}");
137138
},
138139
}
139140
};

crates/hir-def/src/macro_expansion_tests/proc_macros.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ macro_rules! id {
104104
$($t)*
105105
};
106106
}
107-
id /*+errors*/! {
107+
id! {
108108
#[proc_macros::identity]
109109
impl Foo for WrapBj {
110110
async fn foo(&self) {
@@ -113,18 +113,17 @@ id /*+errors*/! {
113113
}
114114
}
115115
"#,
116-
expect![[r##"
116+
expect![[r#"
117117
macro_rules! id {
118118
($($t:tt)*) => {
119119
$($t)*
120120
};
121121
}
122-
/* parse error: expected SEMICOLON */
123122
#[proc_macros::identity] impl Foo for WrapBj {
124123
async fn foo(&self ) {
125124
self .0.id().await ;
126125
}
127126
}
128-
"##]],
127+
"#]],
129128
);
130129
}

crates/mbe/src/syntax_bridge.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn token_tree_to_syntax_node(
9595
parser::Step::Token { kind, n_input_tokens: n_raw_tokens } => {
9696
tree_sink.token(kind, n_raw_tokens)
9797
}
98-
parser::Step::FloatSplit { .. } => tree_sink.token(SyntaxKind::FLOAT_NUMBER, 1),
98+
parser::Step::FloatSplit { has_pseudo_dot } => tree_sink.float_split(has_pseudo_dot),
9999
parser::Step::Enter { kind } => tree_sink.start_node(kind),
100100
parser::Step::Exit => tree_sink.finish_node(),
101101
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> {
797797
}
798798

799799
impl<'a> TtTreeSink<'a> {
800+
fn float_split(&mut self, has_pseudo_dot: bool) {
801+
let (text, _span) = match self.cursor.token_tree() {
802+
Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Literal(lit), _)) => {
803+
(lit.text.as_str(), lit.span)
804+
}
805+
_ => unreachable!(),
806+
};
807+
match text.split_once('.') {
808+
Some((left, right)) => {
809+
assert!(!left.is_empty());
810+
self.inner.start_node(SyntaxKind::NAME_REF);
811+
self.inner.token(SyntaxKind::INT_NUMBER, left);
812+
self.inner.finish_node();
813+
814+
// here we move the exit up, the original exit has been deleted in process
815+
self.inner.finish_node();
816+
817+
self.inner.token(SyntaxKind::DOT, ".");
818+
819+
if has_pseudo_dot {
820+
assert!(right.is_empty());
821+
} else {
822+
self.inner.start_node(SyntaxKind::NAME_REF);
823+
self.inner.token(SyntaxKind::INT_NUMBER, right);
824+
self.inner.finish_node();
825+
826+
// the parser creates an unbalanced start node, we are required to close it here
827+
self.inner.finish_node();
828+
}
829+
}
830+
None => unreachable!(),
831+
}
832+
self.cursor = self.cursor.bump();
833+
}
834+
800835
fn token(&mut self, kind: SyntaxKind, mut n_tokens: u8) {
801836
if kind == LIFETIME_IDENT {
802837
n_tokens = 2;

crates/mbe/src/tt_iter.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,38 @@ impl<'a> TtIter<'a> {
170170
let mut res = vec![];
171171

172172
if cursor.is_root() {
173-
while curr != cursor {
174-
if let Some(token) = curr.token_tree() {
175-
res.push(token.cloned());
173+
if float_splits.is_empty() {
174+
while curr != cursor {
175+
if let Some(token) = curr.token_tree() {
176+
res.push(token.cloned());
177+
}
178+
curr = curr.bump();
179+
}
180+
} else {
181+
// let mut float_splits = float_splits.into_iter().peekable();
182+
// while let Some(tt) = curr.token_tree() {
183+
// let mut tt = tt.cloned();
184+
// let mut tt_mut_ref = &mut tt;
185+
// if let Some(fs) = float_splits.peek() {
186+
// loop {
187+
// curr = curr.bump_subtree();
188+
// if curr == *fs {
189+
// float_splits.next();
190+
// }
191+
// if curr.is_root() {
192+
// break;
193+
// }
194+
// }
195+
// }
196+
// res.push(tt);
197+
// }
198+
199+
while curr != cursor {
200+
if let Some(token) = curr.token_tree() {
201+
res.push(token.cloned());
202+
}
203+
curr = curr.bump();
176204
}
177-
curr = curr.bump();
178205
}
179206
}
180207
self.inner = self.inner.as_slice()[res.len()..].iter();

crates/parser/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ impl TopEntryPoint {
102102
match step {
103103
Step::Enter { .. } => depth += 1,
104104
Step::Exit => depth -= 1,
105-
Step::FloatSplit { .. } | Step::Token { .. } | Step::Error { .. } => (),
105+
Step::FloatSplit { .. } => depth -= 1,
106+
Step::Token { .. } | Step::Error { .. } => (),
106107
}
107108
}
108109
assert!(!first, "no tree at all");
110+
assert_eq!(depth, 0, "unbalanced tree");
109111
}
110112

111113
res

0 commit comments

Comments
 (0)