Skip to content

Make ModuleId a tracked struct #20001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
9 changes: 8 additions & 1 deletion crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,19 @@ impl Files {
}
}

#[salsa_macros::interned(no_lifetime, debug, constructor=from_span)]
#[salsa_macros::interned(no_lifetime, constructor=from_span)]
#[derive(PartialOrd, Ord)]
pub struct EditionedFileId {
pub editioned_file_id: span::EditionedFileId,
}

impl std::fmt::Debug for EditionedFileId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
salsa::with_attached_database(|db| self.editioned_file_id(db).fmt(f))
.unwrap_or_else(|| f.debug_tuple("EditionedFileId").field(&self.0).finish())
}
}

impl EditionedFileId {
// Salsa already uses the name `new`...
#[inline]
Expand Down
28 changes: 12 additions & 16 deletions crates/hir-def/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::{borrow::Cow, convert::identity, hash::Hash, ops};

use base_db::Crate;
use cfg::{CfgExpr, CfgOptions};
use either::Either;
use hir_expand::{
Expand Down Expand Up @@ -56,12 +55,12 @@ impl Attrs {
(**self).iter().find(|attr| attr.id == id)
}

pub(crate) fn expand_cfg_attr(
db: &dyn DefDatabase,
krate: Crate,
pub(crate) fn expand_cfg_attr<'db>(
db: &'db dyn DefDatabase,
cfg_options: impl FnOnce() -> &'db CfgOptions,
raw_attrs: RawAttrs,
) -> Attrs {
Attrs(raw_attrs.expand_cfg_attr(db, krate))
Attrs(raw_attrs.expand_cfg_attr(db, cfg_options))
}

pub(crate) fn is_cfg_enabled_for(
Expand Down Expand Up @@ -105,35 +104,32 @@ impl Attrs {
) -> Arc<ArenaMap<LocalFieldId, Attrs>> {
let _p = tracing::info_span!("fields_attrs_query").entered();
let mut res = ArenaMap::default();
let (fields, file_id, krate) = match v {
let (fields, file_id, module) = match v {
VariantId::EnumVariantId(it) => {
let loc = it.lookup(db);
let krate = loc.parent.lookup(db).container.krate;
let source = loc.source(db);
(source.value.field_list(), source.file_id, krate)
(source.value.field_list(), source.file_id, loc.parent.lookup(db).container)
}
VariantId::StructId(it) => {
let loc = it.lookup(db);
let krate = loc.container.krate;
let source = loc.source(db);
(source.value.field_list(), source.file_id, krate)
(source.value.field_list(), source.file_id, loc.container)
}
VariantId::UnionId(it) => {
let loc = it.lookup(db);
let krate = loc.container.krate;
let source = loc.source(db);
(
source.value.record_field_list().map(ast::FieldList::RecordFieldList),
source.file_id,
krate,
loc.container,
)
}
};
let Some(fields) = fields else {
return Arc::new(res);
};

let cfg_options = krate.cfg_options(db);
let cfg_options = module.krate(db).cfg_options(db);
let span_map = db.span_map(file_id);

match fields {
Expand Down Expand Up @@ -520,7 +516,7 @@ impl AttrsWithOwner {
match def {
AttrDefId::ModuleId(module) => {
let def_map = module.def_map(db);
let mod_data = &def_map[module.local_id];
let mod_data = &def_map[module];

let raw_attrs = match mod_data.origin {
ModuleOrigin::File { definition, declaration_tree_id, declaration, .. } => {
Expand All @@ -544,7 +540,7 @@ impl AttrsWithOwner {
tree.top_level_raw_attrs().clone()
}
};
Attrs::expand_cfg_attr(db, module.krate, raw_attrs)
Attrs::expand_cfg_attr(db, || module.krate(db).cfg_options(db), raw_attrs)
}
AttrDefId::FieldId(it) => db.fields_attrs(it.parent)[it.local_id].clone(),
AttrDefId::EnumVariantId(it) => attrs_from_ast_id_loc(db, it),
Expand Down Expand Up @@ -618,7 +614,7 @@ impl AttrsWithOwner {
// Modules can have 2 attribute owners (the `mod x;` item, and the module file itself).

let def_map = module.def_map(db);
let mod_data = &def_map[module.local_id];
let mod_data = &def_map[module];
match mod_data.declaration_source(db) {
Some(it) => {
let mut map = AttrSourceMap::new(InFile::new(it.file_id, &it.value));
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-def/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,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 @@ -429,7 +429,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 @@ -442,7 +442,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
43 changes: 23 additions & 20 deletions crates/hir-def/src/expr_store/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,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 @@ -545,9 +546,10 @@ impl ExprCollector<'_> {
) -> 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 @@ -561,6 +563,7 @@ impl ExprCollector<'_> {
awaitable_context: None,
current_block_legacy_macro_defs_count: FxHashMap::default(),
outer_impl_trait: false,
krate,
}
}

Expand Down Expand Up @@ -1940,9 +1943,8 @@ impl ExprCollector<'_> {
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 @@ -1954,7 +1956,7 @@ impl ExprCollector<'_> {
.resolve_path(
self.local_def_map,
self.db,
module,
self.module,
path,
crate::item_scope::BuiltinShadowMode::Other,
Some(MacroSubNs::Bang),
Expand All @@ -1965,7 +1967,7 @@ impl ExprCollector<'_> {
self.expander.enter_expand(
self.db,
mcall,
self.module.krate(),
self.krate,
resolver,
&mut |ptr, call| {
_ = self.source_map.expansions.insert(ptr.map(|(it, _)| it), call);
Expand Down Expand Up @@ -2095,7 +2097,8 @@ impl ExprCollector<'_> {
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 @@ -2109,7 +2112,7 @@ impl ExprCollector<'_> {
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 @@ -2155,7 +2158,7 @@ impl ExprCollector<'_> {
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 @@ -2238,7 +2241,7 @@ impl ExprCollector<'_> {
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 @@ -2865,12 +2868,12 @@ impl ExprCollector<'_> {

let new_v1_formatted = LangItem::FormatArguments.ty_rel_path(
self.db,
self.module.krate(),
self.krate,
Name::new_symbol_root(sym::new_v1_formatted),
);
let unsafe_arg_new = LangItem::FormatUnsafeArg.ty_rel_path(
self.db,
self.module.krate(),
self.krate,
Name::new_symbol_root(sym::new),
);
let new_v1_formatted =
Expand Down Expand Up @@ -2961,7 +2964,7 @@ impl ExprCollector<'_> {
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 Expand Up @@ -2996,15 +2999,15 @@ impl ExprCollector<'_> {
let width =
RecordLitField { name: Name::new_symbol_root(sym::width), expr: width_expr };
self.alloc_expr_desugared(Expr::RecordLit {
path: LangItem::FormatPlaceholder.path(self.db, self.module.krate()).map(Box::new),
path: LangItem::FormatPlaceholder.path(self.db, self.krate).map(Box::new),
fields: Box::new([position, flags, precision, width]),
spread: None,
})
} else {
let format_placeholder_new = {
let format_placeholder_new = LangItem::FormatPlaceholder.ty_rel_path(
self.db,
self.module.krate(),
self.krate,
Name::new_symbol_root(sym::new),
);
match format_placeholder_new {
Expand All @@ -3027,7 +3030,7 @@ impl ExprCollector<'_> {
let align = {
let align = LangItem::FormatAlignment.ty_rel_path(
self.db,
self.module.krate(),
self.krate,
match alignment {
Some(FormatAlignment::Left) => Name::new_symbol_root(sym::Left),
Some(FormatAlignment::Right) => Name::new_symbol_root(sym::Right),
Expand Down Expand Up @@ -3080,7 +3083,7 @@ impl ExprCollector<'_> {
)));
let count_is = match LangItem::FormatCount.ty_rel_path(
self.db,
self.module.krate(),
self.krate,
Name::new_symbol_root(sym::Is),
) {
Some(count_is) => self.alloc_expr_desugared(Expr::Path(count_is)),
Expand All @@ -3098,7 +3101,7 @@ impl ExprCollector<'_> {
)));
let count_param = match LangItem::FormatCount.ty_rel_path(
self.db,
self.module.krate(),
self.krate,
Name::new_symbol_root(sym::Param),
) {
Some(count_param) => self.alloc_expr_desugared(Expr::Path(count_param)),
Expand All @@ -3116,7 +3119,7 @@ impl ExprCollector<'_> {
}
None => match LangItem::FormatCount.ty_rel_path(
self.db,
self.module.krate(),
self.krate,
Name::new_symbol_root(sym::Implied),
) {
Some(count_param) => self.alloc_expr_desugared(Expr::Path(count_param)),
Expand All @@ -3138,7 +3141,7 @@ impl ExprCollector<'_> {

let new_fn = match LangItem::FormatArgument.ty_rel_path(
self.db,
self.module.krate(),
self.krate,
Name::new_symbol_root(match ty {
Format(Display) => sym::new_display,
Format(Debug) => sym::new_debug,
Expand All @@ -3161,7 +3164,7 @@ impl ExprCollector<'_> {
// endregion: format

fn lang_path(&self, lang: LangItem) -> Option<Path> {
lang.path(self.db, self.module.krate())
lang.path(self.db, self.krate)
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/hir-def/src/expr_store/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ pub fn print_variant_body_hir(db: &dyn DefDatabase, owner: VariantId, edition: E
let FieldData { name, type_ref, visibility, is_unsafe } = data;
match visibility {
crate::item_tree::RawVisibility::Module(interned, _visibility_explicitness) => {
w!(p, "{}", interned.display(db, p.edition))
w!(p, "pub(in {})", interned.display(db, p.edition))
}
crate::item_tree::RawVisibility::Public => w!(p, "pub "),
crate::item_tree::RawVisibility::PubCrate => w!(p, "pub(crate) "),
crate::item_tree::RawVisibility::PubSelf(_) => w!(p, "pub(self) "),
}
if *is_unsafe {
w!(p, "unsafe ");
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
Loading