Skip to content

debuginfo: min/max destroys WrappingRange semantics for niche discriminant in CPP-like encoding #462

Description

@SebTardif

Bug

In compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs:421-424, the niche discriminant range is computed using valid_range.start.min(valid_range.end) and .max(). WrappingRange can have start > end to represent a wrapping range (e.g., start=200, end=100 means {200..=255, 0..=100}).

Using .min()/.max() converts wrapping range (200, 100) into contiguous range (100, 200), which is the complement of the intended range. The CPP-like debuginfo path emits these as DISCR_BEGIN/DISCR_END for NatVis's is_in_range function, which already handles wrapping correctly. After min/max, the active variant determination is inverted.

Impact

For niche-encoded enums with wrapping valid ranges on MSVC targets (CPP-like debuginfo), the debugger identifies the wrong active variant. Currently latent because wrapping niche ranges are rare with current Rust types, but could manifest with more complex niche optimization.

Fix

Use valid_range.start and valid_range.end directly instead of .min()/.max(), preserving the wrapping semantics that NatVis already handles.

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-wrongWrong result or data corruptionP-mediumMedium impact: affects specific usage patternsbugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions