2019-11-03 14:49:44 -06:00
|
|
|
//! This module implements import-resolution/macro expansion algorithm.
|
|
|
|
//!
|
|
|
|
//! The result of this module is `CrateDefMap`: a data structure which contains:
|
|
|
|
//!
|
|
|
|
//! * a tree of modules for the crate
|
|
|
|
//! * for each module, a set of items visible in the module (directly declared
|
|
|
|
//! or imported)
|
|
|
|
//!
|
|
|
|
//! Note that `CrateDefMap` contains fully macro expanded code.
|
|
|
|
//!
|
|
|
|
//! Computing `CrateDefMap` can be partitioned into several logically
|
|
|
|
//! independent "phases". The phases are mutually recursive though, there's no
|
|
|
|
//! strict ordering.
|
|
|
|
//!
|
|
|
|
//! ## Collecting RawItems
|
|
|
|
//!
|
2019-11-08 15:23:19 -06:00
|
|
|
//! This happens in the `raw` module, which parses a single source file into a
|
|
|
|
//! set of top-level items. Nested imports are desugared to flat imports in this
|
|
|
|
//! phase. Macro calls are represented as a triple of (Path, Option<Name>,
|
|
|
|
//! TokenTree).
|
2019-11-03 14:49:44 -06:00
|
|
|
//!
|
|
|
|
//! ## Collecting Modules
|
|
|
|
//!
|
|
|
|
//! This happens in the `collector` module. In this phase, we recursively walk
|
|
|
|
//! tree of modules, collect raw items from submodules, populate module scopes
|
|
|
|
//! with defined items (so, we assign item ids in this phase) and record the set
|
|
|
|
//! of unresolved imports and macros.
|
|
|
|
//!
|
|
|
|
//! While we walk tree of modules, we also record macro_rules definitions and
|
|
|
|
//! expand calls to macro_rules defined macros.
|
|
|
|
//!
|
|
|
|
//! ## Resolving Imports
|
|
|
|
//!
|
|
|
|
//! We maintain a list of currently unresolved imports. On every iteration, we
|
|
|
|
//! try to resolve some imports from this list. If the import is resolved, we
|
|
|
|
//! record it, by adding an item to current module scope and, if necessary, by
|
|
|
|
//! recursively populating glob imports.
|
|
|
|
//!
|
|
|
|
//! ## Resolving Macros
|
|
|
|
//!
|
|
|
|
//! macro_rules from the same crate use a global mutable namespace. We expand
|
|
|
|
//! them immediately, when we collect modules.
|
|
|
|
//!
|
|
|
|
//! Macros from other crates (including proc-macros) can be used with
|
|
|
|
//! `foo::bar!` syntax. We handle them similarly to imports. There's a list of
|
|
|
|
//! unexpanded macros. On every iteration, we try to resolve each macro call
|
2019-11-08 15:23:19 -06:00
|
|
|
//! path and, upon success, we run macro expansion and "collect module" phase on
|
|
|
|
//! the result
|
2019-10-30 09:40:13 -05:00
|
|
|
|
2019-11-04 12:42:25 -06:00
|
|
|
mod collector;
|
|
|
|
mod mod_resolution;
|
2019-11-08 15:17:17 -06:00
|
|
|
mod path_resolution;
|
2019-10-31 10:45:10 -05:00
|
|
|
|
2019-11-03 14:35:48 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::{CrateId, Edition, FileId};
|
2019-12-20 08:43:45 -06:00
|
|
|
use hir_expand::{diagnostics::DiagnosticSink, name::Name, InFile};
|
2021-01-14 09:47:42 -06:00
|
|
|
use la_arena::Arena;
|
2019-11-24 04:57:45 -06:00
|
|
|
use rustc_hash::FxHashMap;
|
2020-03-28 05:08:19 -05:00
|
|
|
use stdx::format_to;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::ast;
|
2019-10-31 10:45:10 -05:00
|
|
|
|
|
|
|
use crate::{
|
2019-11-23 05:44:43 -06:00
|
|
|
db::DefDatabase,
|
2019-12-20 08:45:12 -06:00
|
|
|
item_scope::{BuiltinShadowMode, ItemScope},
|
2019-11-23 07:53:16 -06:00
|
|
|
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
|
2019-12-13 05:12:36 -06:00
|
|
|
path::ModPath,
|
2019-11-23 07:53:16 -06:00
|
|
|
per_ns::PerNs,
|
2019-12-20 08:58:20 -06:00
|
|
|
AstId, LocalModuleId, ModuleDefId, ModuleId,
|
2019-10-31 10:45:10 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Contains all top-level defs from a macro-expanded crate
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2021-01-18 13:18:05 -06:00
|
|
|
pub struct DefMap {
|
2019-11-24 09:05:12 -06:00
|
|
|
pub root: LocalModuleId,
|
2020-03-19 10:00:11 -05:00
|
|
|
pub modules: Arena<ModuleData>,
|
2019-11-24 09:05:12 -06:00
|
|
|
pub(crate) krate: CrateId,
|
2019-10-31 10:45:10 -05:00
|
|
|
/// The prelude module for this crate. This either comes from an import
|
|
|
|
/// marked with the `prelude_import` attribute, or (in the normal case) from
|
|
|
|
/// a dependency (`std` or `core`).
|
2019-11-24 09:05:12 -06:00
|
|
|
pub(crate) prelude: Option<ModuleId>,
|
|
|
|
pub(crate) extern_prelude: FxHashMap<Name, ModuleDefId>,
|
2019-10-31 10:45:10 -05:00
|
|
|
|
2019-11-24 09:05:12 -06:00
|
|
|
edition: Edition,
|
2019-10-31 10:45:10 -05:00
|
|
|
diagnostics: Vec<DefDiagnostic>,
|
|
|
|
}
|
|
|
|
|
2021-01-18 13:18:05 -06:00
|
|
|
impl std::ops::Index<LocalModuleId> for DefMap {
|
2019-10-31 10:45:10 -05:00
|
|
|
type Output = ModuleData;
|
2019-11-23 07:49:53 -06:00
|
|
|
fn index(&self, id: LocalModuleId) -> &ModuleData {
|
2019-10-31 10:45:10 -05:00
|
|
|
&self.modules[id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 13:58:29 -06:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
|
|
|
|
pub enum ModuleOrigin {
|
2019-12-05 07:33:29 -06:00
|
|
|
CrateRoot {
|
|
|
|
definition: FileId,
|
|
|
|
},
|
2019-12-03 13:58:29 -06:00
|
|
|
/// Note that non-inline modules, by definition, live inside non-macro file.
|
2019-12-05 07:19:27 -06:00
|
|
|
File {
|
2020-06-11 04:04:09 -05:00
|
|
|
is_mod_rs: bool,
|
2019-12-05 07:19:27 -06:00
|
|
|
declaration: AstId<ast::Module>,
|
|
|
|
definition: FileId,
|
|
|
|
},
|
|
|
|
Inline {
|
|
|
|
definition: AstId<ast::Module>,
|
|
|
|
},
|
2019-12-03 13:58:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ModuleOrigin {
|
|
|
|
fn default() -> Self {
|
2019-12-05 07:33:29 -06:00
|
|
|
ModuleOrigin::CrateRoot { definition: FileId(0) }
|
2019-12-03 13:58:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ModuleOrigin {
|
2019-12-05 07:19:27 -06:00
|
|
|
fn declaration(&self) -> Option<AstId<ast::Module>> {
|
2019-12-03 13:58:29 -06:00
|
|
|
match self {
|
2019-12-05 07:19:27 -06:00
|
|
|
ModuleOrigin::File { declaration: module, .. }
|
|
|
|
| ModuleOrigin::Inline { definition: module, .. } => Some(*module),
|
2019-12-05 07:33:29 -06:00
|
|
|
ModuleOrigin::CrateRoot { .. } => None,
|
2019-12-03 13:58:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 07:37:39 -06:00
|
|
|
pub fn file_id(&self) -> Option<FileId> {
|
2019-12-03 13:58:29 -06:00
|
|
|
match self {
|
2019-12-05 07:33:29 -06:00
|
|
|
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
|
|
|
|
Some(*definition)
|
2019-12-05 07:19:27 -06:00
|
|
|
}
|
2019-12-03 13:58:29 -06:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 07:42:57 -06:00
|
|
|
pub fn is_inline(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
ModuleOrigin::Inline { .. } => true,
|
|
|
|
ModuleOrigin::CrateRoot { .. } | ModuleOrigin::File { .. } => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 13:58:29 -06:00
|
|
|
/// Returns a node which defines this module.
|
|
|
|
/// That is, a file or a `mod foo {}` with items.
|
2020-03-13 10:05:46 -05:00
|
|
|
fn definition_source(&self, db: &dyn DefDatabase) -> InFile<ModuleSource> {
|
2019-12-03 13:58:29 -06:00
|
|
|
match self {
|
2019-12-05 07:33:29 -06:00
|
|
|
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
|
|
|
|
let file_id = *definition;
|
2019-12-03 13:58:29 -06:00
|
|
|
let sf = db.parse(file_id).tree();
|
2020-02-18 06:53:02 -06:00
|
|
|
InFile::new(file_id.into(), ModuleSource::SourceFile(sf))
|
2019-12-03 13:58:29 -06:00
|
|
|
}
|
2020-03-13 10:05:46 -05:00
|
|
|
ModuleOrigin::Inline { definition } => InFile::new(
|
|
|
|
definition.file_id,
|
|
|
|
ModuleSource::Module(definition.to_node(db.upcast())),
|
|
|
|
),
|
2019-12-03 13:58:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
#[derive(Default, Debug, PartialEq, Eq)]
|
|
|
|
pub struct ModuleData {
|
2019-11-23 07:49:53 -06:00
|
|
|
pub parent: Option<LocalModuleId>,
|
|
|
|
pub children: FxHashMap<Name, LocalModuleId>,
|
2019-12-20 08:45:12 -06:00
|
|
|
pub scope: ItemScope,
|
2019-11-23 02:14:10 -06:00
|
|
|
|
2019-12-03 13:58:29 -06:00
|
|
|
/// Where does this module come from?
|
|
|
|
pub origin: ModuleOrigin,
|
2019-10-31 10:45:10 -05:00
|
|
|
}
|
|
|
|
|
2021-01-18 13:18:05 -06:00
|
|
|
impl DefMap {
|
|
|
|
pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("crate_def_map_query").detail(|| {
|
2020-10-20 08:38:11 -05:00
|
|
|
db.crate_graph()[krate].display_name.as_deref().unwrap_or_default().to_string()
|
2020-03-16 04:47:52 -05:00
|
|
|
});
|
2019-10-31 10:45:10 -05:00
|
|
|
let def_map = {
|
2020-03-09 05:11:59 -05:00
|
|
|
let edition = db.crate_graph()[krate].edition;
|
2020-03-19 10:00:11 -05:00
|
|
|
let mut modules: Arena<ModuleData> = Arena::default();
|
2019-10-31 10:45:10 -05:00
|
|
|
let root = modules.alloc(ModuleData::default());
|
2021-01-18 13:18:05 -06:00
|
|
|
DefMap {
|
2019-10-31 10:45:10 -05:00
|
|
|
krate,
|
|
|
|
edition,
|
|
|
|
extern_prelude: FxHashMap::default(),
|
|
|
|
prelude: None,
|
|
|
|
root,
|
|
|
|
modules,
|
|
|
|
diagnostics: Vec::new(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let def_map = collector::collect_defs(db, def_map);
|
|
|
|
Arc::new(def_map)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_diagnostics(
|
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-23 07:49:53 -06:00
|
|
|
module: LocalModuleId,
|
2019-10-31 10:45:10 -05:00
|
|
|
sink: &mut DiagnosticSink,
|
|
|
|
) {
|
|
|
|
self.diagnostics.iter().for_each(|it| it.add_to(db, module, sink))
|
|
|
|
}
|
|
|
|
|
2019-11-23 07:49:53 -06:00
|
|
|
pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = LocalModuleId> + '_ {
|
2019-11-15 01:26:31 -06:00
|
|
|
self.modules
|
|
|
|
.iter()
|
2019-12-03 13:58:29 -06:00
|
|
|
.filter(move |(_id, data)| data.origin.file_id() == Some(file_id))
|
2019-11-15 01:26:31 -06:00
|
|
|
.map(|(id, _data)| id)
|
|
|
|
}
|
2019-11-24 04:34:27 -06:00
|
|
|
|
|
|
|
pub(crate) fn resolve_path(
|
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-24 04:34:27 -06:00
|
|
|
original_module: LocalModuleId,
|
2019-12-13 05:12:36 -06:00
|
|
|
path: &ModPath,
|
2019-11-30 09:29:21 -06:00
|
|
|
shadow: BuiltinShadowMode,
|
2019-11-24 04:34:27 -06:00
|
|
|
) -> (PerNs, Option<usize>) {
|
2019-11-30 09:29:21 -06:00
|
|
|
let res =
|
|
|
|
self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path, shadow);
|
2019-11-24 04:34:27 -06:00
|
|
|
(res.resolved_def, res.segment_index)
|
|
|
|
}
|
2020-01-28 09:29:31 -06:00
|
|
|
|
|
|
|
// FIXME: this can use some more human-readable format (ideally, an IR
|
|
|
|
// even), as this should be a great debugging aid.
|
|
|
|
pub fn dump(&self) -> String {
|
|
|
|
let mut buf = String::new();
|
2020-07-20 10:44:44 -05:00
|
|
|
go(&mut buf, self, "crate", self.root);
|
|
|
|
return buf;
|
2020-01-28 09:29:31 -06:00
|
|
|
|
2021-01-18 13:18:05 -06:00
|
|
|
fn go(buf: &mut String, map: &DefMap, path: &str, module: LocalModuleId) {
|
2020-07-20 10:44:44 -05:00
|
|
|
format_to!(buf, "{}\n", path);
|
2020-01-28 09:29:31 -06:00
|
|
|
|
|
|
|
let mut entries: Vec<_> = map.modules[module].scope.resolutions().collect();
|
|
|
|
entries.sort_by_key(|(name, _)| name.clone());
|
|
|
|
|
|
|
|
for (name, def) in entries {
|
2020-07-21 10:52:43 -05:00
|
|
|
format_to!(buf, "{}:", name.map_or("_".to_string(), |name| name.to_string()));
|
2020-01-28 09:29:31 -06:00
|
|
|
|
|
|
|
if def.types.is_some() {
|
2020-07-20 10:44:44 -05:00
|
|
|
buf.push_str(" t");
|
2020-01-28 09:29:31 -06:00
|
|
|
}
|
|
|
|
if def.values.is_some() {
|
2020-07-20 10:44:44 -05:00
|
|
|
buf.push_str(" v");
|
2020-01-28 09:29:31 -06:00
|
|
|
}
|
|
|
|
if def.macros.is_some() {
|
2020-07-20 10:44:44 -05:00
|
|
|
buf.push_str(" m");
|
2020-01-28 09:29:31 -06:00
|
|
|
}
|
|
|
|
if def.is_none() {
|
2020-07-20 10:44:44 -05:00
|
|
|
buf.push_str(" _");
|
2020-01-28 09:29:31 -06:00
|
|
|
}
|
|
|
|
|
2020-12-29 06:35:49 -06:00
|
|
|
buf.push('\n');
|
2020-01-28 09:29:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (name, child) in map.modules[module].children.iter() {
|
2020-07-20 10:44:44 -05:00
|
|
|
let path = format!("{}::{}", path, name);
|
|
|
|
buf.push('\n');
|
2020-01-28 09:29:31 -06:00
|
|
|
go(buf, map, &path, *child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 10:45:10 -05:00
|
|
|
}
|
|
|
|
|
2019-11-23 02:14:10 -06:00
|
|
|
impl ModuleData {
|
|
|
|
/// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
|
2020-03-13 10:05:46 -05:00
|
|
|
pub fn definition_source(&self, db: &dyn DefDatabase) -> InFile<ModuleSource> {
|
2019-12-03 13:58:29 -06:00
|
|
|
self.origin.definition_source(db)
|
2019-11-23 02:14:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
|
2019-12-03 13:58:29 -06:00
|
|
|
/// `None` for the crate root or block.
|
2020-03-13 10:05:46 -05:00
|
|
|
pub fn declaration_source(&self, db: &dyn DefDatabase) -> Option<InFile<ast::Module>> {
|
2019-12-03 13:58:29 -06:00
|
|
|
let decl = self.origin.declaration()?;
|
2020-03-13 10:05:46 -05:00
|
|
|
let value = decl.to_node(db.upcast());
|
2019-11-28 07:00:03 -06:00
|
|
|
Some(InFile { file_id: decl.file_id, value })
|
2019-11-23 02:14:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 14:58:38 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2019-12-03 14:24:02 -06:00
|
|
|
pub enum ModuleSource {
|
|
|
|
SourceFile(ast::SourceFile),
|
|
|
|
Module(ast::Module),
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
mod diagnostics {
|
2020-10-22 12:19:18 -05:00
|
|
|
use cfg::{CfgExpr, CfgOptions};
|
2019-10-31 10:45:10 -05:00
|
|
|
use hir_expand::diagnostics::DiagnosticSink;
|
2020-09-16 07:52:39 -05:00
|
|
|
use hir_expand::hygiene::Hygiene;
|
2020-11-26 13:09:54 -06:00
|
|
|
use hir_expand::{InFile, MacroCallKind};
|
2020-11-27 09:29:40 -06:00
|
|
|
use syntax::ast::AttrsOwner;
|
|
|
|
use syntax::{ast, AstNode, AstPtr, SyntaxKind, SyntaxNodePtr};
|
2019-10-31 10:45:10 -05:00
|
|
|
|
2020-09-16 07:52:39 -05:00
|
|
|
use crate::path::ModPath;
|
|
|
|
use crate::{db::DefDatabase, diagnostics::*, nameres::LocalModuleId, AstId};
|
2019-10-31 10:45:10 -05:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2020-09-16 07:52:39 -05:00
|
|
|
enum DiagnosticKind {
|
|
|
|
UnresolvedModule { declaration: AstId<ast::Module>, candidate: String },
|
|
|
|
|
|
|
|
UnresolvedExternCrate { ast: AstId<ast::ExternCrate> },
|
|
|
|
|
|
|
|
UnresolvedImport { ast: AstId<ast::Use>, index: usize },
|
2020-10-20 10:49:21 -05:00
|
|
|
|
2020-11-26 10:29:09 -06:00
|
|
|
UnconfiguredCode { ast: AstId<ast::Item>, cfg: CfgExpr, opts: CfgOptions },
|
2020-11-26 13:09:54 -06:00
|
|
|
|
|
|
|
UnresolvedProcMacro { ast: MacroCallKind },
|
|
|
|
|
|
|
|
MacroError { ast: MacroCallKind, message: String },
|
2020-09-16 07:52:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub(super) struct DefDiagnostic {
|
|
|
|
in_module: LocalModuleId,
|
|
|
|
kind: DiagnosticKind,
|
2019-10-31 10:45:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DefDiagnostic {
|
2020-09-16 07:52:39 -05:00
|
|
|
pub(super) fn unresolved_module(
|
|
|
|
container: LocalModuleId,
|
|
|
|
declaration: AstId<ast::Module>,
|
|
|
|
candidate: String,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
in_module: container,
|
|
|
|
kind: DiagnosticKind::UnresolvedModule { declaration, candidate },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn unresolved_extern_crate(
|
|
|
|
container: LocalModuleId,
|
|
|
|
declaration: AstId<ast::ExternCrate>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
in_module: container,
|
|
|
|
kind: DiagnosticKind::UnresolvedExternCrate { ast: declaration },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn unresolved_import(
|
|
|
|
container: LocalModuleId,
|
|
|
|
ast: AstId<ast::Use>,
|
|
|
|
index: usize,
|
|
|
|
) -> Self {
|
|
|
|
Self { in_module: container, kind: DiagnosticKind::UnresolvedImport { ast, index } }
|
|
|
|
}
|
|
|
|
|
2020-10-20 10:49:21 -05:00
|
|
|
pub(super) fn unconfigured_code(
|
|
|
|
container: LocalModuleId,
|
2020-11-26 10:29:09 -06:00
|
|
|
ast: AstId<ast::Item>,
|
2020-10-22 12:19:18 -05:00
|
|
|
cfg: CfgExpr,
|
|
|
|
opts: CfgOptions,
|
2020-10-20 10:49:21 -05:00
|
|
|
) -> Self {
|
2020-10-22 12:19:18 -05:00
|
|
|
Self { in_module: container, kind: DiagnosticKind::UnconfiguredCode { ast, cfg, opts } }
|
2020-10-20 10:49:21 -05:00
|
|
|
}
|
|
|
|
|
2020-11-26 13:09:54 -06:00
|
|
|
pub(super) fn unresolved_proc_macro(container: LocalModuleId, ast: MacroCallKind) -> Self {
|
|
|
|
Self { in_module: container, kind: DiagnosticKind::UnresolvedProcMacro { ast } }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn macro_error(
|
|
|
|
container: LocalModuleId,
|
|
|
|
ast: MacroCallKind,
|
|
|
|
message: String,
|
|
|
|
) -> Self {
|
|
|
|
Self { in_module: container, kind: DiagnosticKind::MacroError { ast, message } }
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:45:10 -05:00
|
|
|
pub(super) fn add_to(
|
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-23 07:49:53 -06:00
|
|
|
target_module: LocalModuleId,
|
2019-10-31 10:45:10 -05:00
|
|
|
sink: &mut DiagnosticSink,
|
|
|
|
) {
|
2020-09-16 07:52:39 -05:00
|
|
|
if self.in_module != target_module {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
match &self.kind {
|
|
|
|
DiagnosticKind::UnresolvedModule { declaration, candidate } => {
|
2020-03-13 10:05:46 -05:00
|
|
|
let decl = declaration.to_node(db.upcast());
|
2019-10-31 10:45:10 -05:00
|
|
|
sink.push(UnresolvedModule {
|
2019-11-28 07:00:03 -06:00
|
|
|
file: declaration.file_id,
|
2019-10-31 10:45:10 -05:00
|
|
|
decl: AstPtr::new(&decl),
|
|
|
|
candidate: candidate.clone(),
|
|
|
|
})
|
|
|
|
}
|
2020-09-16 07:52:39 -05:00
|
|
|
|
|
|
|
DiagnosticKind::UnresolvedExternCrate { ast } => {
|
|
|
|
let item = ast.to_node(db.upcast());
|
|
|
|
sink.push(UnresolvedExternCrate {
|
|
|
|
file: ast.file_id,
|
|
|
|
item: AstPtr::new(&item),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
DiagnosticKind::UnresolvedImport { ast, index } => {
|
|
|
|
let use_item = ast.to_node(db.upcast());
|
|
|
|
let hygiene = Hygiene::new(db.upcast(), ast.file_id);
|
|
|
|
let mut cur = 0;
|
|
|
|
let mut tree = None;
|
|
|
|
ModPath::expand_use_item(
|
|
|
|
InFile::new(ast.file_id, use_item),
|
|
|
|
&hygiene,
|
|
|
|
|_mod_path, use_tree, _is_glob, _alias| {
|
|
|
|
if cur == *index {
|
|
|
|
tree = Some(use_tree.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
cur += 1;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(tree) = tree {
|
|
|
|
sink.push(UnresolvedImport { file: ast.file_id, node: AstPtr::new(&tree) });
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 10:49:21 -05:00
|
|
|
|
2020-10-22 12:19:18 -05:00
|
|
|
DiagnosticKind::UnconfiguredCode { ast, cfg, opts } => {
|
2020-11-26 10:29:09 -06:00
|
|
|
let item = ast.to_node(db.upcast());
|
2020-10-22 12:19:18 -05:00
|
|
|
sink.push(InactiveCode {
|
|
|
|
file: ast.file_id,
|
2020-11-26 10:29:09 -06:00
|
|
|
node: AstPtr::new(&item).into(),
|
2020-10-22 12:19:18 -05:00
|
|
|
cfg: cfg.clone(),
|
|
|
|
opts: opts.clone(),
|
|
|
|
});
|
2020-10-20 10:49:21 -05:00
|
|
|
}
|
2020-11-26 13:09:54 -06:00
|
|
|
|
|
|
|
DiagnosticKind::UnresolvedProcMacro { ast } => {
|
2020-11-27 09:29:40 -06:00
|
|
|
let mut precise_location = None;
|
2020-11-26 13:09:54 -06:00
|
|
|
let (file, ast, name) = match ast {
|
|
|
|
MacroCallKind::FnLike(ast) => {
|
|
|
|
let node = ast.to_node(db.upcast());
|
|
|
|
(ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None)
|
|
|
|
}
|
|
|
|
MacroCallKind::Attr(ast, name) => {
|
|
|
|
let node = ast.to_node(db.upcast());
|
2020-11-27 09:29:40 -06:00
|
|
|
|
|
|
|
// Compute the precise location of the macro name's token in the derive
|
|
|
|
// list.
|
|
|
|
// FIXME: This does not handle paths to the macro, but neither does the
|
|
|
|
// rest of r-a.
|
|
|
|
let derive_attrs =
|
|
|
|
node.attrs().filter_map(|attr| match attr.as_simple_call() {
|
|
|
|
Some((name, args)) if name == "derive" => Some(args),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
'outer: for attr in derive_attrs {
|
|
|
|
let tokens =
|
|
|
|
attr.syntax().children_with_tokens().filter_map(|elem| {
|
|
|
|
match elem {
|
|
|
|
syntax::NodeOrToken::Node(_) => None,
|
|
|
|
syntax::NodeOrToken::Token(tok) => Some(tok),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for token in tokens {
|
|
|
|
if token.kind() == SyntaxKind::IDENT
|
2020-12-31 03:13:14 -06:00
|
|
|
&& token.text() == name.as_str()
|
2020-11-27 09:29:40 -06:00
|
|
|
{
|
|
|
|
precise_location = Some(token.text_range());
|
|
|
|
break 'outer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 13:09:54 -06:00
|
|
|
(
|
|
|
|
ast.file_id,
|
|
|
|
SyntaxNodePtr::from(AstPtr::new(&node)),
|
2020-11-27 09:29:40 -06:00
|
|
|
Some(name.clone()),
|
2020-11-26 13:09:54 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
2020-11-27 09:29:40 -06:00
|
|
|
sink.push(UnresolvedProcMacro {
|
|
|
|
file,
|
|
|
|
node: ast,
|
|
|
|
precise_location,
|
|
|
|
macro_name: name,
|
|
|
|
});
|
2020-11-26 13:09:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
DiagnosticKind::MacroError { ast, message } => {
|
|
|
|
let (file, ast) = match ast {
|
|
|
|
MacroCallKind::FnLike(ast) => {
|
|
|
|
let node = ast.to_node(db.upcast());
|
|
|
|
(ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node)))
|
|
|
|
}
|
|
|
|
MacroCallKind::Attr(ast, _) => {
|
|
|
|
let node = ast.to_node(db.upcast());
|
|
|
|
(ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node)))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
sink.push(MacroError { file, node: ast, message: message.clone() });
|
|
|
|
}
|
2019-10-31 10:45:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|