Skip to content
Closed
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
22 changes: 18 additions & 4 deletions crates/hir-def/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use hir_expand::{
EditionedFileId, HirFileId, InFile, Lookup, MacroCallId, MacroDefId, MacroDefKind,
db::ExpandDatabase,
};
use salsa::{Durability, Setter};
use triomphe::Arc;

use crate::{
Expand All @@ -16,10 +17,6 @@ use crate::{

#[query_group::query_group]
pub trait DefDatabase: ExpandDatabase + SourceDatabase {
/// Whether to expand procedural macros during name resolution.
#[salsa::input]
fn expand_proc_attr_macros(&self) -> bool;

/// Computes an [`ItemTree`] for the given file or macro expansion.
#[salsa::invoke(file_item_tree)]
#[salsa::transparent]
Expand Down Expand Up @@ -47,6 +44,23 @@ pub trait DefDatabase: ExpandDatabase + SourceDatabase {
fn include_macro_invoc(&self, crate_id: Crate) -> Arc<[(MacroCallId, EditionedFileId)]>;
}

/// Whether to expand procedural macros during name resolution.
#[salsa::input(singleton, debug)]
pub(crate) struct ExpandProcAttrMacros {
#[returns(copy)]
pub(crate) enabled: bool,
}

pub fn set_expand_proc_attr_macros(db: &mut dyn DefDatabase, enabled: bool) {
if let Some(expand_proc_attr_macros) = ExpandProcAttrMacros::try_get(db) {
if expand_proc_attr_macros.enabled(db) != enabled {
expand_proc_attr_macros.set_enabled(db).with_durability(Durability::HIGH).to(enabled);
}
} else {
_ = ExpandProcAttrMacros::builder(enabled).durability(Durability::HIGH).new(db);
}
}

// return: macro call id and include file id
fn include_macro_invoc(
db: &dyn DefDatabase,
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-def/src/expr_store/tests/body/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ fn f() {
"#,
expect![[r#"
ModuleIdLt {
[salsa id]: Id(3403),
[salsa id]: Id(2c03),
krate: Crate(
Id(2000),
Id(1c00),
),
block: Some(
BlockId(
4401,
3c01,
),
),
containing_module_inside_def_map: None,
Expand Down
16 changes: 8 additions & 8 deletions crates/hir-def/src/macro_expansion_tests/mbe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ macro_rules! f {
};
}

struct#0:MacroRules[BE8F, 0]@58..64#17408# MyTraitMap2#0:MacroCall[BE8F, 0]@31..42#ROOT2024# {#0:MacroRules[BE8F, 0]@72..73#17408#
map#0:MacroRules[BE8F, 0]@86..89#17408#:#0:MacroRules[BE8F, 0]@89..90#17408# #0:MacroRules[BE8F, 0]@89..90#17408#::#0:MacroRules[BE8F, 0]@91..93#17408#std#0:MacroRules[BE8F, 0]@93..96#17408#::#0:MacroRules[BE8F, 0]@96..98#17408#collections#0:MacroRules[BE8F, 0]@98..109#17408#::#0:MacroRules[BE8F, 0]@109..111#17408#HashSet#0:MacroRules[BE8F, 0]@111..118#17408#<#0:MacroRules[BE8F, 0]@118..119#17408#(#0:MacroRules[BE8F, 0]@119..120#17408#)#0:MacroRules[BE8F, 0]@120..121#17408#>#0:MacroRules[BE8F, 0]@121..122#17408#,#0:MacroRules[BE8F, 0]@122..123#17408#
}#0:MacroRules[BE8F, 0]@132..133#17408#
struct#0:MacroRules[BE8F, 0]@58..64#15360# MyTraitMap2#0:MacroCall[BE8F, 0]@31..42#ROOT2024# {#0:MacroRules[BE8F, 0]@72..73#15360#
map#0:MacroRules[BE8F, 0]@86..89#15360#:#0:MacroRules[BE8F, 0]@89..90#15360# #0:MacroRules[BE8F, 0]@89..90#15360#::#0:MacroRules[BE8F, 0]@91..93#15360#std#0:MacroRules[BE8F, 0]@93..96#15360#::#0:MacroRules[BE8F, 0]@96..98#15360#collections#0:MacroRules[BE8F, 0]@98..109#15360#::#0:MacroRules[BE8F, 0]@109..111#15360#HashSet#0:MacroRules[BE8F, 0]@111..118#15360#<#0:MacroRules[BE8F, 0]@118..119#15360#(#0:MacroRules[BE8F, 0]@119..120#15360#)#0:MacroRules[BE8F, 0]@120..121#15360#>#0:MacroRules[BE8F, 0]@121..122#15360#,#0:MacroRules[BE8F, 0]@122..123#15360#
}#0:MacroRules[BE8F, 0]@132..133#15360#
"#]],
);
}
Expand Down Expand Up @@ -197,7 +197,7 @@ macro_rules! mk_struct {
#[macro_use]
mod foo;

struct#1:MacroRules[DB0C, 0]@59..65#17408# Foo#0:MacroCall[DB0C, 0]@32..35#ROOT2024#(#1:MacroRules[DB0C, 0]@70..71#17408#u32#0:MacroCall[DB0C, 0]@41..44#ROOT2024#)#1:MacroRules[DB0C, 0]@74..75#17408#;#1:MacroRules[DB0C, 0]@75..76#17408#
struct#1:MacroRules[DB0C, 0]@59..65#15360# Foo#0:MacroCall[DB0C, 0]@32..35#ROOT2024#(#1:MacroRules[DB0C, 0]@70..71#15360#u32#0:MacroCall[DB0C, 0]@41..44#ROOT2024#)#1:MacroRules[DB0C, 0]@74..75#15360#;#1:MacroRules[DB0C, 0]@75..76#15360#
"#]],
);
}
Expand Down Expand Up @@ -423,10 +423,10 @@ m! { foo, bar }
macro_rules! m {
($($i:ident),*) => ( impl Bar { $(fn $i() {})* } );
}
impl#\17408# Bar#\17408# {#\17408#
fn#\17408# foo#\ROOT2024#(#\17408#)#\17408# {#\17408#}#\17408#
fn#\17408# bar#\ROOT2024#(#\17408#)#\17408# {#\17408#}#\17408#
}#\17408#
impl#\15360# Bar#\15360# {#\15360#
fn#\15360# foo#\ROOT2024#(#\15360#)#\15360# {#\15360#}#\15360#
fn#\15360# bar#\ROOT2024#(#\15360#)#\15360# {#\15360#}#\15360#
}#\15360#
"#]],
);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ impl<'db> DefCollector<'db> {
let (expander, kind) = match self.proc_macros.iter().find(|(n, _, _)| n == &def.name) {
Some(_)
if kind == hir_expand::proc_macro::ProcMacroKind::Attr
&& !self.db.expand_proc_attr_macros() =>
&& !crate::db::ExpandProcAttrMacros::get(self.db).enabled(self.db) =>
{
(CustomProcMacroExpander::disabled_proc_attr(), kind)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/hir-def/src/nameres/tests/incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ fn f() { foo }
"parse_macro_expansion",
"expand_proc_macro",
"macro_arg",
"create_data_ExpandDatabase",
"proc_macro_span_shim",
]
"#]],
Expand Down Expand Up @@ -441,6 +442,7 @@ pub struct S {}
"parse_macro_expansion",
"expand_proc_macro",
"macro_arg",
"create_data_ExpandDatabase",
"proc_macro_span_shim",
]
"#]],
Expand Down
3 changes: 1 addition & 2 deletions crates/hir-def/src/test_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use triomphe::Arc;

use crate::{
Lookup, ModuleDefId, ModuleId,
db::DefDatabase,
expr_store::{Body, scope::ExprScopes},
nameres::{DefMap, ModuleSource, block_def_map, crate_def_map},
src::HasSource,
Expand Down Expand Up @@ -48,7 +47,7 @@ impl Default for TestDB {
crates_map: Default::default(),
nonce: Nonce::new(),
};
this.set_expand_proc_attr_macros_with_durability(true, Durability::HIGH);
crate::db::set_expand_proc_attr_macros(&mut this, true);
// This needs to be here otherwise `CrateGraphBuilder` panics.
set_all_crates_with_durability(&mut this, std::iter::empty(), Durability::HIGH);
_ = base_db::LibraryRoots::builder(Default::default())
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-expand/src/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//! state. Changes are transactional.
use base_db::{CrateGraphBuilder, FileChange, SourceRoot, salsa::Durability};
use span::FileId;
use triomphe::Arc;

use crate::{db::ExpandDatabase, proc_macro::ProcMacrosBuilder};

Expand All @@ -16,12 +15,13 @@ impl ChangeWithProcMacros {
pub fn apply(self, db: &mut impl ExpandDatabase) {
let crates_id_map = self.source_change.apply(db);
if let Some(proc_macros) = self.proc_macros {
let proc_macros = proc_macros.build(
proc_macros.build_in(
db,
Durability::HIGH,
crates_id_map
.as_ref()
.expect("cannot set proc macros without setting the crate graph too"),
);
db.set_proc_macros_with_durability(Arc::new(proc_macros), Durability::HIGH);
}
}

Expand Down
6 changes: 1 addition & 5 deletions crates/hir-expand/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
declarative::DeclarativeMacroExpander,
fixup::{self, SyntaxFixupUndoInfo},
hygiene::{span_with_call_site_ctxt, span_with_def_site_ctxt, span_with_mixed_site_ctxt},
proc_macro::{CrateProcMacros, CustomProcMacroExpander, ProcMacros},
proc_macro::{CrateProcMacros, CustomProcMacroExpander},
span_map::{ExpansionSpanMap, RealSpanMap, SpanMap},
tt,
};
Expand Down Expand Up @@ -50,10 +50,6 @@ pub enum TokenExpander<'db> {

#[query_group::query_group]
pub trait ExpandDatabase: SourceDatabase {
/// The proc macros. Do not use this! Use `proc_macros_for_crate()` instead.
#[salsa::input]
fn proc_macros(&self) -> Arc<ProcMacros>;

/// Incrementality query to prevent queries from directly depending on `ExpandDatabase::proc_macros`.
#[salsa::invoke(crate::proc_macro::proc_macros_for_crate)]
fn proc_macros_for_crate(&self, krate: Crate) -> Option<Arc<CrateProcMacros>>;
Expand Down
33 changes: 26 additions & 7 deletions crates/hir-expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{panic::RefUnwindSafe, sync};
use base_db::{Crate, CrateBuilderId, CratesIdMap, Env, ProcMacroLoadingError};
use intern::Symbol;
use rustc_hash::FxHashMap;
use salsa::{Durability, Setter};
use span::Span;
use triomphe::Arc;

Expand Down Expand Up @@ -83,14 +84,24 @@ impl ProcMacrosBuilder {
);
}

pub(crate) fn build(self, crates_id_map: &CratesIdMap) -> ProcMacros {
/// Builds [`ProcMacros`] and adds id to `db` with the provided `durability`.
pub(crate) fn build_in(
self,
db: &mut dyn ExpandDatabase,
durability: Durability,
crates_id_map: &CratesIdMap,
) {
let mut map = self
.0
.into_iter()
.map(|(krate, proc_macro)| (crates_id_map[&krate], proc_macro))
.collect::<FxHashMap<_, _>>();
map.shrink_to_fit();
ProcMacros(map)
ProcMacros::try_get(db)
.unwrap_or_else(|| ProcMacros::new(db, Default::default()))
.set_by_crate(db)
.with_durability(durability)
.to(map);
Comment on lines -93 to +104

@ada4a ada4a Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for this ugliness is twofold:

  • I can't just do ProcMacros::builder(map).durability(durability).new(db), because ProcMacros is marked as a singleton, and salsa complains (panics) if I create a new instance of it.
  • I can't just use ProcMacros::get, because some tests panic with "Option::unwrap called on None", which I assume means that ProcMacros is sometimes not yet set by the time we call this method.

Let me know if I should add this justification as a comment to the code maybe

View changes since the review

}
}

Expand All @@ -107,11 +118,19 @@ impl FromIterator<(CrateBuilderId, ProcMacroLoadResult)> for ProcMacrosBuilder {
#[derive(Debug, PartialEq, Eq)]
pub struct CrateProcMacros(StoredProcMacroLoadResult);

#[derive(Default, Debug)]
pub struct ProcMacros(FxHashMap<Crate, Arc<CrateProcMacros>>);
#[salsa::input(singleton, debug)]
pub struct ProcMacros {
#[returns(ref)]
pub by_crate: FxHashMap<Crate, Arc<CrateProcMacros>>,
}

impl ProcMacros {
fn get(&self, krate: Crate) -> Option<Arc<CrateProcMacros>> {
self.0.get(&krate).cloned()
pub fn init_default(db: &dyn ExpandDatabase, durability: Durability) {
_ = Self::builder(Default::default()).durability(durability).new(db);
}

fn get_for_crate(&self, db: &dyn ExpandDatabase, krate: Crate) -> Option<Arc<CrateProcMacros>> {
self.by_crate(db).get(&krate).cloned()
}
}

Expand Down Expand Up @@ -352,5 +371,5 @@ pub(crate) fn proc_macros_for_crate(
db: &dyn ExpandDatabase,
krate: Crate,
) -> Option<Arc<CrateProcMacros>> {
db.proc_macros().get(krate)
ProcMacros::get(db).get_for_crate(db, krate)
}
4 changes: 2 additions & 2 deletions crates/hir-ty/src/test_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use base_db::{
SourceRootId, SourceRootInput, all_crates, relevant_crates, set_all_crates_with_durability,
};

use hir_def::{ModuleId, db::DefDatabase, nameres::crate_def_map};
use hir_def::{ModuleId, nameres::crate_def_map};
use hir_expand::EditionedFileId;
use rustc_hash::FxHashMap;
use salsa::Durability;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl Default for TestDB {
crates_map: Default::default(),
nonce: Nonce::new(),
};
this.set_expand_proc_attr_macros_with_durability(true, Durability::HIGH);
hir_def::db::set_expand_proc_attr_macros(&mut this, true);
// This needs to be here otherwise `CrateGraphBuilder` panics.
set_all_crates_with_durability(&mut this, std::iter::empty(), Durability::HIGH);
_ = base_db::LibraryRoots::builder(Default::default())
Expand Down
2 changes: 1 addition & 1 deletion crates/hir/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
//! we didn't do that.
//!
//! But we need this for at least LRU caching at the query level.
pub use hir_def::db::DefDatabase;
pub use hir_def::db::{DefDatabase, set_expand_proc_attr_macros};
pub use hir_expand::db::ExpandDatabase;
pub use hir_ty::db::HirDatabase;
11 changes: 4 additions & 7 deletions crates/ide-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ use base_db::{
CrateGraphBuilder, CratesMap, FileSourceRootInput, FileText, Files, Nonce, SourceDatabase,
SourceRoot, SourceRootId, SourceRootInput, set_all_crates_with_durability,
};
use hir::{
FilePositionWrapper, FileRangeWrapper,
db::{DefDatabase, ExpandDatabase, HirDatabase},
};
use hir::{FilePositionWrapper, FileRangeWrapper, db::HirDatabase};
use triomphe::Arc;

use crate::line_index::LineIndex;
Expand Down Expand Up @@ -203,20 +200,20 @@ impl RootDatabase {
// This needs to be here otherwise `CrateGraphBuilder` will panic.
set_all_crates_with_durability(&mut db, std::iter::empty(), Durability::HIGH);
CrateGraphBuilder::default().set_in_db(&mut db);
db.set_proc_macros_with_durability(Default::default(), Durability::MEDIUM);
hir::ProcMacros::init_default(&db, Durability::MEDIUM);
_ = base_db::LibraryRoots::builder(Default::default())
.durability(Durability::MEDIUM)
.new(&db);
_ = base_db::LocalRoots::builder(Default::default())
.durability(Durability::MEDIUM)
.new(&db);
db.set_expand_proc_attr_macros_with_durability(false, Durability::HIGH);
hir::db::set_expand_proc_attr_macros(&mut db, false);
db.update_base_query_lru_capacities(lru_capacity);
db
}

pub fn enable_proc_attr_macros(&mut self) {
self.set_expand_proc_attr_macros_with_durability(true, Durability::HIGH);
hir::db::set_expand_proc_attr_macros(self, true);
}

pub fn update_base_query_lru_capacities(&mut self, _lru_capacity: Option<u16>) {
Expand Down
Loading
Loading