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

Tag all format-like macros with #[clippy::format_args] #137364

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nyurik
Copy link
Contributor

@nyurik nyurik commented Feb 21, 2025

Help needed

Is there a workaround for #98291 ? The build errors here seem to be triggered by that issue.

Description

Since 1.85, Clippy supports attribute-based discovery of the format!-compatible macros, i.e. macros whose trailing arguments can be passed to format_args! as is.

Tagging core library with the same attribute will allow clippy to stop maintaining a separate list of format-like macros - allowing Clippy to validate macro usage for all lints that work on format arguments. See also rust-lang/rust-clippy#14267

@rustbot
Copy link
Collaborator

rustbot commented Feb 21, 2025

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 21, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@tbu-
Copy link
Contributor

tbu- commented Feb 21, 2025

I think the attribute shouldn't be clippy-specific — other tools might be able to use that as well.

@nyurik
Copy link
Contributor Author

nyurik commented Feb 21, 2025

@tbu- not sure i understand. Anyone can use this attribute, but it would be quiet a challenge to introduce a new attribute namespace to the whole ecosystem, especially because now clippy utils is published as a crate, and can be reused by others... so it already expects this namespace.

Since 1.85, Clippy [supports](See https://doc.rust-lang.org/nightly/clippy/attribs.html#clippyformat_args) attribute-based discovery of the `format!`-compatible macros, i.e. macros whose trailing arguments can be passed to `format_args!` as is.

Tagging core library with the same attribute will allow clippy to stop maintaining a separate list of format-like macros - allowing Clippy to validate macro usage for all lints that work on format arguments. See also rust-lang/rust-clippy#14267
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#22 exporting to docker image format
#22 sending tarball 28.8s done
#22 DONE 31.8s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-18]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Listening on address 127.0.0.1:4226
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-18', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-18/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
   Compiling compiler_builtins v0.1.146
error: macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
    --> library/core/src/macros/mod.rs:64:114
     |
42   | / macro_rules! assert_eq {
43   | |     ($left:expr, $right:expr $(,)?) => {
44   | |         match (&$left, &$right) {
45   | |             (left_val, right_val) => {
...    |
64   | |                     $crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($...
...    |
68   | |     };
69   | | }
     | |_- in this expansion of `assert_eq!`
     | |_- in this expansion of `assert_eq!`
     |
    ::: library/core/src/mem/maybe_uninit.rs:1400:9
     |
1400 |           assert_eq!(self.len(), src.len(), "destination and source slices have different lengths");
     |
     = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
     = note: for more information, see issue #52234 <https://github.com/rust-lang/rust/issues/52234>
note: the macro is defined here
note: the macro is defined here
    --> library/core/src/macros/mod.rs:1096:5
     |
1096 | /     macro_rules! format_args {
1097 | |         ($fmt:expr) => {{ /* compiler built-in */ }};
1098 | |         ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
     | |_____^
     = note: `#[deny(macro_expanded_macro_exports_accessed_by_absolute_paths)]` on by default

error: macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
---
     |
    ::: library/core/src/num/bignum.rs:34:1
     |
34   | / macro_rules! impl_full_ops {
35   | |     ($($ty:ty: add($addfn:path), mul/div($bigty:ident);)*) => (
36   | |         $(
37   | |             impl FullOps for $ty {
...    |
47   | |                     debug_assert!(borrow < other);
...    |
56   | | }
     | |_- in this expansion of `impl_full_ops!` (#1)
57   |
57   |
58   | / impl_full_ops! {
59   | |     u8:  add(intrinsics::u8_add_with_overflow),  mul/div(u16);
60   | |     u16: add(intrinsics::u16_add_with_overflow), mul/div(u32);
61   | |     u32: add(intrinsics::u32_add_with_overflow), mul/div(u64);
64   | | }
     | |_- in this macro invocation (#1)
     |
     = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
     = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
     = note: for more information, see issue #52234 <https://github.com/rust-lang/rust/issues/52234>
note: the macro is defined here
    --> library/core/src/macros/mod.rs:1682:5
     |
1682 | /     macro_rules! assert {
1683 | |         ($cond:expr $(,)?) => {{ /* compiler built-in */ }};
1684 | |         ($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }};
     | |_____^

error: macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
   --> library/core/src/macros/mod.rs:430:13
---
432 | |     };
433 | | }
    | |_- in this expansion of `debug_assert_eq!`
    |
   ::: library/core/src/num/flt2dec/strategy/grisu.rs:202:5
    |
202 |       debug_assert_eq!(plus.e, minus.e);
    |
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #52234 <https://github.com/rust-lang/rust/issues/52234>
note: the macro is defined here
note: the macro is defined here
   --> library/core/src/macros/mod.rs:42:1
    |
42  | / macro_rules! assert_eq {
43  | |     ($left:expr, $right:expr $(,)?) => {
44  | |         match (&$left, &$right) {
45  | |             (left_val, right_val) => {
68  | |     };
69  | | }
    | |_^


error: macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
    --> library/core/src/macros/mod.rs:715:24
     |
713  | / macro_rules! write {
714  | |     ($dst:expr, $($arg:tt)*) => {
     | |                        ^^^^^^^^^^^^^^^^^^^
716  | |     };
717  | | }
     | |_- in this expansion of `write!` (#2)
     | |_- in this expansion of `write!` (#2)
     |
    ::: library/core/src/num/bignum.rs:70:1
     |
70   | / macro_rules! define_bignum {
71   | |     ($name:ident: type=$ty:ty, n=$n:expr) => {
72   | |         /// Stack-allocated arbitrary-precision (up to certain limit) integer.
...    |
...    |
413  | |                 write!(f, "{:#x}", self.base[sz - 1])?;
...    |
420  | |     };
421  | | }
421  | | }
     | |_- in this expansion of `define_bignum!` (#1)
...
426  |   define_bignum!(Big32x40: type=Digit32, n=40);
     |
     = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
     = note: for more information, see issue #52234 <https://github.com/rust-lang/rust/issues/52234>
note: the macro is defined here
note: the macro is defined here
    --> library/core/src/macros/mod.rs:1096:5
     |
1096 | /     macro_rules! format_args {
1097 | |         ($fmt:expr) => {{ /* compiler built-in */ }};
1098 | |         ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
     | |_____^

error: macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
    --> library/core/src/prelude/v1.rs:64:5
    --> library/core/src/prelude/v1.rs:64:5
     |
64   |     assert, cfg, column, compile_error, concat, concat_idents, env, file, format_args,
     |
     = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
     = note: for more information, see issue #52234 <https://github.com/rust-lang/rust/issues/52234>
note: the macro is defined here
note: the macro is defined here
    --> library/core/src/macros/mod.rs:1682:5
     |
1682 | /     macro_rules! assert {
1683 | |         ($cond:expr $(,)?) => {{ /* compiler built-in */ }};
1684 | |         ($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }};
     | |_____^

error: macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
    --> library/core/src/prelude/v1.rs:64:75
    --> library/core/src/prelude/v1.rs:64:75
     |
64   |     assert, cfg, column, compile_error, concat, concat_idents, env, file, format_args,
     |
     = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
     = note: for more information, see issue #52234 <https://github.com/rust-lang/rust/issues/52234>
note: the macro is defined here
note: the macro is defined here
    --> library/core/src/macros/mod.rs:1096:5
     |
1096 | /     macro_rules! format_args {
1097 | |         ($fmt:expr) => {{ /* compiler built-in */ }};
1098 | |         ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
     | |_____^

error: macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
     |
---
137  | / pub macro unreachable_2021 {
138  | |     () => (
139  | |         $crate::panicking::panic("internal error: entered unreachable code")
140  | |     ),
141  | |     ($($t:tt)+) => (
142  | |         $crate::panic!("internal error: entered unreachable code: {}", $crate::format_args!($($t)+))
143  | |     ),
144  | | }
     | |_- in this expansion of `$crate::panic::unreachable_2021!` (#2)
     |
---
note: the macro is defined here
    --> library/core/src/macros/mod.rs:1096:5
     |
1096 | /     macro_rules! format_args {
1097 | |         ($fmt:expr) => {{ /* compiler built-in */ }};
1098 | |         ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
     | |_____^

error: could not compile `core` (lib) due to 7 previous errors
Build completed unsuccessfully in 0:00:22

@blyxyas
Copy link
Member

blyxyas commented Mar 4, 2025

We need to benchmark this in Clippy, it may add way more work for the CPU than it's worth.

I'll benchmark it in the coming days :)

@@ -895,6 +897,7 @@ macro_rules! unreachable {
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "unimplemented_macro")]
#[allow_internal_unstable(panic_internals)]
#[clippy::format_args]
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why this one is not gated on cfg(test) but all others are?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TBH, I do not think any usage of the clippy attribute should be gated on anything - e.g. if its a test, clippy should still do the format-related lints. The bigger issue is the #98291 - which blocks many crates as well as stdlib from usign this feature, and I have no idea if there is a workaround for it...

@joboet
Copy link
Member

joboet commented Mar 5, 2025

I don't feel qualified to review this.

r? @m-ou-se
as Iirc, you've worked on formatting-related lints in clippy before

@rustbot rustbot assigned m-ou-se and unassigned joboet Mar 5, 2025
@bors
Copy link
Contributor

bors commented Mar 8, 2025

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

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. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants