diff options
Diffstat (limited to 'crates/ra_hir_def/src/nameres')
-rw-r--r-- | crates/ra_hir_def/src/nameres/collector.rs | 22 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/tests.rs | 31 |
2 files changed, 44 insertions, 9 deletions
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 30664278e..37d0f3093 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs | |||
@@ -20,7 +20,8 @@ use crate::{ | |||
20 | }, | 20 | }, |
21 | path::{Path, PathKind}, | 21 | path::{Path, PathKind}, |
22 | AdtId, AstId, AstItemDef, ConstId, CrateModuleId, EnumId, EnumVariantId, FunctionId, | 22 | AdtId, AstId, AstItemDef, ConstId, CrateModuleId, EnumId, EnumVariantId, FunctionId, |
23 | LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, UnionId, | 23 | LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, StructOrUnionId, TraitId, TypeAliasId, |
24 | UnionId, | ||
24 | }; | 25 | }; |
25 | 26 | ||
26 | pub(super) fn collect_defs(db: &impl DefDatabase2, mut def_map: CrateDefMap) -> CrateDefMap { | 27 | pub(super) fn collect_defs(db: &impl DefDatabase2, mut def_map: CrateDefMap) -> CrateDefMap { |
@@ -36,11 +37,12 @@ pub(super) fn collect_defs(db: &impl DefDatabase2, mut def_map: CrateDefMap) -> | |||
36 | ); | 37 | ); |
37 | 38 | ||
38 | // look for the prelude | 39 | // look for the prelude |
39 | if def_map.prelude.is_none() { | 40 | // If the dependency defines a prelude, we overwrite an already defined |
40 | let map = db.crate_def_map(dep.crate_id); | 41 | // prelude. This is necessary to import the "std" prelude if a crate |
41 | if map.prelude.is_some() { | 42 | // depends on both "core" and "std". |
42 | def_map.prelude = map.prelude; | 43 | let dep_def_map = db.crate_def_map(dep.crate_id); |
43 | } | 44 | if dep_def_map.prelude.is_some() { |
45 | def_map.prelude = dep_def_map.prelude; | ||
44 | } | 46 | } |
45 | } | 47 | } |
46 | 48 | ||
@@ -665,12 +667,14 @@ where | |||
665 | PerNs::values(FunctionId::from_ast_id(ctx, ast_id).into()) | 667 | PerNs::values(FunctionId::from_ast_id(ctx, ast_id).into()) |
666 | } | 668 | } |
667 | raw::DefKind::Struct(ast_id) => { | 669 | raw::DefKind::Struct(ast_id) => { |
668 | let s = StructId::from_ast_id(ctx, ast_id).into(); | 670 | let id = StructOrUnionId::from_ast_id(ctx, ast_id).into(); |
671 | let s = StructId(id).into(); | ||
669 | PerNs::both(s, s) | 672 | PerNs::both(s, s) |
670 | } | 673 | } |
671 | raw::DefKind::Union(ast_id) => { | 674 | raw::DefKind::Union(ast_id) => { |
672 | let s = UnionId::from_ast_id(ctx, ast_id).into(); | 675 | let id = StructOrUnionId::from_ast_id(ctx, ast_id).into(); |
673 | PerNs::both(s, s) | 676 | let u = UnionId(id).into(); |
677 | PerNs::both(u, u) | ||
674 | } | 678 | } |
675 | raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()), | 679 | raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()), |
676 | raw::DefKind::Const(ast_id) => PerNs::values(ConstId::from_ast_id(ctx, ast_id).into()), | 680 | raw::DefKind::Const(ast_id) => PerNs::values(ConstId::from_ast_id(ctx, ast_id).into()), |
diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs index 52bd0aa91..256f7d4be 100644 --- a/crates/ra_hir_def/src/nameres/tests.rs +++ b/crates/ra_hir_def/src/nameres/tests.rs | |||
@@ -464,6 +464,37 @@ fn values_dont_shadow_extern_crates() { | |||
464 | } | 464 | } |
465 | 465 | ||
466 | #[test] | 466 | #[test] |
467 | fn std_prelude_takes_precedence_above_core_prelude() { | ||
468 | let map = def_map( | ||
469 | r#" | ||
470 | //- /main.rs crate:main deps:core,std | ||
471 | use {Foo, Bar}; | ||
472 | |||
473 | //- /std.rs crate:std deps:core | ||
474 | #[prelude_import] | ||
475 | pub use self::prelude::*; | ||
476 | mod prelude { | ||
477 | pub struct Foo; | ||
478 | pub use core::prelude::Bar; | ||
479 | } | ||
480 | |||
481 | //- /core.rs crate:core | ||
482 | #[prelude_import] | ||
483 | pub use self::prelude::*; | ||
484 | mod prelude { | ||
485 | pub struct Bar; | ||
486 | } | ||
487 | "#, | ||
488 | ); | ||
489 | |||
490 | assert_snapshot!(map, @r###" | ||
491 | ⋮crate | ||
492 | ⋮Bar: t v | ||
493 | ⋮Foo: t v | ||
494 | "###); | ||
495 | } | ||
496 | |||
497 | #[test] | ||
467 | fn cfg_not_test() { | 498 | fn cfg_not_test() { |
468 | let map = def_map( | 499 | let map = def_map( |
469 | r#" | 500 | r#" |