about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPaul Lange <palango@gmx.de>2022-07-26 16:10:26 +0200
committerPaul Lange <palango@gmx.de>2022-07-27 18:16:58 +0200
commita96948195227f98dd18eecea8cd85c58406b6388 (patch)
tree3df231f96f9d7a1b8e1d2141e1c8c19f67b8d8fb
parent9a1ec451d3c8c6bed062ee002b5c613d64ca1ecd (diff)
downloadrust-a96948195227f98dd18eecea8cd85c58406b6388.tar.gz
rust-a96948195227f98dd18eecea8cd85c58406b6388.zip
Add syntax fixup for while loops
-rw-r--r--crates/hir-expand/src/fixup.rs76
1 files changed, 75 insertions, 1 deletions
diff --git a/crates/hir-expand/src/fixup.rs b/crates/hir-expand/src/fixup.rs
index 9999790fae7..6d0766020bd 100644
--- a/crates/hir-expand/src/fixup.rs
+++ b/crates/hir-expand/src/fixup.rs
@@ -5,7 +5,7 @@ use std::mem;
 use mbe::{SyntheticToken, SyntheticTokenId, TokenMap};
 use rustc_hash::FxHashMap;
 use syntax::{
-    ast::{self, AstNode},
+    ast::{self, AstNode, HasLoopBody},
     match_ast, SyntaxElement, SyntaxKind, SyntaxNode, TextRange,
 };
 use tt::Subtree;
@@ -142,6 +142,39 @@ pub(crate) fn fixup_syntax(node: &SyntaxNode) -> SyntaxFixups {
                         ]);
                     }
                 },
+                ast::WhileExpr(it) => {
+                    if it.condition().is_none() {
+                        // insert placeholder token after the while token
+                        let while_token = match it.while_token() {
+                            Some(t) => t,
+                            None => continue,
+                        };
+                        append.insert(while_token.into(), vec![
+                            SyntheticToken {
+                                kind: SyntaxKind::IDENT,
+                                text: "__ra_fixup".into(),
+                                range: end_range,
+                                id: EMPTY_ID,
+                            },
+                        ]);
+                    }
+                    if it.loop_body().is_none() {
+                        append.insert(node.clone().into(), vec![
+                            SyntheticToken {
+                                kind: SyntaxKind::L_CURLY,
+                                text: "{".into(),
+                                range: end_range,
+                                id: EMPTY_ID,
+                            },
+                            SyntheticToken {
+                                kind: SyntaxKind::R_CURLY,
+                                text: "}".into(),
+                                range: end_range,
+                                id: EMPTY_ID,
+                            },
+                        ]);
+                    }
+                },
                 // FIXME: foo::
                 // FIXME: for, loop, match etc.
                 _ => (),
@@ -379,4 +412,45 @@ fn foo () {if {} {}}
 "#]],
         )
     }
+
+    #[test]
+    fn fixup_while_1() {
+        check(
+            r#"
+fn foo() {
+    while
+}
+"#,
+            expect![[r#"
+fn foo () {while __ra_fixup {}}
+"#]],
+        )
+    }
+
+    #[test]
+    fn fixup_while_2() {
+        check(
+            r#"
+fn foo() {
+    while foo
+}
+"#,
+            expect![[r#"
+fn foo () {while foo {}}
+"#]],
+        )
+    }
+    #[test]
+    fn fixup_while_3() {
+        check(
+            r#"
+fn foo() {
+    while {}
+}
+"#,
+            expect![[r#"
+fn foo () {while __ra_fixup {}}
+"#]],
+        )
+    }
 }