Skip to content

Commit cfc959d

Browse files
committed
Auto merge of rust-lang#16143 - Veykril:base-db-no-proc-macros, r=lnicola
internal: Move proc-macro knowledge out of base-db into hir-expand It does not make much sense to me to have that live in base-db, additionally, it kind of conflicts with moving span things out into a separate crate
2 parents 9a82f8c + 3562030 commit cfc959d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+256
-187
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/base-db/src/change.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ use salsa::Durability;
77
use triomphe::Arc;
88
use vfs::FileId;
99

10-
use crate::{CrateGraph, ProcMacros, SourceDatabaseExt, SourceRoot, SourceRootId};
10+
use crate::{CrateGraph, SourceDatabaseExt, SourceRoot, SourceRootId};
1111

1212
/// Encapsulate a bunch of raw `.set` calls on the database.
1313
#[derive(Default)]
14-
pub struct Change {
14+
pub struct FileChange {
1515
pub roots: Option<Vec<SourceRoot>>,
1616
pub files_changed: Vec<(FileId, Option<Arc<str>>)>,
1717
pub crate_graph: Option<CrateGraph>,
18-
pub proc_macros: Option<ProcMacros>,
1918
}
2019

21-
impl fmt::Debug for Change {
20+
impl fmt::Debug for FileChange {
2221
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2322
let mut d = fmt.debug_struct("Change");
2423
if let Some(roots) = &self.roots {
@@ -34,9 +33,9 @@ impl fmt::Debug for Change {
3433
}
3534
}
3635

37-
impl Change {
36+
impl FileChange {
3837
pub fn new() -> Self {
39-
Change::default()
38+
FileChange::default()
4039
}
4140

4241
pub fn set_roots(&mut self, roots: Vec<SourceRoot>) {
@@ -51,10 +50,6 @@ impl Change {
5150
self.crate_graph = Some(graph);
5251
}
5352

54-
pub fn set_proc_macros(&mut self, proc_macros: ProcMacros) {
55-
self.proc_macros = Some(proc_macros);
56-
}
57-
5853
pub fn apply(self, db: &mut dyn SourceDatabaseExt) {
5954
let _p = profile::span("RootDatabase::apply_change");
6055
if let Some(roots) = self.roots {
@@ -79,9 +74,6 @@ impl Change {
7974
if let Some(crate_graph) = self.crate_graph {
8075
db.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH);
8176
}
82-
if let Some(proc_macros) = self.proc_macros {
83-
db.set_proc_macros_with_durability(Arc::new(proc_macros), Durability::HIGH);
84-
}
8577
}
8678
}
8779

crates/base-db/src/input.rs

+1-46
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how
77
//! actual IO is done and lowered to input.
88
9-
use std::{fmt, mem, ops, panic::RefUnwindSafe, str::FromStr, sync};
9+
use std::{fmt, mem, ops, str::FromStr};
1010

1111
use cfg::CfgOptions;
1212
use la_arena::{Arena, Idx};
@@ -15,13 +15,9 @@ use syntax::SmolStr;
1515
use triomphe::Arc;
1616
use vfs::{file_set::FileSet, AbsPathBuf, AnchoredPath, FileId, VfsPath};
1717

18-
use crate::span::SpanData;
19-
2018
// Map from crate id to the name of the crate and path of the proc-macro. If the value is `None`,
2119
// then the crate for the proc-macro hasn't been build yet as the build data is missing.
2220
pub type ProcMacroPaths = FxHashMap<CrateId, Result<(Option<String>, AbsPathBuf), String>>;
23-
pub type ProcMacros = FxHashMap<CrateId, ProcMacroLoadResult>;
24-
2521
/// Files are grouped into source roots. A source root is a directory on the
2622
/// file systems which is watched for changes. Typically it corresponds to a
2723
/// Rust crate. Source roots *might* be nested: in this case, a file belongs to
@@ -242,49 +238,8 @@ impl CrateDisplayName {
242238
CrateDisplayName { crate_name, canonical_name }
243239
}
244240
}
245-
246-
// FIXME: These should not be defined in here? Why does base db know about proc-macros
247-
// ProcMacroKind is used in [`fixture`], but that module probably shouldn't be in this crate either.
248-
249-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
250-
pub struct ProcMacroId(pub u32);
251-
252-
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
253-
pub enum ProcMacroKind {
254-
CustomDerive,
255-
FuncLike,
256-
Attr,
257-
}
258-
259-
pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe {
260-
fn expand(
261-
&self,
262-
subtree: &tt::Subtree<SpanData>,
263-
attrs: Option<&tt::Subtree<SpanData>>,
264-
env: &Env,
265-
def_site: SpanData,
266-
call_site: SpanData,
267-
mixed_site: SpanData,
268-
) -> Result<tt::Subtree<SpanData>, ProcMacroExpansionError>;
269-
}
270-
271-
#[derive(Debug)]
272-
pub enum ProcMacroExpansionError {
273-
Panic(String),
274-
/// Things like "proc macro server was killed by OOM".
275-
System(String),
276-
}
277-
278-
pub type ProcMacroLoadResult = Result<Vec<ProcMacro>, String>;
279241
pub type TargetLayoutLoadResult = Result<Arc<str>, Arc<str>>;
280242

281-
#[derive(Debug, Clone)]
282-
pub struct ProcMacro {
283-
pub name: SmolStr,
284-
pub kind: ProcMacroKind,
285-
pub expander: sync::Arc<dyn ProcMacroExpander>,
286-
}
287-
288243
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
289244
pub enum ReleaseChannel {
290245
Stable,

crates/base-db/src/lib.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
mod input;
66
mod change;
7-
pub mod fixture;
87
pub mod span;
98

109
use std::panic;
@@ -14,12 +13,11 @@ use syntax::{ast, Parse, SourceFile, TextRange, TextSize};
1413
use triomphe::Arc;
1514

1615
pub use crate::{
17-
change::Change,
16+
change::FileChange,
1817
input::{
1918
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
20-
DependencyKind, Edition, Env, LangCrateOrigin, ProcMacro, ProcMacroExpander,
21-
ProcMacroExpansionError, ProcMacroId, ProcMacroKind, ProcMacroLoadResult, ProcMacroPaths,
22-
ProcMacros, ReleaseChannel, SourceRoot, SourceRootId, TargetLayoutLoadResult,
19+
DependencyKind, Edition, Env, LangCrateOrigin, ProcMacroPaths, ReleaseChannel, SourceRoot,
20+
SourceRootId, TargetLayoutLoadResult,
2321
},
2422
};
2523
pub use salsa::{self, Cancelled};
@@ -74,10 +72,6 @@ pub trait SourceDatabase: FileLoader + std::fmt::Debug {
7472
/// The crate graph.
7573
#[salsa::input]
7674
fn crate_graph(&self) -> Arc<CrateGraph>;
77-
78-
/// The proc macros.
79-
#[salsa::input]
80-
fn proc_macros(&self) -> Arc<ProcMacros>;
8175
}
8276

8377
fn parse(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {

crates/hir-def/src/body/scope.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope
267267

268268
#[cfg(test)]
269269
mod tests {
270-
use base_db::{fixture::WithFixture, FileId, SourceDatabase};
271-
use hir_expand::{name::AsName, InFile};
270+
use base_db::{FileId, SourceDatabase};
271+
use hir_expand::{fixture::WithFixture, name::AsName, InFile};
272272
use syntax::{algo::find_node_at_offset, ast, AstNode};
273273
use test_utils::{assert_eq_text, extract_offset};
274274

crates/hir-def/src/body/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
mod block;
22

3-
use base_db::{fixture::WithFixture, SourceDatabase};
3+
use base_db::SourceDatabase;
44
use expect_test::{expect, Expect};
5+
use hir_expand::fixture::WithFixture;
56

67
use crate::{test_db::TestDB, ModuleDefId};
78

crates/hir-def/src/find_path.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,7 @@ fn find_local_import_locations(
585585

586586
#[cfg(test)]
587587
mod tests {
588-
use base_db::fixture::WithFixture;
589-
use hir_expand::db::ExpandDatabase;
588+
use hir_expand::{db::ExpandDatabase, fixture::WithFixture};
590589
use syntax::ast::AstNode;
591590

592591
use crate::test_db::TestDB;

crates/hir-def/src/import_map.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,9 @@ pub fn search_dependencies(
473473

474474
#[cfg(test)]
475475
mod tests {
476-
use base_db::{fixture::WithFixture, SourceDatabase, Upcast};
476+
use base_db::{SourceDatabase, Upcast};
477477
use expect_test::{expect, Expect};
478+
use hir_expand::fixture::WithFixture;
478479

479480
use crate::{db::DefDatabase, test_db::TestDB, ItemContainerId, Lookup};
480481

crates/hir-def/src/item_tree/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use base_db::fixture::WithFixture;
21
use expect_test::{expect, Expect};
2+
use hir_expand::fixture::WithFixture;
33

44
use crate::{db::DefDatabase, test_db::TestDB};
55

crates/hir-def/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use std::{
6363
panic::{RefUnwindSafe, UnwindSafe},
6464
};
6565

66-
use base_db::{impl_intern_key, salsa, span::SyntaxContextId, CrateId, ProcMacroKind};
66+
use base_db::{impl_intern_key, salsa, span::SyntaxContextId, CrateId};
6767
use hir_expand::{
6868
ast_id_map::{AstIdNode, FileAstId},
6969
attrs::{Attr, AttrId, AttrInput},
@@ -73,7 +73,7 @@ use hir_expand::{
7373
db::ExpandDatabase,
7474
eager::expand_eager_macro_input,
7575
name::Name,
76-
proc_macro::ProcMacroExpander,
76+
proc_macro::{CustomProcMacroExpander, ProcMacroKind},
7777
AstId, ExpandError, ExpandResult, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind,
7878
MacroDefId, MacroDefKind,
7979
};
@@ -400,7 +400,7 @@ pub struct ProcMacroId(salsa::InternId);
400400
pub struct ProcMacroLoc {
401401
pub container: CrateRootModuleId,
402402
pub id: ItemTreeId<Function>,
403-
pub expander: ProcMacroExpander,
403+
pub expander: CustomProcMacroExpander,
404404
pub kind: ProcMacroKind,
405405
}
406406
impl_intern!(ProcMacroId, ProcMacroLoc, intern_proc_macro, lookup_intern_proc_macro);

crates/hir-def/src/macro_expansion_tests/mod.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ mod proc_macros;
1616

1717
use std::{iter, ops::Range, sync};
1818

19-
use base_db::{fixture::WithFixture, span::SpanData, ProcMacro, SourceDatabase};
19+
use base_db::{span::SpanData, SourceDatabase};
2020
use expect_test::Expect;
21-
use hir_expand::{db::ExpandDatabase, span::SpanMapRef, InFile, MacroFileId, MacroFileIdExt};
21+
use hir_expand::{
22+
db::ExpandDatabase,
23+
fixture::WithFixture,
24+
proc_macro::{ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind},
25+
span::SpanMapRef,
26+
InFile, MacroFileId, MacroFileIdExt,
27+
};
2228
use stdx::format_to;
2329
use syntax::{
2430
ast::{self, edit::IndentLevel},
@@ -50,7 +56,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
5056
.into(),
5157
ProcMacro {
5258
name: "identity_when_valid".into(),
53-
kind: base_db::ProcMacroKind::Attr,
59+
kind: ProcMacroKind::Attr,
5460
expander: sync::Arc::new(IdentityWhenValidProcMacroExpander),
5561
},
5662
)];
@@ -307,7 +313,7 @@ fn pretty_print_macro_expansion(
307313
// compile errors.
308314
#[derive(Debug)]
309315
struct IdentityWhenValidProcMacroExpander;
310-
impl base_db::ProcMacroExpander for IdentityWhenValidProcMacroExpander {
316+
impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
311317
fn expand(
312318
&self,
313319
subtree: &Subtree,
@@ -316,7 +322,7 @@ impl base_db::ProcMacroExpander for IdentityWhenValidProcMacroExpander {
316322
_: SpanData,
317323
_: SpanData,
318324
_: SpanData,
319-
) -> Result<Subtree, base_db::ProcMacroExpansionError> {
325+
) -> Result<Subtree, ProcMacroExpansionError> {
320326
let (parse, _) =
321327
::mbe::token_tree_to_syntax_node(subtree, ::mbe::TopEntryPoint::MacroItems);
322328
if parse.errors().is_empty() {

crates/hir-def/src/nameres.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ mod tests;
5959

6060
use std::{cmp::Ord, ops::Deref};
6161

62-
use base_db::{CrateId, Edition, FileId, ProcMacroKind};
63-
use hir_expand::{ast_id_map::FileAstId, name::Name, HirFileId, InFile, MacroCallId, MacroDefId};
62+
use base_db::{CrateId, Edition, FileId};
63+
use hir_expand::{
64+
ast_id_map::FileAstId, name::Name, proc_macro::ProcMacroKind, HirFileId, InFile, MacroCallId,
65+
MacroDefId,
66+
};
6467
use itertools::Itertools;
6568
use la_arena::Arena;
6669
use profile::Count;

crates/hir-def/src/nameres/collector.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use hir_expand::{
1515
builtin_derive_macro::find_builtin_derive,
1616
builtin_fn_macro::find_builtin_macro,
1717
name::{name, AsName, Name},
18-
proc_macro::ProcMacroExpander,
18+
proc_macro::CustomProcMacroExpander,
1919
ExpandResult, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroCallLoc,
2020
MacroDefId, MacroDefKind,
2121
};
@@ -95,7 +95,12 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, def_map: DefMap, tree_id: TreeI
9595
ctx: SyntaxContextId::ROOT,
9696
},
9797
};
98-
(name.as_name(), ProcMacroExpander::new(base_db::ProcMacroId(idx as u32)))
98+
(
99+
name.as_name(),
100+
CustomProcMacroExpander::new(hir_expand::proc_macro::ProcMacroId(
101+
idx as u32,
102+
)),
103+
)
99104
})
100105
.collect())
101106
}
@@ -253,7 +258,7 @@ struct DefCollector<'a> {
253258
/// built by the build system, and is the list of proc. macros we can actually expand. It is
254259
/// empty when proc. macro support is disabled (in which case we still do name resolution for
255260
/// them).
256-
proc_macros: Result<Vec<(Name, ProcMacroExpander)>, Box<str>>,
261+
proc_macros: Result<Vec<(Name, CustomProcMacroExpander)>, Box<str>>,
257262
is_proc_macro: bool,
258263
from_glob_import: PerNsGlobImports,
259264
/// If we fail to resolve an attribute on a `ModItem`, we fall back to ignoring the attribute.
@@ -603,7 +608,7 @@ impl DefCollector<'_> {
603608
let (expander, kind) =
604609
match self.proc_macros.as_ref().map(|it| it.iter().find(|(n, _)| n == &def.name)) {
605610
Ok(Some(&(_, expander))) => (expander, kind),
606-
_ => (ProcMacroExpander::dummy(), kind),
611+
_ => (CustomProcMacroExpander::dummy(), kind),
607612
};
608613

609614
let proc_macro_id =
@@ -2363,8 +2368,10 @@ impl ModCollector<'_, '_> {
23632368

23642369
#[cfg(test)]
23652370
mod tests {
2371+
use base_db::SourceDatabase;
2372+
use hir_expand::fixture::WithFixture;
2373+
23662374
use crate::{db::DefDatabase, test_db::TestDB};
2367-
use base_db::{fixture::WithFixture, SourceDatabase};
23682375

23692376
use super::*;
23702377

crates/hir-def/src/nameres/proc_macro.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ pub enum ProcMacroKind {
1919
}
2020

2121
impl ProcMacroKind {
22-
pub(super) fn to_basedb_kind(&self) -> base_db::ProcMacroKind {
22+
pub(super) fn to_basedb_kind(&self) -> hir_expand::proc_macro::ProcMacroKind {
2323
match self {
24-
ProcMacroKind::CustomDerive { .. } => base_db::ProcMacroKind::CustomDerive,
25-
ProcMacroKind::FnLike => base_db::ProcMacroKind::FuncLike,
26-
ProcMacroKind::Attr => base_db::ProcMacroKind::Attr,
24+
ProcMacroKind::CustomDerive { .. } => {
25+
hir_expand::proc_macro::ProcMacroKind::CustomDerive
26+
}
27+
ProcMacroKind::FnLike => hir_expand::proc_macro::ProcMacroKind::FuncLike,
28+
ProcMacroKind::Attr => hir_expand::proc_macro::ProcMacroKind::Attr,
2729
}
2830
}
2931
}

crates/hir-def/src/nameres/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ mod macros;
44
mod mod_resolution;
55
mod primitives;
66

7-
use base_db::{fixture::WithFixture, SourceDatabase};
7+
use base_db::SourceDatabase;
88
use expect_test::{expect, Expect};
9+
use hir_expand::fixture::WithFixture;
910
use triomphe::Arc;
1011

1112
use crate::{db::DefDatabase, nameres::DefMap, test_db::TestDB};

0 commit comments

Comments
 (0)