aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2020-06-24 15:46:44 +0100
committerJonas Schievink <[email protected]>2020-06-24 15:54:21 +0100
commitd6fd7809b016787e8a23f443b1b626840c4ea5c7 (patch)
treef0fa343b116319ceecc736a9f37f23a429a971f0 /crates/ra_hir_def/src
parent3b50b0b2b658006c23d21749627e413af5c2aa44 (diff)
Clean up and fix inner item collection a bit
Diffstat (limited to 'crates/ra_hir_def/src')
-rw-r--r--crates/ra_hir_def/src/item_tree/lower.rs33
-rw-r--r--crates/ra_hir_def/src/item_tree/tests.rs12
2 files changed, 38 insertions, 7 deletions
diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs
index 6e31266a2..f10ad25f7 100644
--- a/crates/ra_hir_def/src/item_tree/lower.rs
+++ b/crates/ra_hir_def/src/item_tree/lower.rs
@@ -82,7 +82,13 @@ impl Ctx {
82 | ast::ModuleItem::TypeAliasDef(_) 82 | ast::ModuleItem::TypeAliasDef(_)
83 | ast::ModuleItem::ConstDef(_) 83 | ast::ModuleItem::ConstDef(_)
84 | ast::ModuleItem::StaticDef(_) 84 | ast::ModuleItem::StaticDef(_)
85 | ast::ModuleItem::MacroCall(_) => self.collect_inner_items(item.syntax()), 85 | ast::ModuleItem::MacroCall(_) => {
86 // Skip this if we're already collecting inner items. We'll descend into all nodes
87 // already.
88 if !inner {
89 self.collect_inner_items(item.syntax());
90 }
91 }
86 92
87 // These are handled in their respective `lower_X` method (since we can't just blindly 93 // These are handled in their respective `lower_X` method (since we can't just blindly
88 // walk them). 94 // walk them).
@@ -403,7 +409,8 @@ impl Ctx {
403 fn lower_trait(&mut self, trait_def: &ast::TraitDef) -> Option<FileItemTreeId<Trait>> { 409 fn lower_trait(&mut self, trait_def: &ast::TraitDef) -> Option<FileItemTreeId<Trait>> {
404 let name = trait_def.name()?.as_name(); 410 let name = trait_def.name()?.as_name();
405 let visibility = self.lower_visibility(trait_def); 411 let visibility = self.lower_visibility(trait_def);
406 let generic_params = self.lower_generic_params(GenericsOwner::Trait(trait_def), trait_def); 412 let generic_params =
413 self.lower_generic_params_and_inner_items(GenericsOwner::Trait(trait_def), trait_def);
407 let auto = trait_def.auto_token().is_some(); 414 let auto = trait_def.auto_token().is_some();
408 let items = trait_def.item_list().map(|list| { 415 let items = trait_def.item_list().map(|list| {
409 self.with_inherited_visibility(visibility, |this| { 416 self.with_inherited_visibility(visibility, |this| {
@@ -432,7 +439,8 @@ impl Ctx {
432 } 439 }
433 440
434 fn lower_impl(&mut self, impl_def: &ast::ImplDef) -> Option<FileItemTreeId<Impl>> { 441 fn lower_impl(&mut self, impl_def: &ast::ImplDef) -> Option<FileItemTreeId<Impl>> {
435 let generic_params = self.lower_generic_params(GenericsOwner::Impl, impl_def); 442 let generic_params =
443 self.lower_generic_params_and_inner_items(GenericsOwner::Impl, impl_def);
436 let target_trait = impl_def.target_trait().map(|tr| self.lower_type_ref(&tr)); 444 let target_trait = impl_def.target_trait().map(|tr| self.lower_type_ref(&tr));
437 let target_type = self.lower_type_ref(&impl_def.target_type()?); 445 let target_type = self.lower_type_ref(&impl_def.target_type()?);
438 let is_negative = impl_def.excl_token().is_some(); 446 let is_negative = impl_def.excl_token().is_some();
@@ -548,6 +556,23 @@ impl Ctx {
548 }) 556 })
549 } 557 }
550 558
559 /// Lowers generics defined on `node` and collects inner items defined within.
560 fn lower_generic_params_and_inner_items(
561 &mut self,
562 owner: GenericsOwner<'_>,
563 node: &impl ast::TypeParamsOwner,
564 ) -> GenericParamsId {
565 // Generics are part of item headers and may contain inner items we need to collect.
566 if let Some(params) = node.type_param_list() {
567 self.collect_inner_items(params.syntax());
568 }
569 if let Some(clause) = node.where_clause() {
570 self.collect_inner_items(clause.syntax());
571 }
572
573 self.lower_generic_params(owner, node)
574 }
575
551 fn lower_generic_params( 576 fn lower_generic_params(
552 &mut self, 577 &mut self,
553 owner: GenericsOwner<'_>, 578 owner: GenericsOwner<'_>,
@@ -617,7 +642,7 @@ impl Ctx {
617 TypeRef::from_ast(&self.body_ctx, type_ref.clone()) 642 TypeRef::from_ast(&self.body_ctx, type_ref.clone())
618 } 643 }
619 fn lower_type_ref_opt(&self, type_ref: Option<ast::TypeRef>) -> TypeRef { 644 fn lower_type_ref_opt(&self, type_ref: Option<ast::TypeRef>) -> TypeRef {
620 TypeRef::from_ast_opt(&self.body_ctx, type_ref) 645 type_ref.map(|ty| self.lower_type_ref(&ty)).unwrap_or(TypeRef::Error)
621 } 646 }
622 647
623 /// Forces the visibility `vis` to be used for all items lowered during execution of `f`. 648 /// Forces the visibility `vis` to be used for all items lowered during execution of `f`.
diff --git a/crates/ra_hir_def/src/item_tree/tests.rs b/crates/ra_hir_def/src/item_tree/tests.rs
index cd4c8a199..dc035d809 100644
--- a/crates/ra_hir_def/src/item_tree/tests.rs
+++ b/crates/ra_hir_def/src/item_tree/tests.rs
@@ -374,11 +374,17 @@ fn cursed_inner_items() {
374 374
375 impl En { 375 impl En {
376 fn assoc() { 376 fn assoc() {
377 trait InnerTrait {} 377 trait InnerTrait<T = [u8; { fn f() {} }]> {}
378 struct InnerStruct {} 378 struct InnerStruct<T = [u8; { fn f() {} }]> {}
379 impl InnerTrait for InnerStruct {} 379 impl<T = [u8; { fn f() {} }]> InnerTrait for InnerStruct {}
380 } 380 }
381 } 381 }
382
383 trait Tr<T = [u8; { fn f() {} }]> {
384 type AssocTy = [u8; { fn f() {} }];
385
386 const AssocConst: [u8; { fn f() {} }];
387 }
382 ", 388 ",
383 ); 389 );
384} 390}