Skip to content

Parser accepts a closure return annotation (x: i32) -> str => x and silently drops it #1508

Closed

Description

@ehartford

Parser.parse_fat_arrow_paren_closure and Parser.parse_closure (src/Parser.w) parse an optional -> TYPE after a closure's parameter list with self.parse_type_expr() and discard the result. The type node is never stored on the NK_CLOSURE node, so Sema never sees it:

fn main:
    let f = (x: i32) -> str => x
    let y: i32 = f(2)
    print(f"{y}")

Actual (stage1 of origin/main f1e3bd2 + the #1402 closure-parameter fix): compiles, runs, and prints 2. The written return type str has no effect.

This is the forbidden silent case: the compiler accepts a construct and behaves as if it were not written (CLAUDE.md, No Silent Fallbacks).

What the spec says: §9.3 shows closures only as x => x + 1, (x, y) => x * y, () => ..., and it says "The -> token is reserved exclusively for return type annotations (e.g., fn foo() -> i32)". The spec does not say whether a closure may declare its return type, so which fix is right is a ruling:

  • (a) the annotation is part of the closure's type: the body is checked against it and the closure's fn type returns it; or
  • (b) the form is not part of the language, and -> after a closure's parameter list is a parse error that names the fix.

Either way, it must not be parsed and then dropped.

Found while fixing #1402 (closure parameter annotations with generic types), which sits in the same two parser functions.

Activity

  1. ehartford commented on Sep 23, 2026

    @ehartford
    ContributorAuthor

    Decision (from canon, no ruling needed): the spec's closure sections (§12.1–12.4) define no closure return annotation, and parsing -> TYPE then dropping it is a No Silent Fallbacks violation. A closure return annotation is a parse error with the fix-it: "closures infer their return type; annotate the binding or the expected function type instead". Implement in the next frontend batch.

  2. added a commit that references this issue on Sep 27, 2026
    d52a6ab
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions