Skip to content

An imported forward dotted datatype is resolved as lib.lib:Type #1514

Open

Description

@jasisz

What you did

Check an entry file that imports a library whose type-level family names a dotted local datatype declared later:

bend main.bend --check-only

main.bend:

import Base
import ./lib.bend as L

def main() -> U32:
  L.main()

lib.bend:

import Base

def Fam(n: Nat) -> Data:
  match n:
    case 0n: Unit
    case 1n+p: Cell.At<p>

type Cell.At<-p: Nat> is Data:
  Cell{}

def main() -> U32:
  6

What happened

lib.bend checks by itself, but importing it fails even though Fam is unused:

SOME PROOFS FAIL
Error:
- message  : a declared datatype (unknown: lib.lib:Cell.At)
Context:
- p : Nat
Location: Fam
5 |     case 0n: Unit
6>|     case 1n+p: Cell.At<p>
  |                ^^^^^^^^^
7 |

Moving type Cell.At above Fam, or renaming it to an undotted CellAt, makes the imported file check. This also reproduces by importing the existing passing tests/check/mutual_type_def.bend, where Vec.Con<p> becomes tests/check/mutual_type_def.tests/check/mutual_type_def:Vec.Con.

The <..> parser appears to resolve the family head twice. parse_var first turns Cell.At into the internal key lib:Cell.At; because the datatype is declared later, the <..> branch then passes that key to parse_reso again and gets lib:lib:Cell.At. Forward datatypes are available during validation, but the malformed key is already stored in the parsed term.

Reproduced on current main at d71f42ee287bd7b9f86b44df44b210ac5eb651bd. I could not turn this into an Empty inhabitant: legal declarations cannot create the two-colon internal key, so this fails closed as an import/order bug.

bend --version

bend 2.0.36

uname -sm

Darwin arm64

clang --version (the first line)

Apple clang version 21.0.0 (clang-2100.0.123.102)

Activity

  1. viridale commented on Oct 12, 2026

    @viridale
    Contributor

    Confirmed on main at 0e14aa8. Your diagnosis is right, and the same double resolution breaks a second shape: a \{..} arm that names a dotted constructor declared later.

    bend2/bend.ts is human-written (AGENTS.md), so here is a patch and a test for a maintainer to apply, not a PR.

    Cause

    parse_var resolves a dotted name once and returns a Ref that holds the key (lib:Cell.At). Three call sites pass a Ref's key to parse_reso again:

    When the name is already in the book, parse_reso returns the key unchanged. For a forward name in an imported file, parse_qual adds the namespace a second time.

    A source name has no :, since char_is_name allows only letters, digits, _ and .. So a key that contains : is already resolved, and parse_reso can return it unchanged:

     export function parse_reso(p: Parse, k: Name): Name {
    +  if (k.includes(":")) {
    +    return k;
    +  }
       const dot = k.indexOf(".");

    Fixing only L2108 (out.$ === "Ref" ? out.k : parse_reso(p, out.k)) clears the family head. The arm still fails:

    - expected : a constructor of forward_dotted_lib.Cell.At (missing, or already matched)
    - observed : \{forward_dotted_lib: (v => v)}
    

    The patch also covers L2137. For a forward T.add used in an imported file's (x + y : T), the import gave a different error (a defined name, observed ops). It now gives the same error the entry file does: that the method is used before it is defined.

    Test

    These go in tests/import/. On main, forward_dotted.bend fails with unknown: forward_dotted_lib.forward_dotted_lib:Cell.At. With the patch it prints 5.

    forward_dotted_lib.bend:

    # a def names a dotted datatype and constructor declared below it. an
    # imported file resolved Cell.At<p>'s head and the Cell.Mk arm's name
    # twice, into lib:lib:Cell.At, a key no file declares (#1514)
    import Base
    
    def Fam(n: Nat) -> Data:
      match n:
        case 0n:
          Unit
        case 1n+p:
          Cell.At<p>
    
    def get(p: Nat, c: Fam(1n+p)) -> U32:
      {\{Cell.Mk: v => v} : Cell.At<p> -> U32}(c)
    
    type Cell.At<-p: Nat> is Data:
      Cell.Mk{v: U32}
    
    def main() -> U32:
      get(2n, Cell.Mk{7})
    
    #|7

    forward_dotted.bend:

    # a module's defs name its dotted datatypes before declaring them
    import Base
    import ./forward_dotted_lib.bend as L
    
    def main() -> U32:
      L.get(3n, L.Cell.Mk{5})
    
    #|5

    With the patch, importing tests/check/mutual_type_def.bend also checks and prints 6.

    What I ran

    • Tests: gates/test.ts's shard script, run locally on this Mac in 4 shards with the patch and the two tests, passes 1606 / 1608. The two failures are gfx_clicks and gfx_window. They expect "no display", but this Mac has a desktop session, so a window opens. main fails the same two here.

    Not run: the cluster gates, Linux and CUDA.

  2. AbarnaaSree commented on Oct 13, 2026

    @AbarnaaSree

    Hi @jasisz, I'd like to contribute to this issue. I reviewed the discussion and the proposed fix for the double resolution of imported forward dotted datatype names. Could you please assign this issue to me if it's still available? I'd like to work on the fix and add the necessary regression tests. Thank you!

  3. HamedShams commented on Oct 15, 2026

    @HamedShams
    Contributor

    @viridale PRs that change bend.ts are fine when a person writes them; the “do not edit it” line is in AGENTS.md and is addressed to coding agents. Contributor commits to that file land regularly. Since the patch and both tests are already done, I suggest you open the PR yourself. It’s a cleaner credit trail than a maintainer lifting it from the thread.

    @AbarnaaSree this repo doesn’t hand out issues, so there’s no assignment to wait for. Those few assignees you’ve seen are the maintainers themselves. Whoever opens the PR has it! Since this one is already written up by @viridale, maybe it's better to proceed with another open issue.

  4. HamedShams commented on Oct 15, 2026

    @HamedShams
    Contributor

    @viridale heads-up: #1585 fixes the <..> head site but not the \{..} arm and with the root file namespaced there, the arm case now fails in a root file as well: your forward_dotted_lib.bend on its own is refused on that branch (details on my comment on #1585). Your guard on top of it makes both files pass, so the patch is still needed. If I were you I’d open the PR against main now; if #1585 lands first, it’s a small rebase

  5. HamedShams commented on Oct 20, 2026

    @HamedShams
    Contributor

    Opened #1785 with the guard and the two tests from this thread, credited to @viridale there. Checked on main at 34003e6: the full test tree is unchanged apart from the two new files, and the repo gate passes.

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