mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +00:00
Shrink MacroCallLoc
This commit is contained in:
parent
87e0bbc534
commit
abe3177445
7 changed files with 69 additions and 45 deletions
|
@ -745,6 +745,7 @@ impl<'a> AssocItemCollector<'a> {
|
||||||
self.collect_macro_items(res, &|| hir_expand::MacroCallKind::FnLike {
|
self.collect_macro_items(res, &|| hir_expand::MacroCallKind::FnLike {
|
||||||
ast_id: InFile::new(file_id, ast_id),
|
ast_id: InFile::new(file_id, ast_id),
|
||||||
expand_to: hir_expand::ExpandTo::Items,
|
expand_to: hir_expand::ExpandTo::Items,
|
||||||
|
eager: None,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Ok(None) => (),
|
Ok(None) => (),
|
||||||
|
@ -754,6 +755,7 @@ impl<'a> AssocItemCollector<'a> {
|
||||||
MacroCallKind::FnLike {
|
MacroCallKind::FnLike {
|
||||||
ast_id: InFile::new(file_id, ast_id),
|
ast_id: InFile::new(file_id, ast_id),
|
||||||
expand_to,
|
expand_to,
|
||||||
|
eager: None,
|
||||||
},
|
},
|
||||||
Clone::clone(path),
|
Clone::clone(path),
|
||||||
));
|
));
|
||||||
|
|
|
@ -1410,10 +1410,10 @@ fn macro_call_as_call_id_with_eager(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_ if def.is_fn_like() => ExpandResult {
|
_ if def.is_fn_like() => ExpandResult {
|
||||||
value: Some(def.as_lazy_macro(
|
value: Some(def.make_call(
|
||||||
db,
|
db,
|
||||||
krate,
|
krate,
|
||||||
MacroCallKind::FnLike { ast_id: call.ast_id, expand_to },
|
MacroCallKind::FnLike { ast_id: call.ast_id, expand_to, eager: None },
|
||||||
call_site,
|
call_site,
|
||||||
)),
|
)),
|
||||||
err: None,
|
err: None,
|
||||||
|
|
|
@ -116,7 +116,7 @@ pub(super) fn attr_macro_as_call_id(
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
def.as_lazy_macro(
|
def.make_call(
|
||||||
db.upcast(),
|
db.upcast(),
|
||||||
krate,
|
krate,
|
||||||
MacroCallKind::Attr {
|
MacroCallKind::Attr {
|
||||||
|
@ -140,7 +140,7 @@ pub(super) fn derive_macro_as_call_id(
|
||||||
let (macro_id, def_id) = resolver(item_attr.path.clone())
|
let (macro_id, def_id) = resolver(item_attr.path.clone())
|
||||||
.filter(|(_, def_id)| def_id.is_derive())
|
.filter(|(_, def_id)| def_id.is_derive())
|
||||||
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
|
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
|
||||||
let call_id = def_id.as_lazy_macro(
|
let call_id = def_id.make_call(
|
||||||
db.upcast(),
|
db.upcast(),
|
||||||
krate,
|
krate,
|
||||||
MacroCallKind::Derive {
|
MacroCallKind::Derive {
|
||||||
|
|
|
@ -1451,7 +1451,11 @@ impl DefCollector<'_> {
|
||||||
if let Err(UnresolvedMacro { path }) = macro_call_as_call_id {
|
if let Err(UnresolvedMacro { path }) = macro_call_as_call_id {
|
||||||
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
|
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
|
||||||
directive.module_id,
|
directive.module_id,
|
||||||
MacroCallKind::FnLike { ast_id: ast_id.ast_id, expand_to: *expand_to },
|
MacroCallKind::FnLike {
|
||||||
|
ast_id: ast_id.ast_id,
|
||||||
|
expand_to: *expand_to,
|
||||||
|
eager: None,
|
||||||
|
},
|
||||||
path,
|
path,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
|
@ -332,15 +332,16 @@ pub(crate) fn parse_with_map(
|
||||||
fn macro_arg(
|
fn macro_arg(
|
||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
id: MacroCallId,
|
id: MacroCallId,
|
||||||
// FIXME: consider the following by putting fixup info into eager call info args
|
|
||||||
// ) -> ValueResult<Arc<(tt::Subtree, SyntaxFixupUndoInfo)>, Arc<Box<[SyntaxError]>>> {
|
|
||||||
) -> ValueResult<(Arc<tt::Subtree>, SyntaxFixupUndoInfo), Arc<Box<[SyntaxError]>>> {
|
) -> ValueResult<(Arc<tt::Subtree>, SyntaxFixupUndoInfo), Arc<Box<[SyntaxError]>>> {
|
||||||
let loc = db.lookup_intern_macro_call(id);
|
let loc = db.lookup_intern_macro_call(id);
|
||||||
if let Some(EagerCallInfo { arg, .. }) = matches!(loc.def.kind, MacroDefKind::BuiltInEager(..))
|
|
||||||
.then(|| loc.eager.as_deref())
|
if let MacroCallLoc {
|
||||||
.flatten()
|
def: MacroDefId { kind: MacroDefKind::BuiltInEager(..), .. },
|
||||||
|
kind: MacroCallKind::FnLike { eager: Some(eager), .. },
|
||||||
|
..
|
||||||
|
} = &loc
|
||||||
{
|
{
|
||||||
return ValueResult::ok((arg.clone(), SyntaxFixupUndoInfo::NONE));
|
return ValueResult::ok((eager.arg.clone(), SyntaxFixupUndoInfo::NONE));
|
||||||
}
|
}
|
||||||
|
|
||||||
let (parse, map) = parse_with_map(db, loc.kind.file_id());
|
let (parse, map) = parse_with_map(db, loc.kind.file_id());
|
||||||
|
@ -518,7 +519,7 @@ fn macro_expand(
|
||||||
) -> ExpandResult<CowArc<tt::Subtree>> {
|
) -> ExpandResult<CowArc<tt::Subtree>> {
|
||||||
let _p = tracing::span!(tracing::Level::INFO, "macro_expand").entered();
|
let _p = tracing::span!(tracing::Level::INFO, "macro_expand").entered();
|
||||||
|
|
||||||
let ExpandResult { value: tt, mut err } = match loc.def.kind {
|
let ExpandResult { value: tt, err } = match loc.def.kind {
|
||||||
MacroDefKind::ProcMacro(..) => return db.expand_proc_macro(macro_call_id).map(CowArc::Arc),
|
MacroDefKind::ProcMacro(..) => return db.expand_proc_macro(macro_call_id).map(CowArc::Arc),
|
||||||
_ => {
|
_ => {
|
||||||
let ValueResult { value: (macro_arg, undo_info), err } = db.macro_arg(macro_call_id);
|
let ValueResult { value: (macro_arg, undo_info), err } = db.macro_arg(macro_call_id);
|
||||||
|
@ -541,23 +542,34 @@ fn macro_expand(
|
||||||
MacroDefKind::BuiltIn(it, _) => {
|
MacroDefKind::BuiltIn(it, _) => {
|
||||||
it.expand(db, macro_call_id, arg).map_err(Into::into)
|
it.expand(db, macro_call_id, arg).map_err(Into::into)
|
||||||
}
|
}
|
||||||
// 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.
|
|
||||||
MacroDefKind::BuiltInEager(..) if loc.eager.is_none() => {
|
|
||||||
return ExpandResult {
|
|
||||||
value: CowArc::Arc(macro_arg.clone()),
|
|
||||||
err: err.map(format_parse_err),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
MacroDefKind::BuiltInDerive(it, _) => {
|
MacroDefKind::BuiltInDerive(it, _) => {
|
||||||
it.expand(db, macro_call_id, arg).map_err(Into::into)
|
it.expand(db, macro_call_id, arg).map_err(Into::into)
|
||||||
}
|
}
|
||||||
MacroDefKind::BuiltInEager(it, _) => {
|
MacroDefKind::BuiltInEager(it, _) => {
|
||||||
it.expand(db, macro_call_id, arg).map_err(Into::into)
|
// 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 {
|
||||||
|
value: CowArc::Arc(macro_arg.clone()),
|
||||||
|
err: err.map(format_parse_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
|
||||||
}
|
}
|
||||||
MacroDefKind::BuiltInAttr(it, _) => {
|
MacroDefKind::BuiltInAttr(it, _) => {
|
||||||
let mut res = it.expand(db, macro_call_id, arg);
|
let mut res = it.expand(db, macro_call_id, arg);
|
||||||
|
@ -574,11 +586,6 @@ fn macro_expand(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(EagerCallInfo { error, .. }) = loc.eager.as_deref() {
|
|
||||||
// FIXME: We should report both errors!
|
|
||||||
err = error.clone().or(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip checking token tree limit for include! macro call
|
// Skip checking token tree limit for include! macro call
|
||||||
if !loc.def.is_include() {
|
if !loc.def.is_include() {
|
||||||
// Set a hard limit for the expanded tt
|
// Set a hard limit for the expanded tt
|
||||||
|
|
|
@ -40,7 +40,6 @@ pub fn expand_eager_macro_input(
|
||||||
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 ast_map = db.ast_id_map(macro_call.file_id);
|
||||||
// the expansion which the ast id map is built upon has no whitespace, so the offsets are wrong as macro_call is from the token tree that has whitespace!
|
|
||||||
let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(¯o_call.value));
|
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);
|
let expand_to = ExpandTo::from_call_site(¯o_call.value);
|
||||||
|
|
||||||
|
@ -51,8 +50,7 @@ pub fn expand_eager_macro_input(
|
||||||
let arg_id = MacroCallLoc {
|
let arg_id = MacroCallLoc {
|
||||||
def,
|
def,
|
||||||
krate,
|
krate,
|
||||||
eager: None,
|
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr, eager: None },
|
||||||
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr },
|
|
||||||
call_site,
|
call_site,
|
||||||
}
|
}
|
||||||
.intern(db);
|
.intern(db);
|
||||||
|
@ -89,8 +87,16 @@ pub fn expand_eager_macro_input(
|
||||||
let loc = MacroCallLoc {
|
let loc = MacroCallLoc {
|
||||||
def,
|
def,
|
||||||
krate,
|
krate,
|
||||||
eager: Some(Arc::new(EagerCallInfo { arg: Arc::new(subtree), arg_id, error: err.clone() })),
|
|
||||||
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
|
kind: MacroCallKind::FnLike {
|
||||||
|
ast_id: call_id,
|
||||||
|
expand_to,
|
||||||
|
eager: Some(Arc::new(EagerCallInfo {
|
||||||
|
arg: Arc::new(subtree),
|
||||||
|
arg_id,
|
||||||
|
error: err.clone(),
|
||||||
|
})),
|
||||||
|
},
|
||||||
call_site,
|
call_site,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -108,7 +114,12 @@ fn lazy_expand(
|
||||||
|
|
||||||
let expand_to = ExpandTo::from_call_site(¯o_call.value);
|
let expand_to = ExpandTo::from_call_site(¯o_call.value);
|
||||||
let ast_id = macro_call.with_value(ast_id);
|
let ast_id = macro_call.with_value(ast_id);
|
||||||
let id = def.as_lazy_macro(db, krate, MacroCallKind::FnLike { ast_id, expand_to }, call_site);
|
let id = def.make_call(
|
||||||
|
db,
|
||||||
|
krate,
|
||||||
|
MacroCallKind::FnLike { ast_id, expand_to, eager: None },
|
||||||
|
call_site,
|
||||||
|
);
|
||||||
let macro_file = id.as_macro_file();
|
let macro_file = id.as_macro_file();
|
||||||
|
|
||||||
db.parse_macro_expansion(macro_file)
|
db.parse_macro_expansion(macro_file)
|
||||||
|
|
|
@ -170,11 +170,6 @@ impl fmt::Display for ExpandError {
|
||||||
pub struct MacroCallLoc {
|
pub struct MacroCallLoc {
|
||||||
pub def: MacroDefId,
|
pub def: MacroDefId,
|
||||||
pub krate: CrateId,
|
pub krate: CrateId,
|
||||||
/// Some if this is a macro call for an eager macro. Note that this is `None`
|
|
||||||
/// for the eager input macro file.
|
|
||||||
// FIXME: This is being interned, subtrees can vary quickly differ just slightly causing
|
|
||||||
// leakage problems here
|
|
||||||
eager: Option<Arc<EagerCallInfo>>,
|
|
||||||
pub kind: MacroCallKind,
|
pub kind: MacroCallKind,
|
||||||
// FIXME: Spans while relative to an anchor, are still rather unstable
|
// FIXME: Spans while relative to an anchor, are still rather unstable
|
||||||
pub call_site: Span,
|
pub call_site: Span,
|
||||||
|
@ -215,6 +210,11 @@ pub enum MacroCallKind {
|
||||||
FnLike {
|
FnLike {
|
||||||
ast_id: AstId<ast::MacroCall>,
|
ast_id: AstId<ast::MacroCall>,
|
||||||
expand_to: ExpandTo,
|
expand_to: ExpandTo,
|
||||||
|
/// Some if this is a macro call for an eager macro. Note that this is `None`
|
||||||
|
/// for the eager input macro file.
|
||||||
|
// FIXME: This is being interned, subtrees can vary quickly differ just slightly causing
|
||||||
|
// leakage problems here
|
||||||
|
eager: Option<Arc<EagerCallInfo>>,
|
||||||
},
|
},
|
||||||
Derive {
|
Derive {
|
||||||
ast_id: AstId<ast::Adt>,
|
ast_id: AstId<ast::Adt>,
|
||||||
|
@ -276,7 +276,7 @@ impl HirFileIdExt for HirFileId {
|
||||||
HirFileIdRepr::MacroFile(file) => {
|
HirFileIdRepr::MacroFile(file) => {
|
||||||
let loc = db.lookup_intern_macro_call(file.macro_call_id);
|
let loc = db.lookup_intern_macro_call(file.macro_call_id);
|
||||||
if loc.def.is_include() {
|
if loc.def.is_include() {
|
||||||
if let Some(eager) = &loc.eager {
|
if let MacroCallKind::FnLike { eager: Some(eager), .. } = &loc.kind {
|
||||||
if let Ok(it) = builtin_fn_macro::include_input_to_file_id(
|
if let Ok(it) = builtin_fn_macro::include_input_to_file_id(
|
||||||
db,
|
db,
|
||||||
file.macro_call_id,
|
file.macro_call_id,
|
||||||
|
@ -406,14 +406,14 @@ impl MacroFileIdExt for MacroFileId {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MacroDefId {
|
impl MacroDefId {
|
||||||
pub fn as_lazy_macro(
|
pub fn make_call(
|
||||||
self,
|
self,
|
||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
kind: MacroCallKind,
|
kind: MacroCallKind,
|
||||||
call_site: Span,
|
call_site: Span,
|
||||||
) -> MacroCallId {
|
) -> MacroCallId {
|
||||||
MacroCallLoc { def: self, krate, eager: None, kind, call_site }.intern(db)
|
MacroCallLoc { def: self, krate, kind, call_site }.intern(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn definition_range(&self, db: &dyn ExpandDatabase) -> InFile<TextRange> {
|
pub fn definition_range(&self, db: &dyn ExpandDatabase) -> InFile<TextRange> {
|
||||||
|
@ -535,7 +535,7 @@ impl MacroCallLoc {
|
||||||
macro_call_id: MacroCallId,
|
macro_call_id: MacroCallId,
|
||||||
) -> Option<FileId> {
|
) -> Option<FileId> {
|
||||||
if self.def.is_include() {
|
if self.def.is_include() {
|
||||||
if let Some(eager) = &self.eager {
|
if let MacroCallKind::FnLike { eager: Some(eager), .. } = &self.kind {
|
||||||
if let Ok(it) =
|
if let Ok(it) =
|
||||||
builtin_fn_macro::include_input_to_file_id(db, macro_call_id, &eager.arg)
|
builtin_fn_macro::include_input_to_file_id(db, macro_call_id, &eager.arg)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue