Skip to content

Swift: branch counts optional types and chaining (T?, x?.y), for as an argument label, and default/case outside a switch (precision 0.66) #8895

Closed

Description

@squid-protocol

What is wrong

The Swift branch rule counts three things that are not decisions under the branch contract (docs/branch_rule_contract.md): optional types and optional chaining (T?, x?.y), for used as an argument label, and a default or case that is not in a switch. On vapor (1,906 Swift functions) the family fires in 267 functions where a parser sees no decision at all: recall 1.00, precision 0.66. Excluding those functions the precision is about 0.98, so the rule is otherwise sound. (The contract already fixed the same shape for other languages: "a generic wildcard (<?>) is a type, not a ternary"; ? is counted only as the ternary, and ?? as one value-selection decision.)

Reproduce (scanned with the pinned scanner)

struct S {
    func optionalType(_ x: Int?) -> Int? { return x }      // branch == 2, should be 0 (two optional types)
    func chaining(_ s: String?) -> Int { return s?.count ?? 0 }   // 3: the type `?`, the chain `?.`, and `??`; only `??` is a decision (1)
    func labelFor(for item: Int) -> Int { return item }    // 1: `for` is an argument label, not a loop
    func realIf(_ a: Int) -> Int { if a > 1 { return 1 }; return 0 }   // 1, correct
}

Expected

branch counts if/else/switch arms/loops/guard/the ternary ?/?? and not the ? of an optional type or chain, not a for label, and not a bare default/case outside a switch. Anchor the ? on ternary position (the contract's rule for java and ruby is the model) and the for on loop position.

How this was found

The keyword-family validity tool (#8864), by an oracle written for Swift under the epic's gate (the args and branch families must agree with the parser at 0.90 or better, to prove the oracle reads functions correctly): args passed at 0.98 / 1.00, branch recall passed at 1.00 and precision failed at 0.66, and the cause was traced to the engine's rule, not the oracle.

Part of #8863. Time the change (CPU seconds on keyword-rosetta and language-crucible); rosetta:rebless-owed if counts move; add the cases above as a test.

Activity

  1. added
    bug
    core-engine
    on Nov 21, 2026
  2. squid-protocol commented on Nov 22, 2026

    @squid-protocol
    OwnerAuthor

    Claimed by promoter-bot host melek, lease renewed 2026-11-23T12:11Z. It lapses after 6 h without a renewal. To take it back, assign someone or remove the claim:melek label.

  3. squid-protocol commented on Nov 23, 2026

    @squid-protocol
    OwnerAuthor

    Fixed by #9111 (merged as fcc440b), which carried this issue's PR (rebuilt with eight others to re-bless the golden snapshots once).

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

    bugclaim:melekcore-enginepriority: medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions