diff options
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 65 |
1 files changed, 47 insertions, 18 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 920899dce..9b6276b51 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -23,7 +23,6 @@ use ra_db::{CrateId, Edition}; | |||
23 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; | 23 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; |
24 | 24 | ||
25 | use crate::{ | 25 | use crate::{ |
26 | adt::VariantDef, | ||
27 | db::{AstDatabase, DefDatabase, HirDatabase}, | 26 | db::{AstDatabase, DefDatabase, HirDatabase}, |
28 | expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId}, | 27 | expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId}, |
29 | generics::{GenericDef, HasGenericParams}, | 28 | generics::{GenericDef, HasGenericParams}, |
@@ -322,9 +321,11 @@ impl Struct { | |||
322 | // take the outer scope... | 321 | // take the outer scope... |
323 | let r = self.module(db).resolver(db); | 322 | let r = self.module(db).resolver(db); |
324 | // ...and add generic params, if present | 323 | // ...and add generic params, if present |
325 | let p = self.generic_params(db); | 324 | r.push_generic_params_scope(db, self.into()) |
326 | let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | 325 | } |
327 | r | 326 | |
327 | fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { | ||
328 | db.struct_data(self.id.into()).variant_data.clone() | ||
328 | } | 329 | } |
329 | } | 330 | } |
330 | 331 | ||
@@ -352,9 +353,7 @@ impl Union { | |||
352 | // take the outer scope... | 353 | // take the outer scope... |
353 | let r = self.module(db).resolver(db); | 354 | let r = self.module(db).resolver(db); |
354 | // ...and add generic params, if present | 355 | // ...and add generic params, if present |
355 | let p = self.generic_params(db); | 356 | r.push_generic_params_scope(db, self.into()) |
356 | let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | ||
357 | r | ||
358 | } | 357 | } |
359 | } | 358 | } |
360 | 359 | ||
@@ -402,8 +401,7 @@ impl Enum { | |||
402 | // take the outer scope... | 401 | // take the outer scope... |
403 | let r = self.module(db).resolver(db); | 402 | let r = self.module(db).resolver(db); |
404 | // ...and add generic params, if present | 403 | // ...and add generic params, if present |
405 | let p = self.generic_params(db); | 404 | let r = r.push_generic_params_scope(db, self.into()); |
406 | let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | ||
407 | r.push_scope(Scope::AdtScope(self.into())) | 405 | r.push_scope(Scope::AdtScope(self.into())) |
408 | } | 406 | } |
409 | } | 407 | } |
@@ -487,6 +485,43 @@ impl Adt { | |||
487 | } | 485 | } |
488 | } | 486 | } |
489 | 487 | ||
488 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
489 | pub enum VariantDef { | ||
490 | Struct(Struct), | ||
491 | EnumVariant(EnumVariant), | ||
492 | } | ||
493 | impl_froms!(VariantDef: Struct, EnumVariant); | ||
494 | |||
495 | impl VariantDef { | ||
496 | pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { | ||
497 | match self { | ||
498 | VariantDef::Struct(it) => it.fields(db), | ||
499 | VariantDef::EnumVariant(it) => it.fields(db), | ||
500 | } | ||
501 | } | ||
502 | |||
503 | pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> { | ||
504 | match self { | ||
505 | VariantDef::Struct(it) => it.field(db, name), | ||
506 | VariantDef::EnumVariant(it) => it.field(db, name), | ||
507 | } | ||
508 | } | ||
509 | |||
510 | pub fn module(self, db: &impl HirDatabase) -> Module { | ||
511 | match self { | ||
512 | VariantDef::Struct(it) => it.module(db), | ||
513 | VariantDef::EnumVariant(it) => it.module(db), | ||
514 | } | ||
515 | } | ||
516 | |||
517 | pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { | ||
518 | match self { | ||
519 | VariantDef::Struct(it) => it.variant_data(db), | ||
520 | VariantDef::EnumVariant(it) => it.variant_data(db), | ||
521 | } | ||
522 | } | ||
523 | } | ||
524 | |||
490 | /// The defs which have a body. | 525 | /// The defs which have a body. |
491 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 526 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
492 | pub enum DefWithBody { | 527 | pub enum DefWithBody { |
@@ -709,9 +744,7 @@ impl Function { | |||
709 | // take the outer scope... | 744 | // take the outer scope... |
710 | let r = self.container(db).map_or_else(|| self.module(db).resolver(db), |c| c.resolver(db)); | 745 | let r = self.container(db).map_or_else(|| self.module(db).resolver(db), |c| c.resolver(db)); |
711 | // ...and add generic params, if present | 746 | // ...and add generic params, if present |
712 | let p = self.generic_params(db); | 747 | r.push_generic_params_scope(db, self.into()) |
713 | let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | ||
714 | r | ||
715 | } | 748 | } |
716 | 749 | ||
717 | pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { | 750 | pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { |
@@ -946,9 +979,7 @@ impl Trait { | |||
946 | pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { | 979 | pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { |
947 | let r = self.module(db).resolver(db); | 980 | let r = self.module(db).resolver(db); |
948 | // add generic params, if present | 981 | // add generic params, if present |
949 | let p = self.generic_params(db); | 982 | r.push_generic_params_scope(db, self.into()) |
950 | let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | ||
951 | r | ||
952 | } | 983 | } |
953 | } | 984 | } |
954 | 985 | ||
@@ -1010,9 +1041,7 @@ impl TypeAlias { | |||
1010 | .map(|ib| ib.resolver(db)) | 1041 | .map(|ib| ib.resolver(db)) |
1011 | .unwrap_or_else(|| self.module(db).resolver(db)); | 1042 | .unwrap_or_else(|| self.module(db).resolver(db)); |
1012 | // ...and add generic params, if present | 1043 | // ...and add generic params, if present |
1013 | let p = self.generic_params(db); | 1044 | r.push_generic_params_scope(db, self.into()) |
1014 | let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; | ||
1015 | r | ||
1016 | } | 1045 | } |
1017 | } | 1046 | } |
1018 | 1047 | ||