2023-12-06 07:36:39 -06:00
|
|
|
use hir::{DescendPreference, InFile, MacroFileIdExt, Semantics};
|
2021-12-13 08:55:13 -06:00
|
|
|
use ide_db::{
|
2022-04-17 06:33:39 -05:00
|
|
|
base_db::FileId, helpers::pick_best_token,
|
|
|
|
syntax_helpers::insert_whitespace_into_node::insert_ws_into, RootDatabase,
|
2021-12-13 08:55:13 -06:00
|
|
|
};
|
2022-02-21 05:57:57 -06:00
|
|
|
use syntax::{ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxNode, T};
|
2020-02-06 05:52:32 -06:00
|
|
|
|
|
|
|
use crate::FilePosition;
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2019-11-19 08:56:48 -06:00
|
|
|
pub struct ExpandedMacro {
|
|
|
|
pub name: String,
|
|
|
|
pub expansion: String,
|
|
|
|
}
|
|
|
|
|
2020-05-31 03:14:36 -05:00
|
|
|
// Feature: Expand Macro Recursively
|
|
|
|
//
|
2023-11-11 04:10:38 -06:00
|
|
|
// Shows the full macro expansion of the macro at the current caret position.
|
2020-05-31 03:14:36 -05:00
|
|
|
//
|
|
|
|
// |===
|
|
|
|
// | Editor | Action Name
|
|
|
|
//
|
2023-11-11 04:10:38 -06:00
|
|
|
// | VS Code | **rust-analyzer: Expand macro recursively at caret**
|
2020-05-31 03:14:36 -05:00
|
|
|
// |===
|
2021-03-30 18:08:10 -05:00
|
|
|
//
|
|
|
|
// image::https://user-images.githubusercontent.com/48062697/113020648-b3973180-917a-11eb-84a9-ecb921293dc5.gif[]
|
2019-11-19 08:56:48 -06:00
|
|
|
pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<ExpandedMacro> {
|
2020-02-18 11:35:10 -06:00
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let file = sema.parse(position.file_id);
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2021-06-22 10:28:07 -05:00
|
|
|
let tok = pick_best_token(file.syntax().token_at_offset(position.offset), |kind| match kind {
|
|
|
|
SyntaxKind::IDENT => 1,
|
|
|
|
_ => 0,
|
|
|
|
})?;
|
2021-09-13 20:00:53 -05:00
|
|
|
|
2022-08-01 06:47:09 -05:00
|
|
|
// due to how rust-analyzer works internally, we need to special case derive attributes,
|
2021-10-26 13:10:09 -05:00
|
|
|
// otherwise they might not get found, e.g. here with the cursor at $0 `#[attr]` would expand:
|
2021-10-26 13:09:14 -05:00
|
|
|
// ```
|
|
|
|
// #[attr]
|
|
|
|
// #[derive($0Foo)]
|
|
|
|
// struct Bar;
|
|
|
|
// ```
|
|
|
|
|
2023-12-03 13:20:38 -06:00
|
|
|
let derive = sema
|
2023-12-05 08:42:39 -06:00
|
|
|
.descend_into_macros(DescendPreference::None, tok.clone())
|
2023-12-03 13:20:38 -06:00
|
|
|
.into_iter()
|
|
|
|
.find_map(|descended| {
|
2023-12-06 07:36:39 -06:00
|
|
|
let macro_file = sema.hir_file_for(&descended.parent()?).macro_file()?;
|
|
|
|
if !macro_file.is_derive_attr_pseudo_expansion(db) {
|
2023-08-16 03:07:18 -05:00
|
|
|
return None;
|
|
|
|
}
|
2022-02-21 04:51:53 -06:00
|
|
|
|
2023-08-16 03:07:18 -05:00
|
|
|
let name = descended.parent_ancestors().filter_map(ast::Path::cast).last()?.to_string();
|
|
|
|
// up map out of the #[derive] expansion
|
2023-11-24 09:38:48 -06:00
|
|
|
let InFile { file_id, value: tokens } =
|
2023-12-06 07:36:39 -06:00
|
|
|
hir::InMacroFile::new(macro_file, descended).upmap_once(db);
|
2023-11-24 09:38:48 -06:00
|
|
|
let token = sema.parse_or_expand(file_id).covering_element(tokens[0]).into_token()?;
|
2023-08-16 03:07:18 -05:00
|
|
|
let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;
|
|
|
|
let expansions = sema.expand_derive_macro(&attr)?;
|
|
|
|
let idx = attr
|
|
|
|
.token_tree()?
|
|
|
|
.token_trees_and_tokens()
|
|
|
|
.filter_map(NodeOrToken::into_token)
|
|
|
|
.take_while(|it| it != &token)
|
|
|
|
.filter(|it| it.kind() == T![,])
|
|
|
|
.count();
|
|
|
|
let expansion = format(
|
|
|
|
db,
|
|
|
|
SyntaxKind::MACRO_ITEMS,
|
|
|
|
position.file_id,
|
|
|
|
expansions.get(idx).cloned()?,
|
|
|
|
);
|
|
|
|
Some(ExpandedMacro { name, expansion })
|
|
|
|
});
|
2021-10-26 13:09:14 -05:00
|
|
|
|
|
|
|
if derive.is_some() {
|
|
|
|
return derive;
|
2021-08-25 19:36:33 -05:00
|
|
|
}
|
2021-09-13 20:00:53 -05:00
|
|
|
|
|
|
|
// FIXME: Intermix attribute and bang! expansions
|
|
|
|
// currently we only recursively expand one of the two types
|
2022-06-10 09:30:09 -05:00
|
|
|
let mut anc = tok.parent_ancestors();
|
2022-04-17 06:46:00 -05:00
|
|
|
let (name, expanded, kind) = loop {
|
|
|
|
let node = anc.next()?;
|
|
|
|
|
2021-06-08 15:13:22 -05:00
|
|
|
if let Some(item) = ast::Item::cast(node.clone()) {
|
2021-08-21 16:24:12 -05:00
|
|
|
if let Some(def) = sema.resolve_attr_macro_call(&item) {
|
2022-04-17 06:46:00 -05:00
|
|
|
break (
|
2023-05-24 11:04:29 -05:00
|
|
|
def.name(db).display(db).to_string(),
|
2022-04-17 06:46:00 -05:00
|
|
|
expand_attr_macro_recur(&sema, &item)?,
|
|
|
|
SyntaxKind::MACRO_ITEMS,
|
|
|
|
);
|
2021-06-07 09:05:36 -05:00
|
|
|
}
|
|
|
|
}
|
2021-06-08 15:13:22 -05:00
|
|
|
if let Some(mac) = ast::MacroCall::cast(node) {
|
2023-04-02 05:45:39 -05:00
|
|
|
let mut name = mac.path()?.segment()?.name_ref()?.to_string();
|
|
|
|
name.push('!');
|
2022-04-17 06:46:00 -05:00
|
|
|
break (
|
2023-04-02 05:45:39 -05:00
|
|
|
name,
|
2022-04-17 06:46:00 -05:00
|
|
|
expand_macro_recur(&sema, &mac)?,
|
|
|
|
mac.syntax().parent().map(|it| it.kind()).unwrap_or(SyntaxKind::MACRO_ITEMS),
|
|
|
|
);
|
2021-06-08 15:13:22 -05:00
|
|
|
}
|
2022-04-17 06:46:00 -05:00
|
|
|
};
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2019-11-19 08:56:28 -06:00
|
|
|
// FIXME:
|
|
|
|
// macro expansion may lose all white space information
|
|
|
|
// But we hope someday we can use ra_fmt for that
|
2022-04-17 06:46:00 -05:00
|
|
|
let expansion = format(db, kind, position.file_id, expanded);
|
2022-04-17 06:33:39 -05:00
|
|
|
|
2022-04-17 06:46:00 -05:00
|
|
|
Some(ExpandedMacro { name, expansion })
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_macro_recur(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2020-02-18 11:35:10 -06:00
|
|
|
macro_call: &ast::MacroCall,
|
2019-11-17 12:47:50 -06:00
|
|
|
) -> Option<SyntaxNode> {
|
2021-04-19 12:43:26 -05:00
|
|
|
let expanded = sema.expand(macro_call)?.clone_for_update();
|
2021-08-21 16:24:12 -05:00
|
|
|
expand(sema, expanded, ast::MacroCall::cast, expand_macro_recur)
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:06:15 -05:00
|
|
|
fn expand_attr_macro_recur(
|
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
|
|
|
item: &ast::Item,
|
|
|
|
) -> Option<SyntaxNode> {
|
2021-08-21 16:24:12 -05:00
|
|
|
let expanded = sema.expand_attr_macro(item)?.clone_for_update();
|
|
|
|
expand(sema, expanded, ast::Item::cast, expand_attr_macro_recur)
|
|
|
|
}
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2021-08-21 16:24:12 -05:00
|
|
|
fn expand<T: AstNode>(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2021-08-21 16:24:12 -05:00
|
|
|
expanded: SyntaxNode,
|
|
|
|
f: impl FnMut(SyntaxNode) -> Option<T>,
|
2022-07-20 08:02:08 -05:00
|
|
|
exp: impl Fn(&Semantics<'_, RootDatabase>, &T) -> Option<SyntaxNode>,
|
2021-08-21 16:24:12 -05:00
|
|
|
) -> Option<SyntaxNode> {
|
|
|
|
let children = expanded.descendants().filter_map(f);
|
2021-04-19 12:43:26 -05:00
|
|
|
let mut replacements = Vec::new();
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2021-04-19 12:43:26 -05:00
|
|
|
for child in children {
|
2021-08-21 16:24:12 -05:00
|
|
|
if let Some(new_node) = exp(sema, &child) {
|
2021-04-19 12:43:26 -05:00
|
|
|
// check if the whole original syntax is replaced
|
2019-11-22 20:33:14 -06:00
|
|
|
if expanded == *child.syntax() {
|
2021-04-19 12:43:26 -05:00
|
|
|
return Some(new_node);
|
2019-11-22 20:33:14 -06:00
|
|
|
}
|
2021-04-19 12:43:26 -05:00
|
|
|
replacements.push((child, new_node));
|
2019-11-21 22:04:20 -06:00
|
|
|
}
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
|
|
|
|
2021-04-19 12:43:26 -05:00
|
|
|
replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
|
|
|
|
Some(expanded)
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
|
|
|
|
2022-04-17 06:33:39 -05:00
|
|
|
fn format(db: &RootDatabase, kind: SyntaxKind, file_id: FileId, expanded: SyntaxNode) -> String {
|
|
|
|
let expansion = insert_ws_into(expanded).to_string();
|
|
|
|
|
|
|
|
_format(db, kind, file_id, &expansion).unwrap_or(expansion)
|
|
|
|
}
|
|
|
|
|
2022-04-17 07:00:19 -05:00
|
|
|
#[cfg(any(test, target_arch = "wasm32", target_os = "emscripten"))]
|
2022-04-17 06:33:39 -05:00
|
|
|
fn _format(
|
|
|
|
_db: &RootDatabase,
|
|
|
|
_kind: SyntaxKind,
|
|
|
|
_file_id: FileId,
|
2023-05-03 05:44:47 -05:00
|
|
|
expansion: &str,
|
2022-04-17 06:33:39 -05:00
|
|
|
) -> Option<String> {
|
2023-05-03 05:44:47 -05:00
|
|
|
// remove trailing spaces for test
|
|
|
|
use itertools::Itertools;
|
|
|
|
Some(expansion.lines().map(|x| x.trim_end()).join("\n"))
|
2022-04-17 06:33:39 -05:00
|
|
|
}
|
|
|
|
|
2022-04-17 07:00:19 -05:00
|
|
|
#[cfg(not(any(test, target_arch = "wasm32", target_os = "emscripten")))]
|
2022-04-17 06:33:39 -05:00
|
|
|
fn _format(
|
|
|
|
db: &RootDatabase,
|
|
|
|
kind: SyntaxKind,
|
|
|
|
file_id: FileId,
|
|
|
|
expansion: &str,
|
|
|
|
) -> Option<String> {
|
|
|
|
use ide_db::base_db::{FileLoader, SourceDatabase};
|
|
|
|
// hack until we get hygiene working (same character amount to preserve formatting as much as possible)
|
2022-12-30 02:05:03 -06:00
|
|
|
const DOLLAR_CRATE_REPLACE: &str = "__r_a_";
|
2022-04-17 06:33:39 -05:00
|
|
|
let expansion = expansion.replace("$crate", DOLLAR_CRATE_REPLACE);
|
|
|
|
let (prefix, suffix) = match kind {
|
|
|
|
SyntaxKind::MACRO_PAT => ("fn __(", ": u32);"),
|
|
|
|
SyntaxKind::MACRO_EXPR | SyntaxKind::MACRO_STMTS => ("fn __() {", "}"),
|
|
|
|
SyntaxKind::MACRO_TYPE => ("type __ =", ";"),
|
|
|
|
_ => ("", ""),
|
|
|
|
};
|
|
|
|
let expansion = format!("{prefix}{expansion}{suffix}");
|
|
|
|
|
|
|
|
let &crate_id = db.relevant_crates(file_id).iter().next()?;
|
|
|
|
let edition = db.crate_graph()[crate_id].edition;
|
|
|
|
|
2024-03-05 03:16:30 -06:00
|
|
|
let mut cmd = std::process::Command::new(toolchain::Tool::Rustfmt.path());
|
2022-04-17 06:33:39 -05:00
|
|
|
cmd.arg("--edition");
|
|
|
|
cmd.arg(edition.to_string());
|
|
|
|
|
|
|
|
let mut rustfmt = cmd
|
|
|
|
.stdin(std::process::Stdio::piped())
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.ok()?;
|
|
|
|
|
|
|
|
std::io::Write::write_all(&mut rustfmt.stdin.as_mut()?, expansion.as_bytes()).ok()?;
|
|
|
|
|
|
|
|
let output = rustfmt.wait_with_output().ok()?;
|
|
|
|
let captured_stdout = String::from_utf8(output.stdout).ok()?;
|
|
|
|
|
|
|
|
if output.status.success() && !captured_stdout.trim().is_empty() {
|
2022-04-17 06:46:00 -05:00
|
|
|
let output = captured_stdout.replace(DOLLAR_CRATE_REPLACE, "$crate");
|
|
|
|
let output = output.trim().strip_prefix(prefix)?;
|
|
|
|
let output = match kind {
|
|
|
|
SyntaxKind::MACRO_PAT => {
|
|
|
|
output.strip_suffix(suffix).or_else(|| output.strip_suffix(": u32,\n);"))?
|
|
|
|
}
|
|
|
|
_ => output.strip_suffix(suffix)?,
|
|
|
|
};
|
|
|
|
let trim_indent = stdx::trim_indent(output);
|
2022-04-17 06:33:39 -05:00
|
|
|
tracing::debug!("expand_macro: formatting succeeded");
|
|
|
|
Some(trim_indent)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-17 12:47:50 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-21 06:19:31 -05:00
|
|
|
use expect_test::{expect, Expect};
|
2019-11-17 12:47:50 -06:00
|
|
|
|
2020-10-02 10:34:31 -05:00
|
|
|
use crate::fixture;
|
2020-02-18 11:35:10 -06:00
|
|
|
|
2021-08-25 19:36:33 -05:00
|
|
|
#[track_caller]
|
2020-07-01 10:52:22 -05:00
|
|
|
fn check(ra_fixture: &str, expect: Expect) {
|
2020-10-02 10:34:31 -05:00
|
|
|
let (analysis, pos) = fixture::position(ra_fixture);
|
2020-07-01 10:52:22 -05:00
|
|
|
let expansion = analysis.expand_macro(pos).unwrap().unwrap();
|
|
|
|
let actual = format!("{}\n{}", expansion.name, expansion.expansion);
|
|
|
|
expect.assert_eq(&actual);
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
|
|
|
|
2021-11-26 20:22:21 -06:00
|
|
|
#[test]
|
|
|
|
fn macro_expand_as_keyword() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
macro_rules! bar {
|
|
|
|
($i:tt) => { $i as _ }
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let x: u64 = ba$0r!(5i64);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2023-04-02 05:45:39 -05:00
|
|
|
bar!
|
2021-11-26 20:22:21 -06:00
|
|
|
5i64 as _"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-23 23:45:58 -05:00
|
|
|
#[test]
|
|
|
|
fn macro_expand_underscore() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
macro_rules! bar {
|
|
|
|
($i:tt) => { for _ in 0..$i {} }
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
ba$0r!(42);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2023-04-02 05:45:39 -05:00
|
|
|
bar!
|
2022-05-24 15:33:42 -05:00
|
|
|
for _ in 0..42{}"#]],
|
2022-05-23 23:45:58 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-17 12:47:50 -06:00
|
|
|
#[test]
|
|
|
|
fn macro_expand_recursive_expansion() {
|
2020-07-01 10:52:22 -05:00
|
|
|
check(
|
2019-11-17 12:47:50 -06:00
|
|
|
r#"
|
2020-07-01 10:52:22 -05:00
|
|
|
macro_rules! bar {
|
|
|
|
() => { fn b() {} }
|
|
|
|
}
|
|
|
|
macro_rules! foo {
|
|
|
|
() => { bar!(); }
|
|
|
|
}
|
|
|
|
macro_rules! baz {
|
|
|
|
() => { foo!(); }
|
|
|
|
}
|
2021-01-06 14:15:48 -06:00
|
|
|
f$0oo!();
|
2020-07-01 10:52:22 -05:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2023-04-02 05:45:39 -05:00
|
|
|
foo!
|
2023-05-03 05:44:47 -05:00
|
|
|
fn b(){}"#]],
|
2019-11-17 12:47:50 -06:00
|
|
|
);
|
2019-11-19 10:12:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_expand_multiple_lines() {
|
2020-07-01 10:52:22 -05:00
|
|
|
check(
|
2019-11-19 10:12:48 -06:00
|
|
|
r#"
|
2020-07-01 10:52:22 -05:00
|
|
|
macro_rules! foo {
|
|
|
|
() => {
|
|
|
|
fn some_thing() -> u32 {
|
|
|
|
let a = 0;
|
|
|
|
a + 10
|
2019-11-19 10:12:48 -06:00
|
|
|
}
|
2020-07-01 10:52:22 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-06 14:15:48 -06:00
|
|
|
f$0oo!();
|
2019-11-19 10:12:48 -06:00
|
|
|
"#,
|
2020-07-01 10:52:22 -05:00
|
|
|
expect![[r#"
|
2023-04-02 05:45:39 -05:00
|
|
|
foo!
|
2021-08-25 19:36:33 -05:00
|
|
|
fn some_thing() -> u32 {
|
2024-02-26 22:23:15 -06:00
|
|
|
let a = 0;
|
|
|
|
a+10
|
2022-05-24 15:33:42 -05:00
|
|
|
}"#]],
|
2019-11-19 10:12:48 -06:00
|
|
|
);
|
2019-11-21 12:35:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_expand_match_ast() {
|
2020-07-01 10:52:22 -05:00
|
|
|
check(
|
2019-11-21 12:35:30 -06:00
|
|
|
r#"
|
2020-07-01 10:52:22 -05:00
|
|
|
macro_rules! match_ast {
|
|
|
|
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
|
|
|
|
(match ($node:expr) {
|
|
|
|
$( ast::$ast:ident($it:ident) => $res:block, )*
|
|
|
|
_ => $catch_all:expr $(,)?
|
|
|
|
}) => {{
|
|
|
|
$( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
|
|
|
|
{ $catch_all }
|
|
|
|
}};
|
|
|
|
}
|
2020-02-06 05:52:32 -06:00
|
|
|
|
2020-07-01 10:52:22 -05:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
mat$0ch_ast! {
|
2020-07-01 10:52:22 -05:00
|
|
|
match container {
|
|
|
|
ast::TraitDef(it) => {},
|
|
|
|
ast::ImplDef(it) => {},
|
|
|
|
_ => { continue },
|
2020-02-06 05:52:32 -06:00
|
|
|
}
|
2019-11-21 12:35:30 -06:00
|
|
|
}
|
|
|
|
}
|
2020-07-01 10:52:22 -05:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2023-04-02 05:45:39 -05:00
|
|
|
match_ast!
|
|
|
|
{
|
2024-02-26 22:23:15 -06:00
|
|
|
if let Some(it) = ast::TraitDef::cast(container.clone()){}
|
|
|
|
else if let Some(it) = ast::ImplDef::cast(container.clone()){}
|
|
|
|
else {
|
|
|
|
{
|
|
|
|
continue
|
|
|
|
}
|
2023-04-02 05:45:39 -05:00
|
|
|
}
|
|
|
|
}"#]],
|
2020-07-01 10:52:22 -05:00
|
|
|
);
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|
2019-11-21 22:04:20 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_expand_match_ast_inside_let_statement() {
|
2020-07-01 10:52:22 -05:00
|
|
|
check(
|
2019-11-21 22:04:20 -06:00
|
|
|
r#"
|
2020-07-01 10:52:22 -05:00
|
|
|
macro_rules! match_ast {
|
|
|
|
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
|
|
|
|
(match ($node:expr) {}) => {{}};
|
|
|
|
}
|
2019-11-21 22:04:20 -06:00
|
|
|
|
2020-07-01 10:52:22 -05:00
|
|
|
fn main() {
|
|
|
|
let p = f(|it| {
|
2021-01-06 14:15:48 -06:00
|
|
|
let res = mat$0ch_ast! { match c {}};
|
2020-07-01 10:52:22 -05:00
|
|
|
Some(res)
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2023-04-02 05:45:39 -05:00
|
|
|
match_ast!
|
2022-05-24 15:33:42 -05:00
|
|
|
{}"#]],
|
2019-11-21 22:04:20 -06:00
|
|
|
);
|
|
|
|
}
|
2019-11-22 20:33:14 -06:00
|
|
|
|
2022-06-28 06:40:22 -05:00
|
|
|
#[test]
|
|
|
|
fn macro_expand_inner_macro_rules() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
macro_rules! foo {
|
|
|
|
($t:tt) => {{
|
|
|
|
macro_rules! bar {
|
|
|
|
() => {
|
|
|
|
$t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bar!()
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo$0!(42);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2023-04-02 05:45:39 -05:00
|
|
|
foo!
|
2022-06-28 06:40:22 -05:00
|
|
|
{
|
2024-02-26 22:23:15 -06:00
|
|
|
macro_rules! bar {
|
|
|
|
() => {
|
|
|
|
42
|
|
|
|
}
|
2022-06-28 06:40:22 -05:00
|
|
|
}
|
2024-02-26 22:23:15 -06:00
|
|
|
42
|
2022-06-28 06:40:22 -05:00
|
|
|
}"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-22 20:33:14 -06:00
|
|
|
#[test]
|
|
|
|
fn macro_expand_inner_macro_fail_to_expand() {
|
2020-07-01 10:52:22 -05:00
|
|
|
check(
|
2019-11-22 20:33:14 -06:00
|
|
|
r#"
|
2020-07-01 10:52:22 -05:00
|
|
|
macro_rules! bar {
|
|
|
|
(BAD) => {};
|
|
|
|
}
|
|
|
|
macro_rules! foo {
|
|
|
|
() => {bar!()};
|
|
|
|
}
|
2019-11-22 20:33:14 -06:00
|
|
|
|
2020-07-01 10:52:22 -05:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
let res = fo$0o!();
|
2020-07-01 10:52:22 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2023-04-02 05:45:39 -05:00
|
|
|
foo!
|
2020-07-01 10:52:22 -05:00
|
|
|
"#]],
|
2019-11-22 20:33:14 -06:00
|
|
|
);
|
|
|
|
}
|
2019-11-26 01:05:53 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_expand_with_dollar_crate() {
|
2020-07-01 10:52:22 -05:00
|
|
|
check(
|
2019-11-26 01:05:53 -06:00
|
|
|
r#"
|
2020-07-01 10:52:22 -05:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! bar {
|
|
|
|
() => {0};
|
|
|
|
}
|
|
|
|
macro_rules! foo {
|
|
|
|
() => {$crate::bar!()};
|
|
|
|
}
|
2019-11-26 01:05:53 -06:00
|
|
|
|
2020-07-01 10:52:22 -05:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
let res = fo$0o!();
|
2020-07-01 10:52:22 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2023-04-02 05:45:39 -05:00
|
|
|
foo!
|
2022-05-23 18:35:47 -05:00
|
|
|
0"#]],
|
2019-11-26 01:05:53 -06:00
|
|
|
);
|
|
|
|
}
|
2021-08-24 09:33:52 -05:00
|
|
|
|
2022-01-06 08:38:25 -06:00
|
|
|
#[test]
|
|
|
|
fn macro_expand_with_dyn_absolute_path() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
macro_rules! foo {
|
|
|
|
() => {fn f<T>(_: &dyn ::std::marker::Copy) {}};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let res = fo$0o!();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2023-04-02 05:45:39 -05:00
|
|
|
foo!
|
2022-05-24 15:33:42 -05:00
|
|
|
fn f<T>(_: &dyn ::std::marker::Copy){}"#]],
|
2022-01-06 08:38:25 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-24 09:33:52 -05:00
|
|
|
#[test]
|
|
|
|
fn macro_expand_derive() {
|
|
|
|
check(
|
|
|
|
r#"
|
2021-10-26 13:15:25 -05:00
|
|
|
//- proc_macros: identity
|
2021-11-17 12:46:32 -06:00
|
|
|
//- minicore: clone, derive
|
2021-08-24 09:33:52 -05:00
|
|
|
|
2021-10-26 13:15:25 -05:00
|
|
|
#[proc_macros::identity]
|
2021-08-24 09:33:52 -05:00
|
|
|
#[derive(C$0lone)]
|
|
|
|
struct Foo {}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
Clone
|
2024-01-08 03:37:09 -06:00
|
|
|
impl < >$crate::clone::Clone for Foo< >where {
|
2024-02-26 22:23:15 -06:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
match self {
|
|
|
|
Foo{}
|
|
|
|
=> Foo{}
|
|
|
|
,
|
2023-05-03 05:44:47 -05:00
|
|
|
|
2024-02-26 22:23:15 -06:00
|
|
|
}
|
|
|
|
}
|
2023-05-03 05:44:47 -05:00
|
|
|
|
2024-02-26 22:23:15 -06:00
|
|
|
}"#]],
|
2021-08-25 19:36:33 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_expand_derive2() {
|
|
|
|
check(
|
|
|
|
r#"
|
2021-11-17 12:46:32 -06:00
|
|
|
//- minicore: copy, clone, derive
|
2021-08-25 19:36:33 -05:00
|
|
|
|
|
|
|
#[derive(Cop$0y)]
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Foo {}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
Copy
|
2024-01-08 03:37:09 -06:00
|
|
|
impl < >$crate::marker::Copy for Foo< >where{}"#]],
|
2021-08-24 09:33:52 -05:00
|
|
|
);
|
|
|
|
}
|
2021-08-25 20:32:34 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn macro_expand_derive_multi() {
|
|
|
|
check(
|
|
|
|
r#"
|
2021-11-17 12:46:32 -06:00
|
|
|
//- minicore: copy, clone, derive
|
2021-08-25 20:32:34 -05:00
|
|
|
|
|
|
|
#[derive(Cop$0y, Clone)]
|
|
|
|
struct Foo {}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2022-02-21 05:57:57 -06:00
|
|
|
Copy
|
2024-01-08 03:37:09 -06:00
|
|
|
impl < >$crate::marker::Copy for Foo< >where{}"#]],
|
2021-08-25 20:32:34 -05:00
|
|
|
);
|
2022-02-22 05:32:15 -06:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- minicore: copy, clone, derive
|
|
|
|
|
|
|
|
#[derive(Copy, Cl$0one)]
|
|
|
|
struct Foo {}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
Clone
|
2024-01-08 03:37:09 -06:00
|
|
|
impl < >$crate::clone::Clone for Foo< >where {
|
2024-02-26 22:23:15 -06:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
match self {
|
|
|
|
Foo{}
|
|
|
|
=> Foo{}
|
|
|
|
,
|
2023-05-03 05:44:47 -05:00
|
|
|
|
2024-02-26 22:23:15 -06:00
|
|
|
}
|
|
|
|
}
|
2023-05-03 05:44:47 -05:00
|
|
|
|
2024-02-26 22:23:15 -06:00
|
|
|
}"#]],
|
2022-02-22 05:32:15 -06:00
|
|
|
);
|
2021-08-25 20:32:34 -05:00
|
|
|
}
|
2019-11-17 12:47:50 -06:00
|
|
|
}
|