From 76ddface089886c88b8b29e3893119f38ef26aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Tue, 23 Jun 2020 09:41:43 +0300 Subject: Fix panic in split and merge import assists --- crates/ra_assists/src/handlers/merge_imports.rs | 12 +++++++++++- crates/ra_assists/src/handlers/split_import.rs | 10 ++++++++++ crates/ra_parser/src/grammar/paths.rs | 8 ++++++++ .../test_data/parser/err/0004_use_path_bad_segment.rast | 5 ++--- .../test_data/parser/inline/err/0015_empty_segment.rast | 15 +++++++++++++++ .../test_data/parser/inline/err/0015_empty_segment.rs | 1 + 6 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 crates/ra_syntax/test_data/parser/inline/err/0015_empty_segment.rast create mode 100644 crates/ra_syntax/test_data/parser/inline/err/0015_empty_segment.rs (limited to 'crates') diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index 972d16241..ac0b3035c 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs @@ -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 @@ bar::baz}; ", ) } + + #[test] + fn test_empty_use() { + check_assist_not_applicable( + merge_imports, + r" +use std::<|> +fn main() {}", + ); + } } diff --git a/crates/ra_assists/src/handlers/split_import.rs b/crates/ra_assists/src/handlers/split_import.rs index c7a874480..38aa199a0 100644 --- a/crates/ra_assists/src/handlers/split_import.rs +++ b/crates/ra_assists/src/handlers/split_import.rs @@ -66,4 +66,14 @@ mod tests { 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() {}", + ); + } } diff --git a/crates/ra_parser/src/grammar/paths.rs b/crates/ra_parser/src/grammar/paths.rs index 428aa711e..fd51189d5 100644 --- a/crates/ra_parser/src/grammar/paths.rs +++ b/crates/ra_parser/src/grammar/paths.rs @@ -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; + } } }; } diff --git a/crates/ra_syntax/test_data/parser/err/0004_use_path_bad_segment.rast b/crates/ra_syntax/test_data/parser/err/0004_use_path_bad_segment.rast index 8c6b89dc2..b3bcf472a 100644 --- a/crates/ra_syntax/test_data/parser/err/0004_use_path_bad_segment.rast +++ b/crates/ra_syntax/test_data/parser/err/0004_use_path_bad_segment.rast @@ -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 index 000000000..da8505607 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/err/0015_empty_segment.rast @@ -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 index 000000000..7510664e1 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/err/0015_empty_segment.rs @@ -0,0 +1 @@ +use crate::; -- cgit v1.2.3