Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions crates/hir-def/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ use syntax::{
use tt::{TextRange, TextSize};

use crate::{
AdtId, AstIdLoc, AttrDefId, FieldId, FunctionId, GenericDefId, HasModule, InternedModuleId,
LifetimeParamId, LocalFieldId, MacroId, TypeOrConstParamId, VariantId,
AdtId, AstIdLoc, AttrDefId, FieldId, FunctionId, GenericDefId, HasModule, LifetimeParamId,
LocalFieldId, MacroId, ModuleId, TypeOrConstParamId, VariantId,
db::DefDatabase,
hir::generics::{GenericParams, LocalLifetimeParamId, LocalTypeOrConstParamId},
nameres::ModuleOrigin,
Expand Down Expand Up @@ -295,9 +295,8 @@ fn attrs_source(
) -> (InFile<ast::AnyHasAttrs>, Option<InFile<ast::Module>>, Crate) {
let (owner, krate) = match owner {
AttrDefId::ModuleId(id) => {
let id = id.loc(db);
let def_map = id.def_map(db);
let (definition, declaration) = match def_map[id.local_id].origin {
let (definition, declaration) = match def_map[id].origin {
ModuleOrigin::CrateRoot { definition } => {
let file = db.parse(definition).tree();
(InFile::new(definition.into(), ast::AnyHasAttrs::from(file)), None)
Expand All @@ -318,7 +317,7 @@ fn attrs_source(
(block.with_value(definition.into()), None)
}
};
return (definition, declaration, id.krate);
return (definition, declaration, def_map.krate());
}
AttrDefId::AdtId(AdtId::StructId(it)) => attrs_from_ast_id_loc(db, it),
AttrDefId::AdtId(AdtId::UnionId(it)) => attrs_from_ast_id_loc(db, it),
Expand Down Expand Up @@ -1201,14 +1200,14 @@ impl AttrFlags {
}

#[inline]
pub fn doc_keyword(db: &dyn DefDatabase, owner: InternedModuleId) -> Option<Symbol> {
pub fn doc_keyword(db: &dyn DefDatabase, owner: ModuleId) -> Option<Symbol> {
if !AttrFlags::query(db, AttrDefId::ModuleId(owner)).contains(AttrFlags::HAS_DOC_KEYWORD) {
return None;
}
return doc_keyword(db, owner);

#[salsa::tracked]
fn doc_keyword(db: &dyn DefDatabase, owner: InternedModuleId) -> Option<Symbol> {
fn doc_keyword(db: &dyn DefDatabase, owner: ModuleId) -> Option<Symbol> {
collect_attrs(db, AttrDefId::ModuleId(owner), |attr| {
if let Meta::TokenTree { path, tt } = attr
&& path.is1("doc")
Expand Down
26 changes: 11 additions & 15 deletions crates/hir-def/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ use la_arena::ArenaMap;
use triomphe::Arc;

use crate::{
AssocItemId, AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, CrateRootModuleId, DefWithBodyId,
EnumId, EnumLoc, EnumVariantId, EnumVariantLoc, ExternBlockId, ExternBlockLoc, ExternCrateId,
ExternCrateLoc, FunctionId, FunctionLoc, GenericDefId, HasModule, ImplId, ImplLoc,
InternedModuleId, LocalFieldId, Macro2Id, Macro2Loc, MacroExpander, MacroId, MacroRulesId,
MacroRulesLoc, MacroRulesLocFlags, ProcMacroId, ProcMacroLoc, StaticId, StaticLoc, StructId,
StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, UseId, UseLoc,
VariantId,
AssocItemId, AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, EnumVariantId,
EnumVariantLoc, ExternBlockId, ExternBlockLoc, ExternCrateId, ExternCrateLoc, FunctionId,
FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalFieldId, Macro2Id, Macro2Loc, MacroExpander,
MacroId, MacroRulesId, MacroRulesLoc, MacroRulesLocFlags, ProcMacroId, ProcMacroLoc, StaticId,
StaticLoc, StructId, StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId,
UnionLoc, UseId, UseLoc, VariantId,
attrs::AttrFlags,
expr_store::{
Body, BodySourceMap, ExpressionStore, ExpressionStoreSourceMap, scope::ExprScopes,
Expand Down Expand Up @@ -83,9 +82,6 @@ pub trait InternDatabase: RootQueryDb {
#[salsa::interned]
fn intern_macro_rules(&self, loc: MacroRulesLoc) -> MacroRulesId;
// endregion: items

#[salsa::interned]
fn intern_block(&self, loc: BlockLoc) -> BlockId;
}

#[query_group::query_group]
Expand Down Expand Up @@ -276,8 +272,8 @@ fn include_macro_invoc(
}

fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: Crate) -> bool {
let root_module = CrateRootModuleId::from(crate_id).module(db);
let attrs = AttrFlags::query(db, AttrDefId::ModuleId(InternedModuleId::new(db, root_module)));
let root_module = crate_def_map(db, crate_id).root_module_id();
let attrs = AttrFlags::query(db, AttrDefId::ModuleId(root_module));
attrs.contains(AttrFlags::IS_NO_STD)
}

Expand All @@ -298,7 +294,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
let loc: Macro2Loc = it.lookup(db);

MacroDefId {
krate: loc.container.krate,
krate: loc.container.krate(db),
kind: kind(loc.expander, loc.id.file_id, loc.id.value.upcast()),
local_inner: false,
allow_internal_unsafe: loc.allow_internal_unsafe,
Expand All @@ -309,7 +305,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
let loc: MacroRulesLoc = it.lookup(db);

MacroDefId {
krate: loc.container.krate,
krate: loc.container.krate(db),
kind: kind(loc.expander, loc.id.file_id, loc.id.value.upcast()),
local_inner: loc.flags.contains(MacroRulesLocFlags::LOCAL_INNER),
allow_internal_unsafe: loc
Expand All @@ -322,7 +318,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
let loc = it.lookup(db);

MacroDefId {
krate: loc.container.krate,
krate: loc.container.krate(db),
kind: MacroDefKind::ProcMacro(loc.id, loc.expander, loc.kind),
local_inner: false,
allow_internal_unsafe: false,
Expand Down
29 changes: 16 additions & 13 deletions crates/hir-def/src/expr_store/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use triomphe::Arc;
use tt::TextRange;

use crate::{
AdtId, BlockId, BlockLoc, DefWithBodyId, FunctionId, GenericDefId, ImplId, MacroId,
AdtId, BlockId, BlockIdLt, DefWithBodyId, FunctionId, GenericDefId, ImplId, MacroId,
ModuleDefId, ModuleId, TraitId, TypeAliasId, UnresolvedMacro,
attrs::AttrFlags,
builtin_type::BuiltinUint,
Expand Down Expand Up @@ -437,6 +437,7 @@ pub struct ExprCollector<'db> {
current_binding_owner: Option<ExprId>,

awaitable_context: Option<Awaitable>,
krate: base_db::Crate,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -524,9 +525,10 @@ impl<'db> ExprCollector<'db> {
) -> ExprCollector<'_> {
let (def_map, local_def_map) = module.local_def_map(db);
let expander = Expander::new(db, current_file_id, def_map);
let krate = module.krate(db);
ExprCollector {
db,
cfg_options: module.krate().cfg_options(db),
cfg_options: krate.cfg_options(db),
module,
def_map,
local_def_map,
Expand All @@ -540,12 +542,13 @@ impl<'db> ExprCollector<'db> {
awaitable_context: None,
current_block_legacy_macro_defs_count: FxHashMap::default(),
outer_impl_trait: false,
krate,
}
}

#[inline]
pub(crate) fn lang_items(&self) -> &'db LangItems {
self.lang_items.get_or_init(|| crate::lang_item::lang_items(self.db, self.module.krate))
self.lang_items.get_or_init(|| crate::lang_item::lang_items(self.db, self.def_map.krate()))
}

#[inline]
Expand Down Expand Up @@ -1915,9 +1918,8 @@ impl<'db> ExprCollector<'db> {
T: ast::AstNode,
{
let macro_call_ptr = self.expander.in_file(syntax_ptr);
let module = self.module.local_id;

let block_call = self.def_map.modules[self.module.local_id].scope.macro_invoc(
let block_call = self.def_map.modules[self.module].scope.macro_invoc(
self.expander.in_file(self.expander.ast_id_map().ast_id_for_ptr(syntax_ptr)),
);
let res = match block_call {
Expand All @@ -1929,7 +1931,7 @@ impl<'db> ExprCollector<'db> {
.resolve_path(
self.local_def_map,
self.db,
module,
self.module,
path,
crate::item_scope::BuiltinShadowMode::Other,
Some(MacroSubNs::Bang),
Expand All @@ -1940,7 +1942,7 @@ impl<'db> ExprCollector<'db> {
self.expander.enter_expand(
self.db,
mcall,
self.module.krate(),
self.krate,
resolver,
&mut |ptr, call| {
_ = self.store.expansions.insert(ptr.map(|(it, _)| it), call);
Expand Down Expand Up @@ -2058,7 +2060,8 @@ impl<'db> ExprCollector<'db> {
return;
};
let name = name.as_name();
let macro_id = self.def_map.modules[DefMap::ROOT].scope.get(&name).take_macros();
let macro_id =
self.def_map.modules[self.def_map.root].scope.get(&name).take_macros();
self.collect_macro_def(statements, macro_id);
}
ast::Stmt::Item(ast::Item::MacroRules(macro_)) => {
Expand All @@ -2072,7 +2075,7 @@ impl<'db> ExprCollector<'db> {
let name = name.as_name();
let macro_defs_count =
self.current_block_legacy_macro_defs_count.entry(name.clone()).or_insert(0);
let macro_id = self.def_map.modules[DefMap::ROOT]
let macro_id = self.def_map.modules[self.def_map.root]
.scope
.get_legacy_macro(&name)
.and_then(|it| it.get(*macro_defs_count))
Expand Down Expand Up @@ -2111,14 +2114,14 @@ impl<'db> ExprCollector<'db> {
) -> ExprId {
let block_id = self.expander.ast_id_map().ast_id_for_block(&block).map(|file_local_id| {
let ast_id = self.expander.in_file(file_local_id);
self.db.intern_block(BlockLoc { ast_id, module: self.module })
unsafe { BlockIdLt::new(self.db, ast_id, self.module).to_static() }
});

let (module, def_map) =
match block_id.map(|block_id| (block_def_map(self.db, block_id), block_id)) {
Some((def_map, block_id)) => {
self.store.block_scopes.push(block_id);
(def_map.module_id(DefMap::ROOT), def_map)
(def_map.root_module_id(), def_map)
}
None => (self.module, self.def_map),
};
Expand Down Expand Up @@ -2201,7 +2204,7 @@ impl<'db> ExprCollector<'db> {
let (resolved, _) = self.def_map.resolve_path(
self.local_def_map,
self.db,
self.module.local_id,
self.module,
&name.clone().into(),
BuiltinShadowMode::Other,
None,
Expand Down Expand Up @@ -3131,7 +3134,7 @@ impl<'db> ExprCollector<'db> {
let precision_expr = self.make_count(precision, argmap);
let width_expr = self.make_count(width, argmap);

if self.module.krate().workspace_data(self.db).is_atleast_187() {
if self.krate.workspace_data(self.db).is_atleast_187() {
// These need to match the constants in library/core/src/fmt/rt.rs.
let align = match alignment {
Some(FormatAlignment::Left) => 0,
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/expr_store/tests/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ fn def_map_at(#[rust_analyzer::rust_fixture] ra_fixture: &str) -> String {
let (db, position) = TestDB::with_position(ra_fixture);

let module = db.module_at_position(position);
module.def_map(&db).dump(&db)
salsa::plumbing::attach(&db, || module.def_map(&db).dump(&db))
}

fn check_block_scopes_at(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
let (db, position) = TestDB::with_position(ra_fixture);

let module = db.module_at_position(position);
let actual = module.def_map(&db).dump_block_scopes(&db);
let actual = salsa::plumbing::attach(&db, || format!("{module:#?}"));
expect.assert_eq(&actual);
}

Expand Down
64 changes: 58 additions & 6 deletions crates/hir-def/src/expr_store/tests/body/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,63 @@ fn f() {
}
"#,
expect![[r#"
BlockId(3801) in BlockRelativeModuleId { block: Some(BlockId(3800)), local_id: Idx::<ModuleData>(1) }
BlockId(3800) in BlockRelativeModuleId { block: None, local_id: Idx::<ModuleData>(0) }
crate scope
"#]],
ModuleIdLt {
[salsa id]: Id(3003),
krate: Crate(
Id(1c00),
),
block: Some(
BlockIdLt {
[salsa id]: Id(3c01),
ast_id: InFileWrapper {
file_id: FileId(
EditionedFileIdData {
editioned_file_id: EditionedFileId(
0,
Edition2024,
),
krate: Crate(
Id(1c00),
),
},
),
value: FileAstId::<syntax::ast::generated::nodes::BlockExpr>(ErasedFileAstId { kind: BlockExpr, index: 0, hash: F9BF }),
},
module: ModuleIdLt {
[salsa id]: Id(3002),
krate: Crate(
Id(1c00),
),
block: Some(
BlockIdLt {
[salsa id]: Id(3c00),
ast_id: InFileWrapper {
file_id: FileId(
EditionedFileIdData {
editioned_file_id: EditionedFileId(
0,
Edition2024,
),
krate: Crate(
Id(1c00),
),
},
),
value: FileAstId::<syntax::ast::generated::nodes::BlockExpr>(ErasedFileAstId { kind: BlockExpr, index: 0, hash: C181 }),
},
module: ModuleIdLt {
[salsa id]: Id(3000),
krate: Crate(
Id(1c00),
),
block: None,
},
},
),
},
},
),
}"#]],
);
}

Expand Down Expand Up @@ -460,7 +513,7 @@ fn foo() {
}

#[test]
fn is_visible_from_same_def_map() {
fn is_visible_from_same_def_map_regression_9481() {
// Regression test for https://github.com/rust-lang/rust-analyzer/issues/9481
check_at(
r#"
Expand All @@ -478,7 +531,6 @@ fn outer() {
- tests : type

(block scope)::tests
- name : _
- outer : value (glob)

crate
Expand Down
Loading
Loading