Skip to content

Commit

Permalink
Auto merge of rust-lang#14468 - lowr:patch/expand-macro-bang, r=Veykril
Browse files Browse the repository at this point in the history
Expand Macro Recursively: don't append "!" to non-bang macro name

When we run `Expand Macro Recursively`, we prepend a comment "Recursive expansion of foo! macro" to the expansion result. I've noticed we unconditionally render the macro name with "!" and, while super subtle, I feel a bit awkward when the macro is either a derive or attribute macro.
  • Loading branch information
bors committed Apr 2, 2023
2 parents 1ebac28 + 613e008 commit 2365762
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
42 changes: 22 additions & 20 deletions crates/ide/src/expand_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
}
}
if let Some(mac) = ast::MacroCall::cast(node) {
let mut name = mac.path()?.segment()?.name_ref()?.to_string();
name.push('!');
break (
mac.path()?.segment()?.name_ref()?.to_string(),
name,
expand_macro_recur(&sema, &mac)?,
mac.syntax().parent().map(|it| it.kind()).unwrap_or(SyntaxKind::MACRO_ITEMS),
);
Expand Down Expand Up @@ -235,7 +237,7 @@ fn main() {
}
"#,
expect![[r#"
bar
bar!
5i64 as _"#]],
);
}
Expand All @@ -252,7 +254,7 @@ fn main() {
}
"#,
expect![[r#"
bar
bar!
for _ in 0..42{}"#]],
);
}
Expand All @@ -273,7 +275,7 @@ macro_rules! baz {
f$0oo!();
"#,
expect![[r#"
foo
foo!
fn b(){}
"#]],
);
Expand All @@ -294,7 +296,7 @@ macro_rules! foo {
f$0oo!();
"#,
expect![[r#"
foo
foo!
fn some_thing() -> u32 {
let a = 0;
a+10
Expand Down Expand Up @@ -328,16 +330,16 @@ fn main() {
}
"#,
expect![[r#"
match_ast
{
if let Some(it) = ast::TraitDef::cast(container.clone()){}
else if let Some(it) = ast::ImplDef::cast(container.clone()){}
else {
{
continue
}
}
}"#]],
match_ast!
{
if let Some(it) = ast::TraitDef::cast(container.clone()){}
else if let Some(it) = ast::ImplDef::cast(container.clone()){}
else {
{
continue
}
}
}"#]],
);
}

Expand All @@ -358,7 +360,7 @@ fn main() {
}
"#,
expect![[r#"
match_ast
match_ast!
{}"#]],
);
}
Expand All @@ -383,7 +385,7 @@ fn main() {
}
"#,
expect![[r#"
foo
foo!
{
macro_rules! bar {
() => {
Expand Down Expand Up @@ -411,7 +413,7 @@ fn main() {
}
"#,
expect![[r#"
foo
foo!
"#]],
);
}
Expand All @@ -433,7 +435,7 @@ fn main() {
}
"#,
expect![[r#"
foo
foo!
0"#]],
);
}
Expand All @@ -451,7 +453,7 @@ fn main() {
}
"#,
expect![[r#"
foo
foo!
fn f<T>(_: &dyn ::std::marker::Copy){}"#]],
);
}
Expand Down
2 changes: 1 addition & 1 deletion editors/code/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ export function viewFullCrateGraph(ctx: CtxInit): Cmd {
// The contents of the file come from the `TextDocumentContentProvider`
export function expandMacro(ctx: CtxInit): Cmd {
function codeFormat(expanded: ra.ExpandedMacro): string {
let result = `// Recursive expansion of ${expanded.name}! macro\n`;
let result = `// Recursive expansion of ${expanded.name} macro\n`;
result += "// " + "=".repeat(result.length - 3);
result += "\n\n";
result += expanded.expansion;
Expand Down

0 comments on commit 2365762

Please sign in to comment.