Skip to content

Generators: every generated TryParse(string?, IFormatProvider?, out) ends in unreachable code #100

Closed

Description

@CaffeinatedCoder

Every generated TryParse(string?, IFormatProvider?, out TSelf) ends with unreachable code after its return: a declaration of __inner and a try/catch (FormatException) that assigns it. From the woven OrderId of the samples (obj/…/metalama/Root/IntroSamples.cs), trimmed:

public static bool TryParse(string? s, IFormatProvider? provider, out OrderId result)
{
    result = default(OrderId);
    return GeneratedParsing.TryParseSpan<Guid>(s, provider ?? CultureInfo.InvariantCulture, out var innerValue) && __TryFromText(innerValue, out result);
    Guid __inner;
    try
    {
        __inner = GeneratedParsing.TryParseSpan<Guid>(s, ...) ? __parsed : throw GeneratedParsing.Unreadable<OrderId, Guid>();
    }
    catch (FormatException)
    {
        return false;
    }
}

The same tail is in the probes' woven code (ProbeId) and in every value object with parsing, so it is not an artefact of a test framework. It came up while reading the expected files of the AspectTesting spike (#93).

Cause

TryParseTemplate in src/CodoMetis.TypeKit.Generators/ValueObjectParsableAspect.Templates.cs returns from a compile-time if (the InnerTryParse and string branches), and the two meta.InsertStatement calls after it are still emitted into the run-time body.

Effect

None at run time: the code is unreachable, and the compiler emits no IL for it. It makes every generated TryParse longer and confusing to read, which matters now that the READMEs show generated code (#92), and a reviewer reading the woven code is led to look for a path that does not exist.

Done when

  • The generated TryParse ends at its return, for every parse strategy (StaticParse, StringConstructor, string, enum, NodaTime).
  • The generated-surface snapshot is unchanged (it lists members, not bodies), and a test reads the woven file and fails on a statement after the final return, shown to fail with the fix reverted.

Activity

  1. added this to the 2.3.0 milestone on Oct 28, 2026
  2. added
    bug
    severity/low
    package/generators
    area/parsing
    on Oct 28, 2026
  3. CaffeinatedCoder commented on Oct 28, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Fixed in PR #106: TryParseTemplate's branches are an if-else chain now, so the InsertStatements run only in the last one. WovenReachabilityTests (conventions) reads the probes' woven files and found 26 unreachable statements before the fix, none after. This closes by hand when #106 merges into release/2.3.0.

  4. CaffeinatedCoder commented on Oct 28, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Done in PR #106, merged into release/2.3.0.

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/parsingbugpackage/generatorsseverity/low

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions