Skip to content

Range unions are not closed: the synthesized protected copy constructor admits a sixth variant #206

Closed

Description

@CaffeinatedCoder

Problem

docs/architecture.md (line 141) and docs/ranges.md (line 91) promise that the private constructor makes the five nested variants the only ranges that can exist, so a consumer's five-arm switch is complete in fact. That is not true for a record. The compiler synthesizes a protected copy constructor on every non-sealed record, and C# refuses to let it be private (CS8878: the copy constructor of a non-sealed record must be public or protected). A consumer can chain to it from a derived record's primary constructor:

public sealed record Rogue(Int32Range Seed) : Int32Range(Seed);

Reproduced today against the built CodoMetis.ValueRanges.dll from a stand-alone console project outside the repo (so Directory.Build.props does not apply). Output:

type=Rogue is Int32Range=True
switch: sixth variant: Rogue            ← the consumer's throwing discard fires
ToString: InvalidOperationException: Unknown range variant: Rogue.
IsEmpty=False
Contains(5)=False
Overlaps=False

The last three lines are the part the external assessment did not mention: the value is not only constructible, the algebra answers it silently. IsEmpty(), IsFinite() etc. are is-tests on the shape interfaces, so a sixth variant that implements none of them is "not empty, not finite, not unbounded, not infinity", and the receiver-shaped predicates in RangeExtensions.cs (Overlaps, line 252 onward) reach their inner _ => false arms. Those arms are legitimately the IEmptyRange case for the five real variants, and EngineDispatchConventionTests only parses src/**/Internals/, so nothing catches this.

Every union is affected the same way: the eight core unions and the three NodaTime ones (and the float families in flight). Reflection on Int32Range, DateRange, DecimalRange shows the synthesized protected .ctor(XRange original) on each.

Proposed change

A compile-time seal is impossible while the unions stay records (CS8878), and turning them into classes would mean hand-written equality on every variant. The cheap runtime seal is an explicit copy constructor on each union that refuses any runtime type that is not one of its own nested variants:

protected Int32Range(Int32Range original)
{
    if (GetType().DeclaringType != typeof(Int32Range))
        throw new InvalidOperationException($"{GetType()} is not a variant of {nameof(Int32Range)}; the union is closed.");
}

GetType() inside a constructor is the runtime type, so a with on a real variant (Finite → DeclaringType == Int32Range) passes and a Rogue throws at construction, before it can reach any switch. One line per union, no change to any existing answer.

Then:

  • a conventions test that defines a derived record against every discovered union (the reproduction above, parameterized) and asserts construction throws — this is also the guard to revert-prove;
  • docs/architecture.md §141 and docs/ranges.md §91 reworded: the seal is enforced at construction, not by the constructor's accessibility;
  • clone-waivers.json / the clone sweep should list a union without the explicit copy constructor as a divergence once the first one has it.

#148 (C# 15 closed hierarchies) is the eventual language-level answer; this closes the gap on net10.0.

Verification

Reproduction project: a console app referencing the built DLL with the Rogue record above, run before and after the fix. After the fix, new Rogue(...) must throw and Int32Range.CreateFinite(1, 10) with { } must not.

Found-By: external assessment (GPT Sol), 2026-10-18; reproduced locally.
Defect-Class: documented guarantee stronger than implementation.

Activity

  1. added this to the 10.0.0 milestone on Oct 18, 2026
  2. added
    bug
    silent
    area/ranges
    package/nodatime
    package/core
    severity/medium
    on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/rangesbugpackage/corepackage/nodatimeseverity/mediumsilent

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions