Skip to content

fix: refuse external subtypes of the range unions in the copy constructor - #213

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/closed-range-unions
Oct 18, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/closed-range-unions

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #206

Problem

The docs promised that the private constructor keeps each range union closed, so a consumer's five-arm switch is complete in fact. A record's copy constructor cannot be private (CS8878), and the compiler synthesizes a protected one on every non-sealed record, so this compiled against the shipped package:

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

The value is Int32Range, implements no shape interface, and the library answered it silently: IsEmpty(), Contains(5) and Overlaps(...) all returned false. Only ToString() and a consumer's throwing discard arm noticed. Found by the external assessment of 2026-10-18 and reproduced in a stand-alone console project outside the repo.

Change

  • src/CodoMetis.ValueRanges/Internals/ClosedUnion.cs — Require<TUnion>(instance): throws InvalidOperationException naming the type and the union unless the runtime type is nested in the union.
  • Every union (seven core, four NodaTime) declares its copy constructor and calls it: protected Int32Range(Int32Range original) => ClosedUnion.Require(this);. with on a real variant still passes (its runtime type is nested in the union); a derived record throws at construction, before the value can reach any switch. No existing answer changes.
  • docs/ranges.md (consumer) and docs/architecture.md (contributor): the seal is the copy constructor's check, not the constructor's accessibility.
  • test/oracles/claims.json: new claim R31-union-closed-at-construction quoting the reworded sentence, held by the new test.
  • CHANGELOG.md: 10.0.0 Fixed.

Tests

ClosedUnionContractTests (conventions project):

  • Int32Range_RefusesASubtypeDeclaredInAnotherAssembly — the consumer's Rogue record in source, expecting InvalidOperationException.
  • EveryUnion_RefusesAnEmittedSubtype_AndStillCopiesItsOwnVariants — for every discovered union, emits a derived type whose constructor chains to the copy constructor (and implements <Clone>$ as the compiler would), constructs it from each of the five shapes and expects the throw; then calls <Clone>$ (the with path) on each real variant and expects an equal value of the same variant. Asserts at least 11 unions were checked.

Revert-proven: with the copy constructors stripped from all eleven unions (helper left in place), both tests fail with "a subtype declared outside the union was constructed … the union is not closed"; restored, both pass. Conventions project 326/326, clone sweep 65 waived / 0 unwaived / 0 stale.

Follow-up: #210 (compile-time analyzer, decided after #148).

🤖 Generated with Claude Code

…ctor

A record's copy constructor cannot be private (CS8878), so the compiler's
synthesized protected one let a record declared anywhere derive from a
union and become a sixth variant that no shape interface described; the
shape predicates answered it false. Every union now declares the copy
constructor itself and throws through ClosedUnion.Require for a runtime
type not nested in the union, before the value can reach a switch.
ClosedUnionContractTests plays the consumer against every discovered
union (an emitted derived type and the source form), and checks the with
path on every real variant.

Closes #206

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

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.0.0 milestone Oct 18, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred area/ranges Range unions, variants and the shape engines package/nodatime Affects CodoMetis.ValueRanges.NodaTime package/core Affects CodoMetis.ValueRanges severity/medium Inconsistency or failure in a narrow scenario labels Oct 18, 2026
@CaffeinatedCoder
CaffeinatedCoder enabled auto-merge (squash) October 18, 2026 17:57
@CaffeinatedCoder
CaffeinatedCoder merged commit 23cb85c into main Oct 18, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/closed-range-unions branch October 18, 2026 18:00
CaffeinatedCoder added a commit that referenced this pull request Oct 18, 2026
The closed-union copy constructor (#213), CompareTo in PostgreSQL's range
order (#183), Shift/Expand (#185) and Split(step) (#197). Values(step)
stays with the discrete types.

Float arithmetic overflows to an infinity rather than throwing, so a moved
bound or split boundary past the finite values throws OverflowException as
for the other types, and a NaN or infinite step throws ArgumentException.
Each split boundary is Start + n * step, rounded once.

The arithmetic and split oracles gain a double grid step and an edge step
of whole ulps at MaxValue, where half a grid unit is absorbed by rounding.
Claims are renumbered onto the free ids after main's (E54-E57, M17, S22),
with R40 and R41 for the new sentences.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/ranges Range unions, variants and the shape engines bug Something isn't working package/core Affects CodoMetis.ValueRanges package/nodatime Affects CodoMetis.ValueRanges.NodaTime severity/medium Inconsistency or failure in a narrow scenario silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant