fix syntax errors in tests

This commit is contained in:
Aleksey Kladov 2019-05-28 18:07:39 +03:00
parent 2e3f5af9d4
commit c8bcfe6a05
7 changed files with 137 additions and 121 deletions

View file

@ -4,7 +4,7 @@ mod input;
use std::{panic, sync::Arc}; use std::{panic, sync::Arc};
use ra_syntax::{TextUnit, TextRange, SourceFile, TreeArc}; use ra_syntax::{TextUnit, TextRange, SourceFile, Parse};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use ra_prof::profile; use ra_prof::profile;
@ -74,7 +74,7 @@ pub trait SourceDatabase: CheckCanceled + std::fmt::Debug {
fn file_text(&self, file_id: FileId) -> Arc<String>; fn file_text(&self, file_id: FileId) -> Arc<String>;
// Parses the file into the syntax tree. // Parses the file into the syntax tree.
#[salsa::invoke(parse_query)] #[salsa::invoke(parse_query)]
fn parse(&self, file_id: FileId) -> TreeArc<SourceFile>; fn parse(&self, file_id: FileId) -> Parse;
/// Path to a file, relative to the root of its source root. /// Path to a file, relative to the root of its source root.
#[salsa::input] #[salsa::input]
fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf; fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf;
@ -98,7 +98,7 @@ fn source_root_crates(db: &impl SourceDatabase, id: SourceRootId) -> Arc<Vec<Cra
Arc::new(res) Arc::new(res)
} }
fn parse_query(db: &impl SourceDatabase, file_id: FileId) -> TreeArc<SourceFile> { fn parse_query(db: &impl SourceDatabase, file_id: FileId) -> Parse {
let _p = profile("parse_query"); let _p = profile("parse_query");
let text = db.file_text(file_id); let text = db.file_text(file_id);
SourceFile::parse(&*text) SourceFile::parse(&*text)

View file

@ -116,7 +116,7 @@ impl ModuleSource {
) -> ModuleSource { ) -> ModuleSource {
match (file_id, decl_id) { match (file_id, decl_id) {
(Some(file_id), _) => { (Some(file_id), _) => {
let source_file = db.parse(file_id); let source_file = db.parse(file_id).tree;
ModuleSource::SourceFile(source_file) ModuleSource::SourceFile(source_file)
} }
(None, Some(item_id)) => { (None, Some(item_id)) => {

View file

@ -190,7 +190,7 @@ mod tests {
}; };
let (db, _source_root, file_id) = MockDatabase::with_single_file(&code); let (db, _source_root, file_id) = MockDatabase::with_single_file(&code);
let file = db.parse(file_id); let file = db.parse(file_id).ok().unwrap();
let marker: &ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); let marker: &ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap();
let analyzer = SourceAnalyzer::new(&db, file_id, marker.syntax(), None); let analyzer = SourceAnalyzer::new(&db, file_id, marker.syntax(), None);
@ -288,7 +288,7 @@ mod tests {
let (off, code) = extract_offset(code); let (off, code) = extract_offset(code);
let (db, _source_root, file_id) = MockDatabase::with_single_file(&code); let (db, _source_root, file_id) = MockDatabase::with_single_file(&code);
let file = db.parse(file_id); let file = db.parse(file_id).ok().unwrap();
let expected_name = find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()) let expected_name = find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into())
.expect("failed to find a name at the target offset"); .expect("failed to find a name at the target offset");
let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap();

View file

@ -75,7 +75,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
} }
let source_map = self.func.body_source_map(db); let source_map = self.func.body_source_map(db);
let file_id = self.func.source(db).0; let file_id = self.func.source(db).0;
let source_file = db.parse(file_id.original_file(db)); let source_file = db.parse(file_id.original_file(db)).tree;
if let Some(field_list_node) = source_map if let Some(field_list_node) = source_map
.expr_syntax(id) .expr_syntax(id)
.map(|ptr| ptr.to_node(source_file.syntax())) .map(|ptr| ptr.to_node(source_file.syntax()))

View file

@ -64,7 +64,7 @@ impl HirFileId {
db.check_canceled(); db.check_canceled();
let _p = profile("parse_or_expand_query"); let _p = profile("parse_or_expand_query");
match file_id.0 { match file_id.0 {
HirFileIdRepr::File(file_id) => Some(db.parse(file_id).syntax().to_owned()), HirFileIdRepr::File(file_id) => Some(db.parse(file_id).tree.syntax().to_owned()),
HirFileIdRepr::Macro(macro_file) => { HirFileIdRepr::Macro(macro_file) => {
let macro_call_id = macro_file.macro_call_id; let macro_call_id = macro_file.macro_call_id;
let tt = db let tt = db

View file

@ -46,7 +46,7 @@ pub fn module_from_declaration(
/// Locates the module by position in the source code. /// Locates the module by position in the source code.
pub fn module_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Module> { pub fn module_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Module> {
let file = db.parse(position.file_id); let file = db.parse(position.file_id).tree;
match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) { match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) {
Some(m) if !m.has_semi() => module_from_inline(db, position.file_id.into(), m), Some(m) if !m.has_semi() => module_from_inline(db, position.file_id.into(), m),
_ => module_from_file_id(db, position.file_id.into()), _ => module_from_file_id(db, position.file_id.into()),

View file

@ -59,7 +59,7 @@ fn test() {
let b: usize = 1; let b: usize = 1;
let c = b; let c = b;
} }
}"#), "#),
@r###" @r###"
[11; 71) '{ ...= b; }': () [11; 71) '{ ...= b; }': ()
[21; 22) 'a': isize [21; 22) 'a': isize
@ -85,7 +85,7 @@ fn test() {
a(); a();
b::c(); b::c();
} }
}"#), "#),
@r###" @r###"
[15; 20) '{ 1 }': u32 [15; 20) '{ 1 }': u32
[17; 18) '1': u32 [17; 18) '1': u32
@ -1004,7 +1004,7 @@ fn infer_tuple_struct_generics() {
assert_snapshot_matches!( assert_snapshot_matches!(
infer(r#" infer(r#"
struct A<T>(T); struct A<T>(T);
enum Option<T> { Some(T), None }; enum Option<T> { Some(T), None }
use Option::*; use Option::*;
fn test() { fn test() {
@ -1017,22 +1017,24 @@ fn test() {
} }
"#), "#),
@r###" @r###"
[77; 185) '{ ...one; }': ()
[83; 84) 'A': A<i32>(T) -> A<T> [76; 184) '{ ...one; }': ()
[83; 88) 'A(42)': A<i32> [82; 83) 'A': A<i32>(T) -> A<T>
[85; 87) '42': i32 [82; 87) 'A(42)': A<i32>
[94; 95) 'A': A<u128>(T) -> A<T> [84; 86) '42': i32
[94; 103) 'A(42u128)': A<u128> [93; 94) 'A': A<u128>(T) -> A<T>
[96; 102) '42u128': u128 [93; 102) 'A(42u128)': A<u128>
[109; 113) 'Some': Some<&str>(T) -> Option<T> [95; 101) '42u128': u128
[109; 118) 'Some("x")': Option<&str> [108; 112) 'Some': Some<&str>(T) -> Option<T>
[114; 117) '"x"': &str [108; 117) 'Some("x")': Option<&str>
[124; 136) 'Option::Some': Some<&str>(T) -> Option<T> [113; 116) '"x"': &str
[124; 141) 'Option...e("x")': Option<&str> [123; 135) 'Option::Some': Some<&str>(T) -> Option<T>
[137; 140) '"x"': &str [123; 140) 'Option...e("x")': Option<&str>
[147; 151) 'None': Option<{unknown}> [136; 139) '"x"': &str
[161; 162) 'x': Option<i64> [146; 150) 'None': Option<{unknown}>
[178; 182) 'None': Option<i64>"### [160; 161) 'x': Option<i64>
[177; 181) 'None': Option<i64>
"###
); );
} }
@ -1268,7 +1270,7 @@ impl Struct {
const FOO: u32 = 1; const FOO: u32 = 1;
} }
enum Enum; enum Enum {}
impl Enum { impl Enum {
const BAR: u32 = 2; const BAR: u32 = 2;
@ -1291,16 +1293,18 @@ fn test() {
} }
"#), "#),
@r###" @r###"
[52; 53) '1': u32
[103; 104) '2': u32 [52; 53) '1': u32
[211; 212) '5': u32 [105; 106) '2': u32
[227; 305) '{ ...:ID; }': () [213; 214) '5': u32
[237; 238) 'x': u32 [229; 307) '{ ...:ID; }': ()
[241; 252) 'Struct::FOO': u32 [239; 240) 'x': u32
[262; 263) 'y': u32 [243; 254) 'Struct::FOO': u32
[266; 275) 'Enum::BAR': u32 [264; 265) 'y': u32
[285; 286) 'z': {unknown} [268; 277) 'Enum::BAR': u32
[289; 302) 'TraitTest::ID': {unknown}"### [287; 288) 'z': {unknown}
[291; 304) 'TraitTest::ID': {unknown}
"###
); );
} }
@ -1308,7 +1312,7 @@ fn test() {
fn infer_associated_method_struct() { fn infer_associated_method_struct() {
assert_snapshot_matches!( assert_snapshot_matches!(
infer(r#" infer(r#"
struct A { x: u32 }; struct A { x: u32 }
impl A { impl A {
fn new() -> A { fn new() -> A {
@ -1321,15 +1325,17 @@ fn test() {
} }
"#), "#),
@r###" @r###"
[50; 76) '{ ... }': A
[60; 70) 'A { x: 0 }': A [49; 75) '{ ... }': A
[67; 68) '0': u32 [59; 69) 'A { x: 0 }': A
[89; 123) '{ ...a.x; }': () [66; 67) '0': u32
[99; 100) 'a': A [88; 122) '{ ...a.x; }': ()
[103; 109) 'A::new': fn new() -> A [98; 99) 'a': A
[103; 111) 'A::new()': A [102; 108) 'A::new': fn new() -> A
[117; 118) 'a': A [102; 110) 'A::new()': A
[117; 120) 'a.x': u32"### [116; 117) 'a': A
[116; 119) 'a.x': u32
"###
); );
} }
@ -1337,7 +1343,7 @@ fn test() {
fn infer_associated_method_enum() { fn infer_associated_method_enum() {
assert_snapshot_matches!( assert_snapshot_matches!(
infer(r#" infer(r#"
enum A { B, C }; enum A { B, C }
impl A { impl A {
pub fn b() -> A { pub fn b() -> A {
@ -1355,19 +1361,21 @@ fn test() {
} }
"#), "#),
@r###" @r###"
[48; 68) '{ ... }': A
[58; 62) 'A::B': A [47; 67) '{ ... }': A
[89; 109) '{ ... }': A [57; 61) 'A::B': A
[99; 103) 'A::C': A [88; 108) '{ ... }': A
[122; 179) '{ ... c; }': () [98; 102) 'A::C': A
[132; 133) 'a': A [121; 178) '{ ... c; }': ()
[136; 140) 'A::b': fn b() -> A [131; 132) 'a': A
[136; 142) 'A::b()': A [135; 139) 'A::b': fn b() -> A
[148; 149) 'a': A [135; 141) 'A::b()': A
[159; 160) 'c': A [147; 148) 'a': A
[163; 167) 'A::c': fn c() -> A [158; 159) 'c': A
[163; 169) 'A::c()': A [162; 166) 'A::c': fn c() -> A
[175; 176) 'c': A"### [162; 168) 'A::c()': A
[174; 175) 'c': A
"###
); );
} }
@ -1540,7 +1548,7 @@ fn test() {
fn infer_type_alias() { fn infer_type_alias() {
assert_snapshot_matches!( assert_snapshot_matches!(
infer(r#" infer(r#"
struct A<X, Y> { x: X, y: Y }; struct A<X, Y> { x: X, y: Y }
type Foo = A<u32, i128>; type Foo = A<u32, i128>;
type Bar<T> = A<T, u128>; type Bar<T> = A<T, u128>;
type Baz<U, V> = A<V, U>; type Baz<U, V> = A<V, U>;
@ -1554,22 +1562,24 @@ fn test(x: Foo, y: Bar<&str>, z: Baz<i8, u8>) {
} }
"#), "#),
@r###" @r###"
[117; 118) 'x': A<u32, i128>
[125; 126) 'y': A<&str, u128> [116; 117) 'x': A<u32, i128>
[139; 140) 'z': A<u8, i8> [124; 125) 'y': A<&str, u128>
[155; 212) '{ ...z.y; }': () [138; 139) 'z': A<u8, i8>
[161; 162) 'x': A<u32, i128> [154; 211) '{ ...z.y; }': ()
[161; 164) 'x.x': u32 [160; 161) 'x': A<u32, i128>
[170; 171) 'x': A<u32, i128> [160; 163) 'x.x': u32
[170; 173) 'x.y': i128 [169; 170) 'x': A<u32, i128>
[179; 180) 'y': A<&str, u128> [169; 172) 'x.y': i128
[179; 182) 'y.x': &str [178; 179) 'y': A<&str, u128>
[188; 189) 'y': A<&str, u128> [178; 181) 'y.x': &str
[188; 191) 'y.y': u128 [187; 188) 'y': A<&str, u128>
[197; 198) 'z': A<u8, i8> [187; 190) 'y.y': u128
[197; 200) 'z.x': u8 [196; 197) 'z': A<u8, i8>
[206; 207) 'z': A<u8, i8> [196; 199) 'z.x': u8
[206; 209) 'z.y': i8"### [205; 206) 'z': A<u8, i8>
[205; 208) 'z.y': i8
"###
) )
} }
@ -1578,7 +1588,7 @@ fn test(x: Foo, y: Bar<&str>, z: Baz<i8, u8>) {
fn recursive_type_alias() { fn recursive_type_alias() {
assert_snapshot_matches!( assert_snapshot_matches!(
infer(r#" infer(r#"
struct A<X> {}; struct A<X> {}
type Foo = Foo; type Foo = Foo;
type Bar = A<Bar>; type Bar = A<Bar>;
fn test(x: Foo) {} fn test(x: Foo) {}
@ -1795,18 +1805,21 @@ fn infer_std_crash_3() {
assert_snapshot_matches!( assert_snapshot_matches!(
infer(r#" infer(r#"
pub fn compute() { pub fn compute() {
match _ { match nope!() {
SizeSkeleton::Pointer { non_zero: true, tail } => {} SizeSkeleton::Pointer { non_zero: true, tail } => {}
} }
} }
"#), "#),
@r###" @r###"
[18; 102) '{ ... } }': ()
[24; 100) 'match ... }': () [18; 108) '{ ... } }': ()
[42; 88) 'SizeSk...tail }': {unknown} [24; 106) 'match ... }': ()
[76; 80) 'true': {unknown} [30; 37) 'nope!()': {unknown}
[82; 86) 'tail': {unknown} [48; 94) 'SizeSk...tail }': {unknown}
[92; 94) '{}': ()"### [82; 86) 'true': {unknown}
[88; 92) 'tail': {unknown}
[98; 100) '{}': ()
"###
); );
} }
@ -1817,20 +1830,21 @@ fn infer_std_crash_4() {
infer(r#" infer(r#"
pub fn primitive_type() { pub fn primitive_type() {
match *self { match *self {
BorrowedRef { type_: box Primitive(p), ..} => {}, BorrowedRef { type_: Primitive(p), ..} => {},
} }
} }
"#), "#),
@r###" @r###"
[25; 110) '{ ... } }': ()
[31; 108) 'match ... }': () [25; 106) '{ ... } }': ()
[37; 42) '*self': {unknown} [31; 104) 'match ... }': ()
[38; 42) 'self': {unknown} [37; 42) '*self': {unknown}
[53; 95) 'Borrow...), ..}': {unknown} [38; 42) 'self': {unknown}
[74; 77) 'box': {unknown} [53; 91) 'Borrow...), ..}': {unknown}
[78; 87) 'Primitive': {unknown} [74; 86) 'Primitive(p)': {unknown}
[88; 89) 'p': {unknown} [84; 85) 'p': {unknown}
[99; 101) '{}': ()"### [95; 97) '{}': ()
"###
); );
} }
@ -2304,8 +2318,8 @@ trait Into<T> {
fn into(self) -> T; fn into(self) -> T;
} }
struct S; struct S;
impl Into<u32> for S; impl Into<u32> for S {}
impl Into<u64> for S; impl Into<u64> for S {}
fn test() { fn test() {
let x: u32 = S.into(); let x: u32 = S.into();
let y: u64 = S.into(); let y: u64 = S.into();
@ -2313,18 +2327,20 @@ fn test() {
} }
"#), "#),
@r###" @r###"
[29; 33) 'self': Self
[107; 198) '{ ...(S); }': () [29; 33) 'self': Self
[117; 118) 'x': u32 [111; 202) '{ ...(S); }': ()
[126; 127) 'S': S [121; 122) 'x': u32
[126; 134) 'S.into()': u32 [130; 131) 'S': S
[144; 145) 'y': u64 [130; 138) 'S.into()': u32
[153; 154) 'S': S [148; 149) 'y': u64
[153; 161) 'S.into()': u64 [157; 158) 'S': S
[171; 172) 'z': {unknown} [157; 165) 'S.into()': u64
[175; 192) 'Into::...::into': {unknown} [175; 176) 'z': {unknown}
[175; 195) 'Into::...nto(S)': {unknown} [179; 196) 'Into::...::into': {unknown}
[193; 194) 'S': S"### [179; 199) 'Into::...nto(S)': {unknown}
[197; 198) 'S': S
"###
); );
} }
@ -2617,7 +2633,7 @@ fn method_resolution_where_clause_1() {
trait Clone {} trait Clone {}
trait Trait { fn foo(self) -> u128; } trait Trait { fn foo(self) -> u128; }
struct S; struct S;
impl Clone for S {}; impl Clone for S {}
impl<T> Trait for T where T: Clone {} impl<T> Trait for T where T: Clone {}
fn test() { S.foo()<|>; } fn test() { S.foo()<|>; }
"#, "#,
@ -2634,7 +2650,7 @@ trait Into<T> { fn into(self) -> T; }
trait From<T> { fn from(other: T) -> Self; } trait From<T> { fn from(other: T) -> Self; }
struct S1; struct S1;
struct S2; struct S2;
impl From<S2> for S1 {}; impl From<S2> for S1 {}
impl<T, U> Into<U> for T where U: From<T> {} impl<T, U> Into<U> for T where U: From<T> {}
fn test() { S2.into()<|>; } fn test() { S2.into()<|>; }
"#, "#,
@ -2651,7 +2667,7 @@ trait Into<T> { fn into(self) -> T; }
trait From<T> { fn from(other: T) -> Self; } trait From<T> { fn from(other: T) -> Self; }
struct S1; struct S1;
struct S2; struct S2;
impl From<S2> for S1 {}; impl From<S2> for S1 {}
impl<T, U: From<T>> Into<U> for T {} impl<T, U: From<T>> Into<U> for T {}
fn test() { S2.into()<|>; } fn test() { S2.into()<|>; }
"#, "#,
@ -2680,8 +2696,8 @@ fn method_resolution_slow() {
//- /main.rs //- /main.rs
trait SendX {} trait SendX {}
struct S1; impl SendX for S1; struct S1; impl SendX for S1 {}
struct S2; impl SendX for S2; struct S2; impl SendX for S2 {}
struct U1; struct U1;
trait Trait { fn method(self); } trait Trait { fn method(self); }
@ -2702,7 +2718,7 @@ fn test() { (S {}).method()<|>; }
} }
fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String {
let file = db.parse(pos.file_id); let file = db.parse(pos.file_id).ok().unwrap();
let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap(); let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap();
let analyzer = SourceAnalyzer::new(db, pos.file_id, expr.syntax(), Some(pos.offset)); let analyzer = SourceAnalyzer::new(db, pos.file_id, expr.syntax(), Some(pos.offset));
let ty = analyzer.type_of(db, expr).unwrap(); let ty = analyzer.type_of(db, expr).unwrap();
@ -2716,7 +2732,7 @@ fn type_at(content: &str) -> String {
fn infer(content: &str) -> String { fn infer(content: &str) -> String {
let (db, _, file_id) = MockDatabase::with_single_file(content); let (db, _, file_id) = MockDatabase::with_single_file(content);
let source_file = db.parse(file_id); let source_file = db.parse(file_id).ok().unwrap();
let mut acc = String::new(); let mut acc = String::new();
acc.push_str("\n"); acc.push_str("\n");
@ -2794,7 +2810,7 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
", ",
); );
{ {
let file = db.parse(pos.file_id); let file = db.parse(pos.file_id).ok().unwrap();
let node = let node =
algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent(); algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent();
let events = db.log_executed(|| { let events = db.log_executed(|| {
@ -2815,7 +2831,7 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
db.query_mut(ra_db::FileTextQuery).set(pos.file_id, Arc::new(new_text)); db.query_mut(ra_db::FileTextQuery).set(pos.file_id, Arc::new(new_text));
{ {
let file = db.parse(pos.file_id); let file = db.parse(pos.file_id).ok().unwrap();
let node = let node =
algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent(); algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent();
let events = db.log_executed(|| { let events = db.log_executed(|| {