2022-01-06 05:30:16 -06:00
|
|
|
//! Post-nameres attribute resolution.
|
|
|
|
|
2023-01-09 12:29:28 -06:00
|
|
|
use hir_expand::{attrs::Attr, MacroCallId};
|
2022-01-06 05:30:16 -06:00
|
|
|
use syntax::{ast, SmolStr};
|
|
|
|
|
|
|
|
use crate::{
|
2023-04-06 12:55:49 -05:00
|
|
|
attr::builtin::{find_builtin_attr_idx, TOOL_MODULES},
|
|
|
|
attr_macro_as_call_id,
|
2022-01-06 05:30:16 -06:00
|
|
|
db::DefDatabase,
|
|
|
|
item_scope::BuiltinShadowMode,
|
2022-03-08 14:41:19 -06:00
|
|
|
macro_id_to_def_id,
|
2022-01-06 05:30:16 -06:00
|
|
|
nameres::path_resolution::ResolveMode,
|
|
|
|
path::{ModPath, PathKind},
|
|
|
|
AstIdWithPath, LocalModuleId, UnresolvedMacro,
|
|
|
|
};
|
|
|
|
|
|
|
|
use super::DefMap;
|
|
|
|
|
|
|
|
pub enum ResolvedAttr {
|
|
|
|
/// Attribute resolved to an attribute macro.
|
|
|
|
Macro(MacroCallId),
|
|
|
|
/// Attribute resolved to something else that does not require expansion.
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DefMap {
|
|
|
|
pub(crate) fn resolve_attr_macro(
|
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
original_module: LocalModuleId,
|
|
|
|
ast_id: AstIdWithPath<ast::Item>,
|
|
|
|
attr: &Attr,
|
|
|
|
) -> Result<ResolvedAttr, UnresolvedMacro> {
|
|
|
|
// NB: does not currently work for derive helpers as they aren't recorded in the `DefMap`
|
|
|
|
|
|
|
|
if self.is_builtin_or_registered_attr(&ast_id.path) {
|
|
|
|
return Ok(ResolvedAttr::Other);
|
|
|
|
}
|
|
|
|
|
|
|
|
let resolved_res = self.resolve_path_fp_with_macro(
|
|
|
|
db,
|
|
|
|
ResolveMode::Other,
|
|
|
|
original_module,
|
|
|
|
&ast_id.path,
|
|
|
|
BuiltinShadowMode::Module,
|
|
|
|
);
|
|
|
|
let def = match resolved_res.resolved_def.take_macros() {
|
|
|
|
Some(def) => {
|
2022-03-08 14:41:19 -06:00
|
|
|
if def.is_attribute(db) {
|
2022-01-06 05:30:16 -06:00
|
|
|
def
|
|
|
|
} else {
|
|
|
|
return Ok(ResolvedAttr::Other);
|
|
|
|
}
|
|
|
|
}
|
2022-03-12 07:35:25 -06:00
|
|
|
None => return Err(UnresolvedMacro { path: ast_id.path }),
|
2022-01-06 05:30:16 -06:00
|
|
|
};
|
|
|
|
|
2022-03-08 14:41:19 -06:00
|
|
|
Ok(ResolvedAttr::Macro(attr_macro_as_call_id(
|
|
|
|
db,
|
|
|
|
&ast_id,
|
|
|
|
attr,
|
|
|
|
self.krate,
|
|
|
|
macro_id_to_def_id(db, def),
|
|
|
|
false,
|
|
|
|
)))
|
2022-01-06 05:30:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn is_builtin_or_registered_attr(&self, path: &ModPath) -> bool {
|
|
|
|
if path.kind != PathKind::Plain {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let segments = path.segments();
|
|
|
|
|
|
|
|
if let Some(name) = segments.first() {
|
|
|
|
let name = name.to_smol_str();
|
|
|
|
let pred = |n: &_| *n == name;
|
|
|
|
|
|
|
|
let registered = self.registered_tools.iter().map(SmolStr::as_str);
|
2023-04-06 12:55:49 -05:00
|
|
|
let is_tool = TOOL_MODULES.iter().copied().chain(registered).any(pred);
|
2022-01-06 05:30:16 -06:00
|
|
|
// FIXME: tool modules can be shadowed by actual modules
|
|
|
|
if is_tool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if segments.len() == 1 {
|
2023-04-06 12:44:06 -05:00
|
|
|
let mut registered = self.registered_attrs.iter().map(SmolStr::as_str);
|
2023-04-06 12:55:49 -05:00
|
|
|
let is_inert = find_builtin_attr_idx(&name).is_some() || registered.any(pred);
|
2022-01-06 05:30:16 -06:00
|
|
|
return is_inert;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|