5004: Fix panic in split/merge import assists r=matklad a=lnicola

Fixes #4368 #4905

Not sure if this is the best solution here. Maybe the `make` functions should be fallible? We generally seem to be playing whack-a-mole with panics in assists, although most of them are `unwrap`s in the assist code.

Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
This commit is contained in:
bors[bot] 2020-06-23 14:55:03 +00:00 committed by GitHub
commit c0b9ae5503
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 4 deletions

View file

@ -127,7 +127,7 @@ fn first_path(path: &ast::Path) -> ast::Path {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::tests::check_assist; use crate::tests::{check_assist, check_assist_not_applicable};
use super::*; use super::*;
@ -276,4 +276,14 @@ bar::baz};
", ",
) )
} }
#[test]
fn test_empty_use() {
check_assist_not_applicable(
merge_imports,
r"
use std::<|>
fn main() {}",
);
}
} }

View file

@ -66,4 +66,14 @@ mod tests {
fn issue4044() { fn issue4044() {
check_assist_not_applicable(split_import, "use crate::<|>:::self;") 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() {}",
);
}
} }

View file

@ -73,8 +73,10 @@ fn path_segment(p: &mut Parser, mode: Mode, first: bool) {
} }
p.expect(T![>]); p.expect(T![>]);
} else { } else {
let mut empty = true;
if first { if first {
p.eat(T![::]); p.eat(T![::]);
empty = false;
} }
match p.current() { match p.current() {
IDENT => { IDENT => {
@ -86,6 +88,12 @@ fn path_segment(p: &mut Parser, mode: Mode, first: bool) {
T![self] | T![super] | T![crate] => p.bump_any(), T![self] | T![super] | T![crate] => p.bump_any(),
_ => { _ => {
p.err_recover("expected identifier", items::ITEM_RECOVERY_SET); p.err_recover("expected identifier", items::ITEM_RECOVERY_SET);
if empty {
// test_err empty_segment
// use crate::;
m.abandon(p);
return;
}
} }
}; };
} }

View file

@ -9,8 +9,7 @@ SOURCE_FILE@0..12
NAME_REF@4..7 NAME_REF@4..7
IDENT@4..7 "foo" IDENT@4..7 "foo"
COLON2@7..9 "::" COLON2@7..9 "::"
PATH_SEGMENT@9..11 ERROR@9..11
ERROR@9..11 INT_NUMBER@9..11 "92"
INT_NUMBER@9..11 "92"
SEMICOLON@11..12 ";" SEMICOLON@11..12 ";"
error 9..9: expected identifier error 9..9: expected identifier

View file

@ -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

View file

@ -0,0 +1 @@
use crate::;

View file

@ -348,6 +348,8 @@ To update test data, run with `UPDATE_EXPECTATIONS` variable:
env UPDATE_EXPECTATIONS=1 cargo qt 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
Logging is done by both rust-analyzer and VS Code, so it might be tricky to Logging is done by both rust-analyzer and VS Code, so it might be tricky to