mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-18 16:20:18 +00:00
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
This commit is contained in:
parent
4666138c91
commit
3f62ab8f51
5 changed files with 132 additions and 8 deletions
|
@ -799,3 +799,70 @@ fn test_doc_comment_preserves_indents() {
|
|||
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
|
||||
assert_eq!("doc1\n```\nfn foo() {\n // ...\n}\n```", module.doc_comment_text().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_where_predicates() {
|
||||
fn assert_bound(text: &str, bound: Option<&TypeBound>) {
|
||||
assert_eq!(text, bound.unwrap().syntax().text().to_string());
|
||||
}
|
||||
|
||||
let file = SourceFile::parse(
|
||||
r#"
|
||||
fn foo()
|
||||
where
|
||||
T: Clone + Copy + Debug + 'static,
|
||||
'a: 'b + 'c,
|
||||
Iterator::Item: 'a + Debug,
|
||||
Iterator::Item: Debug + 'a,
|
||||
<T as Iterator>::Item: Debug + 'a,
|
||||
for<'a> F: Fn(&'a str)
|
||||
{}
|
||||
"#,
|
||||
);
|
||||
let where_clause = file.syntax().descendants().find_map(WhereClause::cast).unwrap();
|
||||
|
||||
let mut predicates = where_clause.predicates();
|
||||
|
||||
let pred = predicates.next().unwrap();
|
||||
let mut bounds = pred.type_bound_list().unwrap().bounds();
|
||||
|
||||
assert_eq!("T", pred.type_ref().unwrap().syntax().text().to_string());
|
||||
assert_bound("Clone", bounds.next());
|
||||
assert_bound("Copy", bounds.next());
|
||||
assert_bound("Debug", bounds.next());
|
||||
assert_bound("'static", bounds.next());
|
||||
|
||||
let pred = predicates.next().unwrap();
|
||||
let mut bounds = pred.type_bound_list().unwrap().bounds();
|
||||
|
||||
assert_eq!("'a", pred.lifetime().unwrap().syntax().text().to_string());
|
||||
|
||||
assert_bound("'b", bounds.next());
|
||||
assert_bound("'c", bounds.next());
|
||||
|
||||
let pred = predicates.next().unwrap();
|
||||
let mut bounds = pred.type_bound_list().unwrap().bounds();
|
||||
|
||||
assert_eq!("Iterator::Item", pred.type_ref().unwrap().syntax().text().to_string());
|
||||
assert_bound("'a", bounds.next());
|
||||
|
||||
let pred = predicates.next().unwrap();
|
||||
let mut bounds = pred.type_bound_list().unwrap().bounds();
|
||||
|
||||
assert_eq!("Iterator::Item", pred.type_ref().unwrap().syntax().text().to_string());
|
||||
assert_bound("Debug", bounds.next());
|
||||
assert_bound("'a", bounds.next());
|
||||
|
||||
let pred = predicates.next().unwrap();
|
||||
let mut bounds = pred.type_bound_list().unwrap().bounds();
|
||||
|
||||
assert_eq!("<T as Iterator>::Item", pred.type_ref().unwrap().syntax().text().to_string());
|
||||
assert_bound("Debug", bounds.next());
|
||||
assert_bound("'a", bounds.next());
|
||||
|
||||
let pred = predicates.next().unwrap();
|
||||
let mut bounds = pred.type_bound_list().unwrap().bounds();
|
||||
|
||||
assert_eq!("for<'a> F", pred.type_ref().unwrap().syntax().text().to_string());
|
||||
assert_bound("Fn(&'a str)", bounds.next());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue