Repository navigation
fix: refuse external subtypes of the range unions in the copy constructor - #213
Merged
Merged
Conversation
…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]>
# Conflicts: # test/oracles/claims.json
CaffeinatedCoder
enabled auto-merge (squash)
October 18, 2026 17:57
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
The value is
Int32Range, implements no shape interface, and the library answered it silently:IsEmpty(),Contains(5)andOverlaps(...)all returnedfalse. OnlyToString()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): throwsInvalidOperationExceptionnaming the type and the union unless the runtime type is nested in the union.protected Int32Range(Int32Range original) => ClosedUnion.Require(this);.withon 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) anddocs/architecture.md(contributor): the seal is the copy constructor's check, not the constructor's accessibility.test/oracles/claims.json: new claimR31-union-closed-at-constructionquoting the reworded sentence, held by the new test.CHANGELOG.md: 10.0.0 Fixed.Tests
ClosedUnionContractTests(conventions project):Int32Range_RefusesASubtypeDeclaredInAnotherAssembly— the consumer'sRoguerecord in source, expectingInvalidOperationException.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>$(thewithpath) 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