aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src')
-rw-r--r--crates/ra_hir_def/src/adt.rs17
-rw-r--r--crates/ra_hir_def/src/body.rs11
-rw-r--r--crates/ra_hir_def/src/body/lower.rs19
-rw-r--r--crates/ra_hir_def/src/data.rs21
-rw-r--r--crates/ra_hir_def/src/item_scope.rs11
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs13
-rw-r--r--crates/ra_hir_def/src/nameres/raw.rs18
-rw-r--r--crates/ra_hir_def/src/nameres/tests.rs2
-rw-r--r--crates/ra_hir_def/src/nameres/tests/macros.rs32
9 files changed, 85 insertions, 59 deletions
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs
index d0912ddaa..2bc34d449 100644
--- a/crates/ra_hir_def/src/adt.rs
+++ b/crates/ra_hir_def/src/adt.rs
@@ -118,11 +118,12 @@ fn lower_enum(
118 module_id: ModuleId, 118 module_id: ModuleId,
119) { 119) {
120 let expander = CfgExpander::new(db, ast.file_id, module_id.krate); 120 let expander = CfgExpander::new(db, ast.file_id, module_id.krate);
121 let variants = 121 let variants = ast
122 ast.value.variant_list().into_iter().flat_map(|it| it.variants()).filter(|var| { 122 .value
123 let attrs = expander.parse_attrs(var); 123 .variant_list()
124 expander.is_cfg_enabled(&attrs) 124 .into_iter()
125 }); 125 .flat_map(|it| it.variants())
126 .filter(|var| expander.is_cfg_enabled(var));
126 for var in variants { 127 for var in variants {
127 trace.alloc( 128 trace.alloc(
128 || var.clone(), 129 || var.clone(),
@@ -215,8 +216,7 @@ fn lower_struct(
215 match &ast.value { 216 match &ast.value {
216 ast::StructKind::Tuple(fl) => { 217 ast::StructKind::Tuple(fl) => {
217 for (i, fd) in fl.fields().enumerate() { 218 for (i, fd) in fl.fields().enumerate() {
218 let attrs = expander.parse_attrs(&fd); 219 if !expander.is_cfg_enabled(&fd) {
219 if !expander.is_cfg_enabled(&attrs) {
220 continue; 220 continue;
221 } 221 }
222 222
@@ -233,8 +233,7 @@ fn lower_struct(
233 } 233 }
234 ast::StructKind::Record(fl) => { 234 ast::StructKind::Record(fl) => {
235 for fd in fl.fields() { 235 for fd in fl.fields() {
236 let attrs = expander.parse_attrs(&fd); 236 if !expander.is_cfg_enabled(&fd) {
237 if !expander.is_cfg_enabled(&attrs) {
238 continue; 237 continue;
239 } 238 }
240 239
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index 4edaad960..f5a7305dc 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -60,7 +60,8 @@ impl CfgExpander {
60 Attrs::new(owner, &self.hygiene) 60 Attrs::new(owner, &self.hygiene)
61 } 61 }
62 62
63 pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { 63 pub(crate) fn is_cfg_enabled(&self, owner: &dyn ast::AttrsOwner) -> bool {
64 let attrs = self.parse_attrs(owner);
64 attrs.is_cfg_enabled(&self.cfg_options) 65 attrs.is_cfg_enabled(&self.cfg_options)
65 } 66 }
66} 67}
@@ -141,12 +142,8 @@ impl Expander {
141 InFile { file_id: self.current_file_id, value } 142 InFile { file_id: self.current_file_id, value }
142 } 143 }
143 144
144 pub(crate) fn parse_attrs(&self, owner: &dyn ast::AttrsOwner) -> Attrs { 145 pub(crate) fn is_cfg_enabled(&self, owner: &dyn ast::AttrsOwner) -> bool {
145 self.cfg_expander.parse_attrs(owner) 146 self.cfg_expander.is_cfg_enabled(owner)
146 }
147
148 pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool {
149 self.cfg_expander.is_cfg_enabled(attrs)
150 } 147 }
151 148
152 fn parse_path(&mut self, path: ast::Path) -> Option<Path> { 149 fn parse_path(&mut self, path: ast::Path) -> Option<Path> {
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 687216dc3..443b057ab 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -162,8 +162,7 @@ impl ExprCollector<'_> {
162 162
163 fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { 163 fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
164 let syntax_ptr = AstPtr::new(&expr); 164 let syntax_ptr = AstPtr::new(&expr);
165 let attrs = self.expander.parse_attrs(&expr); 165 if !self.expander.is_cfg_enabled(&expr) {
166 if !self.expander.is_cfg_enabled(&attrs) {
167 return self.missing_expr(); 166 return self.missing_expr();
168 } 167 }
169 match expr { 168 match expr {
@@ -329,8 +328,7 @@ impl ExprCollector<'_> {
329 .fields() 328 .fields()
330 .inspect(|field| field_ptrs.push(AstPtr::new(field))) 329 .inspect(|field| field_ptrs.push(AstPtr::new(field)))
331 .filter_map(|field| { 330 .filter_map(|field| {
332 let attrs = self.expander.parse_attrs(&field); 331 if !self.expander.is_cfg_enabled(&field) {
333 if !self.expander.is_cfg_enabled(&attrs) {
334 return None; 332 return None;
335 } 333 }
336 let name = field.field_name()?.as_name(); 334 let name = field.field_name()?.as_name();
@@ -575,9 +573,16 @@ impl ExprCollector<'_> {
575 self.body.item_scope.define_def(def); 573 self.body.item_scope.define_def(def);
576 if let Some(name) = name { 574 if let Some(name) = name {
577 let vis = crate::visibility::Visibility::Public; // FIXME determine correctly 575 let vis = crate::visibility::Visibility::Public; // FIXME determine correctly
578 self.body 576 let has_constructor = match def {
579 .item_scope 577 ModuleDefId::AdtId(AdtId::StructId(s)) => {
580 .push_res(name.as_name(), crate::per_ns::PerNs::from_def(def, vis)); 578 self.db.struct_data(s).variant_data.kind() != StructKind::Record
579 }
580 _ => true,
581 };
582 self.body.item_scope.push_res(
583 name.as_name(),
584 crate::per_ns::PerNs::from_def(def, vis, has_constructor),
585 );
581 } 586 }
582 } 587 }
583 } 588 }
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index d4cba4d05..e7eb2bb11 100644
--- a/crates/ra_hir_def/src/data.rs
+++ b/crates/ra_hir_def/src/data.rs
@@ -9,7 +9,7 @@ use hir_expand::{
9}; 9};
10use ra_prof::profile; 10use ra_prof::profile;
11use ra_syntax::ast::{ 11use ra_syntax::ast::{
12 self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner, TypeBoundsOwner, 12 self, AssocItem, AstNode, ModuleItemOwner, NameOwner, TypeAscriptionOwner, TypeBoundsOwner,
13 VisibilityOwner, 13 VisibilityOwner,
14}; 14};
15 15
@@ -164,7 +164,7 @@ impl TraitData {
164 items.extend(collect_items( 164 items.extend(collect_items(
165 db, 165 db,
166 &mut expander, 166 &mut expander,
167 item_list.impl_items(), 167 item_list.assoc_items(),
168 src.file_id, 168 src.file_id,
169 container, 169 container,
170 )); 170 ));
@@ -219,7 +219,7 @@ impl ImplData {
219 if let Some(item_list) = src.value.item_list() { 219 if let Some(item_list) = src.value.item_list() {
220 let mut expander = Expander::new(db, impl_loc.ast_id.file_id, module_id); 220 let mut expander = Expander::new(db, impl_loc.ast_id.file_id, module_id);
221 items.extend( 221 items.extend(
222 collect_items(db, &mut expander, item_list.impl_items(), src.file_id, container) 222 collect_items(db, &mut expander, item_list.assoc_items(), src.file_id, container)
223 .into_iter() 223 .into_iter()
224 .map(|(_, item)| item), 224 .map(|(_, item)| item),
225 ); 225 );
@@ -304,7 +304,7 @@ fn collect_items_in_macro(
304 let mut res = collect_items( 304 let mut res = collect_items(
305 db, 305 db,
306 expander, 306 expander,
307 items.value.items().filter_map(|it| ImplItem::cast(it.syntax().clone())), 307 items.value.items().filter_map(|it| AssocItem::cast(it.syntax().clone())),
308 items.file_id, 308 items.file_id,
309 container, 309 container,
310 ); 310 );
@@ -325,31 +325,30 @@ fn collect_items_in_macro(
325fn collect_items( 325fn collect_items(
326 db: &dyn DefDatabase, 326 db: &dyn DefDatabase,
327 expander: &mut Expander, 327 expander: &mut Expander,
328 impl_items: impl Iterator<Item = ImplItem>, 328 assoc_items: impl Iterator<Item = AssocItem>,
329 file_id: crate::HirFileId, 329 file_id: crate::HirFileId,
330 container: AssocContainerId, 330 container: AssocContainerId,
331) -> Vec<(Name, AssocItemId)> { 331) -> Vec<(Name, AssocItemId)> {
332 let items = db.ast_id_map(file_id); 332 let items = db.ast_id_map(file_id);
333 333
334 impl_items 334 assoc_items
335 .filter_map(|item_node| match item_node { 335 .filter_map(|item_node| match item_node {
336 ast::ImplItem::FnDef(it) => { 336 ast::AssocItem::FnDef(it) => {
337 let name = it.name().map_or_else(Name::missing, |it| it.as_name()); 337 let name = it.name().map_or_else(Name::missing, |it| it.as_name());
338 let attrs = expander.parse_attrs(&it); 338 if !expander.is_cfg_enabled(&it) {
339 if !expander.is_cfg_enabled(&attrs) {
340 return None; 339 return None;
341 } 340 }
342 let def = FunctionLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) } 341 let def = FunctionLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) }
343 .intern(db); 342 .intern(db);
344 Some((name, def.into())) 343 Some((name, def.into()))
345 } 344 }
346 ast::ImplItem::ConstDef(it) => { 345 ast::AssocItem::ConstDef(it) => {
347 let name = it.name().map_or_else(Name::missing, |it| it.as_name()); 346 let name = it.name().map_or_else(Name::missing, |it| it.as_name());
348 let def = ConstLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) } 347 let def = ConstLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) }
349 .intern(db); 348 .intern(db);
350 Some((name, def.into())) 349 Some((name, def.into()))
351 } 350 }
352 ast::ImplItem::TypeAliasDef(it) => { 351 ast::AssocItem::TypeAliasDef(it) => {
353 let name = it.name().map_or_else(Name::missing, |it| it.as_name()); 352 let name = it.name().map_or_else(Name::missing, |it| it.as_name());
354 let def = 353 let def =
355 TypeAliasLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) } 354 TypeAliasLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) }
diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs
index 259b9ff03..fc15948ad 100644
--- a/crates/ra_hir_def/src/item_scope.rs
+++ b/crates/ra_hir_def/src/item_scope.rs
@@ -151,13 +151,20 @@ impl ItemScope {
151} 151}
152 152
153impl PerNs { 153impl PerNs {
154 pub(crate) fn from_def(def: ModuleDefId, v: Visibility) -> PerNs { 154 pub(crate) fn from_def(def: ModuleDefId, v: Visibility, has_constructor: bool) -> PerNs {
155 match def { 155 match def {
156 ModuleDefId::ModuleId(_) => PerNs::types(def, v), 156 ModuleDefId::ModuleId(_) => PerNs::types(def, v),
157 ModuleDefId::FunctionId(_) => PerNs::values(def, v), 157 ModuleDefId::FunctionId(_) => PerNs::values(def, v),
158 ModuleDefId::AdtId(adt) => match adt { 158 ModuleDefId::AdtId(adt) => match adt {
159 AdtId::StructId(_) | AdtId::UnionId(_) => PerNs::both(def, def, v), 159 AdtId::UnionId(_) => PerNs::types(def, v),
160 AdtId::EnumId(_) => PerNs::types(def, v), 160 AdtId::EnumId(_) => PerNs::types(def, v),
161 AdtId::StructId(_) => {
162 if has_constructor {
163 PerNs::both(def, def, v)
164 } else {
165 PerNs::types(def, v)
166 }
167 }
161 }, 168 },
162 ModuleDefId::EnumVariantId(_) => PerNs::both(def, def, v), 169 ModuleDefId::EnumVariantId(_) => PerNs::both(def, def, v),
163 ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def, v), 170 ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def, v),
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index bf3968bd6..db994122a 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -830,7 +830,7 @@ impl ModCollector<'_, '_> {
830 let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res }; 830 let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res };
831 let def: ModuleDefId = module.into(); 831 let def: ModuleDefId = module.into();
832 self.def_collector.def_map.modules[self.module_id].scope.define_def(def); 832 self.def_collector.def_map.modules[self.module_id].scope.define_def(def);
833 self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))], vis); 833 self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis, false))], vis);
834 res 834 res
835 } 835 }
836 836
@@ -844,6 +844,8 @@ impl ModCollector<'_, '_> {
844 let name = def.name.clone(); 844 let name = def.name.clone();
845 let container = ContainerId::ModuleId(module); 845 let container = ContainerId::ModuleId(module);
846 let vis = &def.visibility; 846 let vis = &def.visibility;
847 let mut has_constructor = false;
848
847 let def: ModuleDefId = match def.kind { 849 let def: ModuleDefId = match def.kind {
848 raw::DefKind::Function(ast_id) => FunctionLoc { 850 raw::DefKind::Function(ast_id) => FunctionLoc {
849 container: container.into(), 851 container: container.into(),
@@ -851,7 +853,8 @@ impl ModCollector<'_, '_> {
851 } 853 }
852 .intern(self.def_collector.db) 854 .intern(self.def_collector.db)
853 .into(), 855 .into(),
854 raw::DefKind::Struct(ast_id) => { 856 raw::DefKind::Struct(ast_id, mode) => {
857 has_constructor = mode != raw::StructDefKind::Record;
855 StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) } 858 StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
856 .intern(self.def_collector.db) 859 .intern(self.def_collector.db)
857 .into() 860 .into()
@@ -894,7 +897,11 @@ impl ModCollector<'_, '_> {
894 .def_map 897 .def_map
895 .resolve_visibility(self.def_collector.db, self.module_id, vis) 898 .resolve_visibility(self.def_collector.db, self.module_id, vis)
896 .unwrap_or(Visibility::Public); 899 .unwrap_or(Visibility::Public);
897 self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))], vis) 900 self.def_collector.update(
901 self.module_id,
902 &[(name, PerNs::from_def(def, vis, has_constructor))],
903 vis,
904 )
898 } 905 }
899 906
900 fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { 907 fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) {
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs
index a71503c76..f2716a295 100644
--- a/crates/ra_hir_def/src/nameres/raw.rs
+++ b/crates/ra_hir_def/src/nameres/raw.rs
@@ -156,9 +156,16 @@ pub(super) struct DefData {
156} 156}
157 157
158#[derive(Debug, PartialEq, Eq, Clone, Copy)] 158#[derive(Debug, PartialEq, Eq, Clone, Copy)]
159pub(super) enum StructDefKind {
160 Record,
161 Tuple,
162 Unit,
163}
164
165#[derive(Debug, PartialEq, Eq, Clone, Copy)]
159pub(super) enum DefKind { 166pub(super) enum DefKind {
160 Function(FileAstId<ast::FnDef>), 167 Function(FileAstId<ast::FnDef>),
161 Struct(FileAstId<ast::StructDef>), 168 Struct(FileAstId<ast::StructDef>, StructDefKind),
162 Union(FileAstId<ast::UnionDef>), 169 Union(FileAstId<ast::UnionDef>),
163 Enum(FileAstId<ast::EnumDef>), 170 Enum(FileAstId<ast::EnumDef>),
164 Const(FileAstId<ast::ConstDef>), 171 Const(FileAstId<ast::ConstDef>),
@@ -171,7 +178,7 @@ impl DefKind {
171 pub fn ast_id(&self) -> FileAstId<ast::ModuleItem> { 178 pub fn ast_id(&self) -> FileAstId<ast::ModuleItem> {
172 match self { 179 match self {
173 DefKind::Function(it) => it.upcast(), 180 DefKind::Function(it) => it.upcast(),
174 DefKind::Struct(it) => it.upcast(), 181 DefKind::Struct(it, _) => it.upcast(),
175 DefKind::Union(it) => it.upcast(), 182 DefKind::Union(it) => it.upcast(),
176 DefKind::Enum(it) => it.upcast(), 183 DefKind::Enum(it) => it.upcast(),
177 DefKind::Const(it) => it.upcast(), 184 DefKind::Const(it) => it.upcast(),
@@ -236,9 +243,14 @@ impl RawItemsCollector {
236 return; 243 return;
237 } 244 }
238 ast::ModuleItem::StructDef(it) => { 245 ast::ModuleItem::StructDef(it) => {
246 let kind = match it.kind() {
247 ast::StructKind::Record(_) => StructDefKind::Record,
248 ast::StructKind::Tuple(_) => StructDefKind::Tuple,
249 ast::StructKind::Unit => StructDefKind::Unit,
250 };
239 let id = self.source_ast_id_map.ast_id(&it); 251 let id = self.source_ast_id_map.ast_id(&it);
240 let name = it.name(); 252 let name = it.name();
241 (DefKind::Struct(id), name) 253 (DefKind::Struct(id, kind), name)
242 } 254 }
243 ast::ModuleItem::UnionDef(it) => { 255 ast::ModuleItem::UnionDef(it) => {
244 let id = self.source_ast_id_map.ast_id(&it); 256 let id = self.source_ast_id_map.ast_id(&it);
diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs
index 83120fa36..1b66c1aac 100644
--- a/crates/ra_hir_def/src/nameres/tests.rs
+++ b/crates/ra_hir_def/src/nameres/tests.rs
@@ -67,7 +67,7 @@ fn crate_def_map_smoke_test() {
67 ⋮Baz: t v 67 ⋮Baz: t v
68 ⋮E: t 68 ⋮E: t
69 ⋮EXT: v 69 ⋮EXT: v
70 ⋮U: t v 70 ⋮U: t
71 ⋮ext: v 71 ⋮ext: v
72 "###) 72 "###)
73} 73}
diff --git a/crates/ra_hir_def/src/nameres/tests/macros.rs b/crates/ra_hir_def/src/nameres/tests/macros.rs
index 9bc0e6287..40289e3ca 100644
--- a/crates/ra_hir_def/src/nameres/tests/macros.rs
+++ b/crates/ra_hir_def/src/nameres/tests/macros.rs
@@ -19,12 +19,12 @@ fn macro_rules_are_globally_visible() {
19 ); 19 );
20 assert_snapshot!(map, @r###" 20 assert_snapshot!(map, @r###"
21 ⋮crate 21 ⋮crate
22 ⋮Foo: t v 22 ⋮Foo: t
23 ⋮nested: t 23 ⋮nested: t
24 24
25 ⋮crate::nested 25 ⋮crate::nested
26 ⋮Bar: t v 26 ⋮Bar: t
27 ⋮Baz: t v 27 ⋮Baz: t
28 "###); 28 "###);
29} 29}
30 30
@@ -91,13 +91,13 @@ fn macro_rules_from_other_crates_are_visible() {
91 ); 91 );
92 assert_snapshot!(map, @r###" 92 assert_snapshot!(map, @r###"
93 ⋮crate 93 ⋮crate
94 ⋮Bar: t v 94 ⋮Bar: t
95 ⋮Foo: t v 95 ⋮Foo: t
96 ⋮bar: t 96 ⋮bar: t
97 97
98 ⋮crate::bar 98 ⋮crate::bar
99 ⋮Bar: t v 99 ⋮Bar: t
100 ⋮Foo: t v 100 ⋮Foo: t
101 ⋮bar: t 101 ⋮bar: t
102 "###); 102 "###);
103} 103}
@@ -124,13 +124,13 @@ fn macro_rules_export_with_local_inner_macros_are_visible() {
124 ); 124 );
125 assert_snapshot!(map, @r###" 125 assert_snapshot!(map, @r###"
126 ⋮crate 126 ⋮crate
127 ⋮Bar: t v 127 ⋮Bar: t
128 ⋮Foo: t v 128 ⋮Foo: t
129 ⋮bar: t 129 ⋮bar: t
130 130
131 ⋮crate::bar 131 ⋮crate::bar
132 ⋮Bar: t v 132 ⋮Bar: t
133 ⋮Foo: t v 133 ⋮Foo: t
134 ⋮bar: t 134 ⋮bar: t
135 "###); 135 "###);
136} 136}
@@ -161,13 +161,13 @@ fn local_inner_macros_makes_local_macros_usable() {
161 ); 161 );
162 assert_snapshot!(map, @r###" 162 assert_snapshot!(map, @r###"
163 ⋮crate 163 ⋮crate
164 ⋮Bar: t v 164 ⋮Bar: t
165 ⋮Foo: t v 165 ⋮Foo: t
166 ⋮bar: t 166 ⋮bar: t
167 167
168 ⋮crate::bar 168 ⋮crate::bar
169 ⋮Bar: t v 169 ⋮Bar: t
170 ⋮Foo: t v 170 ⋮Foo: t
171 ⋮bar: t 171 ⋮bar: t
172 "###); 172 "###);
173} 173}
@@ -204,7 +204,7 @@ fn unexpanded_macro_should_expand_by_fixedpoint_loop() {
204 ); 204 );
205 assert_snapshot!(map, @r###" 205 assert_snapshot!(map, @r###"
206 ⋮crate 206 ⋮crate
207 ⋮Foo: t v 207 ⋮Foo: t
208 ⋮bar: m 208 ⋮bar: m
209 ⋮foo: m 209 ⋮foo: m
210 "###); 210 "###);