@@ -5,7 +5,7 @@ use std::mem;
5
5
use mbe:: { SyntheticToken , SyntheticTokenId , TokenMap } ;
6
6
use rustc_hash:: FxHashMap ;
7
7
use syntax:: {
8
- ast:: { self , AstNode } ,
8
+ ast:: { self , AstNode , HasLoopBody } ,
9
9
match_ast, SyntaxElement , SyntaxKind , SyntaxNode , TextRange ,
10
10
} ;
11
11
use tt:: Subtree ;
@@ -142,8 +142,59 @@ pub(crate) fn fixup_syntax(node: &SyntaxNode) -> SyntaxFixups {
142
142
] ) ;
143
143
}
144
144
} ,
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
+ } ,
178
+ ast:: LoopExpr ( it) => {
179
+ if it. loop_body( ) . is_none( ) {
180
+ append. insert( node. clone( ) . into( ) , vec![
181
+ SyntheticToken {
182
+ kind: SyntaxKind :: L_CURLY ,
183
+ text: "{" . into( ) ,
184
+ range: end_range,
185
+ id: EMPTY_ID ,
186
+ } ,
187
+ SyntheticToken {
188
+ kind: SyntaxKind :: R_CURLY ,
189
+ text: "}" . into( ) ,
190
+ range: end_range,
191
+ id: EMPTY_ID ,
192
+ } ,
193
+ ] ) ;
194
+ }
195
+ } ,
145
196
// FIXME: foo::
146
- // FIXME: for, loop, match etc.
197
+ // FIXME: for, match etc.
147
198
_ => ( ) ,
148
199
}
149
200
}
@@ -376,6 +427,61 @@ fn foo() {
376
427
// the {} gets parsed as the condition, I think?
377
428
expect ! [ [ r#"
378
429
fn foo () {if {} {}}
430
+ "# ] ] ,
431
+ )
432
+ }
433
+
434
+ #[ test]
435
+ fn fixup_while_1 ( ) {
436
+ check (
437
+ r#"
438
+ fn foo() {
439
+ while
440
+ }
441
+ "# ,
442
+ expect ! [ [ r#"
443
+ fn foo () {while __ra_fixup {}}
444
+ "# ] ] ,
445
+ )
446
+ }
447
+
448
+ #[ test]
449
+ fn fixup_while_2 ( ) {
450
+ check (
451
+ r#"
452
+ fn foo() {
453
+ while foo
454
+ }
455
+ "# ,
456
+ expect ! [ [ r#"
457
+ fn foo () {while foo {}}
458
+ "# ] ] ,
459
+ )
460
+ }
461
+ #[ test]
462
+ fn fixup_while_3 ( ) {
463
+ check (
464
+ r#"
465
+ fn foo() {
466
+ while {}
467
+ }
468
+ "# ,
469
+ expect ! [ [ r#"
470
+ fn foo () {while __ra_fixup {}}
471
+ "# ] ] ,
472
+ )
473
+ }
474
+
475
+ #[ test]
476
+ fn fixup_loop ( ) {
477
+ check (
478
+ r#"
479
+ fn foo() {
480
+ loop
481
+ }
482
+ "# ,
483
+ expect ! [ [ r#"
484
+ fn foo () {loop {}}
379
485
"# ] ] ,
380
486
)
381
487
}
0 commit comments