Resolve included files to their calling modules in IDE layer

This commit is contained in:
Lukas Wirth 2024-08-12 13:45:33 +02:00
parent 80483e0622
commit 2362975137
6 changed files with 93 additions and 56 deletions

View file

@ -240,14 +240,14 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
fn crate_supports_no_std(&self, crate_id: CrateId) -> bool;
fn include_macro_invoc(&self, crate_id: CrateId) -> Vec<(MacroCallId, EditionedFileId)>;
fn include_macro_invoc(&self, crate_id: CrateId) -> Arc<[(MacroCallId, EditionedFileId)]>;
}
// return: macro call id and include file id
fn include_macro_invoc(
db: &dyn DefDatabase,
krate: CrateId,
) -> Vec<(MacroCallId, EditionedFileId)> {
) -> Arc<[(MacroCallId, EditionedFileId)]> {
db.crate_def_map(krate)
.modules
.values()

View file

@ -770,12 +770,14 @@ impl<'db> SemanticsImpl<'db> {
let file_id = self.find_file(&parent).file_id.file_id()?;
// iterate related crates and find all include! invocations that include_file_id matches
for (invoc, _) in self
for iter in self
.db
.relevant_crates(file_id.file_id())
.iter()
.flat_map(|krate| self.db.include_macro_invoc(*krate))
.filter(|&(_, include_file_id)| include_file_id == file_id)
.map(|krate| self.db.include_macro_invoc(*krate))
{
for (invoc, _) in
iter.iter().filter(|&&(_, include_file_id)| include_file_id == file_id)
{
let macro_file = invoc.as_macro_file();
let expansion_info = {
@ -825,6 +827,7 @@ impl<'db> SemanticsImpl<'db> {
return Some((sa, file_id.into(), t, span));
}
}
}
None
}

View file

@ -94,8 +94,9 @@ use hir_def::{
},
hir::{BindingId, LabelId},
AdtId, BlockId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, ExternCrateId,
FieldId, FunctionId, GenericDefId, GenericParamId, ImplId, LifetimeParamId, MacroId, ModuleId,
StaticId, StructId, TraitAliasId, TraitId, TypeAliasId, TypeParamId, UnionId, UseId, VariantId,
FieldId, FunctionId, GenericDefId, GenericParamId, ImplId, LifetimeParamId, Lookup, MacroId,
ModuleId, StaticId, StructId, TraitAliasId, TraitId, TypeAliasId, TypeParamId, UnionId, UseId,
VariantId,
};
use hir_expand::{
attrs::AttrId, name::AsName, ExpansionInfo, HirFileId, HirFileIdExt, MacroCallId,
@ -131,11 +132,30 @@ impl SourceToDefCtx<'_, '_> {
for &crate_id in self.db.relevant_crates(file).iter() {
// Note: `mod` declarations in block modules cannot be supported here
let crate_def_map = self.db.crate_def_map(crate_id);
mods.extend(
let n_mods = mods.len();
let modules = |file| {
crate_def_map
.modules_for_file(file)
.map(|local_id| crate_def_map.module_id(local_id)),
.map(|local_id| crate_def_map.module_id(local_id))
};
mods.extend(modules(file));
if mods.len() == n_mods {
mods.extend(
self.db
.include_macro_invoc(crate_id)
.iter()
.filter(|&&(_, file_id)| file_id == file)
.flat_map(|(call, _)| {
modules(
call.lookup(self.db.upcast())
.kind
.file_id()
.original_file(self.db.upcast())
.file_id(),
)
}),
);
}
}
if mods.is_empty() {
// FIXME: detached file

View file

@ -273,11 +273,7 @@ fn f() {
#[test]
fn include_does_not_break_diagnostics() {
let mut config = DiagnosticsConfig::test_sample();
config.disabled.insert("inactive-code".to_owned());
config.disabled.insert("unlinked-file".to_owned());
check_diagnostics_with_config(
config,
check_diagnostics(
r#"
//- minicore: include
//- /lib.rs crate:lib

View file

@ -499,6 +499,18 @@ $0
mod bar {
mod foo;
}
"#,
);
}
#[test]
fn include_macro_works() {
check_diagnostics(
r#"
//- minicore: include
//- /main.rs
include!("bar/foo/mod.rs");
//- /bar/foo/mod.rs
"#,
);
}

View file

@ -251,6 +251,12 @@ pub(crate) fn check_diagnostics_with_config(config: DiagnosticsConfig, ra_fixtur
let mut actual = annotations.remove(&file_id).unwrap_or_default();
let expected = extract_annotations(&db.file_text(file_id));
actual.sort_by_key(|(range, _)| range.start());
// FIXME: We should panic on duplicates instead, but includes currently cause us to report
// diagnostics twice for the calling module when both files are queried.
actual.dedup();
// actual.iter().duplicates().for_each(|(range, msg)| {
// panic!("duplicate diagnostic at {:?}: {msg:?}", line_index.line_col(range.start()))
// });
if expected.is_empty() {
// makes minicore smoke test debuggable
for (e, _) in &actual {