Skip to content

fix: refuse a value object's Equals of a base record's type - #202

Merged
CaffeinatedCoder merged 3 commits into
release/2.3.1from
fix/hand-written-equals-tbase-refused-2-3-1
Oct 30, 2026
Merged

CaffeinatedCoder merged 3 commits into
release/2.3.1from
fix/hand-written-equals-tbase-refused-2-3-1

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #196

Problem

A value object over a base record could declare an Equals of a deeper base's type:

public abstract record DeepBase;
public abstract record MiddleBase : DeepBase;

public sealed partial record Tag : MiddleBase, IValue<string>
{
    public bool Equals(DeepBase? other) => ReferenceEquals(this, other);
}

Tag.From("a") == Tag.From("a");                                // true
new HashSet<DeepBase> { Tag.From("a"), Tag.From("a") }.Count;  // 2

It implemented IEquatable<DeepBase>, which the value object lists again through IReimplementedEquatable<DeepBase>, so a collection of the base compared by it while == compared the wrapped value. ValueObjectDeclaration.EqualityDeclaredByHand refused Equals(TSelf) alone, and ValueObjectContractAspect.IntroduceEquality skipped its own new Equals(TBase) because the value object declared one. Measured on release/2.4.0, the code unchanged since 2.3.0.

Change

  • EqualityDeclaredByHand refuses an Equals of the value object's type or of any of its base records' types, nullability aside, with the existing CMTK1011 and its remedy (normalise in Create). One of the direct base's type is C#'s own CS0111, beside the override the record synthesizes (measured).
  • The Generators README's "Equality" says so, held by claim C22-equals-of-a-base-record-s-type. The 2.3.1 changelog's Changed section says a build can fail on upgrade.
  • No decision record of its own: decision 28 refuses a hand-written equality, and the exception to decision 34 is An interface's explicit implementation of a generated interface member is replaced without a diagnostic #191's in the patch 2.3.1 (decision 121 on its PR).

Tests

BuildOutcomeTests.A_hand_written_equality_is_refused_by_name gains two rows:

  • EqualsDeeperBase, an Equals(DeepEqualityBase?) of a deeper base's type, which fails before the fix and when the base types are dropped from the check (1 of 233).
  • EqualsOnClass, a record class's Equals(TSelf?), which had no fixture. It was refused already, so no sibling was found.

Input class (GTS): the parameter type of a hand-written Equals, which is the value object's own (struct, class), a deeper base's, or the direct base's (C#'s CS0111).

Stacked on #200 (build/open-2-3-1): merge it first. Independent of the #191 PR, though both add a line to 2.3.1's changelog.

🤖 Generated with Claude Code

A value object over a base record could declare an Equals of a deeper
base's type, such as Equals(DeepBase?). It implemented IEquatable<DeepBase>,
which the value object lists again through IReimplementedEquatable, so a
HashSet<DeepBase> compared by it, while == compared the wrapped value:
two holding "a" were equal, and the set held both.
EqualityDeclaredByHand refused Equals(TSelf) alone, and
IntroduceEquality skipped its own new Equals(TBase) because the value
object declared one.

EqualityDeclaredByHand now refuses an Equals of the value object's type
or of any of its base records' types, nullability aside, with the
existing CMTK1011 and its remedy (normalise in Create). One of the
direct base's type is C#'s own CS0111, beside the override the record
synthesizes (measured).

Input class (GTS): the parameter type of a hand-written Equals. Catalogue:
the value object's own on a record struct and on a record class (nullable
parameter, already refused), a deeper base's (refused now), and the
direct base's (C#'s CS0111). The record class case had no fixture; it
has one now, and no sibling was found.

The test fails before the fix and with the base types dropped from the
check (1 of 233 BuildOutcomeTests).

Refs #196

Defect-Class: GTS
Found-By: OR
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 2.3.1 milestone Oct 30, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Compiles or answers and does less than it appears to stacked Base is another open PR; merge in order, CI does not run until it targets a release severity/medium Inconsistency or failure in a narrow scenario package/generators Affects CodoMetis.TypeKit.Generators area/value-objects Value-object contracts and what the aspects generate for them labels Oct 30, 2026
Base automatically changed from build/open-2-3-1 to release/2.3.1 October 30, 2026 16:39
@CaffeinatedCoder CaffeinatedCoder removed the stacked Base is another open PR; merge in order, CI does not run until it targets a release label Oct 30, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 70e3d8b into release/2.3.1 Oct 30, 2026
10 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/hand-written-equals-tbase-refused-2-3-1 branch October 30, 2026 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/value-objects Value-object contracts and what the aspects generate for them bug Something isn't working package/generators Affects CodoMetis.TypeKit.Generators severity/medium Inconsistency or failure in a narrow scenario silent Compiles or answers and does less than it appears to

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant