Skip to content

Narrowing-lattice rule table: (narrowing kind × store form × slot kind) → emitted-shape class and verdict, seeded with the Optional store-wrap cells #2488

Open

Description

@antonsynd

Sub-issue 2 of #2486 (owner request 2026-11-08).

What

The second executable table: (narrowing kind, store form, slot kind, value kind) → verdict + emitted-shape class + rule ID, driven by the same generator as the binding table (sub-issue 1), with Python as the control where the surface is in fidelity scope and the emitted-shape class checked by a Roslyn semantic-model pass over the generated C# (the pattern of EmittedOptionalConstructionConformanceTests).

Axes (closed rosters, contract §11):

  • narrowing kind: none; is not None / is None; isinstance(x, T) (reified generics per type_narrowing.md "isinstance on reified generics"); union-case match; walrus-introduced; and/or/ternary expression-level narrowing (type_narrowing.md §"Expression-level narrowing"); loop-carried; after a failed guard.
  • store form: =, augmented (+= …), ??=, tuple element, tuple unpacking target, walrus := (own site AND as a tuple/call-argument element), let declaration with annotation, attribute/subscript store, parameter write-through.
  • slot kind: local T?, parameter T?, closure-captured T?, field T?, element of list[T?], tuple component, object?, Optional<T> from .NET interop, non-Optional control.
  • value kind: payload literal (incl. unsuffixed float into float32?, decimal), Some(v), None(), another T?, an Unknown-typed expression, a call returning T?.

Seed cells (all measured @ 5d0187e, W2.2 ledger, no existing issue found for the first two)

