Skip to content

Commit 2365762

Browse files
committed
Auto merge of rust-lang#14468 - lowr:patch/expand-macro-bang, r=Veykril
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.
2 parents 1ebac28 + 613e008 commit 2365762

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

crates/ide/src/expand_macro.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
8383
}
8484
}
8585
if let Some(mac) = ast::MacroCall::cast(node) {
86+
let mut name = mac.path()?.segment()?.name_ref()?.to_string();
87+
name.push('!');
8688
break (
87-
mac.path()?.segment()?.name_ref()?.to_string(),
89+
name,
8890
expand_macro_recur(&sema, &mac)?,
8991
mac.syntax().parent().map(|it| it.kind()).unwrap_or(SyntaxKind::MACRO_ITEMS),
9092
);
@@ -235,7 +237,7 @@ fn main() {
235237
}
236238
"#,
237239
expect![[r#"
238-
bar
240+
bar!
239241
5i64 as _"#]],
240242
);
241243
}
@@ -252,7 +254,7 @@ fn main() {
252254
}
253255
"#,
254256
expect![[r#"
255-
bar
257+
bar!
256258
for _ in 0..42{}"#]],
257259
);
258260
}
@@ -273,7 +275,7 @@ macro_rules! baz {
273275
f$0oo!();
274276
"#,
275277
expect![[r#"
276-
foo
278+
foo!
277279
fn b(){}
278280
"#]],
279281
);
@@ -294,7 +296,7 @@ macro_rules! foo {
294296
f$0oo!();
295297
"#,
296298
expect![[r#"
297-
foo
299+
foo!
298300
fn some_thing() -> u32 {
299301
let a = 0;
300302
a+10
@@ -328,16 +330,16 @@ fn main() {
328330
}
329331
"#,
330332
expect![[r#"
331-
match_ast
332-
{
333-
if let Some(it) = ast::TraitDef::cast(container.clone()){}
334-
else if let Some(it) = ast::ImplDef::cast(container.clone()){}
335-
else {
336-
{
337-
continue
338-
}
339-
}
340-
}"#]],
333+
match_ast!
334+
{
335+
if let Some(it) = ast::TraitDef::cast(container.clone()){}
336+
else if let Some(it) = ast::ImplDef::cast(container.clone()){}
337+
else {
338+
{
339+
continue
340+
}
341+
}
342+
}"#]],
341343
);
342344
}
343345

@@ -358,7 +360,7 @@ fn main() {
358360
}
359361
"#,
360362
expect![[r#"
361-
match_ast
363+
match_ast!
362364
{}"#]],
363365
);
364366
}
@@ -383,7 +385,7 @@ fn main() {
383385
}
384386
"#,
385387
expect![[r#"
386-
foo
388+
foo!
387389
{
388390
macro_rules! bar {
389391
() => {
@@ -411,7 +413,7 @@ fn main() {
411413
}
412414
"#,
413415
expect![[r#"
414-
foo
416+
foo!
415417
"#]],
416418
);
417419
}
@@ -433,7 +435,7 @@ fn main() {
433435
}
434436
"#,
435437
expect![[r#"
436-
foo
438+
foo!
437439
0"#]],
438440
);
439441
}
@@ -451,7 +453,7 @@ fn main() {
451453
}
452454
"#,
453455
expect![[r#"
454-
foo
456+
foo!
455457
fn f<T>(_: &dyn ::std::marker::Copy){}"#]],
456458
);
457459
}

editors/code/src/commands.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ export function viewFullCrateGraph(ctx: CtxInit): Cmd {
699699
// The contents of the file come from the `TextDocumentContentProvider`
700700
export function expandMacro(ctx: CtxInit): Cmd {
701701
function codeFormat(expanded: ra.ExpandedMacro): string {
702-
let result = `// Recursive expansion of ${expanded.name}! macro\n`;
702+
let result = `// Recursive expansion of ${expanded.name} macro\n`;
703703
result += "// " + "=".repeat(result.length - 3);
704704
result += "\n\n";
705705
result += expanded.expansion;

0 commit comments

Comments
 (0)