From c8b2ec8c20be44ae19d15e90ff812745f029899e Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 12 Apr 2020 12:28:24 +0200 Subject: Add support for bounds on associated types in trait definitions E.g. ``` trait Trait { type Item: SomeOtherTrait; } ``` Note that these don't simply desugar to where clauses; as I understand it, where clauses have to be proved by the *user* of the trait, but these bounds are proved by the *implementor*. (Also, where clauses on associated types are unstable.) --- crates/ra_hir_def/src/data.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 56a20c5bd..5dfde75d9 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -9,7 +9,8 @@ use hir_expand::{ }; use ra_prof::profile; use ra_syntax::ast::{ - self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner, VisibilityOwner, + self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner, TypeBoundsOwner, + VisibilityOwner, }; use crate::{ @@ -106,6 +107,7 @@ pub struct TypeAliasData { pub name: Name, pub type_ref: Option, pub visibility: RawVisibility, + pub bounds: Vec, } impl TypeAliasData { @@ -118,9 +120,17 @@ impl TypeAliasData { let name = node.value.name().map_or_else(Name::missing, |n| n.as_name()); let type_ref = node.value.type_ref().map(TypeRef::from_ast); let vis_default = RawVisibility::default_for_container(loc.container); - let visibility = - RawVisibility::from_ast_with_default(db, vis_default, node.map(|n| n.visibility())); - Arc::new(TypeAliasData { name, type_ref, visibility }) + let visibility = RawVisibility::from_ast_with_default( + db, + vis_default, + node.as_ref().map(|n| n.visibility()), + ); + let bounds = if let Some(bound_list) = node.value.type_bound_list() { + bound_list.bounds().map(TypeBound::from_ast).collect() + } else { + Vec::new() + }; + Arc::new(TypeAliasData { name, type_ref, visibility, bounds }) } } -- cgit v1.2.3