Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-expand/src/db.rs')
-rw-r--r--crates/hir-expand/src/db.rs224
1 files changed, 123 insertions, 101 deletions
diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs
index a7d12f11bc..ec68f2f96e 100644
--- a/crates/hir-expand/src/db.rs
+++ b/crates/hir-expand/src/db.rs
@@ -98,7 +98,7 @@ pub trait ExpandDatabase: SourceDatabase {
/// Lowers syntactic macro call to a token tree representation. That's a firewall
/// query, only typing in the macro call itself changes the returned
/// subtree.
- fn macro_arg(&self, id: MacroCallId) -> (Arc<tt::Subtree>, SyntaxFixupUndoInfo);
+ fn macro_arg(&self, id: MacroCallId) -> (Arc<tt::Subtree>, SyntaxFixupUndoInfo, Span);
/// Fetches the expander for this macro.
#[salsa::transparent]
#[salsa::invoke(TokenExpander::macro_expander)]
@@ -144,14 +144,16 @@ pub fn expand_speculative(
let span_map = RealSpanMap::absolute(FileId::BOGUS);
let span_map = SpanMapRef::RealSpanMap(&span_map);
+ let (_, _, span) = db.macro_arg(actual_macro_call);
+
// Build the subtree and token mapping for the speculative args
let (mut tt, undo_info) = match loc.kind {
MacroCallKind::FnLike { .. } => (
- mbe::syntax_node_to_token_tree(speculative_args, span_map, loc.call_site),
+ mbe::syntax_node_to_token_tree(speculative_args, span_map, span),
SyntaxFixupUndoInfo::NONE,
),
MacroCallKind::Attr { .. } if loc.def.is_attribute_derive() => (
- mbe::syntax_node_to_token_tree(speculative_args, span_map, loc.call_site),
+ mbe::syntax_node_to_token_tree(speculative_args, span_map, span),
SyntaxFixupUndoInfo::NONE,
),
MacroCallKind::Derive { derive_attr_index: index, .. }
@@ -159,12 +161,15 @@ pub fn expand_speculative(
let censor = if let MacroCallKind::Derive { .. } = loc.kind {
censor_derive_input(index, &ast::Adt::cast(speculative_args.clone())?)
} else {
- censor_attr_input(index, &ast::Item::cast(speculative_args.clone())?)
+ attr_source(index, &ast::Item::cast(speculative_args.clone())?)
+ .into_iter()
+ .map(|it| it.syntax().clone().into())
+ .collect()
};
let censor_cfg =
cfg_process::process_cfg_attrs(speculative_args, &loc, db).unwrap_or_default();
- let mut fixups = fixup::fixup_syntax(span_map, speculative_args, loc.call_site);
+ let mut fixups = fixup::fixup_syntax(span_map, speculative_args, span);
fixups.append.retain(|it, _| match it {
syntax::NodeOrToken::Token(_) => true,
it => !censor.contains(it) && !censor_cfg.contains(it),
@@ -178,7 +183,7 @@ pub fn expand_speculative(
span_map,
fixups.append,
fixups.remove,
- loc.call_site,
+ span,
),
fixups.undo_info,
)
@@ -200,9 +205,8 @@ pub fn expand_speculative(
}?;
match attr.token_tree() {
Some(token_tree) => {
- let mut tree =
- syntax_node_to_token_tree(token_tree.syntax(), span_map, loc.call_site);
- tree.delimiter = tt::Delimiter::invisible_spanned(loc.call_site);
+ let mut tree = syntax_node_to_token_tree(token_tree.syntax(), span_map, span);
+ tree.delimiter = tt::Delimiter::invisible_spanned(span);
Some(tree)
}
@@ -216,8 +220,8 @@ pub fn expand_speculative(
// Otherwise the expand query will fetch the non speculative attribute args and pass those instead.
let mut speculative_expansion = match loc.def.kind {
MacroDefKind::ProcMacro(expander, _, ast) => {
- tt.delimiter = tt::Delimiter::invisible_spanned(loc.call_site);
let span = db.proc_macro_span(ast);
+ tt.delimiter = tt::Delimiter::invisible_spanned(span);
expander.expand(
db,
loc.def.krate,
@@ -230,22 +234,21 @@ pub fn expand_speculative(
)
}
MacroDefKind::BuiltInAttr(BuiltinAttrExpander::Derive, _) => {
- pseudo_derive_attr_expansion(&tt, attr_arg.as_ref()?, loc.call_site)
+ pseudo_derive_attr_expansion(&tt, attr_arg.as_ref()?, span)
+ }
+ MacroDefKind::Declarative(it) => {
+ db.decl_macro_expander(loc.krate, it).expand_unhygienic(db, tt, loc.def.krate, span)
+ }
+ MacroDefKind::BuiltIn(it, _) => {
+ it.expand(db, actual_macro_call, &tt, span).map_err(Into::into)
}
- MacroDefKind::Declarative(it) => db.decl_macro_expander(loc.krate, it).expand_unhygienic(
- db,
- tt,
- loc.def.krate,
- loc.call_site,
- ),
- MacroDefKind::BuiltIn(it, _) => it.expand(db, actual_macro_call, &tt).map_err(Into::into),
MacroDefKind::BuiltInDerive(it, ..) => {
- it.expand(db, actual_macro_call, &tt).map_err(Into::into)
+ it.expand(db, actual_macro_call, &tt, span).map_err(Into::into)
}
MacroDefKind::BuiltInEager(it, _) => {
- it.expand(db, actual_macro_call, &tt).map_err(Into::into)
+ it.expand(db, actual_macro_call, &tt, span).map_err(Into::into)
}
- MacroDefKind::BuiltInAttr(it, _) => it.expand(db, actual_macro_call, &tt),
+ MacroDefKind::BuiltInAttr(it, _) => it.expand(db, actual_macro_call, &tt, span),
};
let expand_to = loc.expand_to();
@@ -338,7 +341,10 @@ pub(crate) fn parse_with_map(
// FIXME: for derive attributes, this will return separate copies of the same structures! Though
// they may differ in spans due to differing call sites...
-fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc<tt::Subtree>, SyntaxFixupUndoInfo) {
+fn macro_arg(
+ db: &dyn ExpandDatabase,
+ id: MacroCallId,
+) -> (Arc<tt::Subtree>, SyntaxFixupUndoInfo, Span) {
let loc = db.lookup_intern_macro_call(id);
if let MacroCallLoc {
@@ -347,29 +353,31 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc<tt::Subtree>, Syn
..
} = &loc
{
- return (eager.arg.clone(), SyntaxFixupUndoInfo::NONE);
+ return (eager.arg.clone(), SyntaxFixupUndoInfo::NONE, eager.span);
}
let (parse, map) = parse_with_map(db, loc.kind.file_id());
let root = parse.syntax_node();
- let (censor, item_node) = match loc.kind {
+ let (censor, item_node, span) = match loc.kind {
MacroCallKind::FnLike { ast_id, .. } => {
+ let node = &ast_id.to_ptr(db).to_node(&root);
+ let path_range = node
+ .path()
+ .map_or_else(|| node.syntax().text_range(), |path| path.syntax().text_range());
+ let span = map.span_for_range(path_range);
+
let dummy_tt = |kind| {
(
Arc::new(tt::Subtree {
- delimiter: tt::Delimiter {
- open: loc.call_site,
- close: loc.call_site,
- kind,
- },
+ delimiter: tt::Delimiter { open: span, close: span, kind },
token_trees: Box::default(),
}),
SyntaxFixupUndoInfo::default(),
+ span,
)
};
- let node = &ast_id.to_ptr(db).to_node(&root);
let Some(tt) = node.token_tree() else {
return dummy_tt(tt::DelimiterKind::Invisible);
};
@@ -399,27 +407,43 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc<tt::Subtree>, Syn
return dummy_tt(kind);
}
- let mut tt = mbe::syntax_node_to_token_tree(tt.syntax(), map.as_ref(), loc.call_site);
+ let mut tt = mbe::syntax_node_to_token_tree(tt.syntax(), map.as_ref(), span);
if loc.def.is_proc_macro() {
// proc macros expect their inputs without parentheses, MBEs expect it with them included
tt.delimiter.kind = tt::DelimiterKind::Invisible;
}
- return (Arc::new(tt), SyntaxFixupUndoInfo::NONE);
+ return (Arc::new(tt), SyntaxFixupUndoInfo::NONE, span);
}
MacroCallKind::Derive { ast_id, derive_attr_index, .. } => {
let node = ast_id.to_ptr(db).to_node(&root);
- (censor_derive_input(derive_attr_index, &node), node.into())
+ let censor_derive_input = censor_derive_input(derive_attr_index, &node);
+ let item_node = node.into();
+ let attr_source = attr_source(derive_attr_index, &item_node);
+ // FIXME: This is wrong, this should point to the path of the derive attribute`
+ let span =
+ map.span_for_range(attr_source.as_ref().and_then(|it| it.path()).map_or_else(
+ || item_node.syntax().text_range(),
+ |it| it.syntax().text_range(),
+ ));
+ (censor_derive_input, item_node, span)
}
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
let node = ast_id.to_ptr(db).to_node(&root);
- (censor_attr_input(invoc_attr_index, &node), node)
+ let attr_source = attr_source(invoc_attr_index, &node);
+ let span = map.span_for_range(
+ attr_source
+ .as_ref()
+ .and_then(|it| it.path())
+ .map_or_else(|| node.syntax().text_range(), |it| it.syntax().text_range()),
+ );
+ (attr_source.into_iter().map(|it| it.syntax().clone().into()).collect(), node, span)
}
};
let (mut tt, undo_info) = {
let syntax = item_node.syntax();
let censor_cfg = cfg_process::process_cfg_attrs(syntax, &loc, db).unwrap_or_default();
- let mut fixups = fixup::fixup_syntax(map.as_ref(), syntax, loc.call_site);
+ let mut fixups = fixup::fixup_syntax(map.as_ref(), syntax, span);
fixups.append.retain(|it, _| match it {
syntax::NodeOrToken::Token(_) => true,
it => !censor.contains(it) && !censor_cfg.contains(it),
@@ -433,7 +457,7 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc<tt::Subtree>, Syn
map,
fixups.append,
fixups.remove,
- loc.call_site,
+ span,
),
fixups.undo_info,
)
@@ -444,11 +468,11 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc<tt::Subtree>, Syn
tt.delimiter.kind = tt::DelimiterKind::Invisible;
}
- (Arc::new(tt), undo_info)
+ (Arc::new(tt), undo_info, span)
}
// FIXME: Censoring info should be calculated by the caller! Namely by name resolution
-/// Derives expect all `#[derive(..)]` invocations up to the currently invoked one to be stripped
+/// Derives expect all `#[derive(..)]` invocations up to (and including) the currently invoked one to be stripped
fn censor_derive_input(derive_attr_index: AttrId, node: &ast::Adt) -> FxHashSet<SyntaxElement> {
// FIXME: handle `cfg_attr`
cov_mark::hit!(derive_censoring);
@@ -465,16 +489,11 @@ fn censor_derive_input(derive_attr_index: AttrId, node: &ast::Adt) -> FxHashSet<
.collect()
}
-/// Attributes expect the invoking attribute to be stripped\
-fn censor_attr_input(invoc_attr_index: AttrId, node: &ast::Item) -> FxHashSet<SyntaxElement> {
+/// Attributes expect the invoking attribute to be stripped
+fn attr_source(invoc_attr_index: AttrId, node: &ast::Item) -> Option<ast::Attr> {
// FIXME: handle `cfg_attr`
cov_mark::hit!(attribute_macro_attr_censoring);
- collect_attrs(node)
- .nth(invoc_attr_index.ast_index())
- .and_then(|(_, attr)| Either::left(attr))
- .map(|attr| attr.syntax().clone().into())
- .into_iter()
- .collect()
+ collect_attrs(node).nth(invoc_attr_index.ast_index()).and_then(|(_, attr)| Either::left(attr))
}
impl TokenExpander {
@@ -504,53 +523,54 @@ fn macro_expand(
) -> ExpandResult<CowArc<tt::Subtree>> {
let _p = tracing::span!(tracing::Level::INFO, "macro_expand").entered();
- let ExpandResult { value: tt, err } = match loc.def.kind {
+ let (ExpandResult { value: tt, err }, span) = match loc.def.kind {
MacroDefKind::ProcMacro(..) => return db.expand_proc_macro(macro_call_id).map(CowArc::Arc),
_ => {
- let (macro_arg, undo_info) = db.macro_arg(macro_call_id);
+ let (macro_arg, undo_info, span) = db.macro_arg(macro_call_id);
let arg = &*macro_arg;
- let res = match loc.def.kind {
- MacroDefKind::Declarative(id) => {
- db.decl_macro_expander(loc.def.krate, id).expand(db, arg.clone(), macro_call_id)
- }
- MacroDefKind::BuiltIn(it, _) => {
- it.expand(db, macro_call_id, arg).map_err(Into::into)
- }
- MacroDefKind::BuiltInDerive(it, _) => {
- it.expand(db, macro_call_id, arg).map_err(Into::into)
- }
- MacroDefKind::BuiltInEager(it, _) => {
- // This might look a bit odd, but we do not expand the inputs to eager macros here.
- // Eager macros inputs are expanded, well, eagerly when we collect the macro calls.
- // That kind of expansion uses the ast id map of an eager macros input though which goes through
- // the HirFileId machinery. As eager macro inputs are assigned a macro file id that query
- // will end up going through here again, whereas we want to just want to inspect the raw input.
- // As such we just return the input subtree here.
- let eager = match &loc.kind {
- MacroCallKind::FnLike { eager: None, .. } => {
- return ExpandResult::ok(CowArc::Arc(macro_arg.clone()));
+ let res =
+ match loc.def.kind {
+ MacroDefKind::Declarative(id) => db
+ .decl_macro_expander(loc.def.krate, id)
+ .expand(db, arg.clone(), macro_call_id, span),
+ MacroDefKind::BuiltIn(it, _) => {
+ it.expand(db, macro_call_id, arg, span).map_err(Into::into)
+ }
+ MacroDefKind::BuiltInDerive(it, _) => {
+ it.expand(db, macro_call_id, arg, span).map_err(Into::into)
+ }
+ MacroDefKind::BuiltInEager(it, _) => {
+ // This might look a bit odd, but we do not expand the inputs to eager macros here.
+ // Eager macros inputs are expanded, well, eagerly when we collect the macro calls.
+ // That kind of expansion uses the ast id map of an eager macros input though which goes through
+ // the HirFileId machinery. As eager macro inputs are assigned a macro file id that query
+ // will end up going through here again, whereas we want to just want to inspect the raw input.
+ // As such we just return the input subtree here.
+ let eager = match &loc.kind {
+ MacroCallKind::FnLike { eager: None, .. } => {
+ return ExpandResult::ok(CowArc::Arc(macro_arg.clone()));
+ }
+ MacroCallKind::FnLike { eager: Some(eager), .. } => Some(&**eager),
+ _ => None,
+ };
+
+ let mut res = it.expand(db, macro_call_id, arg, span).map_err(Into::into);
+
+ if let Some(EagerCallInfo { error, .. }) = eager {
+ // FIXME: We should report both errors!
+ res.err = error.clone().or(res.err);
}
- MacroCallKind::FnLike { eager: Some(eager), .. } => Some(&**eager),
- _ => None,
- };
-
- let mut res = it.expand(db, macro_call_id, arg).map_err(Into::into);
-
- if let Some(EagerCallInfo { error, .. }) = eager {
- // FIXME: We should report both errors!
- res.err = error.clone().or(res.err);
+ res
}
- res
- }
- MacroDefKind::BuiltInAttr(it, _) => {
- let mut res = it.expand(db, macro_call_id, arg);
- fixup::reverse_fixups(&mut res.value, &undo_info);
- res
- }
- _ => unreachable!(),
- };
- ExpandResult { value: res.value, err: res.err }
+ MacroDefKind::BuiltInAttr(it, _) => {
+ let mut res = it.expand(db, macro_call_id, arg, span);
+ fixup::reverse_fixups(&mut res.value, &undo_info);
+ res
+ }
+ _ => unreachable!(),
+ };
+ (ExpandResult { value: res.value, err: res.err }, span)
}
};
@@ -560,7 +580,7 @@ fn macro_expand(
if let Err(value) = check_tt_count(&tt) {
return value.map(|()| {
CowArc::Owned(tt::Subtree {
- delimiter: tt::Delimiter::invisible_spanned(loc.call_site),
+ delimiter: tt::Delimiter::invisible_spanned(span),
token_trees: Box::new([]),
})
});
@@ -583,7 +603,7 @@ fn proc_macro_span(db: &dyn ExpandDatabase, ast: AstId<ast::Fn>) -> Span {
fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<Arc<tt::Subtree>> {
let loc = db.lookup_intern_macro_call(id);
- let (macro_arg, undo_info) = db.macro_arg(id);
+ let (macro_arg, undo_info, span) = db.macro_arg(id);
let (expander, ast) = match loc.def.kind {
MacroDefKind::ProcMacro(expander, _, ast) => (expander, ast),
@@ -595,23 +615,25 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<A
_ => None,
};
- let span = db.proc_macro_span(ast);
- let ExpandResult { value: mut tt, err } = expander.expand(
- db,
- loc.def.krate,
- loc.krate,
- &macro_arg,
- attr_arg,
- span_with_def_site_ctxt(db, span, id),
- span_with_call_site_ctxt(db, span, id),
- span_with_mixed_site_ctxt(db, span, id),
- );
+ let ExpandResult { value: mut tt, err } = {
+ let span = db.proc_macro_span(ast);
+ expander.expand(
+ db,
+ loc.def.krate,
+ loc.krate,
+ &macro_arg,
+ attr_arg,
+ span_with_def_site_ctxt(db, span, id),
+ span_with_call_site_ctxt(db, span, id),
+ span_with_mixed_site_ctxt(db, span, id),
+ )
+ };
// Set a hard limit for the expanded tt
if let Err(value) = check_tt_count(&tt) {
return value.map(|()| {
Arc::new(tt::Subtree {
- delimiter: tt::Delimiter::invisible_spanned(loc.call_site),
+ delimiter: tt::Delimiter::invisible_spanned(span),
token_trees: Box::new([]),
})
});