Under rust-lldb on macOS, an `Option<String>` the code set to `None` prints as `Some(<error: ...>)`; what is happening, and how do you check the real value?
answer
- no tag field to read
- the value hides in capacity
- variant names versus $discr$
- fallback to the dataful variant
- frame variable -R
basics
~20 sThe program is fine; the formatter picked the wrong variant. Option<String> stores None as an impossible capacity value, and when the provider cannot match that value to a variant it falls back to Some. frame variable -R shows the raw discriminant.
solid answer
~50 s`Option<String>` has no separate tag: it is 24 bytes, like `String`, and stores `None` as a value the capacity field can never hold (a **niche**). LLDB shows the enum as `$variants$` members named `$variant$<value>`, each with a `$discr$` child, plus one unnamed `$variant$` for `Some`. The provider in `lldb_providers.py` picks the member whose name equals `$variant$` plus the `$discr$` it reads, and otherwise **falls back to the member without `$discr$`**, the dataful one. In this session the member was named `$variant$4294967295` while `$discr$` read `18446744073709551615`: the name carried only the low 32 bits. No name matched, so `Some` was shown and its `String` summary read garbage. Check with `frame variable -R missing` or `memory read`, or print with `{:?}` in the program. Enums whose niche is a null pointer, and tagged enums such as `Option<u32>`, display correctly.
code
rust · 8 linesfn main() {
let user: Option<String> = Some("ana".to_string());
let missing: Option<String> = None;
let code: Result<u32, String> = Ok(5);
let boxed: Option<Box<u32>> = None;
let sizes = (size_of::<String>(), size_of::<Option<String>>());
println!("{user:?} {missing:?} {code:?} {boxed:?} {sizes:?}");
}go deeper
Remember that a debugger's pretty display can be wrong, and that a raw view or a {:?} print settles what a value really is.
Explain niche encoding: Option<String> has no tag and stores None as an impossible capacity, so a debugger must decode the variant from that field.
Walk through the provider's choice: member names against $discr$, fallback to the dataful variant, and confirm with frame variable -R or memory read before touching code.
Set the expectation that formatter output is evidence only after cross-checking, and ask bug reports to include raw views or Debug output for enums.
## Some enums have no tag to read A Rust enum usually stores a **discriminant** (a tag) beside its payload. `Option<u32>` does: the tag says `None` or `Some`. `Option<String>` does not. It is 24 bytes, the same as `String` (`size_of` confirms 24 for both on a 64-bit target), because the compiler stores `None` as a bit pattern the `String`'s capacity field can never hold. That spare pattern is a **niche**; why the layout works this way belongs to the type-layout material. In the sessions behind this answer, `memory read` showed the first word of the `None` value holding `0xffffffffffffffff`. For a debugger, a niche means there is no tag to read. To decide the variant, it must read the shared field and compare it against the values the debug info assigns to each variant. ## How LLDB and the providers pick a variant LLDB presents a Rust enum like this as a struct with one member, `$variants$`, which holds one member per variant: - each variant identified by a value is named `$variant$<value>` and has a `$discr$` child that reads the shared field; - the variant that owns the payload storage (the **dataful** variant, here `Some`) is named just `$variant$` and has no `$discr$`. `ClangEncodedEnumProvider` in `lldb_providers.py` loops over those members. When a member's name equals `$variant$` followed by the `$discr$` value it read, that variant wins. A member without `$discr$` is remembered as the default, and when no name matches, the provider returns that default. ## What went wrong in this session 1. `frame variable -R missing` showed a member named `$variant$4294967295` with `$discr$ = 18446744073709551615`. 2. 4294967295 is `0xffffffff`: the member name carried only the low 32 bits of the 64-bit value in the field. A user enum with a `String` payload showed the same pattern, with a value of `0x8000000000000002` in memory and a member named `$variant$2`. 3. No name matched any `$discr$`, so the provider fell back to the dataful variant and displayed `Some`. 4. The `String` summary then read whatever the pointer and length words held, giving `<error: memory read failed for ...>`, `<error: String pointer is null>` or `""` depending on the stack contents. This was observed with the formatters in rustup's stable 1.99 toolchain and the system LLDB from the macOS developer tools. A different LLDB build may name the members differently, which is exactly why the raw view is the thing to trust. ## Which values are affected | Value | Stored as | rust-lldb showed | |---|---|---| | `Option<u32>` = `None` | a separate tag | `None` (correct) | | `Option<Box<u32>>` = `None` | a null pointer, niche value 0 | `None` (correct) | | `Option<&str>` = `None` | a null pointer, niche value 0 | `None` (correct) | | `Option<String>` = `None` | a reserved capacity value | `Some(<error: ...>)` | | `Result<u32, String>` = `Ok(5)` | a reserved capacity value | `Err("")` or an error | | `Idle` in an enum with a `Tail(String)` variant | a reserved capacity value | `Tail(...)` with garbage | The pattern in these sessions: when the niche value does not fit in 32 bits, as with the reserved capacity values of `String` and `Vec`, the display can name the wrong variant. Niches at zero, and tagged enums, come out right. ## Confirming the real value - **`frame variable -R missing`** (`--raw-output`, "Don't use formatting options") prints the `$variants$` tree and the `$discr$` value with no provider involved. - **`memory read -s8 -fx -c3 &missing`** dumps the three words; a capacity word above `isize::MAX` means a niche-encoded variant, not a real `String`. - **Printing from the program** with `{:?}` or `dbg!` uses the type's own `Debug` impl and always names the right variant. ## How to carry this into a team - Treat any enum whose payload shows `<error: ...>` as a display problem first, and confirm with the raw view before "fixing" code that is not broken. - Do not take the variant name from the summary line in a bug report; include the raw view or a `{:?}` line. - When an editor front-end built on LLDB shows the same thing, the likely cause is the same formatter, since many such front-ends load the same Python module.
- Why does `Option<Box<u32>>` set to `None` display correctly when `Option<String>` does not?Both are niche-encoded, but `Box`'s niche is the null pointer, so `None` is stored as 0. The member LLDB builds is named `$variant$0` and `$discr$` reads 0, so the provider's name match succeeds. `String`'s niche values are capacity values above `isize::MAX`, which the member name in this session truncated to 32 bits, so the match failed.
- Which view always names the right variant, whatever the debugger build?The program's own `Debug` output, through `{:?}` or `dbg!`, because it runs the type's derived logic. Inside the debugger, `frame variable -R` or a `memory read` of the value shows what is stored without any provider deciding; you then read the stored field against the type's layout yourself.
- Some(<error: memory read failed>) on a None proves the Option was corrupted.
- Every Option prints wrongly under rust-lldb, so enums cannot be debugged.
- frame variable -A shows the raw value without the Rust formatters.
- Option<String> carries a separate one-byte tag the debugger reads.
- Rewriting the code to use Option<Box<String>> is the right fix.