Skip to content

fix: Fix panic when a macro passes a float token to another macro #12178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions crates/hir-def/src/macro_expansion_tests/mbe/tt_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,33 @@ const _: () = 0e0;
"#]],
);
}

#[test]
fn float_literal_in_tt() {
check(
r#"
macro_rules! constant {
($( $ret:expr; )*) => {};
}

macro_rules! float_const_impl {
() => ( constant!(0.3; 3.3;); );
}

float_const_impl! {}
"#,
expect![[r#"
macro_rules! constant {
($( $ret:expr; )*) => {};
}

macro_rules! float_const_impl {
() => ( constant!(0.3; 3.3;); );
}

constant!(0.3;
3.3;
);
"#]],
);
}
50 changes: 50 additions & 0 deletions crates/mbe/src/syntax_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ struct TtTreeSink<'a> {
text_pos: TextSize,
inner: SyntaxTreeBuilder,
token_map: TokenMap,
remaining_float_lit_text: String,
}

impl<'a> TtTreeSink<'a> {
Expand All @@ -751,6 +752,7 @@ impl<'a> TtTreeSink<'a> {
text_pos: 0.into(),
inner: SyntaxTreeBuilder::default(),
token_map: TokenMap::default(),
remaining_float_lit_text: String::new(),
}
}

Expand All @@ -777,6 +779,54 @@ impl<'a> TtTreeSink<'a> {
n_tokens = 2;
}

// We need to split a float `tt::Literal` into up to 3 tokens consumed by the parser.
match self.cursor.token_tree() {
Some(tt::buffer::TokenTreeRef::Subtree(sub, _)) if sub.delimiter.is_none() => {
self.cursor = self.cursor.subtree().unwrap()
}
_ => {}
}
let literal = match self.cursor.token_tree() {
Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Literal(lit), _)) => Some(lit),
_ => None,
};
if matches!(
kind,
FLOAT_NUMBER_PART | FLOAT_NUMBER_START_0 | FLOAT_NUMBER_START_1 | FLOAT_NUMBER_START_2
) {
if self.remaining_float_lit_text.is_empty() {
always!(
literal.is_some(),
"kind={:?}, cursor tt={:?}",
kind,
self.cursor.token_tree()
);
let text = literal.map_or(String::new(), |lit| lit.to_string());
self.cursor = self.cursor.bump();
match text.split_once('.') {
Some((start, end)) => {
self.inner.token(kind, start);
self.remaining_float_lit_text = format!(".{end}");
return;
}
None => {
self.inner.token(kind, &text);
return;
}
}
} else {
self.inner.token(kind, &self.remaining_float_lit_text);
self.remaining_float_lit_text.clear();
return;
}
}
if kind == DOT && !self.remaining_float_lit_text.is_empty() {
always!(self.remaining_float_lit_text.chars().next() == Some('.'));
self.inner.token(kind, ".");
self.remaining_float_lit_text = self.remaining_float_lit_text[1..].to_string();
return;
}

let mut last = self.cursor;
for _ in 0..n_tokens {
let tmp: u8;
Expand Down