Skip to content

User-defined Clo.apply collides with the compiler closure sentinel and duplicates a linear File #875

Closed

Description

@jasisz

What I did

bun bend2/main.ts clo_apply_duplicate_file.bend
bun bend2/main.ts clo_apply_duplicate_file.bend -o out.js
bun out.js
bun bend2/main.ts clo_apply_duplicate_file.bend -o out
./out

The source defines an ordinary function named Clo.apply. Its body always returns False{}, and the checker accepts a proof of that result:

def static_tag() -> {runtime_tag() == False{} : Bool}:
  {==}

False{} selects the linear File payload and keeps two distinct files. The program closes the first file and writes to the second, so its source result is True.

What happened

The default IO lane and emitted JS print:

False

The native binary aborts with:

bend: an alien request

Changing only Clo.apply to User.apply makes the default lane, emitted JS, and native binary all print:

True

The compiler uses the source-level string "Clo.apply" as its synthetic closure-application sentinel:

const CLO_APPLY = "Clo.apply";

The JS emitter therefore compiles the direct user call Clo.apply(return_true, 0) as a dynamic call of return_true. The emitted user function exists but is never called:

function $runtime_tag$() {
  return run_tail(run_clo((x_0) => {
    return run_jump($return_true$, [x_0]);
  }), 0);
}

function $Clo$apply$(f_0, x_0) {
  return false;
}

That changes the dependent tag from the checker's False{} to runtime True{}. The True{} branch was checked with a Data payload, so it duplicates a. At runtime, a is the first File from the statically selected linear branch. Both result fields therefore contain the same file handle. The program closes fst; writing through snd then fails because it is the same closed handle, while the distinct second file is dropped.

This is observable wrong-code, dependent subject-reduction failure, and contraction of an opaque linear runtime handle. File is declared as an opaque Type law specifically so programs cannot forge, copy, or reuse it. The reproducer uses no @unsafe, user FFI, open user law, template, malformed pattern, or custom built-in datatype. It has the same dependent-grade consequence as #872, but a different cause: a legal source definition collides with a compiler-internal call sentinel.

A smaller pure reproducer isolates the same collision: the evaluator returns 12, emitted JS returns 22, and native C returns 0.

The file

import Base

def Clo.apply(f: U32 -> Bool, x: U32) -> Bool:
  False{}

def return_true(x: U32) -> Bool:
  True{}

def runtime_tag() -> Bool:
  Clo.apply(return_true, 0)

def static_tag() -> {runtime_tag() == False{} : Bool}:
  {==}

def payload_grade(tag: Bool) -> Quant:
  match tag:
    case False{}:
      &1
    case True{}:
      &2

def Payload(tag: Bool) -> Kind(payload_grade(tag)):
  match tag:
    case False{}:
      File
    case True{}:
      U32

type Copies<tag: Bool> is Kind(payload_grade(tag)):
  CopiesValue{fst: Payload(tag), snd: Payload(tag)}

def forge(tag: Bool, a: Payload(tag), b: Payload(tag)) -> Copies<tag>:
  match tag:
    case False{}:
      CopiesValue{a, b}
    case True{}:
      +copy = a
      CopiesValue{copy, copy}

def status_bool(status: Result<&1, &1, U32 & String, Unit>) -> Bool:
  match status:
    case Done{value}:
      True{}
    case Fail{error}:
      False{}

def after_write(result: File & Result<&1, &1, U32 & String, Unit>) -> IO(Bool):
  (file, status) = result
  do IO<Bool>:
    Unit <- File.close(file)
    return status_bool(status)

def use(copies: Copies<False{}>) -> IO(Bool):
  CopiesValue{fst, snd} = copies
  do IO<Bool>:
    Unit <- File.close(fst)
    result : File & Result<&1, &1, U32 & String, Unit> <- File.write(snd, "x")
    after_write(result)

def with_first(first: File) -> IO(Bool):
  do IO<Bool>:
    second : File <- IO.try(File, File.open("/tmp/bend_clo_apply_second", "w"))
    use(forge(runtime_tag(), first, second))

def main() -> IO(Unit):
  do IO<Unit>:
    first : File <- IO.try(File, File.open("/tmp/bend_clo_apply_first", "w"))
    result : Bool <- with_first(first)
    IO.print(Bool.show(result))

Likely fix

Do not represent synthetic closure application with a source-level Name. An internal tagged call kind would make it impossible for a user definition to compare equal to the sentinel. Rejecting Clo.apply as a source name in both the frontend and module loader would be a narrow guard.

Version

bend 2.0.16
commit 15ae0c86
Bun 1.4.2
Darwin arm64
Apple clang version 21.0.0 (clang-2100.0.123.102)

Activity

  1. VictorTaelin commented on Sep 19, 2026

    @VictorTaelin
    Contributor

    Fixed in 2.0.18, by refusal. Clo.apply is the def the compiler synthesizes to apply a closure; a def of your own at that name was never called. No file may declare it now:

    $ bend clo_apply_duplicate_file.bend
    Error: Clo.apply is a name the compiler encodes itself: name yours apart
    

    Renaming it to User.apply gives True on every lane, as you found. Pinned as tests/check/name_owned_def.bend.

    The same release closes the wider case you and #870 both hit: the emitters know base.bend's types by their names alone, so a file without import Base that declares its own Nat, Bool or Array checks and runs, and the build refuses it instead of encoding it as the base type.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

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