Turn ImportSource into a struct

This commit is contained in:
Lukas Wirth 2024-10-05 14:55:39 +02:00
parent f7ca085690
commit a898493b82

View file

@ -127,8 +127,11 @@ impl PartialResolvedImport {
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
enum ImportSource { struct ImportSource {
Use { use_tree: Idx<ast::UseTree>, id: UseId, is_prelude: bool, kind: ImportKind }, use_tree: Idx<ast::UseTree>,
id: UseId,
is_prelude: bool,
kind: ImportKind,
} }
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
@ -154,7 +157,7 @@ impl Import {
path, path,
alias, alias,
visibility: visibility.clone(), visibility: visibility.clone(),
source: ImportSource::Use { use_tree: idx, id, is_prelude, kind }, source: ImportSource { use_tree: idx, id, is_prelude, kind },
}); });
}); });
} }
@ -780,8 +783,6 @@ impl DefCollector<'_> {
let _p = tracing::info_span!("resolve_import", import_path = %import.path.display(self.db.upcast(), Edition::LATEST)) let _p = tracing::info_span!("resolve_import", import_path = %import.path.display(self.db.upcast(), Edition::LATEST))
.entered(); .entered();
tracing::debug!("resolving import: {:?} ({:?})", import, self.def_map.data.edition); tracing::debug!("resolving import: {:?} ({:?})", import, self.def_map.data.edition);
match import.source {
ImportSource::Use { .. } => {
let res = self.def_map.resolve_path_fp_with_macro( let res = self.def_map.resolve_path_fp_with_macro(
self.db, self.db,
ResolveMode::Import, ResolveMode::Import,
@ -809,8 +810,6 @@ impl DefCollector<'_> {
PartialResolvedImport::Indeterminate(def) PartialResolvedImport::Indeterminate(def)
} }
} }
}
}
fn record_resolved_import(&mut self, directive: &ImportDirective) { fn record_resolved_import(&mut self, directive: &ImportDirective) {
let _p = tracing::info_span!("record_resolved_import").entered(); let _p = tracing::info_span!("record_resolved_import").entered();
@ -824,7 +823,12 @@ impl DefCollector<'_> {
.unwrap_or(Visibility::Public); .unwrap_or(Visibility::Public);
match import.source { match import.source {
ImportSource::Use { kind: ImportKind::Plain | ImportKind::TypeOnly, .. } => { ImportSource {
kind: kind @ (ImportKind::Plain | ImportKind::TypeOnly),
id,
use_tree,
..
} => {
let name = match &import.alias { let name = match &import.alias {
Some(ImportAlias::Alias(name)) => Some(name), Some(ImportAlias::Alias(name)) => Some(name),
Some(ImportAlias::Underscore) => None, Some(ImportAlias::Underscore) => None,
@ -837,24 +841,20 @@ impl DefCollector<'_> {
}, },
}; };
let imp = match import.source {
ImportSource::Use { kind, id, use_tree, .. } => {
if kind == ImportKind::TypeOnly { if kind == ImportKind::TypeOnly {
def.values = None; def.values = None;
def.macros = None; def.macros = None;
} }
ImportType::Import(ImportId { import: id, idx: use_tree }) let imp = ImportType::Import(ImportId { import: id, idx: use_tree });
}
};
tracing::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def); tracing::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def);
self.update(module_id, &[(name.cloned(), def)], vis, Some(imp)); self.update(module_id, &[(name.cloned(), def)], vis, Some(imp));
} }
ImportSource::Use { kind: ImportKind::Glob, id, .. } => { ImportSource { kind: ImportKind::Glob, id, is_prelude, .. } => {
tracing::debug!("glob import: {:?}", import); tracing::debug!("glob import: {:?}", import);
match def.take_types() { match def.take_types() {
Some(ModuleDefId::ModuleId(m)) => { Some(ModuleDefId::ModuleId(m)) => {
if let ImportSource::Use { id, is_prelude: true, .. } = import.source { if is_prelude {
// Note: This dodgily overrides the injected prelude. The rustc // Note: This dodgily overrides the injected prelude. The rustc
// implementation seems to work the same though. // implementation seems to work the same though.
cov_mark::hit!(std_prelude); cov_mark::hit!(std_prelude);
@ -1509,18 +1509,26 @@ impl DefCollector<'_> {
} }
// Emit diagnostics for all remaining unresolved imports. // Emit diagnostics for all remaining unresolved imports.
for directive in &self.unresolved_imports { for import in &self.unresolved_imports {
let ImportSource::Use { use_tree, id, is_prelude: _, kind: _ } = let &ImportDirective {
directive.import.source; module_id,
import:
Import {
ref path,
source: ImportSource { use_tree, id, is_prelude: _, kind: _ },
..
},
..
} = import;
if matches!( if matches!(
(directive.import.path.segments().first(), &directive.import.path.kind), (path.segments().first(), &path.kind),
(Some(krate), PathKind::Plain | PathKind::Abs) if self.unresolved_extern_crates.contains(krate) (Some(krate), PathKind::Plain | PathKind::Abs) if self.unresolved_extern_crates.contains(krate)
) { ) {
continue; continue;
} }
let item_tree_id = id.lookup(self.db).id; let item_tree_id = id.lookup(self.db).id;
self.def_map.diagnostics.push(DefDiagnostic::unresolved_import( self.def_map.diagnostics.push(DefDiagnostic::unresolved_import(
directive.module_id, module_id,
item_tree_id, item_tree_id,
use_tree, use_tree,
)); ));