Skip to content

Commit

Permalink
Auto merge of #16143 - Veykril:base-db-no-proc-macros, r=lnicola
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Dec 18, 2023
2 parents 9a82f8c + 3562030 commit cfc959d
Show file tree
Hide file tree
Showing 50 changed files with 256 additions and 187 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 5 additions & 13 deletions crates/base-db/src/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ use salsa::Durability;
use triomphe::Arc;
use vfs::FileId;

use crate::{CrateGraph, ProcMacros, SourceDatabaseExt, SourceRoot, SourceRootId};
use crate::{CrateGraph, SourceDatabaseExt, SourceRoot, SourceRootId};

/// Encapsulate a bunch of raw `.set` calls on the database.
#[derive(Default)]
pub struct Change {
pub struct FileChange {
pub roots: Option<Vec<SourceRoot>>,
pub files_changed: Vec<(FileId, Option<Arc<str>>)>,
pub crate_graph: Option<CrateGraph>,
pub proc_macros: Option<ProcMacros>,
}

impl fmt::Debug for Change {
impl fmt::Debug for FileChange {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = fmt.debug_struct("Change");
if let Some(roots) = &self.roots {
Expand All @@ -34,9 +33,9 @@ impl fmt::Debug for Change {
}
}

impl Change {
impl FileChange {
pub fn new() -> Self {
Change::default()
FileChange::default()
}

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

pub fn set_proc_macros(&mut self, proc_macros: ProcMacros) {
self.proc_macros = Some(proc_macros);
}

pub fn apply(self, db: &mut dyn SourceDatabaseExt) {
let _p = profile::span("RootDatabase::apply_change");
if let Some(roots) = self.roots {
Expand All @@ -79,9 +74,6 @@ impl Change {
if let Some(crate_graph) = self.crate_graph {
db.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH);
}
if let Some(proc_macros) = self.proc_macros {
db.set_proc_macros_with_durability(Arc::new(proc_macros), Durability::HIGH);
}
}
}

Expand Down
47 changes: 1 addition & 46 deletions crates/base-db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how
//! actual IO is done and lowered to input.

use std::{fmt, mem, ops, panic::RefUnwindSafe, str::FromStr, sync};
use std::{fmt, mem, ops, str::FromStr};

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

use crate::span::SpanData;

// Map from crate id to the name of the crate and path of the proc-macro. If the value is `None`,
// then the crate for the proc-macro hasn't been build yet as the build data is missing.
pub type ProcMacroPaths = FxHashMap<CrateId, Result<(Option<String>, AbsPathBuf), String>>;
pub type ProcMacros = FxHashMap<CrateId, ProcMacroLoadResult>;

/// Files are grouped into source roots. A source root is a directory on the
/// file systems which is watched for changes. Typically it corresponds to a
/// Rust crate. Source roots *might* be nested: in this case, a file belongs to
Expand Down Expand Up @@ -242,49 +238,8 @@ impl CrateDisplayName {
CrateDisplayName { crate_name, canonical_name }
}
}

// FIXME: These should not be defined in here? Why does base db know about proc-macros
// ProcMacroKind is used in [`fixture`], but that module probably shouldn't be in this crate either.

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct ProcMacroId(pub u32);

#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub enum ProcMacroKind {
CustomDerive,
FuncLike,
Attr,
}

pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe {
fn expand(
&self,
subtree: &tt::Subtree<SpanData>,
attrs: Option<&tt::Subtree<SpanData>>,
env: &Env,
def_site: SpanData,
call_site: SpanData,
mixed_site: SpanData,
) -> Result<tt::Subtree<SpanData>, ProcMacroExpansionError>;
}

#[derive(Debug)]
pub enum ProcMacroExpansionError {
Panic(String),
/// Things like "proc macro server was killed by OOM".
System(String),
}

pub type ProcMacroLoadResult = Result<Vec<ProcMacro>, String>;
pub type TargetLayoutLoadResult = Result<Arc<str>, Arc<str>>;

#[derive(Debug, Clone)]
pub struct ProcMacro {
pub name: SmolStr,
pub kind: ProcMacroKind,
pub expander: sync::Arc<dyn ProcMacroExpander>,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ReleaseChannel {
Stable,
Expand Down
12 changes: 3 additions & 9 deletions crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

mod input;
mod change;
pub mod fixture;
pub mod span;

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

pub use crate::{
change::Change,
change::FileChange,
input::{
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
DependencyKind, Edition, Env, LangCrateOrigin, ProcMacro, ProcMacroExpander,
ProcMacroExpansionError, ProcMacroId, ProcMacroKind, ProcMacroLoadResult, ProcMacroPaths,
ProcMacros, ReleaseChannel, SourceRoot, SourceRootId, TargetLayoutLoadResult,
DependencyKind, Edition, Env, LangCrateOrigin, ProcMacroPaths, ReleaseChannel, SourceRoot,
SourceRootId, TargetLayoutLoadResult,
},
};
pub use salsa::{self, Cancelled};
Expand Down Expand Up @@ -74,10 +72,6 @@ pub trait SourceDatabase: FileLoader + std::fmt::Debug {
/// The crate graph.
#[salsa::input]
fn crate_graph(&self) -> Arc<CrateGraph>;

/// The proc macros.
#[salsa::input]
fn proc_macros(&self) -> Arc<ProcMacros>;
}

fn parse(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/body/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope

#[cfg(test)]
mod tests {
use base_db::{fixture::WithFixture, FileId, SourceDatabase};
use hir_expand::{name::AsName, InFile};
use base_db::{FileId, SourceDatabase};
use hir_expand::{fixture::WithFixture, name::AsName, InFile};
use syntax::{algo::find_node_at_offset, ast, AstNode};
use test_utils::{assert_eq_text, extract_offset};

Expand Down
3 changes: 2 additions & 1 deletion crates/hir-def/src/body/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
mod block;

use base_db::{fixture::WithFixture, SourceDatabase};
use base_db::SourceDatabase;
use expect_test::{expect, Expect};
use hir_expand::fixture::WithFixture;

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

Expand Down
3 changes: 1 addition & 2 deletions crates/hir-def/src/find_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,7 @@ fn find_local_import_locations(

#[cfg(test)]
mod tests {
use base_db::fixture::WithFixture;
use hir_expand::db::ExpandDatabase;
use hir_expand::{db::ExpandDatabase, fixture::WithFixture};
use syntax::ast::AstNode;

use crate::test_db::TestDB;
Expand Down
3 changes: 2 additions & 1 deletion crates/hir-def/src/import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,9 @@ pub fn search_dependencies(

#[cfg(test)]
mod tests {
use base_db::{fixture::WithFixture, SourceDatabase, Upcast};
use base_db::{SourceDatabase, Upcast};
use expect_test::{expect, Expect};
use hir_expand::fixture::WithFixture;

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

Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/item_tree/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use base_db::fixture::WithFixture;
use expect_test::{expect, Expect};
use hir_expand::fixture::WithFixture;

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

Expand Down
6 changes: 3 additions & 3 deletions crates/hir-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use std::{
panic::{RefUnwindSafe, UnwindSafe},
};

use base_db::{impl_intern_key, salsa, span::SyntaxContextId, CrateId, ProcMacroKind};
use base_db::{impl_intern_key, salsa, span::SyntaxContextId, CrateId};
use hir_expand::{
ast_id_map::{AstIdNode, FileAstId},
attrs::{Attr, AttrId, AttrInput},
Expand All @@ -73,7 +73,7 @@ use hir_expand::{
db::ExpandDatabase,
eager::expand_eager_macro_input,
name::Name,
proc_macro::ProcMacroExpander,
proc_macro::{CustomProcMacroExpander, ProcMacroKind},
AstId, ExpandError, ExpandResult, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind,
MacroDefId, MacroDefKind,
};
Expand Down Expand Up @@ -400,7 +400,7 @@ pub struct ProcMacroId(salsa::InternId);
pub struct ProcMacroLoc {
pub container: CrateRootModuleId,
pub id: ItemTreeId<Function>,
pub expander: ProcMacroExpander,
pub expander: CustomProcMacroExpander,
pub kind: ProcMacroKind,
}
impl_intern!(ProcMacroId, ProcMacroLoc, intern_proc_macro, lookup_intern_proc_macro);
Expand Down
16 changes: 11 additions & 5 deletions crates/hir-def/src/macro_expansion_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ mod proc_macros;

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

use base_db::{fixture::WithFixture, span::SpanData, ProcMacro, SourceDatabase};
use base_db::{span::SpanData, SourceDatabase};
use expect_test::Expect;
use hir_expand::{db::ExpandDatabase, span::SpanMapRef, InFile, MacroFileId, MacroFileIdExt};
use hir_expand::{
db::ExpandDatabase,
fixture::WithFixture,
proc_macro::{ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind},
span::SpanMapRef,
InFile, MacroFileId, MacroFileIdExt,
};
use stdx::format_to;
use syntax::{
ast::{self, edit::IndentLevel},
Expand Down Expand Up @@ -50,7 +56,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
.into(),
ProcMacro {
name: "identity_when_valid".into(),
kind: base_db::ProcMacroKind::Attr,
kind: ProcMacroKind::Attr,
expander: sync::Arc::new(IdentityWhenValidProcMacroExpander),
},
)];
Expand Down Expand Up @@ -307,7 +313,7 @@ fn pretty_print_macro_expansion(
// compile errors.
#[derive(Debug)]
struct IdentityWhenValidProcMacroExpander;
impl base_db::ProcMacroExpander for IdentityWhenValidProcMacroExpander {
impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
fn expand(
&self,
subtree: &Subtree,
Expand All @@ -316,7 +322,7 @@ impl base_db::ProcMacroExpander for IdentityWhenValidProcMacroExpander {
_: SpanData,
_: SpanData,
_: SpanData,
) -> Result<Subtree, base_db::ProcMacroExpansionError> {
) -> Result<Subtree, ProcMacroExpansionError> {
let (parse, _) =
::mbe::token_tree_to_syntax_node(subtree, ::mbe::TopEntryPoint::MacroItems);
if parse.errors().is_empty() {
Expand Down
7 changes: 5 additions & 2 deletions crates/hir-def/src/nameres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ mod tests;

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

use base_db::{CrateId, Edition, FileId, ProcMacroKind};
use hir_expand::{ast_id_map::FileAstId, name::Name, HirFileId, InFile, MacroCallId, MacroDefId};
use base_db::{CrateId, Edition, FileId};
use hir_expand::{
ast_id_map::FileAstId, name::Name, proc_macro::ProcMacroKind, HirFileId, InFile, MacroCallId,
MacroDefId,
};
use itertools::Itertools;
use la_arena::Arena;
use profile::Count;
Expand Down
17 changes: 12 additions & 5 deletions crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use hir_expand::{
builtin_derive_macro::find_builtin_derive,
builtin_fn_macro::find_builtin_macro,
name::{name, AsName, Name},
proc_macro::ProcMacroExpander,
proc_macro::CustomProcMacroExpander,
ExpandResult, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind, MacroCallLoc,
MacroDefId, MacroDefKind,
};
Expand Down Expand Up @@ -95,7 +95,12 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, def_map: DefMap, tree_id: TreeI
ctx: SyntaxContextId::ROOT,
},
};
(name.as_name(), ProcMacroExpander::new(base_db::ProcMacroId(idx as u32)))
(
name.as_name(),
CustomProcMacroExpander::new(hir_expand::proc_macro::ProcMacroId(
idx as u32,
)),
)
})
.collect())
}
Expand Down Expand Up @@ -253,7 +258,7 @@ struct DefCollector<'a> {
/// built by the build system, and is the list of proc. macros we can actually expand. It is
/// empty when proc. macro support is disabled (in which case we still do name resolution for
/// them).
proc_macros: Result<Vec<(Name, ProcMacroExpander)>, Box<str>>,
proc_macros: Result<Vec<(Name, CustomProcMacroExpander)>, Box<str>>,
is_proc_macro: bool,
from_glob_import: PerNsGlobImports,
/// If we fail to resolve an attribute on a `ModItem`, we fall back to ignoring the attribute.
Expand Down Expand Up @@ -603,7 +608,7 @@ impl DefCollector<'_> {
let (expander, kind) =
match self.proc_macros.as_ref().map(|it| it.iter().find(|(n, _)| n == &def.name)) {
Ok(Some(&(_, expander))) => (expander, kind),
_ => (ProcMacroExpander::dummy(), kind),
_ => (CustomProcMacroExpander::dummy(), kind),
};

let proc_macro_id =
Expand Down Expand Up @@ -2363,8 +2368,10 @@ impl ModCollector<'_, '_> {

#[cfg(test)]
mod tests {
use base_db::SourceDatabase;
use hir_expand::fixture::WithFixture;

use crate::{db::DefDatabase, test_db::TestDB};
use base_db::{fixture::WithFixture, SourceDatabase};

use super::*;

Expand Down
10 changes: 6 additions & 4 deletions crates/hir-def/src/nameres/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ pub enum ProcMacroKind {
}

impl ProcMacroKind {
pub(super) fn to_basedb_kind(&self) -> base_db::ProcMacroKind {
pub(super) fn to_basedb_kind(&self) -> hir_expand::proc_macro::ProcMacroKind {
match self {
ProcMacroKind::CustomDerive { .. } => base_db::ProcMacroKind::CustomDerive,
ProcMacroKind::FnLike => base_db::ProcMacroKind::FuncLike,
ProcMacroKind::Attr => base_db::ProcMacroKind::Attr,
ProcMacroKind::CustomDerive { .. } => {
hir_expand::proc_macro::ProcMacroKind::CustomDerive
}
ProcMacroKind::FnLike => hir_expand::proc_macro::ProcMacroKind::FuncLike,
ProcMacroKind::Attr => hir_expand::proc_macro::ProcMacroKind::Attr,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/hir-def/src/nameres/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ mod macros;
mod mod_resolution;
mod primitives;

use base_db::{fixture::WithFixture, SourceDatabase};
use base_db::SourceDatabase;
use expect_test::{expect, Expect};
use hir_expand::fixture::WithFixture;
use triomphe::Arc;

use crate::{db::DefDatabase, nameres::DefMap, test_db::TestDB};
Expand Down
Loading

0 comments on commit cfc959d

Please sign in to comment.