diff options
author | Ville Penttinen <[email protected]> | 2019-03-24 17:45:11 +0000 |
---|---|---|
committer | Ville Penttinen <[email protected]> | 2019-03-31 11:20:56 +0100 |
commit | 3f62ab8f510fdac1eeab515c515c437d6dd141e6 (patch) | |
tree | 91bb5a70b999474e4d8212245fe265e71c3afde4 /crates/ra_syntax/src/ast.rs | |
parent | 4666138c910a655deb0e8129b0a1dc5974b49bdf (diff) |
Add WherePred to allow predicate access in WhereClause
This also unifies parsing of WHERE_PRED bounds, now Lifetime bounds will also be
parsed using TYPE_BOUND_LIST
Diffstat (limited to 'crates/ra_syntax/src/ast.rs')
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 4fddc00ea..fd7e63f84 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs | |||
@@ -799,3 +799,70 @@ fn test_doc_comment_preserves_indents() { | |||
799 | let module = file.syntax().descendants().find_map(Module::cast).unwrap(); | 799 | let module = file.syntax().descendants().find_map(Module::cast).unwrap(); |
800 | assert_eq!("doc1\n```\nfn foo() {\n // ...\n}\n```", module.doc_comment_text().unwrap()); | 800 | assert_eq!("doc1\n```\nfn foo() {\n // ...\n}\n```", module.doc_comment_text().unwrap()); |
801 | } | 801 | } |
802 | |||
803 | #[test] | ||
804 | fn test_where_predicates() { | ||
805 | fn assert_bound(text: &str, bound: Option<&TypeBound>) { | ||
806 | assert_eq!(text, bound.unwrap().syntax().text().to_string()); | ||
807 | } | ||
808 | |||
809 | let file = SourceFile::parse( | ||
810 | r#" | ||
811 | fn foo() | ||
812 | where | ||
813 | T: Clone + Copy + Debug + 'static, | ||
814 | 'a: 'b + 'c, | ||
815 | Iterator::Item: 'a + Debug, | ||
816 | Iterator::Item: Debug + 'a, | ||
817 | <T as Iterator>::Item: Debug + 'a, | ||
818 | for<'a> F: Fn(&'a str) | ||
819 | {} | ||
820 | "#, | ||
821 | ); | ||
822 | let where_clause = file.syntax().descendants().find_map(WhereClause::cast).unwrap(); | ||
823 | |||
824 | let mut predicates = where_clause.predicates(); | ||
825 | |||
826 | let pred = predicates.next().unwrap(); | ||
827 | let mut bounds = pred.type_bound_list().unwrap().bounds(); | ||
828 | |||
829 | assert_eq!("T", pred.type_ref().unwrap().syntax().text().to_string()); | ||
830 | assert_bound("Clone", bounds.next()); | ||
831 | assert_bound("Copy", bounds.next()); | ||
832 | assert_bound("Debug", bounds.next()); | ||
833 | assert_bound("'static", bounds.next()); | ||
834 | |||
835 | let pred = predicates.next().unwrap(); | ||
836 | let mut bounds = pred.type_bound_list().unwrap().bounds(); | ||
837 | |||
838 | assert_eq!("'a", pred.lifetime().unwrap().syntax().text().to_string()); | ||
839 | |||
840 | assert_bound("'b", bounds.next()); | ||
841 | assert_bound("'c", bounds.next()); | ||
842 | |||
843 | let pred = predicates.next().unwrap(); | ||
844 | let mut bounds = pred.type_bound_list().unwrap().bounds(); | ||
845 | |||
846 | assert_eq!("Iterator::Item", pred.type_ref().unwrap().syntax().text().to_string()); | ||
847 | assert_bound("'a", bounds.next()); | ||
848 | |||
849 | let pred = predicates.next().unwrap(); | ||
850 | let mut bounds = pred.type_bound_list().unwrap().bounds(); | ||
851 | |||
852 | assert_eq!("Iterator::Item", pred.type_ref().unwrap().syntax().text().to_string()); | ||
853 | assert_bound("Debug", bounds.next()); | ||
854 | assert_bound("'a", bounds.next()); | ||
855 | |||
856 | let pred = predicates.next().unwrap(); | ||
857 | let mut bounds = pred.type_bound_list().unwrap().bounds(); | ||
858 | |||
859 | assert_eq!("<T as Iterator>::Item", pred.type_ref().unwrap().syntax().text().to_string()); | ||
860 | assert_bound("Debug", bounds.next()); | ||
861 | assert_bound("'a", bounds.next()); | ||
862 | |||
863 | let pred = predicates.next().unwrap(); | ||
864 | let mut bounds = pred.type_bound_list().unwrap().bounds(); | ||
865 | |||
866 | assert_eq!("for<'a> F", pred.type_ref().unwrap().syntax().text().to_string()); | ||
867 | assert_bound("Fn(&'a str)", bounds.next()); | ||
868 | } | ||