Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e59487d

Browse files
committed
Add tests for float access macro call inputs
1 parent f6539b1 commit e59487d

File tree

5 files changed

+58
-37
lines changed

5 files changed

+58
-37
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,41 @@ fn#19 main#20(#21)#21 {#22
9797
"##]],
9898
);
9999
}
100+
#[test]
101+
fn float_field_acces_macro_input() {
102+
check(
103+
r#"
104+
macro_rules! foo {
105+
($expr:expr) => {
106+
fn foo() {
107+
$expr;
108+
}
109+
};
110+
}
111+
foo!(x .0.1);
112+
foo!(x .2. 3);
113+
foo!(x .4 .5);
114+
"#,
115+
expect![[r#"
116+
macro_rules! foo {
117+
($expr:expr) => {
118+
fn foo() {
119+
$expr;
120+
}
121+
};
122+
}
123+
fn foo() {
124+
(x.0.1);
125+
}
126+
fn foo() {
127+
(x.2.3);
128+
}
129+
fn foo() {
130+
(x.4.5);
131+
}
132+
"#]],
133+
);
134+
}
100135

101136
#[test]
102137
fn mbe_smoke_test() {

crates/mbe/src/syntax_bridge.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ impl<'a> TtTreeSink<'a> {
817817
self.inner.token(SyntaxKind::DOT, ".");
818818

819819
if has_pseudo_dot {
820-
assert!(right.is_empty());
820+
assert!(right.is_empty(), "{left}.{right}");
821821
} else {
822822
self.inner.start_node(SyntaxKind::NAME_REF);
823823
self.inner.token(SyntaxKind::INT_NUMBER, right);

crates/mbe/src/to_parser_input.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ pub(crate) fn to_parser_input(buffer: &TokenBuffer<'_>) -> parser::Input {
4545
.unwrap_or_else(|| panic!("Fail to convert given literal {:#?}", &lit));
4646

4747
res.push(kind);
48+
49+
if kind == FLOAT_NUMBER && !inner_text.ends_with('.') {
50+
res.was_joint();
51+
}
4852
}
4953
tt::Leaf::Ident(ident) => match ident.text.as_ref() {
5054
"_" => res.push(T![_]),

crates/mbe/src/tt_iter.rs

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ impl<'a> TtIter<'a> {
140140

141141
let mut cursor = buffer.begin();
142142
let mut error = false;
143-
let mut float_splits = vec![];
144143
for step in tree_traversal.iter() {
145144
match step {
146145
parser::Step::Token { kind, mut n_input_tokens } => {
@@ -152,7 +151,8 @@ impl<'a> TtIter<'a> {
152151
}
153152
}
154153
parser::Step::FloatSplit { .. } => {
155-
float_splits.push(cursor);
154+
// FIXME: We need to split the tree properly here, but mutating the token trees
155+
// in the buffer is somewhat tricky to pull off.
156156
cursor = cursor.bump_subtree();
157157
}
158158
parser::Step::Enter { .. } | parser::Step::Exit => (),
@@ -170,40 +170,13 @@ impl<'a> TtIter<'a> {
170170
let mut res = vec![];
171171

172172
if cursor.is_root() {
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();
204-
}
173+
while curr != cursor {
174+
let Some(token) = curr.token_tree() else { break };
175+
res.push(token.cloned());
176+
curr = curr.bump();
205177
}
206178
}
179+
207180
self.inner = self.inner.as_slice()[res.len()..].iter();
208181
let res = match res.len() {
209182
0 | 1 => res.pop(),

crates/tt/src/buffer.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ use crate::{Leaf, Subtree, TokenTree};
77
struct EntryId(usize);
88

99
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
10-
struct EntryPtr(EntryId, usize);
10+
struct EntryPtr(
11+
/// The index of the buffer containing the entry.
12+
EntryId,
13+
/// The index of the entry within the buffer.
14+
usize,
15+
);
1116

1217
/// Internal type which is used instead of `TokenTree` to represent a token tree
1318
/// within a `TokenBuffer`.
@@ -229,7 +234,11 @@ impl<'a, Span> Cursor<'a, Span> {
229234
Some(&Entry::Subtree(_, _, entry_id)) => {
230235
Cursor::create(self.buffer, EntryPtr(entry_id, 0))
231236
}
232-
_ => self.bump(),
237+
Some(Entry::End(exit)) => match exit {
238+
Some(exit) => Cursor::create(self.buffer, *exit),
239+
None => self,
240+
},
241+
_ => Cursor::create(self.buffer, EntryPtr(self.ptr.0, self.ptr.1 + 1)),
233242
}
234243
}
235244

0 commit comments

Comments
 (0)