mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Don't bail eager expansion when inner macros fail to resolve
This commit is contained in:
parent
c7b34e4873
commit
d999d34e39
3 changed files with 46 additions and 18 deletions
|
@ -1160,7 +1160,7 @@ fn macro_call_as_call_id_(
|
||||||
let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db));
|
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, &|path| {
|
||||||
resolver(path).filter(MacroDefId::is_fn_like)
|
resolver(path).filter(MacroDefId::is_fn_like)
|
||||||
})?
|
})
|
||||||
}
|
}
|
||||||
_ if def.is_fn_like() => ExpandResult {
|
_ if def.is_fn_like() => ExpandResult {
|
||||||
value: Some(def.as_lazy_macro(
|
value: Some(def.as_lazy_macro(
|
||||||
|
|
|
@ -99,6 +99,30 @@ fn#19 main#20(#21)#21 {#22
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn eager_expands_with_unresolved_within() {
|
||||||
|
check(
|
||||||
|
r#"
|
||||||
|
#[rustc_builtin_macro]
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! format_args {}
|
||||||
|
|
||||||
|
fn main(foo: ()) {
|
||||||
|
format_args!("{} {} {}", format_args!("{}", 0), foo, identity!(10), "bar")
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r##"
|
||||||
|
#[rustc_builtin_macro]
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! format_args {}
|
||||||
|
|
||||||
|
fn main(foo: ()) {
|
||||||
|
/* error: unresolved macro identity */::core::fmt::Arguments::new_v1(&["", " ", " ", ], &[::core::fmt::ArgumentV1::new(&(::core::fmt::Arguments::new_v1(&["", ], &[::core::fmt::ArgumentV1::new(&(0), ::core::fmt::Display::fmt), ])), ::core::fmt::Display::fmt), ::core::fmt::ArgumentV1::new(&(foo), ::core::fmt::Display::fmt), ::core::fmt::ArgumentV1::new(&(identity!(10)), ::core::fmt::Display::fmt), ])
|
||||||
|
}
|
||||||
|
"##]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn token_mapping_eager() {
|
fn token_mapping_eager() {
|
||||||
check(
|
check(
|
||||||
|
|
|
@ -29,7 +29,7 @@ use crate::{
|
||||||
hygiene::Hygiene,
|
hygiene::Hygiene,
|
||||||
mod_path::ModPath,
|
mod_path::ModPath,
|
||||||
EagerCallInfo, ExpandError, ExpandResult, ExpandTo, InFile, MacroCallId, MacroCallKind,
|
EagerCallInfo, ExpandError, ExpandResult, ExpandTo, InFile, MacroCallId, MacroCallKind,
|
||||||
MacroCallLoc, MacroDefId, MacroDefKind, UnresolvedMacro,
|
MacroCallLoc, MacroDefId, MacroDefKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn expand_eager_macro_input(
|
pub fn expand_eager_macro_input(
|
||||||
|
@ -38,7 +38,7 @@ pub fn expand_eager_macro_input(
|
||||||
macro_call: InFile<ast::MacroCall>,
|
macro_call: InFile<ast::MacroCall>,
|
||||||
def: MacroDefId,
|
def: MacroDefId,
|
||||||
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
||||||
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
|
) -> 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!
|
// 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));
|
||||||
|
@ -71,12 +71,12 @@ pub fn expand_eager_macro_input(
|
||||||
InFile::new(arg_id.as_file(), arg_exp.syntax_node()),
|
InFile::new(arg_id.as_file(), arg_exp.syntax_node()),
|
||||||
krate,
|
krate,
|
||||||
resolver,
|
resolver,
|
||||||
)?
|
)
|
||||||
};
|
};
|
||||||
let err = parse_err.or(err);
|
let err = parse_err.or(err);
|
||||||
|
|
||||||
let Some((expanded_eager_input, mapping)) = expanded_eager_input else {
|
let Some((expanded_eager_input, mapping)) = expanded_eager_input else {
|
||||||
return Ok(ExpandResult { value: None, err });
|
return ExpandResult { value: None, err };
|
||||||
};
|
};
|
||||||
|
|
||||||
let (mut subtree, expanded_eager_input_token_map) =
|
let (mut subtree, expanded_eager_input_token_map) =
|
||||||
|
@ -98,7 +98,7 @@ pub fn expand_eager_macro_input(
|
||||||
// remap from eager input expansion to original eager input
|
// remap from eager input expansion to original eager input
|
||||||
if let Some(&og_range) = ws_mapping.get(og_range) {
|
if let Some(&og_range) = ws_mapping.get(og_range) {
|
||||||
if let Some(og_token) = og_tmap.token_by_range(og_range) {
|
if let Some(og_token) = og_tmap.token_by_range(og_range) {
|
||||||
ids_used.insert(id);
|
ids_used.insert(og_token);
|
||||||
return og_token;
|
return og_token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,7 +124,7 @@ pub fn expand_eager_macro_input(
|
||||||
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
|
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(ExpandResult { value: Some(db.intern_macro_call(loc)), err })
|
ExpandResult { value: Some(db.intern_macro_call(loc)), err }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lazy_expand(
|
fn lazy_expand(
|
||||||
|
@ -150,13 +150,13 @@ fn eager_macro_recur(
|
||||||
curr: InFile<SyntaxNode>,
|
curr: InFile<SyntaxNode>,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
macro_resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
macro_resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
||||||
) -> Result<ExpandResult<Option<(SyntaxNode, FxHashMap<TextRange, TextRange>)>>, UnresolvedMacro> {
|
) -> ExpandResult<Option<(SyntaxNode, FxHashMap<TextRange, TextRange>)>> {
|
||||||
let original = curr.value.clone_for_update();
|
let original = curr.value.clone_for_update();
|
||||||
let mut mapping = FxHashMap::default();
|
let mut mapping = FxHashMap::default();
|
||||||
|
|
||||||
let mut replacements = Vec::new();
|
let mut replacements = Vec::new();
|
||||||
|
|
||||||
// Note: We only report a single error inside of eager expansions
|
// FIXME: We only report a single error inside of eager expansions
|
||||||
let mut error = None;
|
let mut error = None;
|
||||||
let mut offset = 0i32;
|
let mut offset = 0i32;
|
||||||
let apply_offset = |it: TextSize, offset: i32| {
|
let apply_offset = |it: TextSize, offset: i32| {
|
||||||
|
@ -187,7 +187,14 @@ fn eager_macro_recur(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let def = match call.path().and_then(|path| ModPath::from_src(db, path, hygiene)) {
|
let def = match call.path().and_then(|path| ModPath::from_src(db, path, hygiene)) {
|
||||||
Some(path) => macro_resolver(path.clone()).ok_or(UnresolvedMacro { path })?,
|
Some(path) => match macro_resolver(path.clone()) {
|
||||||
|
Some(def) => def,
|
||||||
|
None => {
|
||||||
|
error =
|
||||||
|
Some(ExpandError::other(format!("unresolved macro {}", path.display(db))));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
},
|
||||||
None => {
|
None => {
|
||||||
error = Some(ExpandError::other("malformed macro invocation"));
|
error = Some(ExpandError::other("malformed macro invocation"));
|
||||||
continue;
|
continue;
|
||||||
|
@ -195,16 +202,13 @@ fn eager_macro_recur(
|
||||||
};
|
};
|
||||||
let ExpandResult { value, err } = match def.kind {
|
let ExpandResult { value, err } = match def.kind {
|
||||||
MacroDefKind::BuiltInEager(..) => {
|
MacroDefKind::BuiltInEager(..) => {
|
||||||
let ExpandResult { value, err } = match expand_eager_macro_input(
|
let ExpandResult { value, err } = expand_eager_macro_input(
|
||||||
db,
|
db,
|
||||||
krate,
|
krate,
|
||||||
curr.with_value(call.clone()),
|
curr.with_value(call.clone()),
|
||||||
def,
|
def,
|
||||||
macro_resolver,
|
macro_resolver,
|
||||||
) {
|
);
|
||||||
Ok(it) => it,
|
|
||||||
Err(err) => return Err(err),
|
|
||||||
};
|
|
||||||
match value {
|
match value {
|
||||||
Some(call_id) => {
|
Some(call_id) => {
|
||||||
let ExpandResult { value, err: err2 } =
|
let ExpandResult { value, err: err2 } =
|
||||||
|
@ -254,7 +258,7 @@ fn eager_macro_recur(
|
||||||
parse.as_ref().map(|it| it.syntax_node()),
|
parse.as_ref().map(|it| it.syntax_node()),
|
||||||
krate,
|
krate,
|
||||||
macro_resolver,
|
macro_resolver,
|
||||||
)?;
|
);
|
||||||
let err = err.or(error);
|
let err = err.or(error);
|
||||||
|
|
||||||
if let Some(tt) = call.token_tree() {
|
if let Some(tt) = call.token_tree() {
|
||||||
|
@ -284,7 +288,7 @@ fn eager_macro_recur(
|
||||||
}
|
}
|
||||||
// check if the whole original syntax is replaced
|
// check if the whole original syntax is replaced
|
||||||
if call.syntax() == &original {
|
if call.syntax() == &original {
|
||||||
return Ok(ExpandResult { value: value.zip(Some(mapping)), err: error });
|
return ExpandResult { value: value.zip(Some(mapping)), err: error };
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(insert) = value {
|
if let Some(insert) = value {
|
||||||
|
@ -295,5 +299,5 @@ fn eager_macro_recur(
|
||||||
}
|
}
|
||||||
|
|
||||||
replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
|
replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
|
||||||
Ok(ExpandResult { value: Some((original, mapping)), err: error })
|
ExpandResult { value: Some((original, mapping)), err: error }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue