aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/doc_tests/generated.rs2
-rw-r--r--crates/ra_assists/src/handlers/add_missing_impl_members.rs34
-rw-r--r--crates/ra_hir_def/src/adt.rs20
-rw-r--r--crates/ra_hir_def/src/attr.rs1
-rw-r--r--crates/ra_hir_def/src/body.rs57
-rw-r--r--crates/ra_hir_def/src/body/lower.rs20
-rw-r--r--crates/ra_hir_def/src/data.rs37
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs14
-rw-r--r--crates/ra_hir_def/src/nameres/tests/incremental.rs6
-rw-r--r--crates/ra_syntax/src/ast/make.rs3
10 files changed, 123 insertions, 71 deletions
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs
index 64444ee3a..24392836c 100644
--- a/crates/ra_assists/src/doc_tests/generated.rs
+++ b/crates/ra_assists/src/doc_tests/generated.rs
@@ -180,7 +180,7 @@ trait Trait<T> {
180} 180}
181 181
182impl Trait<u32> for () { 182impl Trait<u32> for () {
183 fn foo(&self) -> u32 { unimplemented!() } 183 fn foo(&self) -> u32 { todo!() }
184 184
185} 185}
186"#####, 186"#####,
diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs
index 722f207e2..2d6d44980 100644
--- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs
@@ -40,7 +40,7 @@ enum AddMissingImplMembersMode {
40// } 40// }
41// 41//
42// impl Trait<u32> for () { 42// impl Trait<u32> for () {
43// fn foo(&self) -> u32 { unimplemented!() } 43// fn foo(&self) -> u32 { todo!() }
44// 44//
45// } 45// }
46// ``` 46// ```
@@ -165,7 +165,7 @@ fn add_missing_impl_members_inner(
165 165
166fn add_body(fn_def: ast::FnDef) -> ast::FnDef { 166fn add_body(fn_def: ast::FnDef) -> ast::FnDef {
167 if fn_def.body().is_none() { 167 if fn_def.body().is_none() {
168 fn_def.with_body(make::block_from_expr(make::expr_unimplemented())) 168 fn_def.with_body(make::block_from_expr(make::expr_todo()))
169 } else { 169 } else {
170 fn_def 170 fn_def
171 } 171 }
@@ -215,8 +215,8 @@ impl Foo for S {
215 fn bar(&self) {} 215 fn bar(&self) {}
216 <|>type Output; 216 <|>type Output;
217 const CONST: usize = 42; 217 const CONST: usize = 42;
218 fn foo(&self) { unimplemented!() } 218 fn foo(&self) { todo!() }
219 fn baz(&self) { unimplemented!() } 219 fn baz(&self) { todo!() }
220 220
221}", 221}",
222 ); 222 );
@@ -250,7 +250,7 @@ struct S;
250 250
251impl Foo for S { 251impl Foo for S {
252 fn bar(&self) {} 252 fn bar(&self) {}
253 <|>fn foo(&self) { unimplemented!() } 253 <|>fn foo(&self) { todo!() }
254 254
255}", 255}",
256 ); 256 );
@@ -268,7 +268,7 @@ impl Foo for S { <|> }",
268trait Foo { fn foo(&self); } 268trait Foo { fn foo(&self); }
269struct S; 269struct S;
270impl Foo for S { 270impl Foo for S {
271 <|>fn foo(&self) { unimplemented!() } 271 <|>fn foo(&self) { todo!() }
272}", 272}",
273 ); 273 );
274 } 274 }
@@ -285,7 +285,7 @@ impl Foo<u32> for S { <|> }",
285trait Foo<T> { fn foo(&self, t: T) -> &T; } 285trait Foo<T> { fn foo(&self, t: T) -> &T; }
286struct S; 286struct S;
287impl Foo<u32> for S { 287impl Foo<u32> for S {
288 <|>fn foo(&self, t: u32) -> &u32 { unimplemented!() } 288 <|>fn foo(&self, t: u32) -> &u32 { todo!() }
289}", 289}",
290 ); 290 );
291 } 291 }
@@ -302,7 +302,7 @@ impl<U> Foo<U> for S { <|> }",
302trait Foo<T> { fn foo(&self, t: T) -> &T; } 302trait Foo<T> { fn foo(&self, t: T) -> &T; }
303struct S; 303struct S;
304impl<U> Foo<U> for S { 304impl<U> Foo<U> for S {
305 <|>fn foo(&self, t: U) -> &U { unimplemented!() } 305 <|>fn foo(&self, t: U) -> &U { todo!() }
306}", 306}",
307 ); 307 );
308 } 308 }
@@ -319,7 +319,7 @@ impl Foo for S {}<|>",
319trait Foo { fn foo(&self); } 319trait Foo { fn foo(&self); }
320struct S; 320struct S;
321impl Foo for S { 321impl Foo for S {
322 <|>fn foo(&self) { unimplemented!() } 322 <|>fn foo(&self) { todo!() }
323}", 323}",
324 ) 324 )
325 } 325 }
@@ -342,7 +342,7 @@ mod foo {
342} 342}
343struct S; 343struct S;
344impl foo::Foo for S { 344impl foo::Foo for S {
345 <|>fn foo(&self, bar: foo::Bar) { unimplemented!() } 345 <|>fn foo(&self, bar: foo::Bar) { todo!() }
346}", 346}",
347 ); 347 );
348 } 348 }
@@ -365,7 +365,7 @@ mod foo {
365} 365}
366struct S; 366struct S;
367impl foo::Foo for S { 367impl foo::Foo for S {
368 <|>fn foo(&self, bar: foo::Bar<u32>) { unimplemented!() } 368 <|>fn foo(&self, bar: foo::Bar<u32>) { todo!() }
369}", 369}",
370 ); 370 );
371 } 371 }
@@ -388,7 +388,7 @@ mod foo {
388} 388}
389struct S; 389struct S;
390impl foo::Foo<u32> for S { 390impl foo::Foo<u32> for S {
391 <|>fn foo(&self, bar: foo::Bar<u32>) { unimplemented!() } 391 <|>fn foo(&self, bar: foo::Bar<u32>) { todo!() }
392}", 392}",
393 ); 393 );
394 } 394 }
@@ -414,7 +414,7 @@ mod foo {
414struct Param; 414struct Param;
415struct S; 415struct S;
416impl foo::Foo<Param> for S { 416impl foo::Foo<Param> for S {
417 <|>fn foo(&self, bar: Param) { unimplemented!() } 417 <|>fn foo(&self, bar: Param) { todo!() }
418}", 418}",
419 ); 419 );
420 } 420 }
@@ -439,7 +439,7 @@ mod foo {
439} 439}
440struct S; 440struct S;
441impl foo::Foo for S { 441impl foo::Foo for S {
442 <|>fn foo(&self, bar: foo::Bar<u32>::Assoc) { unimplemented!() } 442 <|>fn foo(&self, bar: foo::Bar<u32>::Assoc) { todo!() }
443}", 443}",
444 ); 444 );
445 } 445 }
@@ -464,7 +464,7 @@ mod foo {
464} 464}
465struct S; 465struct S;
466impl foo::Foo for S { 466impl foo::Foo for S {
467 <|>fn foo(&self, bar: foo::Bar<foo::Baz>) { unimplemented!() } 467 <|>fn foo(&self, bar: foo::Bar<foo::Baz>) { todo!() }
468}", 468}",
469 ); 469 );
470 } 470 }
@@ -487,7 +487,7 @@ mod foo {
487} 487}
488struct S; 488struct S;
489impl foo::Foo for S { 489impl foo::Foo for S {
490 <|>fn foo(&self, bar: dyn Fn(u32) -> i32) { unimplemented!() } 490 <|>fn foo(&self, bar: dyn Fn(u32) -> i32) { todo!() }
491}", 491}",
492 ); 492 );
493 } 493 }
@@ -544,7 +544,7 @@ trait Foo {
544struct S; 544struct S;
545impl Foo for S { 545impl Foo for S {
546 <|>type Output; 546 <|>type Output;
547 fn foo(&self) { unimplemented!() } 547 fn foo(&self) { todo!() }
548}"#, 548}"#,
549 ) 549 )
550 } 550 }
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs
index be4b0accb..7c0d93691 100644
--- a/crates/ra_hir_def/src/adt.rs
+++ b/crates/ra_hir_def/src/adt.rs
@@ -4,7 +4,6 @@ use std::sync::Arc;
4 4
5use either::Either; 5use either::Either;
6use hir_expand::{ 6use hir_expand::{
7 hygiene::Hygiene,
8 name::{AsName, Name}, 7 name::{AsName, Name},
9 InFile, 8 InFile,
10}; 9};
@@ -13,7 +12,7 @@ use ra_prof::profile;
13use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, VisibilityOwner}; 12use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, VisibilityOwner};
14 13
15use crate::{ 14use crate::{
16 attr::Attrs, db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, 15 body::CfgExpander, db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace,
17 type_ref::TypeRef, visibility::RawVisibility, EnumId, HasModule, LocalEnumVariantId, 16 type_ref::TypeRef, visibility::RawVisibility, EnumId, HasModule, LocalEnumVariantId,
18 LocalStructFieldId, Lookup, ModuleId, StructId, UnionId, VariantId, 17 LocalStructFieldId, Lookup, ModuleId, StructId, UnionId, VariantId,
19}; 18};
@@ -125,8 +124,9 @@ fn lower_enum(
125 124
126impl VariantData { 125impl VariantData {
127 fn new(db: &dyn DefDatabase, flavor: InFile<ast::StructKind>, module_id: ModuleId) -> Self { 126 fn new(db: &dyn DefDatabase, flavor: InFile<ast::StructKind>, module_id: ModuleId) -> Self {
127 let mut expander = CfgExpander::new(db, flavor.file_id, module_id.krate);
128 let mut trace = Trace::new_for_arena(); 128 let mut trace = Trace::new_for_arena();
129 match lower_struct(db, &mut trace, &flavor, module_id) { 129 match lower_struct(db, &mut expander, &mut trace, &flavor) {
130 StructKind::Tuple => VariantData::Tuple(trace.into_arena()), 130 StructKind::Tuple => VariantData::Tuple(trace.into_arena()),
131 StructKind::Record => VariantData::Record(trace.into_arena()), 131 StructKind::Record => VariantData::Record(trace.into_arena()),
132 StructKind::Unit => VariantData::Unit, 132 StructKind::Unit => VariantData::Unit,
@@ -178,8 +178,9 @@ impl HasChildSource for VariantId {
178 it.lookup(db).container.module(db), 178 it.lookup(db).container.module(db),
179 ), 179 ),
180 }; 180 };
181 let mut expander = CfgExpander::new(db, src.file_id, module_id.krate);
181 let mut trace = Trace::new_for_map(); 182 let mut trace = Trace::new_for_map();
182 lower_struct(db, &mut trace, &src, module_id); 183 lower_struct(db, &mut expander, &mut trace, &src);
183 src.with_value(trace.into_map()) 184 src.with_value(trace.into_map())
184 } 185 }
185} 186}
@@ -193,16 +194,15 @@ pub enum StructKind {
193 194
194fn lower_struct( 195fn lower_struct(
195 db: &dyn DefDatabase, 196 db: &dyn DefDatabase,
197 expander: &mut CfgExpander,
196 trace: &mut Trace<StructFieldData, Either<ast::TupleFieldDef, ast::RecordFieldDef>>, 198 trace: &mut Trace<StructFieldData, Either<ast::TupleFieldDef, ast::RecordFieldDef>>,
197 ast: &InFile<ast::StructKind>, 199 ast: &InFile<ast::StructKind>,
198 module_id: ModuleId,
199) -> StructKind { 200) -> StructKind {
200 let crate_graph = db.crate_graph();
201 match &ast.value { 201 match &ast.value {
202 ast::StructKind::Tuple(fl) => { 202 ast::StructKind::Tuple(fl) => {
203 for (i, fd) in fl.fields().enumerate() { 203 for (i, fd) in fl.fields().enumerate() {
204 let attrs = Attrs::new(&fd, &Hygiene::new(db.upcast(), ast.file_id)); 204 let attrs = expander.parse_attrs(&fd);
205 if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) { 205 if !expander.is_cfg_enabled(&attrs) {
206 continue; 206 continue;
207 } 207 }
208 208
@@ -219,8 +219,8 @@ fn lower_struct(
219 } 219 }
220 ast::StructKind::Record(fl) => { 220 ast::StructKind::Record(fl) => {
221 for fd in fl.fields() { 221 for fd in fl.fields() {
222 let attrs = Attrs::new(&fd, &Hygiene::new(db.upcast(), ast.file_id)); 222 let attrs = expander.parse_attrs(&fd);
223 if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) { 223 if !expander.is_cfg_enabled(&attrs) {
224 continue; 224 continue;
225 } 225 }
226 226
diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs
index 7b0c506b1..2f2e3e5ba 100644
--- a/crates/ra_hir_def/src/attr.rs
+++ b/crates/ra_hir_def/src/attr.rs
@@ -93,6 +93,7 @@ impl Attrs {
93 } 93 }
94 94
95 pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool { 95 pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool {
96 // FIXME: handle cfg_attr :-)
96 self.by_key("cfg").tt_values().all(|tt| cfg_options.is_cfg_enabled(tt) != Some(false)) 97 self.by_key("cfg").tt_values().all(|tt| cfg_options.is_cfg_enabled(tt) != Some(false))
97 } 98 }
98} 99}
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index e09996c6f..7fac6ce66 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -14,6 +14,7 @@ use ra_syntax::{ast, AstNode, AstPtr};
14use rustc_hash::FxHashMap; 14use rustc_hash::FxHashMap;
15 15
16use crate::{ 16use crate::{
17 attr::Attrs,
17 db::DefDatabase, 18 db::DefDatabase,
18 expr::{Expr, ExprId, Pat, PatId}, 19 expr::{Expr, ExprId, Pat, PatId},
19 item_scope::BuiltinShadowMode, 20 item_scope::BuiltinShadowMode,
@@ -23,26 +24,62 @@ use crate::{
23 src::HasSource, 24 src::HasSource,
24 AsMacroCall, DefWithBodyId, HasModule, Lookup, ModuleId, 25 AsMacroCall, DefWithBodyId, HasModule, Lookup, ModuleId,
25}; 26};
27use ra_cfg::CfgOptions;
28use ra_db::CrateId;
29
30/// A subser of Exander that only deals with cfg attributes. We only need it to
31/// avoid cyclic queries in crate def map during enum processing.
32pub(crate) struct CfgExpander {
33 cfg_options: CfgOptions,
34 hygiene: Hygiene,
35}
26 36
27pub(crate) struct Expander { 37pub(crate) struct Expander {
38 cfg_expander: CfgExpander,
28 crate_def_map: Arc<CrateDefMap>, 39 crate_def_map: Arc<CrateDefMap>,
29 current_file_id: HirFileId, 40 current_file_id: HirFileId,
30 hygiene: Hygiene,
31 ast_id_map: Arc<AstIdMap>, 41 ast_id_map: Arc<AstIdMap>,
32 module: ModuleId, 42 module: ModuleId,
33 recursive_limit: usize, 43 recursive_limit: usize,
34} 44}
35 45
46impl CfgExpander {
47 pub(crate) fn new(
48 db: &dyn DefDatabase,
49 current_file_id: HirFileId,
50 krate: CrateId,
51 ) -> CfgExpander {
52 let hygiene = Hygiene::new(db.upcast(), current_file_id);
53 let cfg_options = db.crate_graph()[krate].cfg_options.clone();
54 CfgExpander { cfg_options, hygiene }
55 }
56
57 pub(crate) fn parse_attrs(&self, owner: &dyn ast::AttrsOwner) -> Attrs {
58 Attrs::new(owner, &self.hygiene)
59 }
60
61 pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool {
62 attrs.is_cfg_enabled(&self.cfg_options)
63 }
64}
65
36impl Expander { 66impl Expander {
37 pub(crate) fn new( 67 pub(crate) fn new(
38 db: &dyn DefDatabase, 68 db: &dyn DefDatabase,
39 current_file_id: HirFileId, 69 current_file_id: HirFileId,
40 module: ModuleId, 70 module: ModuleId,
41 ) -> Expander { 71 ) -> Expander {
72 let cfg_expander = CfgExpander::new(db, current_file_id, module.krate);
42 let crate_def_map = db.crate_def_map(module.krate); 73 let crate_def_map = db.crate_def_map(module.krate);
43 let hygiene = Hygiene::new(db.upcast(), current_file_id);
44 let ast_id_map = db.ast_id_map(current_file_id); 74 let ast_id_map = db.ast_id_map(current_file_id);
45 Expander { crate_def_map, current_file_id, hygiene, ast_id_map, module, recursive_limit: 0 } 75 Expander {
76 cfg_expander,
77 crate_def_map,
78 current_file_id,
79 ast_id_map,
80 module,
81 recursive_limit: 0,
82 }
46 } 83 }
47 84
48 pub(crate) fn enter_expand<T: ast::AstNode>( 85 pub(crate) fn enter_expand<T: ast::AstNode>(
@@ -75,7 +112,7 @@ impl Expander {
75 ast_id_map: mem::take(&mut self.ast_id_map), 112 ast_id_map: mem::take(&mut self.ast_id_map),
76 bomb: DropBomb::new("expansion mark dropped"), 113 bomb: DropBomb::new("expansion mark dropped"),
77 }; 114 };
78 self.hygiene = Hygiene::new(db.upcast(), file_id); 115 self.cfg_expander.hygiene = Hygiene::new(db.upcast(), file_id);
79 self.current_file_id = file_id; 116 self.current_file_id = file_id;
80 self.ast_id_map = db.ast_id_map(file_id); 117 self.ast_id_map = db.ast_id_map(file_id);
81 self.recursive_limit += 1; 118 self.recursive_limit += 1;
@@ -91,7 +128,7 @@ impl Expander {
91 } 128 }
92 129
93 pub(crate) fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) { 130 pub(crate) fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) {
94 self.hygiene = Hygiene::new(db.upcast(), mark.file_id); 131 self.cfg_expander.hygiene = Hygiene::new(db.upcast(), mark.file_id);
95 self.current_file_id = mark.file_id; 132 self.current_file_id = mark.file_id;
96 self.ast_id_map = mem::take(&mut mark.ast_id_map); 133 self.ast_id_map = mem::take(&mut mark.ast_id_map);
97 self.recursive_limit -= 1; 134 self.recursive_limit -= 1;
@@ -102,8 +139,16 @@ impl Expander {
102 InFile { file_id: self.current_file_id, value } 139 InFile { file_id: self.current_file_id, value }
103 } 140 }
104 141
142 pub(crate) fn parse_attrs(&self, owner: &dyn ast::AttrsOwner) -> Attrs {
143 self.cfg_expander.parse_attrs(owner)
144 }
145
146 pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool {
147 self.cfg_expander.is_cfg_enabled(attrs)
148 }
149
105 fn parse_path(&mut self, path: ast::Path) -> Option<Path> { 150 fn parse_path(&mut self, path: ast::Path) -> Option<Path> {
106 Path::from_src(path, &self.hygiene) 151 Path::from_src(path, &self.cfg_expander.hygiene)
107 } 152 }
108 153
109 fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> { 154 fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> {
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 9d6ee095e..c1d7eb826 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -2,9 +2,7 @@
2//! representation. 2//! representation.
3 3
4use either::Either; 4use either::Either;
5
6use hir_expand::{ 5use hir_expand::{
7 hygiene::Hygiene,
8 name::{name, AsName, Name}, 6 name::{name, AsName, Name},
9 MacroDefId, MacroDefKind, 7 MacroDefId, MacroDefKind,
10}; 8};
@@ -18,10 +16,8 @@ use ra_syntax::{
18}; 16};
19use test_utils::tested_by; 17use test_utils::tested_by;
20 18
21use super::{ExprSource, PatSource};
22use crate::{ 19use crate::{
23 adt::StructKind, 20 adt::StructKind,
24 attr::Attrs,
25 body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax}, 21 body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax},
26 builtin_type::{BuiltinFloat, BuiltinInt}, 22 builtin_type::{BuiltinFloat, BuiltinInt},
27 db::DefDatabase, 23 db::DefDatabase,
@@ -33,10 +29,12 @@ use crate::{
33 path::GenericArgs, 29 path::GenericArgs,
34 path::Path, 30 path::Path,
35 type_ref::{Mutability, TypeRef}, 31 type_ref::{Mutability, TypeRef},
36 AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, HasModule, Intern, 32 AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId,
37 ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, 33 StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
38}; 34};
39 35
36use super::{ExprSource, PatSource};
37
40pub(super) fn lower( 38pub(super) fn lower(
41 db: &dyn DefDatabase, 39 db: &dyn DefDatabase,
42 def: DefWithBodyId, 40 def: DefWithBodyId,
@@ -300,7 +298,6 @@ impl ExprCollector<'_> {
300 self.alloc_expr(Expr::Return { expr }, syntax_ptr) 298 self.alloc_expr(Expr::Return { expr }, syntax_ptr)
301 } 299 }
302 ast::Expr::RecordLit(e) => { 300 ast::Expr::RecordLit(e) => {
303 let crate_graph = self.db.crate_graph();
304 let path = e.path().and_then(|path| self.expander.parse_path(path)); 301 let path = e.path().and_then(|path| self.expander.parse_path(path));
305 let mut field_ptrs = Vec::new(); 302 let mut field_ptrs = Vec::new();
306 let record_lit = if let Some(nfl) = e.record_field_list() { 303 let record_lit = if let Some(nfl) = e.record_field_list() {
@@ -308,13 +305,8 @@ impl ExprCollector<'_> {
308 .fields() 305 .fields()
309 .inspect(|field| field_ptrs.push(AstPtr::new(field))) 306 .inspect(|field| field_ptrs.push(AstPtr::new(field)))
310 .filter_map(|field| { 307 .filter_map(|field| {
311 let module_id = ContainerId::DefWithBodyId(self.def).module(self.db); 308 let attrs = self.expander.parse_attrs(&field);
312 let attrs = Attrs::new( 309 if !self.expander.is_cfg_enabled(&attrs) {
313 &field,
314 &Hygiene::new(self.db.upcast(), self.expander.current_file_id),
315 );
316
317 if !attrs.is_cfg_enabled(&crate_graph[module_id.krate].cfg_options) {
318 return None; 310 return None;
319 } 311 }
320 312
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index b8fbf0ed4..56a20c5bd 100644
--- a/crates/ra_hir_def/src/data.rs
+++ b/crates/ra_hir_def/src/data.rs
@@ -20,7 +20,7 @@ use crate::{
20 type_ref::{Mutability, TypeBound, TypeRef}, 20 type_ref::{Mutability, TypeBound, TypeRef},
21 visibility::RawVisibility, 21 visibility::RawVisibility,
22 AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, 22 AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule,
23 ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, 23 ImplId, Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc,
24}; 24};
25 25
26#[derive(Debug, Clone, PartialEq, Eq)] 26#[derive(Debug, Clone, PartialEq, Eq)]
@@ -218,10 +218,17 @@ impl ImplData {
218 let mut items = Vec::new(); 218 let mut items = Vec::new();
219 219
220 if let Some(item_list) = src.value.item_list() { 220 if let Some(item_list) = src.value.item_list() {
221 items.extend(collect_impl_items(db, item_list.impl_items(), src.file_id, id)); 221 let mut expander = Expander::new(db, impl_loc.ast_id.file_id, module_id);
222 items.extend(collect_impl_items(
223 db,
224 &mut expander,
225 item_list.impl_items(),
226 src.file_id,
227 id,
228 ));
222 items.extend(collect_impl_items_in_macros( 229 items.extend(collect_impl_items_in_macros(
223 db, 230 db,
224 module_id, 231 &mut expander,
225 &src.with_value(item_list), 232 &src.with_value(item_list),
226 id, 233 id,
227 )); 234 ));
@@ -268,18 +275,17 @@ impl ConstData {
268 275
269fn collect_impl_items_in_macros( 276fn collect_impl_items_in_macros(
270 db: &dyn DefDatabase, 277 db: &dyn DefDatabase,
271 module_id: ModuleId, 278 expander: &mut Expander,
272 impl_def: &InFile<ast::ItemList>, 279 impl_def: &InFile<ast::ItemList>,
273 id: ImplId, 280 id: ImplId,
274) -> Vec<AssocItemId> { 281) -> Vec<AssocItemId> {
275 let mut expander = Expander::new(db, impl_def.file_id, module_id);
276 let mut res = Vec::new(); 282 let mut res = Vec::new();
277 283
278 // We set a limit to protect against infinite recursion 284 // We set a limit to protect against infinite recursion
279 let limit = 100; 285 let limit = 100;
280 286
281 for m in impl_def.value.syntax().children().filter_map(ast::MacroCall::cast) { 287 for m in impl_def.value.syntax().children().filter_map(ast::MacroCall::cast) {
282 res.extend(collect_impl_items_in_macro(db, &mut expander, m, id, limit)) 288 res.extend(collect_impl_items_in_macro(db, expander, m, id, limit))
283 } 289 }
284 290
285 res 291 res
@@ -300,6 +306,7 @@ fn collect_impl_items_in_macro(
300 let items: InFile<ast::MacroItems> = expander.to_source(items); 306 let items: InFile<ast::MacroItems> = expander.to_source(items);
301 let mut res = collect_impl_items( 307 let mut res = collect_impl_items(
302 db, 308 db,
309 expander,
303 items.value.items().filter_map(|it| ImplItem::cast(it.syntax().clone())), 310 items.value.items().filter_map(|it| ImplItem::cast(it.syntax().clone())),
304 items.file_id, 311 items.file_id,
305 id, 312 id,
@@ -319,32 +326,26 @@ fn collect_impl_items_in_macro(
319 326
320fn collect_impl_items( 327fn collect_impl_items(
321 db: &dyn DefDatabase, 328 db: &dyn DefDatabase,
329 expander: &mut Expander,
322 impl_items: impl Iterator<Item = ImplItem>, 330 impl_items: impl Iterator<Item = ImplItem>,
323 file_id: crate::HirFileId, 331 file_id: crate::HirFileId,
324 id: ImplId, 332 id: ImplId,
325) -> Vec<AssocItemId> { 333) -> Vec<AssocItemId> {
326 let items = db.ast_id_map(file_id); 334 let items = db.ast_id_map(file_id);
327 let crate_graph = db.crate_graph();
328 let module_id = id.lookup(db).container.module(db);
329 335
330 impl_items 336 impl_items
331 .filter_map(|item_node| match item_node { 337 .filter_map(|item_node| match item_node {
332 ast::ImplItem::FnDef(it) => { 338 ast::ImplItem::FnDef(it) => {
339 let attrs = expander.parse_attrs(&it);
340 if !expander.is_cfg_enabled(&attrs) {
341 return None;
342 }
333 let def = FunctionLoc { 343 let def = FunctionLoc {
334 container: AssocContainerId::ImplId(id), 344 container: AssocContainerId::ImplId(id),
335 ast_id: AstId::new(file_id, items.ast_id(&it)), 345 ast_id: AstId::new(file_id, items.ast_id(&it)),
336 } 346 }
337 .intern(db); 347 .intern(db);
338 348 Some(def.into())
339 if !db
340 .function_data(def)
341 .attrs
342 .is_cfg_enabled(&crate_graph[module_id.krate].cfg_options)
343 {
344 None
345 } else {
346 Some(def.into())
347 }
348 } 349 }
349 ast::ImplItem::ConstDef(it) => { 350 ast::ImplItem::ConstDef(it) => {
350 let def = ConstLoc { 351 let def = ConstLoc {
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 8fe3f8617..98c74fe25 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -462,6 +462,14 @@ impl DefCollector<'_> {
462 Some(ModuleDefId::AdtId(AdtId::EnumId(e))) => { 462 Some(ModuleDefId::AdtId(AdtId::EnumId(e))) => {
463 tested_by!(glob_enum); 463 tested_by!(glob_enum);
464 // glob import from enum => just import all the variants 464 // glob import from enum => just import all the variants
465
466 // XXX: urgh, so this works by accident! Here, we look at
467 // the enum data, and, in theory, this might require us to
468 // look back at the crate_def_map, creating a cycle. For
469 // example, `enum E { crate::some_macro!(); }`. Luckely, the
470 // only kind of macro that is allowed inside enum is a
471 // `cfg_macro`, and we don't need to run name resolution for
472 // it, but this is sheer luck!
465 let enum_data = self.db.enum_data(e); 473 let enum_data = self.db.enum_data(e);
466 let resolutions = enum_data 474 let resolutions = enum_data
467 .variants 475 .variants
@@ -977,11 +985,7 @@ impl ModCollector<'_, '_> {
977 } 985 }
978 986
979 fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { 987 fn is_cfg_enabled(&self, attrs: &Attrs) -> bool {
980 // FIXME: handle cfg_attr :-) 988 attrs.is_cfg_enabled(self.def_collector.cfg_options)
981 attrs
982 .by_key("cfg")
983 .tt_values()
984 .all(|tt| self.def_collector.cfg_options.is_cfg_enabled(tt) != Some(false))
985 } 989 }
986} 990}
987 991
diff --git a/crates/ra_hir_def/src/nameres/tests/incremental.rs b/crates/ra_hir_def/src/nameres/tests/incremental.rs
index 496fc6b08..87165ac33 100644
--- a/crates/ra_hir_def/src/nameres/tests/incremental.rs
+++ b/crates/ra_hir_def/src/nameres/tests/incremental.rs
@@ -32,6 +32,9 @@ fn typing_inside_a_function_should_not_invalidate_def_map() {
32 32
33 use crate::foo::bar::Baz; 33 use crate::foo::bar::Baz;
34 34
35 enum E { A, B }
36 use E::*;
37
35 fn foo() -> i32 { 38 fn foo() -> i32 {
36 1 + 1 39 1 + 1
37 } 40 }
@@ -46,6 +49,9 @@ fn typing_inside_a_function_should_not_invalidate_def_map() {
46 49
47 use crate::foo::bar::Baz; 50 use crate::foo::bar::Baz;
48 51
52 enum E { A, B }
53 use E::*;
54
49 fn foo() -> i32 { 92 } 55 fn foo() -> i32 { 92 }
50 ", 56 ",
51 ); 57 );
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs
index f39559e9e..0f4a50be4 100644
--- a/crates/ra_syntax/src/ast/make.rs
+++ b/crates/ra_syntax/src/ast/make.rs
@@ -100,6 +100,9 @@ pub fn expr_empty_block() -> ast::Expr {
100pub fn expr_unimplemented() -> ast::Expr { 100pub fn expr_unimplemented() -> ast::Expr {
101 expr_from_text("unimplemented!()") 101 expr_from_text("unimplemented!()")
102} 102}
103pub fn expr_todo() -> ast::Expr {
104 expr_from_text("todo!()")
105}
103pub fn expr_path(path: ast::Path) -> ast::Expr { 106pub fn expr_path(path: ast::Path) -> ast::Expr {
104 expr_from_text(&path.to_string()) 107 expr_from_text(&path.to_string())
105} 108}