Skip to content

non_fmt_panics suggestion applied many times when it appears inside a macro_rules #88115

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

Closed
m-ou-se opened this issue Aug 17, 2021 · 10 comments
Closed
Assignees
Labels
A-edition-2021 Area: The 2021 edition A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-edition Diagnostics: An error or lint that should account for edition differences. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@m-ou-se
Copy link
Member

m-ou-se commented Aug 17, 2021

This showed up in a small crater run for the 2021 edition migrations:

The suggestion gets applied for every usage of the macro, resulting in:

                 panic!("{}", "{}", "{}", "{}", "{}", "{}", "{}", "{}", err)
@m-ou-se m-ou-se added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-edition Diagnostics: An error or lint that should account for edition differences. A-edition-2021 Area: The 2021 edition labels Aug 17, 2021
@m-ou-se m-ou-se self-assigned this Aug 17, 2021
@m-ou-se
Copy link
Member Author

m-ou-se commented Aug 17, 2021

@estebank Is this a known problem with suggestions inside macro_rules definitions?

@estebank
Copy link
Contributor

estebank commented Aug 17, 2021

Oh, that definitely looks like a previously existing outstanding bug :-/

I also noticed this other issue as well:

[INFO] [stdout] error: format argument must be a string literal
[INFO] [stdout]    --> src/imageops/sample.rs:904:21
[INFO] [stdout]     |
[INFO] [stdout] 904 |                     format!("alpha value: {}, {:?}", alpha, filter)
[INFO] [stdout]     |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[INFO] [stdout]     |
[INFO] [stdout]     = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
[INFO] [stdout] help: you might be missing a string literal to format with
[INFO] [stdout]     |
[INFO] [stdout] 904 |                     "{}", format!("alpha value: {}, {:?}", alpha, filter)
[INFO] [stdout]     |                     +++++

@m-ou-se
Copy link
Member Author

m-ou-se commented Aug 17, 2021

That other issue looks confusing because the surrounding assert!( and ) are on separate lines:

                assert!(
                    alpha != 254 && alpha != 253,
                    format!("alpha value: {}, {:?}", alpha, filter)
                );

but the error is correct.

(It could suggest removing format!( and ) instead of adding "{}", though, like the non_fmt_panics lint does in 2015/2018 code:

warning
[INFO] [stdout] warning: panic message is not a string literal
[INFO] [stdout]    --> src/imageops/sample.rs:904:21
[INFO] [stdout]     |
[INFO] [stdout] 904 |                     format!("alpha value: {}, {:?}", alpha, filter)
[INFO] [stdout]     |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[INFO] [stdout]     |
[INFO] [stdout]     = note: this usage of assert!() is deprecated; it will be a hard error in Rust 2021
[INFO] [stdout]     = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/panic-macro-consistency.html>
[INFO] [stdout]     = note: the assert!() macro supports formatting, so there's no need for the format!() macro here
[INFO] [stdout] help: remove the `format!(..)` macro call
[INFO] [stdout]     |
[INFO] [stdout] 904 -                     format!("alpha value: {}, {:?}", alpha, filter)
[INFO] [stdout] 904 +                     "alpha value: {}, {:?}", alpha, filter
[INFO] [stdout]     | 
)

@m-ou-se
Copy link
Member Author

m-ou-se commented Aug 17, 2021

Oh, that definitely looks like a previously existing outstanding bug :-/

@estebank So I assume there's no existing mechanism for 'deduplicating' suggestions like that, such that they only appear on the first macro invocation or something? :(

@estebank
Copy link
Contributor

I believe we have a mechanism to do that, but it might be relying on the Span information, and because these spans have different provenance they are considered "different". We could maybe make rustfix smarter here instead :-/

What's the timeline for the edition?

@m-ou-se
Copy link
Member Author

m-ou-se commented Aug 17, 2021

What's the timeline for the edition?

We have 17 days until 1.56 branches off. In 10'000 crates we only ran into this twice though, so it doesn't seem like a big issue; definitely not a blocker.

@m-ou-se
Copy link
Member Author

m-ou-se commented Aug 17, 2021

It seems to work fine though when the suggestion is to replace panic! by panic_any, even if it's duplicated six times. I'm guessing whatever makes that work isn't working for inserting "{}", , because it's inserting (replacing an empty span) rather than replacing something.

@m-ou-se
Copy link
Member Author

m-ou-se commented Aug 17, 2021

https://twitter.com/artichokeruby/status/1427720656817332227

This same thing happened with 2018 edition fix ups for anonymous lifetime parameters for Debug impls defined in macros.

@estebank
Copy link
Contributor

Less than ideal, but I think it is acceptable to go out of the door with this unaddressed. We should fix it soonish regardless.

@m-ou-se
Copy link
Member Author

m-ou-se commented Aug 30, 2021

This is an instance of rust-lang/cargo#13027.

Closing this as duplicate.

@m-ou-se m-ou-se closed this as completed Aug 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2021 Area: The 2021 edition A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-edition Diagnostics: An error or lint that should account for edition differences. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants