Skip to content

Add test for #8855 #8857

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Aug 20, 2022
Merged

Add test for #8855 #8857

merged 1 commit into from
Aug 20, 2022

Conversation

smoelius
Copy link
Contributor

Fix #8855

Here is what I think is going on.

First, the expression format!("{:>6} {:>6}", a, b.to_string()) expands to:

{
    let res =
        ::alloc::fmt::format(::core::fmt::Arguments::new_v1_formatted(&["",
                            " "],
                &[::core::fmt::ArgumentV1::new_display(&a),
                            ::core::fmt::ArgumentV1::new_display(&b.to_string())],
                &[::core::fmt::rt::v1::Argument {
                                position: 0usize,
                                format: ::core::fmt::rt::v1::FormatSpec {
                                    fill: ' ',
                                    align: ::core::fmt::rt::v1::Alignment::Right,
                                    flags: 0u32,
                                    precision: ::core::fmt::rt::v1::Count::Implied,
                                    width: ::core::fmt::rt::v1::Count::Is(6usize),
                                },
                            },
                            ::core::fmt::rt::v1::Argument {
                                position: 1usize,
                                format: ::core::fmt::rt::v1::FormatSpec {
                                    fill: ' ',
                                    align: ::core::fmt::rt::v1::Alignment::Right,
                                    flags: 0u32,
                                    precision: ::core::fmt::rt::v1::Count::Implied,
                                    width: ::core::fmt::rt::v1::Count::Is(6usize),
                                },
                            }], unsafe { ::core::fmt::UnsafeArg::new() }));
    res
}

When I dump the expressions that get past the call to has_string_formatting here, I see more than I would expect.

In particular, I see this subexpression of the above:

                &[::core::fmt::ArgumentV1::new_display(&a),
                            ::core::fmt::ArgumentV1::new_display(&b.to_string())],

This suggests to me that more expressions are getting past this call to FormatArgsExpn::parse than should.

Those expressions are then visited, but no ::core::fmt::rt::v1::Arguments are found and pushed here.

As a result, the expressions appear unformatted, hence, the false positive.

My proposed fix is to restrict FormatArgsExpn::parse so that it only matches Call expressions.

cc: @akanalytics

changelog: none

@rust-highfive
Copy link

r? @flip1995

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 20, 2022
@flip1995
Copy link
Member

Thanks for this. As you pointed out in #9349 this is closely related to the other format/write/print PRs. So I'll mark this a blocked until those are merged and will then test if this fix is still necessary or if the rewrite already resolved it somehow.

@flip1995 flip1995 added S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Aug 19, 2022
@bors
Copy link
Contributor

bors commented Aug 19, 2022

☔ The latest upstream changes (presumably #9349) made this pull request unmergeable. Please resolve the merge conflicts.

@smoelius smoelius changed the title Fix to_string_in_format_args false positive Add test for #8855 Aug 20, 2022
@smoelius
Copy link
Contributor Author

#9349 does seem to have resolved the issue.

I think the only question that remains is whether you would like this PR's test.

@flip1995
Copy link
Member

Ah great! Thanks for revisiting this. I would have gone back to this once the write.rs rewrite was merged. We definitely want the regression test.

@bors r+

@bors
Copy link
Contributor

bors commented Aug 20, 2022

📌 Commit 6f3d398 has been approved by flip1995

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Aug 20, 2022

⌛ Testing commit 6f3d398 with merge 41309df...

@bors
Copy link
Contributor

bors commented Aug 20, 2022

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing 41309df to master...

@bors bors merged commit 41309df into rust-lang:master Aug 20, 2022
@smoelius smoelius deleted the fix-8855 branch January 8, 2023 12:18
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

to_string() in a format string when format specifiers are used (eg :<20) is NOT redundant
4 participants