Skip to content

Commit 9cbafa2

Browse files
committed
Remove Params and Fields from AstIdMap
1 parent 200a01a commit 9cbafa2

File tree

16 files changed

+494
-449
lines changed

16 files changed

+494
-449
lines changed

crates/hir-def/src/attr.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use triomphe::Arc;
2020

2121
use crate::{
2222
db::DefDatabase,
23-
item_tree::{AttrOwner, Fields, ItemTreeNode},
23+
item_tree::{AttrOwner, FieldParent, ItemTreeNode},
2424
lang_item::LangItem,
2525
nameres::{ModuleOrigin, ModuleSource},
2626
src::{HasChildSource, HasSource},
@@ -76,40 +76,36 @@ impl Attrs {
7676
let mut res = ArenaMap::default();
7777

7878
let crate_graph = db.crate_graph();
79-
let (fields, item_tree, krate) = match v {
79+
let item_tree;
80+
let (parent, fields, krate) = match v {
8081
VariantId::EnumVariantId(it) => {
8182
let loc = it.lookup(db);
8283
let krate = loc.parent.lookup(db).container.krate;
83-
let item_tree = loc.id.item_tree(db);
84+
item_tree = loc.id.item_tree(db);
8485
let variant = &item_tree[loc.id.value];
85-
(variant.fields.clone(), item_tree, krate)
86+
(FieldParent::Variant(loc.id.value), &variant.fields, krate)
8687
}
8788
VariantId::StructId(it) => {
8889
let loc = it.lookup(db);
8990
let krate = loc.container.krate;
90-
let item_tree = loc.id.item_tree(db);
91+
item_tree = loc.id.item_tree(db);
9192
let struct_ = &item_tree[loc.id.value];
92-
(struct_.fields.clone(), item_tree, krate)
93+
(FieldParent::Struct(loc.id.value), &struct_.fields, krate)
9394
}
9495
VariantId::UnionId(it) => {
9596
let loc = it.lookup(db);
9697
let krate = loc.container.krate;
97-
let item_tree = loc.id.item_tree(db);
98+
item_tree = loc.id.item_tree(db);
9899
let union_ = &item_tree[loc.id.value];
99-
(union_.fields.clone(), item_tree, krate)
100+
(FieldParent::Union(loc.id.value), &union_.fields, krate)
100101
}
101102
};
102103

103-
let fields = match fields {
104-
Fields::Record(fields) | Fields::Tuple(fields) => fields,
105-
Fields::Unit => return Arc::new(res),
106-
};
107-
108104
let cfg_options = &crate_graph[krate].cfg_options;
109105

110106
let mut idx = 0;
111-
for field in fields {
112-
let attrs = item_tree.attrs(db, krate, field.into());
107+
for (id, _field) in fields.iter().enumerate() {
108+
let attrs = item_tree.attrs(db, krate, AttrOwner::make_field_indexed(parent, id));
113109
if attrs.is_cfg_enabled(cfg_options) {
114110
res.insert(Idx::from_raw(RawIdx::from(idx)), attrs);
115111
idx += 1;

crates/hir-def/src/body.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::ops::{Deref, Index};
1111
use base_db::CrateId;
1212
use cfg::{CfgExpr, CfgOptions};
1313
use hir_expand::{name::Name, InFile};
14-
use la_arena::{Arena, ArenaMap};
14+
use la_arena::{Arena, ArenaMap, Idx, RawIdx};
1515
use rustc_hash::FxHashMap;
1616
use smallvec::SmallVec;
1717
use span::MacroFileId;
@@ -24,6 +24,7 @@ use crate::{
2424
hir::{
2525
dummy_expr_id, Binding, BindingId, Expr, ExprId, Label, LabelId, Pat, PatId, RecordFieldPat,
2626
},
27+
item_tree::AttrOwner,
2728
nameres::DefMap,
2829
path::{ModPath, Path},
2930
src::HasSource,
@@ -136,16 +137,23 @@ impl Body {
136137
let data = db.function_data(f);
137138
let f = f.lookup(db);
138139
let src = f.source(db);
139-
params = src.value.param_list().map(|param_list| {
140+
params = src.value.param_list().map(move |param_list| {
140141
let item_tree = f.id.item_tree(db);
141142
let func = &item_tree[f.id.value];
142143
let krate = f.container.module(db).krate;
143144
let crate_graph = db.crate_graph();
144145
(
145146
param_list,
146-
func.params.clone().map(move |param| {
147+
(0..func.params.len()).map(move |idx| {
147148
item_tree
148-
.attrs(db, krate, param.into())
149+
.attrs(
150+
db,
151+
krate,
152+
AttrOwner::Param(
153+
f.id.value,
154+
Idx::from_raw(RawIdx::from(idx as u32)),
155+
),
156+
)
149157
.is_cfg_enabled(&crate_graph[krate].cfg_options)
150158
}),
151159
)

crates/hir-def/src/data.rs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use hir_expand::{
77
name::Name, AstId, ExpandResult, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefKind,
88
};
99
use intern::{sym, Interned, Symbol};
10+
use la_arena::{Idx, RawIdx};
1011
use smallvec::SmallVec;
1112
use syntax::{ast, Parse};
1213
use triomphe::Arc;
@@ -58,32 +59,15 @@ impl FunctionData {
5859

5960
let crate_graph = db.crate_graph();
6061
let cfg_options = &crate_graph[krate].cfg_options;
61-
let enabled_params = func
62-
.params
63-
.clone()
64-
.filter(|&param| item_tree.attrs(db, krate, param.into()).is_cfg_enabled(cfg_options));
65-
66-
// If last cfg-enabled param is a `...` param, it's a varargs function.
67-
let is_varargs = enabled_params
68-
.clone()
69-
.next_back()
70-
.map_or(false, |param| item_tree[param].type_ref.is_none());
62+
let attr_owner = |idx| {
63+
item_tree::AttrOwner::Param(loc.id.value, Idx::from_raw(RawIdx::from(idx as u32)))
64+
};
7165

7266
let mut flags = func.flags;
73-
if is_varargs {
74-
flags |= FnFlags::IS_VARARGS;
75-
}
7667
if flags.contains(FnFlags::HAS_SELF_PARAM) {
7768
// If there's a self param in the syntax, but it is cfg'd out, remove the flag.
78-
let is_cfgd_out = match func.params.clone().next() {
79-
Some(param) => {
80-
!item_tree.attrs(db, krate, param.into()).is_cfg_enabled(cfg_options)
81-
}
82-
None => {
83-
stdx::never!("fn HAS_SELF_PARAM but no parameters allocated");
84-
true
85-
}
86-
};
69+
let is_cfgd_out =
70+
!item_tree.attrs(db, krate, attr_owner(0usize)).is_cfg_enabled(cfg_options);
8771
if is_cfgd_out {
8872
cov_mark::hit!(cfgd_out_self_param);
8973
flags.remove(FnFlags::HAS_SELF_PARAM);
@@ -101,9 +85,14 @@ impl FunctionData {
10185

10286
Arc::new(FunctionData {
10387
name: func.name.clone(),
104-
params: enabled_params
105-
.clone()
106-
.filter_map(|id| item_tree[id].type_ref.clone())
88+
params: func
89+
.params
90+
.iter()
91+
.enumerate()
92+
.filter(|&(idx, _)| {
93+
item_tree.attrs(db, krate, attr_owner(idx)).is_cfg_enabled(cfg_options)
94+
})
95+
.map(|(_, param)| param.type_ref.clone())
10796
.collect(),
10897
ret_type: func.ret_type.clone(),
10998
attrs: item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()),
@@ -629,7 +618,8 @@ impl<'a> AssocItemCollector<'a> {
629618
if !attrs.is_cfg_enabled(self.expander.cfg_options()) {
630619
self.diagnostics.push(DefDiagnostic::unconfigured_code(
631620
self.module_id.local_id,
632-
InFile::new(self.expander.current_file_id(), item.ast_id(item_tree).erase()),
621+
tree_id,
622+
ModItem::from(item).into(),
633623
attrs.cfg().unwrap(),
634624
self.expander.cfg_options().clone(),
635625
));

0 commit comments

Comments
 (0)