Skip to content

Commit 7db5531

Browse files
author
Jonas Schievink
committed
Skip only the tt::Literal when consuming float tokens
1 parent 10dd471 commit 7db5531

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ macro_rules! f3 { ($i:_) => () }
8080

8181
#[test]
8282
fn test_rustc_issue_57597() {
83-
// <https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-57597.rs>
83+
// <https://github.com/rust-lang/rust/blob/master/src/test/ui/macros/issue-57597.rs>
8484
check(
8585
r#"
8686
macro_rules! m0 { ($($($i:ident)?)+) => {}; }

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ macro_rules! m {
3838
let _ = 12E+99_f64;
3939
let _ = "rust1";
4040
let _ = -92;
41+
let _ = -1.3e4f32;
4142
}
4243
}
4344
fn f() {
@@ -52,6 +53,7 @@ macro_rules! m {
5253
let _ = 12E+99_f64;
5354
let _ = "rust1";
5455
let _ = -92;
56+
let _ = -1.3e4f32;
5557
}
5658
}
5759
fn f() {
@@ -60,6 +62,7 @@ fn f() {
6062
let _ = 12E+99_f64;
6163
let _ = "rust1";
6264
let _ = -92;
65+
let _ = -1.3e4f32;
6366
}
6467
"#]],
6568
);
@@ -148,3 +151,27 @@ $ = ();
148151
"#]],
149152
)
150153
}
154+
155+
#[test]
156+
fn float_literal_in_output() {
157+
check(
158+
r#"
159+
macro_rules! constant {
160+
($e:expr ;) => {$e};
161+
}
162+
163+
const _: () = constant!(0.0;);
164+
const _: () = constant!(0.;);
165+
const _: () = constant!(0e0;);
166+
"#,
167+
expect![[r#"
168+
macro_rules! constant {
169+
($e:expr ;) => {$e};
170+
}
171+
172+
const _: () = 0.0;
173+
const _: () = 0.;
174+
const _: () = 0e0;
175+
"#]],
176+
);
177+
}

crates/mbe/src/syntax_bridge.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,13 @@ fn convert_tokens<C: TokenConvertor>(conv: &mut C) -> tt::Subtree {
266266
let mut text = token.to_text(conv).to_string();
267267
if kind == FLOAT_NUMBER_START_1 || kind == FLOAT_NUMBER_START_2 {
268268
let (dot, dot_range) = conv.bump().unwrap();
269+
assert_eq!(dot.kind(conv), DOT);
269270
text += &*dot.to_text(conv);
270271
range = TextRange::new(range.start(), dot_range.end());
271272

272273
if kind == FLOAT_NUMBER_START_2 {
273274
let (tail, tail_range) = conv.bump().unwrap();
275+
assert_eq!(tail.kind(conv), FLOAT_NUMBER_PART);
274276
text += &*tail.to_text(conv);
275277
range = TextRange::new(range.start(), tail_range.end());
276278
}

crates/mbe/src/tt_iter.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,20 @@ impl<'a> TtIter<'a> {
9090

9191
let mut cursor = buffer.begin();
9292
let mut error = false;
93+
let mut float_fragments_to_skip = 0;
9394
for step in tree_traversal.iter() {
9495
match step {
9596
parser::Step::Token { kind, mut n_input_tokens } => {
97+
if float_fragments_to_skip > 0 {
98+
float_fragments_to_skip -= 1;
99+
n_input_tokens = 0;
100+
}
101+
match kind {
102+
SyntaxKind::LIFETIME_IDENT => n_input_tokens = 2,
103+
SyntaxKind::FLOAT_NUMBER_START_1 => float_fragments_to_skip = 1,
104+
SyntaxKind::FLOAT_NUMBER_START_2 => float_fragments_to_skip = 2,
105+
_ => {}
106+
}
96107
if kind == SyntaxKind::LIFETIME_IDENT {
97108
n_input_tokens = 2;
98109
}

0 commit comments

Comments
 (0)