Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/lib.rs')
-rw-r--r--crates/hir-def/src/lib.rs53
1 files changed, 33 insertions, 20 deletions
diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs
index fd8f64d670..7cf13a202e 100644
--- a/crates/hir-def/src/lib.rs
+++ b/crates/hir-def/src/lib.rs
@@ -7,7 +7,7 @@
//! Note that `hir_def` is a work in progress, so not all of the above is
//! actually true.
-#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
+#![warn(rust_2018_idioms, unused_lifetimes)]
#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))]
#[allow(unused)]
@@ -63,7 +63,7 @@ use std::{
panic::{RefUnwindSafe, UnwindSafe},
};
-use base_db::{impl_intern_key, salsa, CrateId, ProcMacroKind};
+use base_db::{impl_intern_key, salsa, span::SyntaxContextId, CrateId, ProcMacroKind};
use hir_expand::{
ast_id_map::{AstIdNode, FileAstId},
attrs::{Attr, AttrId, AttrInput},
@@ -72,19 +72,18 @@ use hir_expand::{
builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
db::ExpandDatabase,
eager::expand_eager_macro_input,
- hygiene::Hygiene,
name::Name,
proc_macro::ProcMacroExpander,
AstId, ExpandError, ExpandResult, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind,
- MacroDefId, MacroDefKind, UnresolvedMacro,
+ MacroDefId, MacroDefKind,
};
use item_tree::ExternBlock;
use la_arena::Idx;
use nameres::DefMap;
use stdx::impl_from;
-use syntax::ast;
+use syntax::{ast, AstNode};
-use ::tt::token_id as tt;
+pub use hir_expand::tt;
use crate::{
builtin_type::BuiltinType,
@@ -1166,16 +1165,20 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
let expands_to = hir_expand::ExpandTo::from_call_site(self.value);
let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
- let h = Hygiene::new(db, self.file_id);
- let path = self.value.path().and_then(|path| path::ModPath::from_src(db, path, &h));
+ let span_map = db.span_map(self.file_id);
+ let path =
+ self.value.path().and_then(|path| path::ModPath::from_src(db, path, span_map.as_ref()));
let Some(path) = path else {
return Ok(ExpandResult::only_err(ExpandError::other("malformed macro invocation")));
};
+ let call_site = span_map.span_for_range(self.value.syntax().text_range()).ctx;
+
macro_call_as_call_id_with_eager(
db,
&AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
+ call_site,
expands_to,
krate,
resolver,
@@ -1200,17 +1203,19 @@ impl<T: AstIdNode> AstIdWithPath<T> {
fn macro_call_as_call_id(
db: &dyn ExpandDatabase,
call: &AstIdWithPath<ast::MacroCall>,
+ call_site: SyntaxContextId,
expand_to: ExpandTo,
krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
) -> Result<Option<MacroCallId>, UnresolvedMacro> {
- macro_call_as_call_id_with_eager(db, call, expand_to, krate, resolver, resolver)
+ macro_call_as_call_id_with_eager(db, call, call_site, expand_to, krate, resolver, resolver)
.map(|res| res.value)
}
fn macro_call_as_call_id_with_eager(
db: &dyn ExpandDatabase,
call: &AstIdWithPath<ast::MacroCall>,
+ call_site: SyntaxContextId,
expand_to: ExpandTo,
krate: CrateId,
resolver: impl FnOnce(path::ModPath) -> Option<MacroDefId>,
@@ -1222,7 +1227,7 @@ fn macro_call_as_call_id_with_eager(
let res = match def.kind {
MacroDefKind::BuiltInEager(..) => {
let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db));
- expand_eager_macro_input(db, krate, macro_call, def, &|path| {
+ expand_eager_macro_input(db, krate, macro_call, def, call_site, &|path| {
eager_resolver(path).filter(MacroDefId::is_fn_like)
})
}
@@ -1231,6 +1236,7 @@ fn macro_call_as_call_id_with_eager(
db,
krate,
MacroCallKind::FnLike { ast_id: call.ast_id, expand_to },
+ call_site,
)),
err: None,
},
@@ -1315,6 +1321,7 @@ fn derive_macro_as_call_id(
item_attr: &AstIdWithPath<ast::Adt>,
derive_attr_index: AttrId,
derive_pos: u32,
+ call_site: SyntaxContextId,
krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
@@ -1329,6 +1336,7 @@ fn derive_macro_as_call_id(
derive_index: derive_pos,
derive_attr_index,
},
+ call_site,
);
Ok((macro_id, def_id, call_id))
}
@@ -1341,15 +1349,13 @@ fn attr_macro_as_call_id(
def: MacroDefId,
) -> MacroCallId {
let arg = match macro_attr.input.as_deref() {
- Some(AttrInput::TokenTree(tt)) => (
- {
- let mut tt = tt.0.clone();
- tt.delimiter = tt::Delimiter::UNSPECIFIED;
- tt
- },
- tt.1.clone(),
- ),
- _ => (tt::Subtree::empty(), Default::default()),
+ Some(AttrInput::TokenTree(tt)) => {
+ let mut tt = tt.as_ref().clone();
+ tt.delimiter = tt::Delimiter::DUMMY_INVISIBLE;
+ Some(tt)
+ }
+
+ _ => None,
};
def.as_lazy_macro(
@@ -1357,11 +1363,18 @@ fn attr_macro_as_call_id(
krate,
MacroCallKind::Attr {
ast_id: item_attr.ast_id,
- attr_args: Arc::new(arg),
+ attr_args: arg.map(Arc::new),
invoc_attr_index: macro_attr.id,
},
+ macro_attr.ctxt,
)
}
+
+#[derive(Debug)]
+pub struct UnresolvedMacro {
+ pub path: hir_expand::mod_path::ModPath,
+}
+
intern::impl_internable!(
crate::type_ref::TypeRef,
crate::type_ref::TraitRef,