rust/compiler/rustc_builtin_macros/src
Guillaume Gomez 015e9371e0
Rollup merge of #123940 - kornelski:remove-derived-debug, r=Urgau
debug-fmt-detail option

I'd like to propose a new option that makes `#[derive(Debug)]` generate no-op implementations that don't print anything, and makes `{:?}` in format strings a no-op.

There are a couple of motivations for this:

1. A more thorough stripping of debug symbols. Binaries stripped of debug symbols still retain some of them through `Debug` implementations. It's hard to avoid that without compiler's help, because debug formatting can be used in many places, including dependencies, and their loggers, asserts, panics, etc.
   * In my testing it gives about 2% binary size reduction on top of all other binary-minimizing best practices (including `panic_immediate_abort`). There are targets like Web WASM or embedded where users pay attention to binary sizes.
   * Users distributing closed-source binaries may not want to "leak" any symbol names as a matter of principle.
2. Adds ability to test whether code depends on specifics of the `Debug` format implementation in unwise ways (e.g. trying to get data unavailable via public interface, or using it as a serialization format). Because current Rust's debug implementation doesn't change, there's a risk of it becoming a fragile de-facto API that [won't be possible to change in the future](https://www.hyrumslaw.com/). An option that "breaks" it can act as a [grease](https://www.rfc-editor.org/rfc/rfc8701.html).

This implementation is a `-Z fmt-debug=opt` flag that takes:

* `full` — the default, current state.
* `none` — makes derived `Debug` and `{:?}` no-ops. Explicit `impl Debug for T` implementations are left unharmed, but `{:?}` format won't use them, so they may get dead-code eliminated if they aren't invoked directly.
* `shallow` — makes derived `Debug` print only the type's name, without recursing into fields. Fieldless enums print their variant names. `{:?}` works.

The `shallow` option is a compromise between minimizing the `Debug` code, and compatibility. There are popular proc-macro crates that use `Debug::fmt` as a way to convert enum values into their Rust source code.

There's a corresponding `cfg` flag: `#[cfg(fmt_debug = "none")]` that can be used in user code to react to this setting to minimize custom `Debug` implementations or remove unnecessary formatting helper functions.
2024-08-29 16:21:46 +02:00
..
assert Reformat use declarations. 2024-07-29 08:26:52 +10:00
deriving Rollup merge of #123940 - kornelski:remove-derived-debug, r=Urgau 2024-08-29 16:21:46 +02:00
format_foreign Use as_deref in compiler (but only where it makes sense) 2022-11-16 21:58:58 +00:00
alloc_error_handler.rs chore: use shorthand initializer 2024-08-02 13:22:28 -04:00
asm.rs Use impl PartialEq<TokenKind> for Token more. 2024-08-14 16:37:09 +10:00
assert.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
cfg_accessible.rs Stabilize unsafe_attributes 2024-08-07 03:12:13 -05:00
cfg_eval.rs Distinguish the two kinds of token range. 2024-08-01 19:30:40 +10:00
cfg.rs Add toggle for parse_meta_item unsafe parsing 2024-07-30 18:28:43 -05:00
cmdline_attrs.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
compile_error.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
concat_bytes.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
concat_idents.rs Adjust some pubs. 2024-04-26 13:29:20 +10:00
concat.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
derive.rs Stabilize unsafe_attributes 2024-08-07 03:12:13 -05:00
edition_panic.rs Adjust some pubs. 2024-04-26 13:29:20 +10:00
env.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
errors.rs Add warn(unreachable_pub) to rustc_builtin_macros. 2024-08-16 08:46:57 +10:00
format_foreign.rs Add warn(unreachable_pub) to rustc_builtin_macros. 2024-08-16 08:46:57 +10:00
format.rs Rollup merge of #128762 - fmease:use-more-slice-pats, r=compiler-errors 2024-08-11 07:51:51 +02:00
global_allocator.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
lib.rs Add warn(unreachable_pub) to rustc_builtin_macros. 2024-08-16 08:46:57 +10:00
log_syntax.rs Adjust some pubs. 2024-04-26 13:29:20 +10:00
pattern_type.rs Mark Parser::eat/check methods as must_use 2024-07-29 21:29:08 -04:00
proc_macro_harness.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
source_util.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
standard_library_imports.rs Rename all ParseSess variables/fields/lifetimes as psess. 2024-03-05 08:11:45 +11:00
test_harness.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
test.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
trace_macros.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
util.rs Stabilize unsafe_attributes 2024-08-07 03:12:13 -05:00