Stronger typing for macro_arg query

This commit is contained in:
Lukas Wirth 2024-03-13 17:42:01 +01:00
parent e8182a5bb3
commit 87e0bbc534
2 changed files with 158 additions and 171 deletions

View File

@ -6,19 +6,16 @@
use mbe::{syntax_node_to_token_tree, ValueResult}; use mbe::{syntax_node_to_token_tree, ValueResult};
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use span::{AstIdMap, SyntaxContextData, SyntaxContextId}; use span::{AstIdMap, SyntaxContextData, SyntaxContextId};
use syntax::{ use syntax::{ast, AstNode, Parse, SyntaxElement, SyntaxError, SyntaxNode, SyntaxToken, T};
ast::{self, HasAttrs},
AstNode, Parse, SyntaxElement, SyntaxError, SyntaxNode, SyntaxToken, T,
};
use triomphe::Arc; use triomphe::Arc;
use crate::{ use crate::{
attrs::collect_attrs, attrs::{collect_attrs, AttrId},
builtin_attr_macro::pseudo_derive_attr_expansion, builtin_attr_macro::pseudo_derive_attr_expansion,
builtin_fn_macro::EagerExpander, builtin_fn_macro::EagerExpander,
cfg_process, cfg_process,
declarative::DeclarativeMacroExpander, declarative::DeclarativeMacroExpander,
fixup::{self, reverse_fixups, SyntaxFixupUndoInfo}, fixup::{self, SyntaxFixupUndoInfo},
hygiene::{span_with_call_site_ctxt, span_with_def_site_ctxt, span_with_mixed_site_ctxt}, hygiene::{span_with_call_site_ctxt, span_with_def_site_ctxt, span_with_mixed_site_ctxt},
proc_macro::ProcMacros, proc_macro::ProcMacros,
span_map::{RealSpanMap, SpanMap, SpanMapRef}, span_map::{RealSpanMap, SpanMap, SpanMapRef},
@ -149,8 +146,14 @@ pub fn expand_speculative(
mbe::syntax_node_to_token_tree(speculative_args, span_map, loc.call_site), mbe::syntax_node_to_token_tree(speculative_args, span_map, loc.call_site),
SyntaxFixupUndoInfo::NONE, SyntaxFixupUndoInfo::NONE,
), ),
MacroCallKind::Derive { .. } | MacroCallKind::Attr { .. } => { MacroCallKind::Derive { derive_attr_index: index, .. }
let censor = censor_for_macro_input(&loc, speculative_args); | MacroCallKind::Attr { invoc_attr_index: index, .. } => {
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())?)
};
let censor_cfg = let censor_cfg =
cfg_process::process_cfg_attrs(speculative_args, &loc, db).unwrap_or_default(); 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, loc.call_site);
@ -324,7 +327,8 @@ pub(crate) fn parse_with_map(
} }
} }
// FIXME: for derive attributes, this will return separate copies of the same structures! // 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( fn macro_arg(
db: &dyn ExpandDatabase, db: &dyn ExpandDatabase,
id: MacroCallId, id: MacroCallId,
@ -336,12 +340,13 @@ fn macro_arg(
.then(|| loc.eager.as_deref()) .then(|| loc.eager.as_deref())
.flatten() .flatten()
{ {
ValueResult::ok((arg.clone(), SyntaxFixupUndoInfo::NONE)) return ValueResult::ok((arg.clone(), SyntaxFixupUndoInfo::NONE));
} else { }
let (parse, map) = parse_with_map(db, loc.kind.file_id()); let (parse, map) = parse_with_map(db, loc.kind.file_id());
let root = parse.syntax_node(); let root = parse.syntax_node();
let syntax = match loc.kind { let (censor, item_node) = match loc.kind {
MacroCallKind::FnLike { ast_id, .. } => { MacroCallKind::FnLike { ast_id, .. } => {
let dummy_tt = |kind| { let dummy_tt = |kind| {
( (
@ -399,23 +404,36 @@ fn macro_arg(
)])), )])),
); );
} }
tt.syntax().clone()
} let tt = mbe::syntax_node_to_token_tree(tt.syntax(), map.as_ref(), loc.call_site);
MacroCallKind::Derive { ast_id, .. } => { let val = (Arc::new(tt), SyntaxFixupUndoInfo::NONE);
ast_id.to_ptr(db).to_node(&root).syntax().clone() return if matches!(loc.def.kind, MacroDefKind::BuiltInEager(..)) {
} match parse.errors() {
MacroCallKind::Attr { ast_id, .. } => ast_id.to_ptr(db).to_node(&root).syntax().clone(), errors if errors.is_empty() => ValueResult::ok(val),
}; errors => ValueResult::new(
let (mut tt, undo_info) = match loc.kind { val,
MacroCallKind::FnLike { .. } => ( // Box::<[_]>::from(res.errors()), not stable yet
mbe::syntax_node_to_token_tree(&syntax, map.as_ref(), loc.call_site), Arc::new(errors.to_vec().into_boxed_slice()),
SyntaxFixupUndoInfo::NONE,
), ),
MacroCallKind::Derive { .. } | MacroCallKind::Attr { .. } => { }
let censor = censor_for_macro_input(&loc, &syntax); } else {
let censor_cfg = ValueResult::ok(val)
cfg_process::process_cfg_attrs(&syntax, &loc, db).unwrap_or_default(); };
let mut fixups = fixup::fixup_syntax(map.as_ref(), &syntax, loc.call_site); }
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())
}
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 (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);
fixups.append.retain(|it, _| match it { fixups.append.retain(|it, _| match it {
syntax::NodeOrToken::Token(_) => true, syntax::NodeOrToken::Token(_) => true,
it => !censor.contains(it) && !censor_cfg.contains(it), it => !censor.contains(it) && !censor_cfg.contains(it),
@ -423,19 +441,9 @@ fn macro_arg(
fixups.remove.extend(censor); fixups.remove.extend(censor);
fixups.remove.extend(censor_cfg); fixups.remove.extend(censor_cfg);
{
let mut tt = mbe::syntax_node_to_token_tree_modified(
&syntax,
map.as_ref(),
fixups.append.clone(),
fixups.remove.clone(),
loc.call_site,
);
reverse_fixups(&mut tt, &fixups.undo_info);
}
( (
mbe::syntax_node_to_token_tree_modified( mbe::syntax_node_to_token_tree_modified(
&syntax, syntax,
map, map,
fixups.append, fixups.append,
fixups.remove, fixups.remove,
@ -443,7 +451,6 @@ fn macro_arg(
), ),
fixups.undo_info, fixups.undo_info,
) )
}
}; };
if loc.def.is_proc_macro() { if loc.def.is_proc_macro() {
@ -451,35 +458,17 @@ fn macro_arg(
tt.delimiter.kind = tt::DelimiterKind::Invisible; tt.delimiter.kind = tt::DelimiterKind::Invisible;
} }
if matches!(loc.def.kind, MacroDefKind::BuiltInEager(..)) {
match parse.errors() {
errors if errors.is_empty() => ValueResult::ok((Arc::new(tt), undo_info)),
errors => ValueResult::new(
(Arc::new(tt), undo_info),
// Box::<[_]>::from(res.errors()), not stable yet
Arc::new(errors.to_vec().into_boxed_slice()),
),
}
} else {
ValueResult::ok((Arc::new(tt), undo_info)) ValueResult::ok((Arc::new(tt), undo_info))
}
}
} }
// FIXME: Censoring info should be calculated by the caller! Namely by name resolution // FIXME: Censoring info should be calculated by the caller! Namely by name resolution
/// Certain macro calls expect some nodes in the input to be preprocessed away, namely: /// Derives expect all `#[derive(..)]` invocations up to the currently invoked one to be stripped
/// - derives expect all `#[derive(..)]` invocations up to the currently invoked one to be stripped fn censor_derive_input(derive_attr_index: AttrId, node: &ast::Adt) -> FxHashSet<SyntaxElement> {
/// - attributes expect the invoking attribute to be stripped
fn censor_for_macro_input(loc: &MacroCallLoc, node: &SyntaxNode) -> FxHashSet<SyntaxElement> {
// FIXME: handle `cfg_attr` // FIXME: handle `cfg_attr`
(|| {
let censor = match loc.kind {
MacroCallKind::FnLike { .. } => return None,
MacroCallKind::Derive { derive_attr_index, .. } => {
cov_mark::hit!(derive_censoring); cov_mark::hit!(derive_censoring);
ast::Item::cast(node.clone())? collect_attrs(node)
.attrs()
.take(derive_attr_index.ast_index() + 1) .take(derive_attr_index.ast_index() + 1)
.filter_map(|(_, attr)| Either::left(attr))
// FIXME, this resolution should not be done syntactically // FIXME, this resolution should not be done syntactically
// derive is a proper macro now, no longer builtin // derive is a proper macro now, no longer builtin
// But we do not have resolution at this stage, this means // But we do not have resolution at this stage, this means
@ -488,21 +477,18 @@ fn censor_for_macro_input(loc: &MacroCallLoc, node: &SyntaxNode) -> FxHashSet<Sy
.filter(|attr| attr.simple_name().as_deref() == Some("derive")) .filter(|attr| attr.simple_name().as_deref() == Some("derive"))
.map(|it| it.syntax().clone().into()) .map(|it| it.syntax().clone().into())
.collect() .collect()
} }
MacroCallKind::Attr { .. } if loc.def.is_attribute_derive() => return None,
MacroCallKind::Attr { invoc_attr_index, .. } => { /// Attributes expect the invoking attribute to be stripped\
fn censor_attr_input(invoc_attr_index: AttrId, node: &ast::Item) -> FxHashSet<SyntaxElement> {
// FIXME: handle `cfg_attr`
cov_mark::hit!(attribute_macro_attr_censoring); cov_mark::hit!(attribute_macro_attr_censoring);
collect_attrs(&ast::Item::cast(node.clone())?) collect_attrs(node)
.nth(invoc_attr_index.ast_index()) .nth(invoc_attr_index.ast_index())
.and_then(|x| Either::left(x.1)) .and_then(|(_, attr)| Either::left(attr))
.map(|attr| attr.syntax().clone().into()) .map(|attr| attr.syntax().clone().into())
.into_iter() .into_iter()
.collect() .collect()
}
};
Some(censor)
})()
.unwrap_or_default()
} }
impl TokenExpander { impl TokenExpander {

View File

@ -176,6 +176,7 @@ pub struct MacroCallLoc {
// leakage problems here // leakage problems here
eager: Option<Arc<EagerCallInfo>>, eager: Option<Arc<EagerCallInfo>>,
pub kind: MacroCallKind, pub kind: MacroCallKind,
// FIXME: Spans while relative to an anchor, are still rather unstable
pub call_site: Span, pub call_site: Span,
} }
impl_intern_value_trivial!(MacroCallLoc); impl_intern_value_trivial!(MacroCallLoc);