Skip to content

feat: Virtual macro files #19130

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 2 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
1 change: 1 addition & 0 deletions Cargo.lock

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

11 changes: 11 additions & 0 deletions crates/base-db/src/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use std::fmt;

use rustc_hash::FxHashSet;
use salsa::Durability;
use triomphe::Arc;
use vfs::FileId;
Expand Down Expand Up @@ -49,15 +50,25 @@ impl FileChange {
pub fn apply(self, db: &mut dyn RootQueryDb) -> Option<CratesIdMap> {
let _p = tracing::info_span!("FileChange::apply").entered();
if let Some(roots) = self.roots {
let mut local_roots = FxHashSet::default();
let mut library_roots = FxHashSet::default();
for (idx, root) in roots.into_iter().enumerate() {
let root_id = SourceRootId(idx as u32);
if root.is_library {
library_roots.insert(root_id);
} else {
local_roots.insert(root_id);
}

let durability = source_root_durability(&root);
for file_id in root.iter() {
db.set_file_source_root_with_durability(file_id, root_id, durability);
}

db.set_source_root_with_durability(root_id, Arc::new(root), durability);
}
db.set_local_roots_with_durability(Arc::new(local_roots), Durability::MEDIUM);
db.set_library_roots_with_durability(Arc::new(library_roots), Durability::MEDIUM);
}

for (file_id, text) in self.files_changed {
Expand Down
10 changes: 10 additions & 0 deletions crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ pub trait RootQueryDb: SourceDatabase + salsa::Database {
#[salsa::invoke(input::transitive_rev_deps)]
#[salsa::transparent]
fn transitive_rev_deps(&self, of: Crate) -> FxHashSet<Crate>;

/// The set of "local" (that is, from the current workspace) roots.
/// Files in local roots are assumed to change frequently.
#[salsa::input]
fn local_roots(&self) -> Arc<FxHashSet<SourceRootId>>;

/// The set of roots for crates.io libraries.
/// Files in libraries are assumed to never change.
#[salsa::input]
fn library_roots(&self) -> Arc<FxHashSet<SourceRootId>>;
}

pub fn transitive_deps(db: &dyn SourceDatabase, crate_id: Crate) -> FxHashSet<Crate> {
Expand Down
17 changes: 17 additions & 0 deletions crates/hir-def/src/nameres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,23 @@ impl DefMap {
.map(|(id, _data)| id)
}

pub fn inline_modules_for_macro_file(
&self,
file_id: MacroCallId,
) -> impl Iterator<Item = LocalModuleId> + '_ {
self.modules
.iter()
.filter(move |(_id, data)| {
(match data.origin {
ModuleOrigin::Inline { definition_tree_id, .. } => {
definition_tree_id.file_id().macro_file()
}
_ => None,
}) == Some(file_id)
})
.map(|(id, _data)| id)
}

pub fn modules(&self) -> impl Iterator<Item = (LocalModuleId, &ModuleData)> + '_ {
self.modules.iter()
}
Expand Down
3 changes: 2 additions & 1 deletion crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,7 @@ intern::impl_internable!(ModPath, attrs::AttrInput);

#[salsa_macros::interned(no_lifetime, debug, revisions = usize::MAX)]
#[doc(alias = "MacroFileId")]
#[derive(PartialOrd, Ord)]
pub struct MacroCallId {
pub loc: MacroCallLoc,
}
Expand All @@ -1086,7 +1087,7 @@ impl From<MacroCallId> for span::MacroCallId {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, salsa_macros::Supertype)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, salsa_macros::Supertype)]
pub enum HirFileId {
FileId(EditionedFileId),
MacroFile(MacroCallId),
Expand Down
1 change: 1 addition & 0 deletions crates/hir-expand/src/prettify_macro_expansion_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn prettify_macro_expansion(
let span_offset = syn.text_range().start();
let target_crate = target_crate_id.data(db);
let mut syntax_ctx_id_to_dollar_crate_replacement = FxHashMap::default();

syntax_bridge::prettify_macro_expansion::prettify_macro_expansion(
syn,
&mut |dollar_crate| {
Expand Down
40 changes: 33 additions & 7 deletions crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,18 +284,25 @@ impl<DB: HirDatabase + ?Sized> Semantics<'_, DB> {
self.imp.resolve_variant(record_lit).map(VariantDef::from)
}

pub fn file_to_module_def(&self, file: impl Into<FileId>) -> Option<Module> {
self.imp.file_to_module_defs(file.into()).next()
pub fn file_to_module_def(&self, file: EditionedFileId) -> Option<Module> {
self.imp.ed_file_to_module_defs(file).next()
}

pub fn file_to_module_defs(&self, file: impl Into<FileId>) -> impl Iterator<Item = Module> {
self.imp.file_to_module_defs(file.into())
pub fn file_to_module_def2(&self, file: FileId) -> Option<Module> {
self.imp.file_to_module_defs(file).next()
}

pub fn hir_file_to_module_def(&self, file: impl Into<HirFileId>) -> Option<Module> {
self.imp.hir_file_to_module_defs(file.into()).next()
}

pub fn file_to_module_defs(
&self,
file: impl Into<EditionedFileId>,
) -> impl Iterator<Item = Module> {
self.imp.ed_file_to_module_defs(file.into())
}

pub fn hir_file_to_module_defs(
&self,
file: impl Into<HirFileId>,
Expand Down Expand Up @@ -380,6 +387,19 @@ impl<'db> SemanticsImpl<'db> {
}
}

/// If not crate is found for the file, try to return the last crate in topological order.
pub fn first_crate_hir(&self, file: HirFileId) -> Option<Crate> {
match file {
HirFileId::FileId(editioned_file_id) => {
self.first_crate(editioned_file_id.file_id(self.db))
}
HirFileId::MacroFile(macro_call_id) => {
let macro_call = self.db.lookup_intern_macro_call(macro_call_id);
Some(macro_call.krate.into())
}
}
}

pub fn attach_first_edition(&self, file: FileId) -> Option<EditionedFileId> {
Some(EditionedFileId::new(
self.db,
Expand Down Expand Up @@ -412,6 +432,7 @@ impl<'db> SemanticsImpl<'db> {
HirFileId::FileId(file_id) => {
let module = self.file_to_module_defs(file_id.file_id(self.db)).next()?;
let def_map = crate_def_map(self.db, module.krate().id);

match def_map[module.id.local_id].origin {
ModuleOrigin::CrateRoot { .. } => None,
ModuleOrigin::File { declaration, declaration_tree_id, .. } => {
Expand Down Expand Up @@ -770,7 +791,7 @@ impl<'db> SemanticsImpl<'db> {
// FIXME: Type the return type
/// Returns the range (pre-expansion) in the string literal corresponding to the resolution,
/// absolute file range (post-expansion)
/// of the part in the format string, the corresponding string token and the resolution if it
/// of the part in the format string (post-expansion), the corresponding string token and the resolution if it
/// exists.
pub fn check_for_format_args_template_with_file(
&self,
Expand Down Expand Up @@ -904,7 +925,6 @@ impl<'db> SemanticsImpl<'db> {
None => return res,
};
let file = self.find_file(node.syntax());

if first == last {
// node is just the token, so descend the token
self.descend_into_macros_all(
Expand Down Expand Up @@ -1877,9 +1897,15 @@ impl<'db> SemanticsImpl<'db> {
self.with_ctx(|ctx| ctx.file_to_def(file).to_owned()).into_iter().map(Module::from)
}

fn ed_file_to_module_defs(&self, file: EditionedFileId) -> impl Iterator<Item = Module> {
self.with_ctx(|ctx| ctx.file_to_def(file.file_id(self.db)).to_owned())
.into_iter()
.map(Module::from)
}

fn hir_file_to_module_defs(&self, file: HirFileId) -> impl Iterator<Item = Module> {
// FIXME: Do we need to care about inline modules for macro expansions?
self.file_to_module_defs(file.original_file_respecting_includes(self.db).file_id(self.db))
self.ed_file_to_module_defs(file.original_file_respecting_includes(self.db))
}

pub fn scope(&self, node: &SyntaxNode) -> Option<SemanticsScope<'db>> {
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/convert_bool_to_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ fn replace_usages(
target_module: &hir::Module,
delayed_mutations: &mut Vec<(ImportScope, ast::Path)>,
) {
for (file_id, references) in usages {
for (file_id, references) in usages.map_out_of_macros(&ctx.sema) {
edit.edit_file(file_id.file_id(ctx.db()));

let refs_with_imports = augment_references_with_imports(ctx, references, target_module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn edit_struct_references(
};
let usages = strukt_def.usages(&ctx.sema).include_self_refs().all();

for (file_id, refs) in usages {
for (file_id, refs) in usages.map_out_of_macros(&ctx.sema) {
edit.edit_file(file_id.file_id(ctx.db()));
for r in refs {
process_struct_name_reference(ctx, r, edit);
Expand Down Expand Up @@ -234,7 +234,7 @@ fn edit_field_references(
};
let def = Definition::Field(field);
let usages = def.usages(&ctx.sema).all();
for (file_id, refs) in usages {
for (file_id, refs) in usages.map_out_of_macros(&ctx.sema) {
edit.edit_file(file_id.file_id(ctx.db()));
for r in refs {
if let Some(name_ref) = r.name.as_name_ref() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ide_db::{
defs::Definition,
helpers::mod_path_to_ast,
imports::insert_use::{ImportScope, insert_use},
search::{FileReference, UsageSearchResult},
search::{FileReference, RealFileUsageSearchResult},
source_change::SourceChangeBuilder,
syntax_helpers::node_ext::{for_each_tail_expr, walk_expr},
};
Expand Down Expand Up @@ -70,7 +70,8 @@ pub(crate) fn convert_tuple_return_type_to_struct(
let ret_type = edit.make_mut(ret_type);
let fn_ = edit.make_mut(fn_);

let usages = Definition::Function(fn_def).usages(&ctx.sema).all();
let usages =
Definition::Function(fn_def).usages(&ctx.sema).all().map_out_of_macros(&ctx.sema);
let struct_name = format!("{}Result", stdx::to_camel_case(&fn_name.to_string()));
let parent = fn_.syntax().ancestors().find_map(<Either<ast::Impl, ast::Trait>>::cast);
add_tuple_struct_def(
Expand Down Expand Up @@ -101,7 +102,7 @@ pub(crate) fn convert_tuple_return_type_to_struct(
fn replace_usages(
edit: &mut SourceChangeBuilder,
ctx: &AssistContext<'_>,
usages: &UsageSearchResult,
usages: &RealFileUsageSearchResult,
struct_name: &str,
target_module: &hir::Module,
) {
Expand Down Expand Up @@ -231,7 +232,7 @@ fn augment_references_with_imports(
fn add_tuple_struct_def(
edit: &mut SourceChangeBuilder,
ctx: &AssistContext<'_>,
usages: &UsageSearchResult,
usages: &RealFileUsageSearchResult,
parent: &SyntaxNode,
tuple_ty: &ast::TupleType,
struct_name: &str,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ fn edit_struct_references(
Either::Left(s) => Definition::Adt(hir::Adt::Struct(s)),
Either::Right(v) => Definition::Variant(v),
};
let usages = strukt_def.usages(&ctx.sema).include_self_refs().all();
let usages =
strukt_def.usages(&ctx.sema).include_self_refs().all().map_out_of_macros(&ctx.sema);

let edit_node = |edit: &mut SourceChangeBuilder, node: SyntaxNode| -> Option<()> {
match_ast! {
Expand Down Expand Up @@ -228,7 +229,7 @@ fn edit_field_references(
None => continue,
};
let def = Definition::Field(field);
let usages = def.usages(&ctx.sema).all();
let usages = def.usages(&ctx.sema).all().map_out_of_macros(&ctx.sema);
for (file_id, refs) in usages {
edit.edit_file(file_id.file_id(ctx.db()));
for r in refs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ fn collect_data(ident_pat: IdentPat, ctx: &AssistContext<'_>) -> Option<TupleDat
.usages(&ctx.sema)
.in_scope(&SearchScope::single_file(ctx.file_id()))
.all()
.map_out_of_macros(&ctx.sema)
.iter()
.next()
.map(|(_, refs)| refs.to_vec())
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/extract_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl Module {
let out_of_sel = |node: &SyntaxNode| !self.text_range.contains_range(node.text_range());
let mut use_stmts_set = FxHashSet::default();

for (file_id, refs) in node_def.usages(&ctx.sema).all() {
for (file_id, refs) in node_def.usages(&ctx.sema).all().map_out_of_macros(&ctx.sema) {
let source_file = ctx.sema.parse(file_id);
let usages = refs.into_iter().filter_map(|FileReference { range, .. }| {
// handle normal usages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub(crate) fn extract_struct_from_enum_variant(
visited_modules_set.insert(current_module);
// record file references of the file the def resides in, we only want to swap to the edited file in the builder once
let mut def_file_references = None;
for (file_id, references) in usages {
for (file_id, references) in usages.map_out_of_macros(&ctx.sema) {
if file_id == ctx.file_id() {
def_file_references = Some(references);
continue;
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/generate_delegate_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl Field {
) -> Option<Field> {
let db = ctx.sema.db;

let module = ctx.sema.file_to_module_def(ctx.vfs_file_id())?;
let module = ctx.sema.file_to_module_def(ctx.file_id())?;
let edition = module.krate().edition(ctx.db());

let (name, range, ty) = match f {
Expand Down
9 changes: 5 additions & 4 deletions crates/ide-assists/src/handlers/inline_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) ->
"Inline into all callers",
name.syntax().text_range(),
|builder| {
let mut usages = usages.all();
let mut usages = usages.all().map_out_of_macros(&ctx.sema);
let current_file_usage = usages.references.remove(&def_file);

let mut remove_def = true;
let mut inline_refs_for_file = |file_id: EditionedFileId, refs: Vec<FileReference>| {
let call_krate = ctx.sema.file_to_module_def(file_id).map(|it| it.krate());
let file_id = file_id.file_id(ctx.db());
builder.edit_file(file_id);
let call_krate = ctx.sema.file_to_module_def(file_id).map(|it| it.krate());
let count = refs.len();
// The collects are required as we are otherwise iterating while mutating 🙅‍♀️🙅‍♂️
let (name_refs, name_refs_use) = split_refs_and_uses(builder, refs, Some);
Expand Down Expand Up @@ -140,7 +140,7 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) ->
remove_def = false;
}
};
for (file_id, refs) in usages.into_iter() {
for (file_id, refs) in usages {
inline_refs_for_file(file_id, refs);
}
match current_file_usage {
Expand Down Expand Up @@ -196,7 +196,7 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
let name_ref: ast::NameRef = ctx.find_node_at_offset()?;
let call_info = CallInfo::from_name_ref(
name_ref.clone(),
ctx.sema.file_to_module_def(ctx.vfs_file_id())?.krate().into(),
ctx.sema.file_to_module_def(ctx.file_id())?.krate().into(),
)?;
let (function, label) = match &call_info.node {
ast::CallableExpr::Call(call) => {
Expand Down Expand Up @@ -337,6 +337,7 @@ fn inline(
Definition::Local(local)
.usages(sema)
.all()
.map_out_of_macros(sema)
.references
.remove(&function_def_file_id)
.unwrap_or_default()
Expand Down
8 changes: 5 additions & 3 deletions crates/ide-assists/src/handlers/inline_local_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use hir::{PathResolution, Semantics};
use ide_db::{
EditionedFileId, RootDatabase,
defs::Definition,
search::{FileReference, FileReferenceNode, UsageSearchResult},
search::{FileReference, FileReferenceNode, RealFileUsageSearchResult},
};
use syntax::{
SyntaxElement, TextRange,
Expand Down Expand Up @@ -142,7 +142,8 @@ fn inline_let(
}

let local = sema.to_def(&bind_pat)?;
let UsageSearchResult { mut references } = Definition::Local(local).usages(sema).all();
let RealFileUsageSearchResult { mut references } =
Definition::Local(local).usages(sema).all().map_out_of_macros(sema);
match references.remove(&file_id) {
Some(references) => Some(InlineData {
let_stmt,
Expand Down Expand Up @@ -189,7 +190,8 @@ fn inline_usage(

let let_stmt = ast::LetStmt::cast(bind_pat.syntax().parent()?)?;

let UsageSearchResult { mut references } = Definition::Local(local).usages(sema).all();
let RealFileUsageSearchResult { mut references } =
Definition::Local(local).usages(sema).all().map_out_of_macros(sema);
let mut references = references.remove(&file_id)?;
let delete_let = references.len() == 1;
references.retain(|fref| fref.name.as_name_ref() == Some(&name));
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/inline_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::{AssistContext, AssistId, Assists};
pub(crate) fn inline_macro(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let unexpanded = ctx.find_node_at_offset::<ast::MacroCall>()?;
let macro_call = ctx.sema.to_def(&unexpanded)?;
let target_crate_id = ctx.sema.file_to_module_def(ctx.vfs_file_id())?.krate().into();
let target_crate_id = ctx.sema.file_to_module_def(ctx.file_id())?.krate().into();
let text_range = unexpanded.syntax().text_range();

acc.add(
Expand Down
Loading
Loading