Skip to content

Commit a969481

Browse files
committed
Add syntax fixup for while loops
1 parent 9a1ec45 commit a969481

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

crates/hir-expand/src/fixup.rs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::mem;
55
use mbe::{SyntheticToken, SyntheticTokenId, TokenMap};
66
use rustc_hash::FxHashMap;
77
use syntax::{
8-
ast::{self, AstNode},
8+
ast::{self, AstNode, HasLoopBody},
99
match_ast, SyntaxElement, SyntaxKind, SyntaxNode, TextRange,
1010
};
1111
use tt::Subtree;
@@ -142,6 +142,39 @@ pub(crate) fn fixup_syntax(node: &SyntaxNode) -> SyntaxFixups {
142142
]);
143143
}
144144
},
145+
ast::WhileExpr(it) => {
146+
if it.condition().is_none() {
147+
// insert placeholder token after the while token
148+
let while_token = match it.while_token() {
149+
Some(t) => t,
150+
None => continue,
151+
};
152+
append.insert(while_token.into(), vec![
153+
SyntheticToken {
154+
kind: SyntaxKind::IDENT,
155+
text: "__ra_fixup".into(),
156+
range: end_range,
157+
id: EMPTY_ID,
158+
},
159+
]);
160+
}
161+
if it.loop_body().is_none() {
162+
append.insert(node.clone().into(), vec![
163+
SyntheticToken {
164+
kind: SyntaxKind::L_CURLY,
165+
text: "{".into(),
166+
range: end_range,
167+
id: EMPTY_ID,
168+
},
169+
SyntheticToken {
170+
kind: SyntaxKind::R_CURLY,
171+
text: "}".into(),
172+
range: end_range,
173+
id: EMPTY_ID,
174+
},
175+
]);
176+
}
177+
},
145178
// FIXME: foo::
146179
// FIXME: for, loop, match etc.
147180
_ => (),
@@ -376,6 +409,47 @@ fn foo() {
376409
// the {} gets parsed as the condition, I think?
377410
expect![[r#"
378411
fn foo () {if {} {}}
412+
"#]],
413+
)
414+
}
415+
416+
#[test]
417+
fn fixup_while_1() {
418+
check(
419+
r#"
420+
fn foo() {
421+
while
422+
}
423+
"#,
424+
expect![[r#"
425+
fn foo () {while __ra_fixup {}}
426+
"#]],
427+
)
428+
}
429+
430+
#[test]
431+
fn fixup_while_2() {
432+
check(
433+
r#"
434+
fn foo() {
435+
while foo
436+
}
437+
"#,
438+
expect![[r#"
439+
fn foo () {while foo {}}
440+
"#]],
441+
)
442+
}
443+
#[test]
444+
fn fixup_while_3() {
445+
check(
446+
r#"
447+
fn foo() {
448+
while {}
449+
}
450+
"#,
451+
expect![[r#"
452+
fn foo () {while __ra_fixup {}}
379453
"#]],
380454
)
381455
}

0 commit comments

Comments
 (0)