Skip to content

A ?? in arithmetic operand position gives its carrier no demand: total += s.parse() ?? 0 cannot infer #2724

Closed

Description

@ehartford

With #959's str.parse() (D160, on the fix-campaign stack), a ?? whose result is an arithmetic operand gives its carrier no demand, so a generic call on its left cannot infer its type:

var total: i64 = 0
for w in "40 x 2".fields(): total += w.parse() ?? 0
error: cannot infer generic method type parameter 'T'

The same for total = total + (w.parse() ?? 0). A typed binding works (let v: i64 = w.parse() ?? 0), and so does let n: i64 = s.parse() ?? 0.

Where: check_binary_operator (SemaCheck.w) gives an arithmetic operand its peer's type only when the operand is an untyped literal (§4.2.1 rule 3). A ?? operand is checked in value context, so the ?? has no expected type to hand its carrier.

The question is how far a peer type reaches. D103 says an operator's operands "are not demand sites; the peer type reaches an operand as a hint". Hinting a ?? operand with the peer would make i64_value + (m.get(k) ?? 0) over a HashMap[_, i32] join at i64 instead of i32 then widen, which may change which programs are accepted. That needs a look before it changes; it is why #959 did not take it.

This is awk's {s += $2} (#959's acceptance line for the one-liner lane).

Activity

  1. ehartford commented on Oct 26, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by 2360317 (stack #2743, battery GREEN on its top ddb68e8: behavior-tests 1949/1949, gate, uat and battery-checks green).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions