mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Simplify
This commit is contained in:
parent
abe3177445
commit
9767156a29
4 changed files with 33 additions and 24 deletions
|
@ -1403,12 +1403,15 @@ fn macro_call_as_call_id_with_eager(
|
||||||
resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
|
resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
|
||||||
|
|
||||||
let res = match def.kind {
|
let res = match def.kind {
|
||||||
MacroDefKind::BuiltInEager(..) => {
|
MacroDefKind::BuiltInEager(..) => expand_eager_macro_input(
|
||||||
let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db));
|
db,
|
||||||
expand_eager_macro_input(db, krate, macro_call, def, call_site, &|path| {
|
krate,
|
||||||
eager_resolver(path).filter(MacroDefId::is_fn_like)
|
&call.ast_id.to_node(db),
|
||||||
})
|
call.ast_id,
|
||||||
}
|
def,
|
||||||
|
call_site,
|
||||||
|
&|path| eager_resolver(path).filter(MacroDefId::is_fn_like),
|
||||||
|
),
|
||||||
_ if def.is_fn_like() => ExpandResult {
|
_ if def.is_fn_like() => ExpandResult {
|
||||||
value: Some(def.make_call(
|
value: Some(def.make_call(
|
||||||
db,
|
db,
|
||||||
|
|
|
@ -117,7 +117,7 @@ fn derive_expand(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pseudo_derive_attr_expansion(
|
pub fn pseudo_derive_attr_expansion(
|
||||||
tt: &tt::Subtree,
|
_: &tt::Subtree,
|
||||||
args: &tt::Subtree,
|
args: &tt::Subtree,
|
||||||
call_site: Span,
|
call_site: Span,
|
||||||
) -> ExpandResult<tt::Subtree> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
|
@ -141,7 +141,7 @@ pub fn pseudo_derive_attr_expansion(
|
||||||
token_trees.push(mk_leaf(']'));
|
token_trees.push(mk_leaf(']'));
|
||||||
}
|
}
|
||||||
ExpandResult::ok(tt::Subtree {
|
ExpandResult::ok(tt::Subtree {
|
||||||
delimiter: tt.delimiter,
|
delimiter: args.delimiter,
|
||||||
token_trees: token_trees.into_boxed_slice(),
|
token_trees: token_trees.into_boxed_slice(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,6 +146,10 @@ 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::Attr { .. } if loc.def.is_attribute_derive() => (
|
||||||
|
mbe::syntax_node_to_token_tree(speculative_args, span_map, loc.call_site),
|
||||||
|
SyntaxFixupUndoInfo::NONE,
|
||||||
|
),
|
||||||
MacroCallKind::Derive { derive_attr_index: index, .. }
|
MacroCallKind::Derive { derive_attr_index: index, .. }
|
||||||
| MacroCallKind::Attr { invoc_attr_index: index, .. } => {
|
| MacroCallKind::Attr { invoc_attr_index: index, .. } => {
|
||||||
let censor = if let MacroCallKind::Derive { .. } = loc.kind {
|
let censor = if let MacroCallKind::Derive { .. } = loc.kind {
|
||||||
|
@ -406,7 +410,11 @@ fn macro_arg(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let 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(), loc.call_site);
|
||||||
|
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;
|
||||||
|
}
|
||||||
let val = (Arc::new(tt), SyntaxFixupUndoInfo::NONE);
|
let val = (Arc::new(tt), SyntaxFixupUndoInfo::NONE);
|
||||||
return if matches!(loc.def.kind, MacroDefKind::BuiltInEager(..)) {
|
return if matches!(loc.def.kind, MacroDefKind::BuiltInEager(..)) {
|
||||||
match parse.errors() {
|
match parse.errors() {
|
||||||
|
|
|
@ -27,21 +27,20 @@ use crate::{
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode},
|
||||||
db::ExpandDatabase,
|
db::ExpandDatabase,
|
||||||
mod_path::ModPath,
|
mod_path::ModPath,
|
||||||
EagerCallInfo, ExpandError, ExpandResult, ExpandTo, ExpansionSpanMap, InFile, Intern,
|
AstId, EagerCallInfo, ExpandError, ExpandResult, ExpandTo, ExpansionSpanMap, InFile, Intern,
|
||||||
MacroCallId, MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind,
|
MacroCallId, MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn expand_eager_macro_input(
|
pub fn expand_eager_macro_input(
|
||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
macro_call: InFile<ast::MacroCall>,
|
macro_call: &ast::MacroCall,
|
||||||
|
ast_id: AstId<ast::MacroCall>,
|
||||||
def: MacroDefId,
|
def: MacroDefId,
|
||||||
call_site: Span,
|
call_site: Span,
|
||||||
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
||||||
) -> ExpandResult<Option<MacroCallId>> {
|
) -> ExpandResult<Option<MacroCallId>> {
|
||||||
let ast_map = db.ast_id_map(macro_call.file_id);
|
let expand_to = ExpandTo::from_call_site(macro_call);
|
||||||
let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(¯o_call.value));
|
|
||||||
let expand_to = ExpandTo::from_call_site(¯o_call.value);
|
|
||||||
|
|
||||||
// Note:
|
// Note:
|
||||||
// When `lazy_expand` is called, its *parent* file must already exist.
|
// When `lazy_expand` is called, its *parent* file must already exist.
|
||||||
|
@ -50,7 +49,7 @@ pub fn expand_eager_macro_input(
|
||||||
let arg_id = MacroCallLoc {
|
let arg_id = MacroCallLoc {
|
||||||
def,
|
def,
|
||||||
krate,
|
krate,
|
||||||
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr, eager: None },
|
kind: MacroCallKind::FnLike { ast_id, expand_to: ExpandTo::Expr, eager: None },
|
||||||
call_site,
|
call_site,
|
||||||
}
|
}
|
||||||
.intern(db);
|
.intern(db);
|
||||||
|
@ -87,9 +86,8 @@ pub fn expand_eager_macro_input(
|
||||||
let loc = MacroCallLoc {
|
let loc = MacroCallLoc {
|
||||||
def,
|
def,
|
||||||
krate,
|
krate,
|
||||||
|
|
||||||
kind: MacroCallKind::FnLike {
|
kind: MacroCallKind::FnLike {
|
||||||
ast_id: call_id,
|
ast_id,
|
||||||
expand_to,
|
expand_to,
|
||||||
eager: Some(Arc::new(EagerCallInfo {
|
eager: Some(Arc::new(EagerCallInfo {
|
||||||
arg: Arc::new(subtree),
|
arg: Arc::new(subtree),
|
||||||
|
@ -106,14 +104,12 @@ pub fn expand_eager_macro_input(
|
||||||
fn lazy_expand(
|
fn lazy_expand(
|
||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
def: &MacroDefId,
|
def: &MacroDefId,
|
||||||
macro_call: InFile<ast::MacroCall>,
|
macro_call: &ast::MacroCall,
|
||||||
|
ast_id: AstId<ast::MacroCall>,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
call_site: Span,
|
call_site: Span,
|
||||||
) -> ExpandResult<(InFile<Parse<SyntaxNode>>, Arc<ExpansionSpanMap>)> {
|
) -> ExpandResult<(InFile<Parse<SyntaxNode>>, Arc<ExpansionSpanMap>)> {
|
||||||
let ast_id = db.ast_id_map(macro_call.file_id).ast_id(¯o_call.value);
|
let expand_to = ExpandTo::from_call_site(macro_call);
|
||||||
|
|
||||||
let expand_to = ExpandTo::from_call_site(¯o_call.value);
|
|
||||||
let ast_id = macro_call.with_value(ast_id);
|
|
||||||
let id = def.make_call(
|
let id = def.make_call(
|
||||||
db,
|
db,
|
||||||
krate,
|
krate,
|
||||||
|
@ -183,12 +179,14 @@ fn eager_macro_recur(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let ast_id = db.ast_id_map(curr.file_id).ast_id(&call);
|
||||||
let ExpandResult { value, err } = match def.kind {
|
let ExpandResult { value, err } = match def.kind {
|
||||||
MacroDefKind::BuiltInEager(..) => {
|
MacroDefKind::BuiltInEager(..) => {
|
||||||
let ExpandResult { value, err } = expand_eager_macro_input(
|
let ExpandResult { value, err } = expand_eager_macro_input(
|
||||||
db,
|
db,
|
||||||
krate,
|
krate,
|
||||||
curr.with_value(call.clone()),
|
&call,
|
||||||
|
curr.with_value(ast_id),
|
||||||
def,
|
def,
|
||||||
call_site,
|
call_site,
|
||||||
macro_resolver,
|
macro_resolver,
|
||||||
|
@ -218,7 +216,7 @@ fn eager_macro_recur(
|
||||||
| MacroDefKind::BuiltInDerive(..)
|
| MacroDefKind::BuiltInDerive(..)
|
||||||
| MacroDefKind::ProcMacro(..) => {
|
| MacroDefKind::ProcMacro(..) => {
|
||||||
let ExpandResult { value: (parse, tm), err } =
|
let ExpandResult { value: (parse, tm), err } =
|
||||||
lazy_expand(db, &def, curr.with_value(call.clone()), krate, call_site);
|
lazy_expand(db, &def, &call, curr.with_value(ast_id), krate, call_site);
|
||||||
|
|
||||||
// replace macro inside
|
// replace macro inside
|
||||||
let ExpandResult { value, err: error } = eager_macro_recur(
|
let ExpandResult { value, err: error } = eager_macro_recur(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue