Skip to content

Implicit main: a helper fn cannot see the script's top-level bindings ("undefined variable") — the spec does not say whether they are globals #2951

Closed

Description

@ehartford

Conformance audit 2026-11-02. AGENTS.md: "a .w file needs no fn main; top-level statements are the program, beside helper fns". What a helper sees is unspecified, and today it sees nothing:

var count = 0
fn bump():
    count = count + 1
fn peek() -> i32: count
bump()
print(peek())
error: undefined variable   (count, in bump and in peek)

Every one-liner/script shape that keeps state (a counter, a table a helper fills) hits this and must pass the value through parameters. The spec has no text for the implicit main's scope (§18.5 names the mode; nothing says whether a top-level let/var is a module global or a local of the synthesized main). Needs Eric's ruling; prediction: a script's top-level bindings are the module's globals (as in Python, and as D175 already orders module globals), so helpers see them.

Activity

  1. ehartford commented on Nov 2, 2026

    @ehartford
    ContributorAuthor

    D184: ruled (spec v7.35 §29.15, #2970): a script's top-level bindings are module globals assigned in statement order; a read before its statement is D175's compile-time error. Implementation next.

  2. ehartford commented on Nov 2, 2026

    @ehartford
    ContributorAuthor

    D184 amended by Eric (spec v7.35 §29.15, merged): a script's top-level binding is a local of the implicit main, typed by its uses in main; a helper that names it captures it (observe or mutate, never consume; an ephemeral capture makes the helper non-escaping); helper locals shadow it (D173). Implementation follows on a new branch; the globals WIP (d184-script-globals) is superseded.

  3. ehartford commented on Nov 2, 2026

    @ehartford
    ContributorAuthor

    Implemented by #2983 under the amended D184 (§29.15): a script's top-level bindings are locals of the implicit main; a helper that names one captures it. The non-escaping rule for a captured view is #2984.

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