]> git.lizzy.rs Git - rust.git/commitdiff
Fix panic in split and merge import assists
authorLaurențiu Nicola <lnicola@dend.ro>
Tue, 23 Jun 2020 06:41:43 +0000 (09:41 +0300)
committerLaurențiu Nicola <lnicola@dend.ro>
Tue, 23 Jun 2020 13:51:07 +0000 (16:51 +0300)
crates/ra_assists/src/handlers/merge_imports.rs
crates/ra_assists/src/handlers/split_import.rs
crates/ra_parser/src/grammar/paths.rs
crates/ra_syntax/test_data/parser/err/0004_use_path_bad_segment.rast
crates/ra_syntax/test_data/parser/inline/err/0015_empty_segment.rast [new file with mode: 0644]
crates/ra_syntax/test_data/parser/inline/err/0015_empty_segment.rs [new file with mode: 0644]
docs/dev/README.md

index 972d162419468d830b8aa08ea6595b4db840b791..ac0b3035c8fcc881277dd6806496536b878cf822 100644 (file)
@@ -127,7 +127,7 @@ fn first_path(path: &ast::Path) -> ast::Path {
 
 #[cfg(test)]
 mod tests {
-    use crate::tests::check_assist;
+    use crate::tests::{check_assist, check_assist_not_applicable};
 
     use super::*;
 
@@ -276,4 +276,14 @@ fn test_double_comma() {
 ",
         )
     }
+
+    #[test]
+    fn test_empty_use() {
+        check_assist_not_applicable(
+            merge_imports,
+            r"
+use std::<|>
+fn main() {}",
+        );
+    }
 }
index c7a8744802dccb6ac7ad1b055dc4c6626f2b15f3..38aa199a06ff311e509dcd45d6a197ad52cfc67f 100644 (file)
@@ -66,4 +66,14 @@ fn split_import_target() {
     fn issue4044() {
         check_assist_not_applicable(split_import, "use crate::<|>:::self;")
     }
+
+    #[test]
+    fn test_empty_use() {
+        check_assist_not_applicable(
+            split_import,
+            r"
+use std::<|>
+fn main() {}",
+        );
+    }
 }
index 428aa711e161aa0b7ee0ee6a91489d29ccf9ca42..fd51189d596c765d32a99358e5ca8bf587c72fb0 100644 (file)
@@ -73,8 +73,10 @@ fn path_segment(p: &mut Parser, mode: Mode, first: bool) {
         }
         p.expect(T![>]);
     } else {
+        let mut empty = true;
         if first {
             p.eat(T![::]);
+            empty = false;
         }
         match p.current() {
             IDENT => {
@@ -86,6 +88,12 @@ fn path_segment(p: &mut Parser, mode: Mode, first: bool) {
             T![self] | T![super] | T![crate] => p.bump_any(),
             _ => {
                 p.err_recover("expected identifier", items::ITEM_RECOVERY_SET);
+                if empty {
+                    // test_err empty_segment
+                    // use crate::;
+                    m.abandon(p);
+                    return;
+                }
             }
         };
     }
index 8c6b89dc25f39c50c501c02d305e14e19ed02c91..b3bcf472a2ce6c234200b3a0d76f42d4006e55b1 100644 (file)
@@ -9,8 +9,7 @@ SOURCE_FILE@0..12
             NAME_REF@4..7
               IDENT@4..7 "foo"
         COLON2@7..9 "::"
-        PATH_SEGMENT@9..11
-          ERROR@9..11
-            INT_NUMBER@9..11 "92"
+        ERROR@9..11
+          INT_NUMBER@9..11 "92"
     SEMICOLON@11..12 ";"
 error 9..9: expected identifier
diff --git a/crates/ra_syntax/test_data/parser/inline/err/0015_empty_segment.rast b/crates/ra_syntax/test_data/parser/inline/err/0015_empty_segment.rast
new file mode 100644 (file)
index 0000000..da85056
--- /dev/null
@@ -0,0 +1,15 @@
+SOURCE_FILE@0..13
+  USE_ITEM@0..12
+    USE_KW@0..3 "use"
+    WHITESPACE@3..4 " "
+    USE_TREE@4..12
+      PATH@4..12
+        PATH@4..9
+          PATH_SEGMENT@4..9
+            CRATE_KW@4..9 "crate"
+        COLON2@9..11 "::"
+        ERROR@11..12
+          SEMICOLON@11..12 ";"
+  WHITESPACE@12..13 "\n"
+error 11..11: expected identifier
+error 12..12: expected SEMICOLON
diff --git a/crates/ra_syntax/test_data/parser/inline/err/0015_empty_segment.rs b/crates/ra_syntax/test_data/parser/inline/err/0015_empty_segment.rs
new file mode 100644 (file)
index 0000000..7510664
--- /dev/null
@@ -0,0 +1 @@
+use crate::;
index 1b63d8223620591f7e4e1b6bd6c0d31903aec032..76e1da6cf46f36cba6203899395aa586b3cf7e6d 100644 (file)
@@ -348,6 +348,8 @@ To update test data, run with `UPDATE_EXPECTATIONS` variable:
 env UPDATE_EXPECTATIONS=1 cargo qt
 ```
 
+After adding a new inline test you need to run `cargo xtest codegen` and also update the test data as described above.
+
 # Logging
 
 Logging is done by both rust-analyzer and VS Code, so it might be tricky to