rust-analyzer/crates
bors 15864ffeb5 Auto merge of #136932 - m-ou-se:fmt-width-precision-u16, r=scottmcm
Reduce formatting `width` and `precision` to 16 bits

This is part of https://github.com/rust-lang/rust/issues/99012

This is reduces the `width` and `precision` fields in format strings to 16 bits. They are currently full `usize`s, but it's a bit nonsensical that we need to support the case where someone wants to pad their value to eighteen quintillion spaces and/or have eighteen quintillion digits of precision.

By reducing these fields to 16 bit, we can reduce `FormattingOptions` to 64 bits (see https://github.com/rust-lang/rust/pull/136974) and improve the in memory representation of `format_args!()`. (See additional context below.)

This also fixes a bug where the width or precision is silently truncated when cross-compiling to a target with a smaller `usize`. By reducing the width and precision fields to the minimum guaranteed size of `usize`, 16 bits, this bug is eliminated.

This is a breaking change, but affects almost no existing code.

---

Details of this change:

There are three ways to set a width or precision today:

1. Directly a formatting string, e.g. `println!("{a:1234}")`
2. Indirectly in a formatting string, e.g. `println!("{a:width$}", width=1234)`
3. Through the unstable `FormattingOptions::width` method.

This PR:

- Adds a compiler error for 1. (`println!("{a:9999999}")` no longer compiles and gives a clear error.)
- Adds a runtime check for 2. (`println!("{a:width$}, width=9999999)` will panic.)
- Changes the signatures of the (unstable) `FormattingOptions::[get_]width` methods to use a `u16` instead.

---

Additional context for improving `FormattingOptions` and `fmt::Arguments`:

All the formatting flags and options are currently:

- The `+` flag (1 bit)
- The `-` flag (1 bit)
- The `#` flag (1 bit)
- The `0` flag (1 bit)
- The `x?` flag (1 bit)
- The `X?` flag (1 bit)
- The alignment (2 bits)
- The fill character (21 bits)
- Whether a width is specified (1 bit)
- Whether a precision is specified (1 bit)
- If used, the width (a full usize)
- If used, the precision (a full usize)

Everything except the last two can simply fit in a `u32` (those add up to 31 bits in total).

If we can accept a max width and precision of u16::MAX, we can make a `FormattingOptions` that is exactly 64 bits in size; the same size as a thin reference on most platforms.

If, additionally, we also limit the number of formatting arguments, we can also reduce the size of `fmt::Arguments` (that is, of a `format_args!()` expression).
2025-03-11 04:07:05 +00:00
..
base-db enable doctest 2025-02-27 14:58:46 +01:00
cfg Merge pull request #19243 from Veykril/push-qrrqsywkwyzp 2025-03-03 11:42:40 +00:00
edition Make edition per-token, not per-file 2025-01-09 05:43:08 +02:00
hir Merge pull request #19330 from ChayimFriedman2/normalize-projection 2025-03-10 09:15:35 +00:00
hir-def Auto merge of #136932 - m-ou-se:fmt-width-precision-u16, r=scottmcm 2025-03-11 04:07:05 +00:00
hir-expand Merge pull request #19327 from Veykril/push-qyyvkulltzpz 2025-03-10 08:45:04 +00:00
hir-ty Merge pull request #19331 from lnicola/sync-from-rust 2025-03-10 09:52:17 +00:00
ide Merge pull request #19330 from ChayimFriedman2/normalize-projection 2025-03-10 09:15:35 +00:00
ide-assists Merge pull request #19330 from ChayimFriedman2/normalize-projection 2025-03-10 09:15:35 +00:00
ide-completion Merge pull request #19330 from ChayimFriedman2/normalize-projection 2025-03-10 09:15:35 +00:00
ide-db Merge pull request #19331 from lnicola/sync-from-rust 2025-03-10 09:52:17 +00:00
ide-diagnostics Pass the target crate in HirFormatter 2025-03-06 21:00:05 +02:00
ide-ssr Pass the target crate in HirFormatter 2025-03-06 21:00:05 +02:00
intern Fix path macro hygiene 2025-03-10 09:29:27 +01:00
load-cargo Log build script error output in load_cargo::load_workspace_at 2025-03-07 16:07:56 +01:00
mbe Fix syntax fixup producing invalid punctuation 2025-03-08 13:21:00 +01:00
parser fix(hir): VariantDef is impl HasSource 2025-03-08 01:25:27 +05:30
paths ignore doc test that only fails on windows 2025-02-27 15:57:08 +01:00
proc-macro-api enable doctest 2025-02-27 14:58:46 +01:00
proc-macro-srv enable doctest 2025-02-27 14:58:46 +01:00
proc-macro-srv-cli Use interior mutability for loaded ProcMacrorv::expanders 2025-02-05 12:01:57 +01:00
profile Use size_of from the prelude instead of imported 2025-03-05 00:48:44 -08:00
project-model Move project MSRV back to 1.78 2025-03-07 07:51:53 +01:00
ra-salsa Downgrade salsa log levels 2024-12-28 18:23:24 +01:00
rust-analyzer Merge pull request #19330 from ChayimFriedman2/normalize-projection 2025-03-10 09:15:35 +00:00
span Make edition per-token, not per-file 2025-01-09 05:43:08 +02:00
stdx fix doc tests 2025-02-27 14:58:46 +01:00
syntax Merge pull request #19314 from snprajwal/variantdef-impl 2025-03-10 08:13:24 +00:00
syntax-bridge Fix syntax fixup producing invalid punctuation 2025-03-08 13:21:00 +01:00
test-fixture Use correct working directory for non-workspace proc-macro execution 2025-02-17 10:36:32 -05:00
test-utils Use size_of from the prelude instead of imported 2025-03-05 00:48:44 -08:00
toolchain enable doctest 2025-02-27 14:58:46 +01:00
tt Fix syntax fixup producing invalid punctuation 2025-03-08 13:21:00 +01:00
vfs ignore another test that fails on windows 2025-02-27 16:09:00 +01:00
vfs-notify enable doctest 2025-02-27 14:58:46 +01:00