Skip to content

A generic fn cannot read its own module's globals: undefined variable at instantiation (pub or private, let or const) #1743

Closed

Description

@ehartford

Spec: §18.1/§18.3 — a module's declarations are visible to that module's own code; §9.1c globals.

Repro (two files, base = branch gen-push-impl d06b5bc, release binary; installed main behaves the same):

own.w

pub let K: i32 = 5
pub fn g[T](a: T) -> i32: K

main.w

use own
fn main:
    print(f"{g(1)}")

Actual: error: undefined variable --> own.w:2:27 ... K (help: did you mean 'a'?). The same with let K (private) or const K. A private fn called from the generic body resolves (fn helper() -> i32: 2 / pub fn g[T](a: T) -> i32: helper() prints 2), so the instantiation sees the module's fns but not its values.

Expected: 5 — the generic body's module-level names are its module's, wherever it is instantiated.

Found while testing per-module global identity (#1703) on branch ruled-fixes; not caused by it (base reproduces). Workaround: none needed there — the fixture's generic reads no global.

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