From 3ac605e6876056fa56098231cc2f96553faab8f0 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 20 Dec 2018 21:56:28 +0100 Subject: Add beginnings of type infrastructure --- crates/ra_syntax/src/ast/generated.rs | 44 ++++++++++++++++++++++++++++++----- crates/ra_syntax/src/grammar.ron | 14 +++++------ 2 files changed, 45 insertions(+), 13 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index bf056131e..91f27fb26 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -523,7 +523,15 @@ impl> CastExprNode { } -impl<'a> CastExpr<'a> {} +impl<'a> CastExpr<'a> { + pub fn expr(self) -> Option> { + super::child_opt(self) + } + + pub fn type_ref(self) -> Option> { + super::child_opt(self) + } +} // Char #[derive(Debug, Clone, Copy,)] @@ -2312,6 +2320,10 @@ impl<'a> Param<'a> { pub fn pat(self) -> Option> { super::child_opt(self) } + + pub fn type_ref(self) -> Option> { + super::child_opt(self) + } } // ParamList @@ -2394,7 +2406,11 @@ impl> ParenExprNode { } -impl<'a> ParenExpr<'a> {} +impl<'a> ParenExpr<'a> { + pub fn expr(self) -> Option> { + super::child_opt(self) + } +} // ParenType #[derive(Debug, Clone, Copy,)] @@ -2829,7 +2845,11 @@ impl> PrefixExprNode { } -impl<'a> PrefixExpr<'a> {} +impl<'a> PrefixExpr<'a> { + pub fn expr(self) -> Option> { + super::child_opt(self) + } +} // RangeExpr #[derive(Debug, Clone, Copy,)] @@ -2940,7 +2960,11 @@ impl> RefExprNode { } -impl<'a> RefExpr<'a> {} +impl<'a> RefExpr<'a> { + pub fn expr(self) -> Option> { + super::child_opt(self) + } +} // RefPat #[derive(Debug, Clone, Copy,)] @@ -3088,7 +3112,11 @@ impl> ReturnExprNode { } -impl<'a> ReturnExpr<'a> {} +impl<'a> ReturnExpr<'a> { + pub fn expr(self) -> Option> { + super::child_opt(self) + } +} // SelfParam #[derive(Debug, Clone, Copy,)] @@ -3578,7 +3606,11 @@ impl> TryExprNode { } -impl<'a> TryExpr<'a> {} +impl<'a> TryExpr<'a> { + pub fn expr(self) -> Option> { + super::child_opt(self) + } +} // TupleExpr #[derive(Debug, Clone, Copy,)] diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index eed67637e..c43db51b6 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -346,7 +346,7 @@ Grammar( "TupleExpr": (), "ArrayExpr": (), - "ParenExpr": (), + "ParenExpr": (options: ["Expr"]), "PathExpr": (options: ["Path"]), "LambdaExpr": ( options: [ @@ -377,7 +377,7 @@ Grammar( "BlockExpr": ( options: [ "Block" ] ), - "ReturnExpr": (), + "ReturnExpr": (options: ["Expr"]), "MatchExpr": ( options: [ "Expr", "MatchArmList" ], ), @@ -405,10 +405,10 @@ Grammar( ), "IndexExpr": (), "FieldExpr": (), - "TryExpr": (), - "CastExpr": (), - "RefExpr": (), - "PrefixExpr": (), + "TryExpr": (options: ["Expr"]), + "CastExpr": (options: ["Expr", "TypeRef"]), + "RefExpr": (options: ["Expr"]), + "PrefixExpr": (options: ["Expr"]), "RangeExpr": (), "BinExpr": (), "String": (), @@ -521,7 +521,7 @@ Grammar( ), "SelfParam": (), "Param": ( - options: [ "Pat" ], + options: [ "Pat", "TypeRef" ], ), "UseItem": ( options: [ "UseTree" ] -- cgit v1.2.3 From 3899898d75176ce3cd87f9e2acecd7e3a987dda5 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 22 Dec 2018 22:17:55 +0100 Subject: Parse integer / float types --- crates/ra_syntax/src/ast/generated.rs | 6 +++++- crates/ra_syntax/src/grammar.ron | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 91f27fb26..74bf4d3cc 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -2697,7 +2697,11 @@ impl> PathTypeNode { } -impl<'a> PathType<'a> {} +impl<'a> PathType<'a> { + pub fn path(self) -> Option> { + super::child_opt(self) + } +} // PlaceholderPat #[derive(Debug, Clone, Copy,)] diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index c43db51b6..29b84854a 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -304,7 +304,7 @@ Grammar( "ParenType": (), "TupleType": (), "NeverType": (), - "PathType": (), + "PathType": (options: ["Path"]), "PointerType": (), "ArrayType": (), "SliceType": (), -- cgit v1.2.3 From 7348f7883fa2bd571fff036c82e98c102d05c362 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 23 Dec 2018 12:05:54 +0100 Subject: Add testing infrastructure for type inference - move dir_tests to test_utils for that. --- crates/ra_syntax/tests/test.rs | 105 ++--------------------------------------- 1 file changed, 5 insertions(+), 100 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/tests/test.rs b/crates/ra_syntax/tests/test.rs index 4266864bd..9d94a1a23 100644 --- a/crates/ra_syntax/tests/test.rs +++ b/crates/ra_syntax/tests/test.rs @@ -9,6 +9,7 @@ use std::{ path::{Path, PathBuf, Component}, }; +use test_utils::{project_dir, dir_tests, read_text, collect_tests}; use ra_syntax::{ utils::{check_fuzz_invariants, dump_tree}, SourceFileNode, @@ -16,7 +17,7 @@ use ra_syntax::{ #[test] fn lexer_tests() { - dir_tests(&["lexer"], |text, _| { + dir_tests(&test_data_dir(), &["lexer"], |text, _| { let tokens = ra_syntax::tokenize(text); dump_tokens(&tokens, text) }) @@ -24,7 +25,7 @@ fn lexer_tests() { #[test] fn parser_tests() { - dir_tests(&["parser/inline/ok", "parser/ok"], |text, path| { + dir_tests(&test_data_dir(), &["parser/inline/ok", "parser/ok"], |text, path| { let file = SourceFileNode::parse(text); let errors = file.errors(); assert_eq!( @@ -35,7 +36,7 @@ fn parser_tests() { ); dump_tree(file.syntax()) }); - dir_tests(&["parser/err", "parser/inline/err"], |text, path| { + dir_tests(&test_data_dir(), &["parser/err", "parser/inline/err"], |text, path| { let file = SourceFileNode::parse(text); let errors = file.errors(); assert_ne!( @@ -50,7 +51,7 @@ fn parser_tests() { #[test] fn parser_fuzz_tests() { - for (_, text) in collect_tests(&["parser/fuzz-failures"]) { + for (_, text) in collect_tests(&test_data_dir(), &["parser/fuzz-failures"]) { check_fuzz_invariants(&text) } } @@ -92,102 +93,6 @@ fn self_hosting_parsing() { "self_hosting_parsing found too few files - is it running in the right directory?" ) } -/// Read file and normalize newlines. -/// -/// `rustc` seems to always normalize `\r\n` newlines to `\n`: -/// -/// ``` -/// let s = " -/// "; -/// assert_eq!(s.as_bytes(), &[10]); -/// ``` -/// -/// so this should always be correct. -fn read_text(path: &Path) -> String { - fs::read_to_string(path) - .expect(&format!("File at {:?} should be valid", path)) - .replace("\r\n", "\n") -} - -fn dir_tests(paths: &[&str], f: F) -where - F: Fn(&str, &Path) -> String, -{ - for (path, input_code) in collect_tests(paths) { - let parse_tree = f(&input_code, &path); - let path = path.with_extension("txt"); - if !path.exists() { - println!("\nfile: {}", path.display()); - println!("No .txt file with expected result, creating...\n"); - println!("{}\n{}", input_code, parse_tree); - fs::write(&path, &parse_tree).unwrap(); - panic!("No expected result") - } - let expected = read_text(&path); - let expected = expected.as_str(); - let parse_tree = parse_tree.as_str(); - assert_equal_text(expected, parse_tree, &path); - } -} - -const REWRITE: bool = false; - -fn assert_equal_text(expected: &str, actual: &str, path: &Path) { - if expected == actual { - return; - } - let dir = project_dir(); - let pretty_path = path.strip_prefix(&dir).unwrap_or_else(|_| path); - if expected.trim() == actual.trim() { - println!("whitespace difference, rewriting"); - println!("file: {}\n", pretty_path.display()); - fs::write(path, actual).unwrap(); - return; - } - if REWRITE { - println!("rewriting {}", pretty_path.display()); - fs::write(path, actual).unwrap(); - return; - } - assert_eq_text!(expected, actual, "file: {}", pretty_path.display()); -} - -fn collect_tests(paths: &[&str]) -> Vec<(PathBuf, String)> { - paths - .iter() - .flat_map(|path| { - let path = test_data_dir().join(path); - test_from_dir(&path).into_iter() - }) - .map(|path| { - let text = read_text(&path); - (path, text) - }) - .collect() -} - -fn test_from_dir(dir: &Path) -> Vec { - let mut acc = Vec::new(); - for file in fs::read_dir(&dir).unwrap() { - let file = file.unwrap(); - let path = file.path(); - if path.extension().unwrap_or_default() == "rs" { - acc.push(path); - } - } - acc.sort(); - acc -} - -fn project_dir() -> PathBuf { - let dir = env!("CARGO_MANIFEST_DIR"); - PathBuf::from(dir) - .parent() - .unwrap() - .parent() - .unwrap() - .to_owned() -} fn test_data_dir() -> PathBuf { project_dir().join("crates/ra_syntax/tests/data") -- cgit v1.2.3 From 515c3bc59bfc227cbbb82f80b53c5c125be4fc30 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 23 Dec 2018 12:15:46 +0100 Subject: Cleanup --- crates/ra_syntax/tests/test.rs | 56 +++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 25 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/tests/test.rs b/crates/ra_syntax/tests/test.rs index 9d94a1a23..2235dc401 100644 --- a/crates/ra_syntax/tests/test.rs +++ b/crates/ra_syntax/tests/test.rs @@ -1,12 +1,10 @@ extern crate ra_syntax; -#[macro_use] extern crate test_utils; extern crate walkdir; use std::{ fmt::Write, - fs, - path::{Path, PathBuf, Component}, + path::{PathBuf, Component}, }; use test_utils::{project_dir, dir_tests, read_text, collect_tests}; @@ -25,28 +23,36 @@ fn lexer_tests() { #[test] fn parser_tests() { - dir_tests(&test_data_dir(), &["parser/inline/ok", "parser/ok"], |text, path| { - let file = SourceFileNode::parse(text); - let errors = file.errors(); - assert_eq!( - &*errors, - &[] as &[ra_syntax::SyntaxError], - "There should be no errors in the file {:?}", - path.display() - ); - dump_tree(file.syntax()) - }); - dir_tests(&test_data_dir(), &["parser/err", "parser/inline/err"], |text, path| { - let file = SourceFileNode::parse(text); - let errors = file.errors(); - assert_ne!( - &*errors, - &[] as &[ra_syntax::SyntaxError], - "There should be errors in the file {:?}", - path.display() - ); - dump_tree(file.syntax()) - }); + dir_tests( + &test_data_dir(), + &["parser/inline/ok", "parser/ok"], + |text, path| { + let file = SourceFileNode::parse(text); + let errors = file.errors(); + assert_eq!( + &*errors, + &[] as &[ra_syntax::SyntaxError], + "There should be no errors in the file {:?}", + path.display() + ); + dump_tree(file.syntax()) + }, + ); + dir_tests( + &test_data_dir(), + &["parser/err", "parser/inline/err"], + |text, path| { + let file = SourceFileNode::parse(text); + let errors = file.errors(); + assert_ne!( + &*errors, + &[] as &[ra_syntax::SyntaxError], + "There should be errors in the file {:?}", + path.display() + ); + dump_tree(file.syntax()) + }, + ); } #[test] -- cgit v1.2.3 From 93ffbf80c632a7d38fc8bbdf6357bfd26a96a35a Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 23 Dec 2018 13:22:29 +0100 Subject: Make let statements kind of work --- crates/ra_syntax/src/ast/generated.rs | 4 ++++ crates/ra_syntax/src/grammar.ron | 1 + 2 files changed, 5 insertions(+) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 74bf4d3cc..b15c4ef6f 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -1561,6 +1561,10 @@ impl<'a> LetStmt<'a> { super::child_opt(self) } + pub fn type_ref(self) -> Option> { + super::child_opt(self) + } + pub fn initializer(self) -> Option> { super::child_opt(self) } diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 29b84854a..8dca493ee 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -499,6 +499,7 @@ Grammar( ), "LetStmt": ( options: [ ["pat", "Pat"], + ["type_ref", "TypeRef"], ["initializer", "Expr"], ]), "Condition": ( -- cgit v1.2.3 From ef67581104eb00a0c199f0b2a3b558da8a6f90a2 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 23 Dec 2018 17:13:11 +0100 Subject: Resolve paths to defs (functions currently) during type inference --- crates/ra_syntax/src/ast/generated.rs | 6 +++++- crates/ra_syntax/src/grammar.ron | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index b15c4ef6f..c73533861 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -3083,7 +3083,11 @@ impl> RetTypeNode { } -impl<'a> RetType<'a> {} +impl<'a> RetType<'a> { + pub fn type_ref(self) -> Option> { + super::child_opt(self) + } +} // ReturnExpr #[derive(Debug, Clone, Copy,)] diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 8dca493ee..e3b9032a0 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -254,7 +254,7 @@ Grammar( ], options: [ "ParamList", ["body", "Block"], "RetType" ], ), - "RetType": (), + "RetType": (options: ["TypeRef"]), "StructDef": ( traits: [ "NameOwner", -- cgit v1.2.3