narrowing store slot value today expected
isinstance(x, int) += local int? int SPY0908 CS0030 on ((int)x!) runs, Python output
is not None = local float32? 0.25 Optional<float>.Some(0.25d) → SPY0908 CS1503 runs
is not None walrus as tuple element local int? int prints (Some(42), 1); typed tuple CS0029 (42, 1) (fixed by #2443 W2.2, option B)
is not None walrus own site local int? int (c = 42) implicit T → Optional<T> (#1720 forbids) Optional<int>.Some(42) wrap
none = into T? any payload refused SPY0604/SPY0222 refused (control)

The rule they all instantiate is already in the spec (type_narrowing.md "Stores Use the Declared Type"): a store into a narrowed Optional slot wraps the payload explicitly, whatever the store form. The table makes "whatever the store form" a roster the compiler is checked against.

Drift prevention

  • Every row names its rule ID; DiagnosticExplanations for SPY0604/SPY0222/SPY0220 and the Optional-construction guard cite the rows they enforce (sub-issue 3 checks the cites).
  • The generator renders one program per cell; the verdict (refuse code / run+stdout) and the emitted-shape class (explicit Some(...) wrap present / no implicit conversion, by Roslyn conversion analysis) are both compared; disagreements go to an allowlist citing issues, drained on fix.
  • Totality: every roster value from a compiler enumeration (the store forms from the parser's assignment/target node kinds; narrowing kinds from TypeNarrowingContext's producers) or a named closed set; mutations: drop a row → red; flip a shape class → red on the named cell.

Relation to P82 (#2443)

P82 moves the store-wrap FACT onto IR nodes (_optionalStoreWraps tranche 1). This table checks the DECISION that produces the fact, across the whole lattice, and would have caught both walrus cells before a plan did.

Placement

With #2486 at the next /batch-issues; the generator is sub-issue 1's; the Roslyn shape check reuses EmittedOptionalConstructionConformanceTests' analysis.

Activity

  1. added
    lane:tooling
    reach:program
    on Nov 8, 2026
  2. antonsynd commented on Nov 8, 2026

    @antonsynd
    OwnerAuthor

    Batching 2026-11-08 @ 10e1c1b — charter docs/process/rounds/remediation-round-2026-09-20-batching.md §3.12, P51 — narrowing authority (order 12): this table is P51's Generator: REQUIRED FIRST (R-LO a). (narrowing kind × store form × slot kind) → emitted-shape class and verdict, as data with rule IDs, seeded with the five store-wrap cells and #2494's four; StoreTargetMatrixTests gains the narrowing-kind and target-kind axes and is checked against the table; the differential (table verdict vs compiled C#) is the allowlist ratchet. Since P82 tranche 1 the wrap fact is IrOptionalStoreWrap from LoweringPass.OptionalStoreWrapOf, so the table's verdicts are what that derivation must reproduce. Lane / reach: tooling · program. Tracker: #2486.


    Generated by Claude Code

  3. antonsynd commented on Nov 12, 2026

    @antonsynd
    OwnerAuthor

    Cell: a walrus-introduced is not None test does not narrow a value-type payload

    Measured with run @ afcbead (dev c1a8c11 plus test-only commits), oracle CPython 3.15.2. Found while probing plan-4e2867; outside that plan's contract.

    def f() -> int | None:
        return 3
    
    def g(n: int) -> int:
        return n + 1
    
    def main():
        if (x := f()) is not None:
            print(g(x))
    Use of x inside the branch int | None (walrus) str | None (walrus) int | None, x = f() then if x is not None: CPython
    argument g(x) SPY0220 runs runs, 4 4
    store y: int = x SPY0220 runs n/a runs
    operand x + 1 SPY0222 n/a n/a 4

    Table coordinates: narrowing kind = walrus-introduced, slot kind = local T? with a value-type payload; the reference payload narrows, the value payload does not. Lane: false-refusal, reach: program. walrus_operator.md gives if (m := …) is not None: as the idiom.


    Generated by Claude Code

  4. antonsynd commented on Nov 19, 2026

    @antonsynd
    OwnerAuthor

    Reproduction sweep, 2026-11-16: PARTIAL (at least one cell is right today and at least one is not).

    Re-measured against dev @ d81425f with a Debug sharpyc, about eight cells per issue; CPython 3.15.2 where python is the standard. Copied at batching call 46 (2026-11-19) from docs/process/rounds/sweep-2026-11-16.md. Not re-run since: P77 merged after the sweep, so a plan re-measures before relying on a row.

    Today: The table itself does not exist (static). Of the seed cells, the two walrus cells are fixed ((42, 1); own-site emits Optional<int>.Some(42)) and the control still refuses SPY0604; isinstance '+=' is still SPY0908 CS0030 and the float32 literal still SPY0908 CS1503. The comment cell still reproduces: SPY0220 / SPY0220 / SPY0222 for 'int | None' via walrus.

    Cells (controls left out):

    • reproduces — seed 1: isinstance(x, int) narrowing, 'x += 1' on local int?: error[SPY0908]: internal error: generated C# failed to compile (CS0030: Cannot convert type 'Sharpy.Optional<int>' to 'int')
    • reproduces — seed 2: 'is not None' narrowing, 'f = 0.25' into local float32?: error[SPY0908]: internal error: generated C# failed to compile (CS1503: Argument 1: cannot convert from 'double' to 'float')
    • fixed — seed 3: walrus as an unparenthesized tuple element into a narrowed int? (untyped tuple; cell3b: typed tuple[int, int]): (42, 1) for both the untyped and the typed tuple
    • fixed — seed 4: walrus own-site store into narrowed int?: emitted shape: runs, prints 42 / 42; emitted: global::Sharpy.Builtins.Print(((c = Optional<int>.Some(42)).Unwrap()));
    • reproduces — comment cell: walrus 'int | None' is not None, argument g(x): error[SPY0220]: Cannot pass argument of type 'int32 | None' to parameter of type 'int32' @9:17
    • reproduces — comment cell: walrus 'int | None', store 'y: int = x': error[SPY0220]: Cannot assign type 'int32 | None' to variable of type 'int32' @6:9
    • reproduces — comment cell: walrus 'int | None', operand 'x + 1': error[SPY0222]: Type 'int32 | None' does not support operator '+' with operand of type 'int32' @6:15
    • fixed — comment-cell controls: walrus 'str | None' (argument and store) and plain-binding 'int | None': ab! / ab / 4

    Generated by Claude Code

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

    lane:toolingreach:program

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions