From 563d58559400c4e5b67377bd10f40fee7695ea46 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 25 Jun 2020 16:47:21 +0200 Subject: Use IdRange for variants --- crates/ra_hir_def/src/item_tree.rs | 24 ++++++++++++++++++++++-- crates/ra_hir_def/src/item_tree/lower.rs | 6 +++--- crates/ra_hir_def/src/item_tree/tests.rs | 8 ++++---- 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; mod tests; use std::{ + any::type_name, fmt::{self, Debug}, hash::{Hash, Hasher}, marker::PhantomData, @@ -540,7 +541,7 @@ pub struct Enum { pub name: Name, pub visibility: RawVisibilityId, pub generic_params: GenericParamsId, - pub variants: Range>, + pub variants: IdRange, pub ast_id: FileAstId, } @@ -698,7 +699,6 @@ pub struct Variant { pub fields: Fields, } -#[derive(Debug, Clone, PartialEq, Eq)] pub struct IdRange { range: Range, _p: PhantomData, @@ -717,6 +717,26 @@ impl Iterator for IdRange { } } +impl fmt::Debug for IdRange { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple(&format!("IdRange::<{}>", type_name::())).field(&self.range).finish() + } +} + +impl Clone for IdRange { + fn clone(&self) -> Self { + Self { range: self.range.clone(), _p: PhantomData } + } +} + +impl PartialEq for IdRange { + fn eq(&self, other: &Self) -> bool { + self.range == other.range + } +} + +impl Eq for IdRange {} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum Fields { Record(IdRange), 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 { let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_); let variants = match &enum_.variant_list() { Some(variant_list) => self.lower_variants(variant_list), - None => self.next_variant_idx()..self.next_variant_idx(), + None => IdRange::new(self.next_variant_idx()..self.next_variant_idx()), }; let ast_id = self.source_ast_id_map.ast_id(enum_); let res = Enum { name, visibility, generic_params, variants, ast_id }; Some(id(self.data().enums.alloc(res))) } - fn lower_variants(&mut self, variants: &ast::EnumVariantList) -> Range> { + fn lower_variants(&mut self, variants: &ast::EnumVariantList) -> IdRange { let start = self.next_variant_idx(); for variant in variants.variants() { if let Some(data) = self.lower_variant(&variant) { @@ -273,7 +273,7 @@ impl Ctx { } } let end = self.next_variant_idx(); - start..end + IdRange::new(start..end) } fn lower_variant(&mut self, variant: &ast::EnumVariant) -> Option { 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 #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct0"))] }, input: None }]) }] Struct { name: Name(Text("Struct0")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(1), fields: Unit, ast_id: FileAstId::(3), kind: Unit } #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct1"))] }, input: None }]) }] -Struct { name: Name(Text("Struct1")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(2), fields: Tuple(FieldIds { range: 0..1 }), ast_id: FileAstId::(4), kind: Tuple } +Struct { name: Name(Text("Struct1")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(2), fields: Tuple(IdRange::(0..1)), ast_id: FileAstId::(4), kind: Tuple } #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct2"))] }, input: None }]) }] -Struct { name: Name(Text("Struct2")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(3), fields: Record(FieldIds { range: 1..2 }), ast_id: FileAstId::(5), kind: Record } +Struct { name: Name(Text("Struct2")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(3), fields: Record(IdRange::(1..2)), ast_id: FileAstId::(5), kind: Record } #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("en"))] }, input: None }]) }] -Enum { name: Name(Text("En")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), variants: Idx::(0)..Idx::(1), ast_id: FileAstId::(6) } +Enum { name: Name(Text("En")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), variants: IdRange::(0..1), ast_id: FileAstId::(6) } #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("un"))] }, input: None }]) }] -Union { name: Name(Text("Un")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), fields: Record(FieldIds { range: 3..4 }), ast_id: FileAstId::(7) } +Union { name: Name(Text("Un")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(4294967295), fields: Record(IdRange::(3..4)), ast_id: FileAstId::(7) } "###); } -- cgit v1.2.3