Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand format_args! with more details #14820

Merged
merged 3 commits into from May 18, 2023
Merged

Conversation

HKalbasi
Copy link
Member

No description provided.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 16, 2023
@@ -171,5 +171,5 @@
<span class="macro">assert</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="bool_literal macro">true</span><span class="comma macro">,</span> <span class="string_literal macro">"</span><span class="format_specifier">{</span><span class="format_specifier">}</span><span class="string_literal macro"> asdasd"</span><span class="comma macro">,</span> <span class="numeric_literal macro">1</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="macro">toho</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"</span><span class="format_specifier">{</span><span class="format_specifier">}</span><span class="string_literal macro">fmt"</span><span class="comma macro">,</span> <span class="numeric_literal macro">0</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="macro unsafe">asm</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"mov eax, </span><span class="format_specifier">{</span><span class="numeric_literal">0</span><span class="format_specifier">}</span><span class="string_literal macro">"</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="macro">format_args</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="none macro">concat</span><span class="punctuation macro">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"</span><span class="format_specifier">{</span><span class="format_specifier">}</span><span class="string_literal macro">"</span><span class="parenthesis macro">)</span><span class="comma macro">,</span> <span class="string_literal macro">"{}"</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="macro">format_args</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="none macro">concat</span><span class="punctuation macro">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"</span><span class="format_specifier">{</span><span class="format_specifier">}</span><span class="string_literal macro">"</span><span class="parenthesis macro">)</span><span class="comma macro">,</span> <span class="string_literal macro">"</span><span class="format_specifier">{</span><span class="format_specifier">}</span><span class="string_literal macro">"</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test regressed, but I can't see why. The recursive expansion of format_args!(concat!("{}"), "{}") is correct but it still highlights the {} in the second argument.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks like we aren't mapping down the argument into the format_args expansion anymore, resulting in the highlight. Our current logic for checking if a string is a format_args template argument is just checking if its in a token tree of a format_args (or similar) named macro call,

pub fn is_format_string(string: &ast::String) -> bool {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The strange thing is that if I remove the concat! it will work correctly (so mapping down is working in at least some cases), and that concat! should not play any role here...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, maybe our token mapping infra is not handling eager macros properly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's probably something related to eager macros. In this code:

let a = 2;
format_args!("{} {} {:?}", a, concat!("{}", "xxx"), a);

go to definition on first a works but on second a doesn't.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a deep issue with eager macro expansion, and fixing it is out of scope for this PR. Do you consider this a blocker?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends on whether this affects more things or not. But if its just caused by having anothereager macro call in the format_args macro it seems fine . Though what exactly do we gain from this change right now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if its just caused by having anothereager macro call in the format_args macro it seems fine.

I think it is not limited to the eager macros. Any argument after a macro argument in any eager macro will lose its span, and this PR is making the format_args! eager (we can keep format_args! lazy but it has its own problems: false positive expansion diagnostic when someone construct the first argument with macros).

Though what exactly do we gain from this change right now?

For users, a diagnostics when there are less arguments provided in a format_args! based macro + correct and meaningful inline-macro and Expand macro recursively output + more precise closure captures in case of #11260 which theoretically may cause false positives for mir based diagnostics. For me, I can panic-debugging the mir interpreter, seeing the variable values by panicking them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aight, that's a lot of benefits from this. I think it's fine to merge then, but do create an issue tracking this eager expansion problem once merged please (once bors is alive again).

@HKalbasi
Copy link
Member Author

@bors r+

@bors
Copy link
Collaborator

bors commented May 18, 2023

📌 Commit a2fba7c has been approved by HKalbasi

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented May 18, 2023

⌛ Testing commit a2fba7c with merge 6b48950...

bors added a commit that referenced this pull request May 18, 2023
Expand `format_args!` with more details
@HKalbasi
Copy link
Member Author

@bors r-

This has some problems with raw strings.

@bors
Copy link
Collaborator

bors commented May 18, 2023

☀️ Try build successful - checks-actions
Build commit: 6b48950 (6b489502291db17882f15057daab616875bbf1f3)

1 similar comment
@bors
Copy link
Collaborator

bors commented May 18, 2023

☀️ Try build successful - checks-actions
Build commit: 6b48950 (6b489502291db17882f15057daab616875bbf1f3)

@HKalbasi
Copy link
Member Author

@bors r+

@bors
Copy link
Collaborator

bors commented May 18, 2023

📌 Commit b5efcfc has been approved by HKalbasi

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented May 18, 2023

⌛ Testing commit b5efcfc with merge ed7090c...

bors added a commit that referenced this pull request May 18, 2023
Expand `format_args!` with more details
@bors
Copy link
Collaborator

bors commented May 18, 2023

💔 Test failed - checks-actions

@HKalbasi
Copy link
Member Author

@bors r+

@bors
Copy link
Collaborator

bors commented May 18, 2023

📌 Commit 5c83e22 has been approved by HKalbasi

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented May 18, 2023

⌛ Testing commit 5c83e22 with merge f9be796...

@bors
Copy link
Collaborator

bors commented May 18, 2023

☀️ Test successful - checks-actions
Approved by: HKalbasi
Pushing f9be796 to master...

@bors bors merged commit f9be796 into rust-lang:master May 18, 2023
10 checks passed
@HKalbasi HKalbasi mentioned this pull request May 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants