aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/item_tree
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-06-25 16:18:35 +0100
committerGitHub <[email protected]>2020-06-25 16:18:35 +0100
commitba7286345256eb1140853dc0daa3c276e2ddcbbe (patch)
treee5f328bc2e9794cc93413ecba4a121f8f6f020ad /crates/ra_hir_def/src/item_tree
parent96d335d578984b42c2e00c715c924bceca391a6c (diff)
parentdad2f75b91bcc6ac7620326fec082aca7edea7ce (diff)
Merge #5063
5063: Store field/variant attrs in ItemTree and use it for adt.rs queries r=jonas-schievink a=jonas-schievink Co-authored-by: Jonas Schievink <[email protected]>
Diffstat (limited to 'crates/ra_hir_def/src/item_tree')
-rw-r--r--crates/ra_hir_def/src/item_tree/lower.rs39
-rw-r--r--crates/ra_hir_def/src/item_tree/tests.rs10
2 files changed, 26 insertions, 23 deletions
diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs
index f10ad25f7..5149dd141 100644
--- a/crates/ra_hir_def/src/item_tree/lower.rs
+++ b/crates/ra_hir_def/src/item_tree/lower.rs
@@ -126,15 +126,15 @@ impl Ctx {
126 126
127 if !attrs.is_empty() { 127 if !attrs.is_empty() {
128 for item in items.iter().flat_map(|items| &items.0) { 128 for item in items.iter().flat_map(|items| &items.0) {
129 self.add_attrs(*item, attrs.clone()); 129 self.add_attrs((*item).into(), attrs.clone());
130 } 130 }
131 } 131 }
132 132
133 items 133 items
134 } 134 }
135 135
136 fn add_attrs(&mut self, item: ModItem, attrs: Attrs) { 136 fn add_attrs(&mut self, item: AttrOwner, attrs: Attrs) {
137 match self.tree.attrs.entry(AttrOwner::ModItem(item)) { 137 match self.tree.attrs.entry(item) {
138 Entry::Occupied(mut entry) => { 138 Entry::Occupied(mut entry) => {
139 *entry.get_mut() = entry.get().merge(attrs); 139 *entry.get_mut() = entry.get().merge(attrs);
140 } 140 }
@@ -196,15 +196,16 @@ impl Ctx {
196 } 196 }
197 } 197 }
198 198
199 fn lower_record_fields(&mut self, fields: &ast::RecordFieldDefList) -> Range<Idx<Field>> { 199 fn lower_record_fields(&mut self, fields: &ast::RecordFieldDefList) -> IdRange<Field> {
200 let start = self.next_field_idx(); 200 let start = self.next_field_idx();
201 for field in fields.fields() { 201 for field in fields.fields() {
202 if let Some(data) = self.lower_record_field(&field) { 202 if let Some(data) = self.lower_record_field(&field) {
203 self.data().fields.alloc(data); 203 let idx = self.data().fields.alloc(data);
204 self.add_attrs(idx.into(), Attrs::new(&field, &self.hygiene));
204 } 205 }
205 } 206 }
206 let end = self.next_field_idx(); 207 let end = self.next_field_idx();
207 start..end 208 IdRange::new(start..end)
208 } 209 }
209 210
210 fn lower_record_field(&mut self, field: &ast::RecordFieldDef) -> Option<Field> { 211 fn lower_record_field(&mut self, field: &ast::RecordFieldDef) -> Option<Field> {
@@ -215,15 +216,16 @@ impl Ctx {
215 Some(res) 216 Some(res)
216 } 217 }
217 218
218 fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldDefList) -> Range<Idx<Field>> { 219 fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldDefList) -> IdRange<Field> {
219 let start = self.next_field_idx(); 220 let start = self.next_field_idx();
220 for (i, field) in fields.fields().enumerate() { 221 for (i, field) in fields.fields().enumerate() {
221 if let Some(data) = self.lower_tuple_field(i, &field) { 222 if let Some(data) = self.lower_tuple_field(i, &field) {
222 self.data().fields.alloc(data); 223 let idx = self.data().fields.alloc(data);
224 self.add_attrs(idx.into(), Attrs::new(&field, &self.hygiene));
223 } 225 }
224 } 226 }
225 let end = self.next_field_idx(); 227 let end = self.next_field_idx();
226 start..end 228 IdRange::new(start..end)
227 } 229 }
228 230
229 fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleFieldDef) -> Option<Field> { 231 fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleFieldDef) -> Option<Field> {
@@ -242,7 +244,7 @@ impl Ctx {
242 Some(record_field_def_list) => { 244 Some(record_field_def_list) => {
243 self.lower_fields(&StructKind::Record(record_field_def_list)) 245 self.lower_fields(&StructKind::Record(record_field_def_list))
244 } 246 }
245 None => Fields::Record(self.next_field_idx()..self.next_field_idx()), 247 None => Fields::Record(IdRange::new(self.next_field_idx()..self.next_field_idx())),
246 }; 248 };
247 let ast_id = self.source_ast_id_map.ast_id(union); 249 let ast_id = self.source_ast_id_map.ast_id(union);
248 let res = Union { name, visibility, generic_params, fields, ast_id }; 250 let res = Union { name, visibility, generic_params, fields, ast_id };
@@ -255,22 +257,23 @@ impl Ctx {
255 let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_); 257 let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_);
256 let variants = match &enum_.variant_list() { 258 let variants = match &enum_.variant_list() {
257 Some(variant_list) => self.lower_variants(variant_list), 259 Some(variant_list) => self.lower_variants(variant_list),
258 None => self.next_variant_idx()..self.next_variant_idx(), 260 None => IdRange::new(self.next_variant_idx()..self.next_variant_idx()),
259 }; 261 };
260 let ast_id = self.source_ast_id_map.ast_id(enum_); 262 let ast_id = self.source_ast_id_map.ast_id(enum_);
261 let res = Enum { name, visibility, generic_params, variants, ast_id }; 263 let res = Enum { name, visibility, generic_params, variants, ast_id };
262 Some(id(self.data().enums.alloc(res))) 264 Some(id(self.data().enums.alloc(res)))
263 } 265 }
264 266
265 fn lower_variants(&mut self, variants: &ast::EnumVariantList) -> Range<Idx<Variant>> { 267 fn lower_variants(&mut self, variants: &ast::EnumVariantList) -> IdRange<Variant> {
266 let start = self.next_variant_idx(); 268 let start = self.next_variant_idx();
267 for variant in variants.variants() { 269 for variant in variants.variants() {
268 if let Some(data) = self.lower_variant(&variant) { 270 if let Some(data) = self.lower_variant(&variant) {
269 self.data().variants.alloc(data); 271 let idx = self.data().variants.alloc(data);
272 self.add_attrs(idx.into(), Attrs::new(&variant, &self.hygiene));
270 } 273 }
271 } 274 }
272 let end = self.next_variant_idx(); 275 let end = self.next_variant_idx();
273 start..end 276 IdRange::new(start..end)
274 } 277 }
275 278
276 fn lower_variant(&mut self, variant: &ast::EnumVariant) -> Option<Variant> { 279 fn lower_variant(&mut self, variant: &ast::EnumVariant) -> Option<Variant> {
@@ -419,7 +422,7 @@ impl Ctx {
419 let attrs = Attrs::new(&item, &this.hygiene); 422 let attrs = Attrs::new(&item, &this.hygiene);
420 this.collect_inner_items(item.syntax()); 423 this.collect_inner_items(item.syntax());
421 this.lower_assoc_item(&item).map(|item| { 424 this.lower_assoc_item(&item).map(|item| {
422 this.add_attrs(item.into(), attrs); 425 this.add_attrs(ModItem::from(item).into(), attrs);
423 item 426 item
424 }) 427 })
425 }) 428 })
@@ -453,7 +456,7 @@ impl Ctx {
453 self.collect_inner_items(item.syntax()); 456 self.collect_inner_items(item.syntax());
454 let assoc = self.lower_assoc_item(&item)?; 457 let assoc = self.lower_assoc_item(&item)?;
455 let attrs = Attrs::new(&item, &self.hygiene); 458 let attrs = Attrs::new(&item, &self.hygiene);
456 self.add_attrs(assoc.into(), attrs); 459 self.add_attrs(ModItem::from(assoc).into(), attrs);
457 Some(assoc) 460 Some(assoc)
458 }) 461 })
459 .collect(); 462 .collect();
@@ -539,7 +542,7 @@ impl Ctx {
539 .filter_map(|item| { 542 .filter_map(|item| {
540 self.collect_inner_items(item.syntax()); 543 self.collect_inner_items(item.syntax());
541 let attrs = Attrs::new(&item, &self.hygiene); 544 let attrs = Attrs::new(&item, &self.hygiene);
542 let id = match item { 545 let id: ModItem = match item {
543 ast::ExternItem::FnDef(ast) => { 546 ast::ExternItem::FnDef(ast) => {
544 let func = self.lower_function(&ast)?; 547 let func = self.lower_function(&ast)?;
545 func.into() 548 func.into()
@@ -549,7 +552,7 @@ impl Ctx {
549 statik.into() 552 statik.into()
550 } 553 }
551 }; 554 };
552 self.add_attrs(id, attrs); 555 self.add_attrs(id.into(), attrs);
553 Some(id) 556 Some(id)
554 }) 557 })
555 .collect() 558 .collect()
diff --git a/crates/ra_hir_def/src/item_tree/tests.rs b/crates/ra_hir_def/src/item_tree/tests.rs
index dc035d809..08559fb92 100644
--- a/crates/ra_hir_def/src/item_tree/tests.rs
+++ b/crates/ra_hir_def/src/item_tree/tests.rs
@@ -92,7 +92,7 @@ fn print_item_tree(ra_fixture: &str) -> String {
92} 92}
93 93
94fn fmt_mod_item(out: &mut String, tree: &ItemTree, item: ModItem) { 94fn fmt_mod_item(out: &mut String, tree: &ItemTree, item: ModItem) {
95 let attrs = tree.attrs(item); 95 let attrs = tree.attrs(item.into());
96 if !attrs.is_empty() { 96 if !attrs.is_empty() {
97 format_to!(out, "#[{:?}]\n", attrs); 97 format_to!(out, "#[{:?}]\n", attrs);
98 } 98 }
@@ -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(Idx::<Field>(0)..Idx::<Field>(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(Idx::<Field>(1)..Idx::<Field>(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(Idx::<Field>(3)..Idx::<Field>(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