aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2020-06-25 15:47:21 +0100
committerJonas Schievink <[email protected]>2020-06-25 15:52:15 +0100
commit563d58559400c4e5b67377bd10f40fee7695ea46 (patch)
tree9840f7e43d1c8f512e57606d6420529c26921263
parentb06214d16408e0b699f9d6051ba244e5fe766578 (diff)
Use IdRange for variants
-rw-r--r--crates/ra_hir_def/src/item_tree.rs24
-rw-r--r--crates/ra_hir_def/src/item_tree/lower.rs6
-rw-r--r--crates/ra_hir_def/src/item_tree/tests.rs8
3 files changed, 29 insertions, 9 deletions
diff --git a/crates/ra_hir_def/src/item_tree.rs b/crates/ra_hir_def/src/item_tree.rs
index 8c93e3adf..3e603bd55 100644
--- a/crates/ra_hir_def/src/item_tree.rs
+++ b/crates/ra_hir_def/src/item_tree.rs
@@ -5,6 +5,7 @@ mod lower;
5mod tests; 5mod tests;
6 6
7use std::{ 7use std::{
8 any::type_name,
8 fmt::{self, Debug}, 9 fmt::{self, Debug},
9 hash::{Hash, Hasher}, 10 hash::{Hash, Hasher},
10 marker::PhantomData, 11 marker::PhantomData,
@@ -540,7 +541,7 @@ pub struct Enum {
540 pub name: Name, 541 pub name: Name,
541 pub visibility: RawVisibilityId, 542 pub visibility: RawVisibilityId,
542 pub generic_params: GenericParamsId, 543 pub generic_params: GenericParamsId,
543 pub variants: Range<Idx<Variant>>, 544 pub variants: IdRange<Variant>,
544 pub ast_id: FileAstId<ast::EnumDef>, 545 pub ast_id: FileAstId<ast::EnumDef>,
545} 546}
546 547
@@ -698,7 +699,6 @@ pub struct Variant {
698 pub fields: Fields, 699 pub fields: Fields,
699} 700}
700 701
701#[derive(Debug, Clone, PartialEq, Eq)]
702pub struct IdRange<T> { 702pub struct IdRange<T> {
703 range: Range<u32>, 703 range: Range<u32>,
704 _p: PhantomData<T>, 704 _p: PhantomData<T>,
@@ -717,6 +717,26 @@ impl<T> Iterator for IdRange<T> {
717 } 717 }
718} 718}
719 719
720impl<T> fmt::Debug for IdRange<T> {
721 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
722 f.debug_tuple(&format!("IdRange::<{}>", type_name::<T>())).field(&self.range).finish()
723 }
724}
725
726impl<T> Clone for IdRange<T> {
727 fn clone(&self) -> Self {
728 Self { range: self.range.clone(), _p: PhantomData }
729 }
730}
731
732impl<T> PartialEq for IdRange<T> {
733 fn eq(&self, other: &Self) -> bool {
734 self.range == other.range
735 }
736}
737
738impl<T> Eq for IdRange<T> {}
739
720#[derive(Debug, Clone, PartialEq, Eq)] 740#[derive(Debug, Clone, PartialEq, Eq)]
721pub enum Fields { 741pub enum Fields {
722 Record(IdRange<Field>), 742 Record(IdRange<Field>),
diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs
index 93e8cc0c8..5149dd141 100644
--- a/crates/ra_hir_def/src/item_tree/lower.rs
+++ b/crates/ra_hir_def/src/item_tree/lower.rs
@@ -257,14 +257,14 @@ impl Ctx {
257 let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_); 257 let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_);
258 let variants = match &enum_.variant_list() { 258 let variants = match &enum_.variant_list() {
259 Some(variant_list) => self.lower_variants(variant_list), 259 Some(variant_list) => self.lower_variants(variant_list),
260 None => self.next_variant_idx()..self.next_variant_idx(), 260 None => IdRange::new(self.next_variant_idx()..self.next_variant_idx()),
261 }; 261 };
262 let ast_id = self.source_ast_id_map.ast_id(enum_); 262 let ast_id = self.source_ast_id_map.ast_id(enum_);
263 let res = Enum { name, visibility, generic_params, variants, ast_id }; 263 let res = Enum { name, visibility, generic_params, variants, ast_id };
264 Some(id(self.data().enums.alloc(res))) 264 Some(id(self.data().enums.alloc(res)))
265 } 265 }
266 266
267 fn lower_variants(&mut self, variants: &ast::EnumVariantList) -> Range<Idx<Variant>> { 267 fn lower_variants(&mut self, variants: &ast::EnumVariantList) -> IdRange<Variant> {
268 let start = self.next_variant_idx(); 268 let start = self.next_variant_idx();
269 for variant in variants.variants() { 269 for variant in variants.variants() {
270 if let Some(data) = self.lower_variant(&variant) { 270 if let Some(data) = self.lower_variant(&variant) {
@@ -273,7 +273,7 @@ impl Ctx {
273 } 273 }
274 } 274 }
275 let end = self.next_variant_idx(); 275 let end = self.next_variant_idx();
276 start..end 276 IdRange::new(start..end)
277 } 277 }
278 278
279 fn lower_variant(&mut self, variant: &ast::EnumVariant) -> Option<Variant> { 279 fn lower_variant(&mut self, variant: &ast::EnumVariant) -> Option<Variant> {
diff --git a/crates/ra_hir_def/src/item_tree/tests.rs b/crates/ra_hir_def/src/item_tree/tests.rs
index 18df42050..08559fb92 100644
--- a/crates/ra_hir_def/src/item_tree/tests.rs
+++ b/crates/ra_hir_def/src/item_tree/tests.rs
@@ -237,13 +237,13 @@ Trait { name: Name(Text("Tr")), visibility: RawVisibilityId("pub(self)"), generi
237#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct0"))] }, input: None }]) }] 237#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct0"))] }, input: None }]) }]
238Struct { name: Name(Text("Struct0")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(1), fields: Unit, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(3), kind: Unit } 238Struct { name: Name(Text("Struct0")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(1), fields: Unit, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(3), kind: Unit }
239#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct1"))] }, input: None }]) }] 239#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct1"))] }, input: None }]) }]
240Struct { name: Name(Text("Struct1")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(2), fields: Tuple(FieldIds { range: 0..1 }), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(4), kind: Tuple } 240Struct { name: Name(Text("Struct1")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(2), fields: Tuple(IdRange::<ra_hir_def::item_tree::Field>(0..1)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(4), kind: Tuple }
241#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct2"))] }, input: None }]) }] 241#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct2"))] }, input: None }]) }]
242Struct { name: Name(Text("Struct2")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(3), fields: Record(FieldIds { range: 1..2 }), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(5), kind: Record } 242Struct { name: Name(Text("Struct2")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(3), fields: Record(IdRange::<ra_hir_def::item_tree::Field>(1..2)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(5), kind: Record }
243#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("en"))] }, input: None }]) }] 243#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("en"))] }, input: None }]) }]
244Enum { name: Name(Text("En")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), variants: Idx::<Variant>(0)..Idx::<Variant>(1), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::EnumDef>(6) } 244Enum { name: Name(Text("En")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), variants: IdRange::<ra_hir_def::item_tree::Variant>(0..1), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::EnumDef>(6) }
245#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("un"))] }, input: None }]) }] 245#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("un"))] }, input: None }]) }]
246Union { name: Name(Text("Un")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), fields: Record(FieldIds { range: 3..4 }), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::UnionDef>(7) } 246Union { name: Name(Text("Un")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), fields: Record(IdRange::<ra_hir_def::item_tree::Field>(3..4)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::UnionDef>(7) }
247 "###); 247 "###);
248} 248}
249 249