Skip to content

fix: capability method bodies did not know their own parameter types - #560

Merged
godofecht merged 1 commit into
mainfrom
fix/capability-method-parameter-types
Aug 19, 2026
Merged

godofecht merged 1 commit into
mainfrom
fix/capability-method-parameter-types

Conversation

@godofecht

@godofecht godofecht commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Found while investigating #547. Unrelated to that crash, and real on its own.

The bug

_gen_capability_method emitted the signature and then generated the body without registering the method's parameters. Inside the body every argument therefore resolved as unknown, and print() fell back to the float default.

capability ConsoleProgress {
    effect Progress,
    function on_start(idx: i32, total: i32, label: string) -> void {
        print(idx)
        print(total)
        print(label)
    },
}
generated
before FLOW_LOG("%g", idx) FLOW_LOG("%g", total) FLOW_LOG("%g", label)
after FLOW_LOG("%d", idx) FLOW_LOG("%d", total) FLOW_LOG("%s", label)

The identical body in a plain function has always generated %d, %d, %s. Only capability methods were affected, and every one of them was.

On arm64 that prints a double read out of an int slot, so any capability method printing its own arguments printed garbage. In flow-scikit's build, ConsoleProgress_on_fit_start printed two of its three arguments wrongly:

void ConsoleProgress_on_fit_start(int32_t candidate_idx, int32_t total, char* params) {
    FLOW_LOG("%d", (candidate_idx + 1));   // correct
    FLOW_LOG("%f", total);                 // int32_t through %f
    FLOW_LOG("%g\n", params);              // char* through %g
}

clang had been reporting these as nine -Wformat warnings the whole time; the build just never surfaced them.

The fix

Mirrors what _gen_function already does: save the scope, register each parameter with the overload resolver and the type table, generate the body, restore in a finally so an error inside the body cannot leak the scope into the next function.

Tests

Two, because one alone would not have caught it:

  • the formats themselves are %d, %d, %s and no %g appears
  • a plain function and a capability method with identical bodies generate identical FLOW_LOG lines

Both fail on the unfixed generator; I checked by reverting only c_generator.py and re-running.

Not a fix for #547

Worth saying plainly, because it looked like one at first. Rebuilding flow-scikit with this change made the #547 SIGBUS disappear, 20 runs out of 20. That is not evidence of a fix — adding a dead function did the same thing. A minimal C program confirms that printing an int through %f misreads but leaves the allocator intact:

int through %f: 0.000000
ptr through %g: 2.14497e-314
allocator survived

The real cause of #547 is an undersized malloc in the example itself, reported there with the AddressSanitizer trace.

Validation

  • Unit suite 1517 passed, 2 skipped
  • ./flow test --tier2 --strict 687/687
  • ruff check src clean

_gen_capability_method emitted the signature and then generated the body
without registering the method's parameters, so inside the body every
argument resolved as unknown. print() picked its format from that, and fell
back to the float default:

    capability ConsoleProgress {
        effect Progress,
        function on_start(idx: i32, total: i32, label: string) -> void {
            print(idx) print(total) print(label)
        },
    }

    before   FLOW_LOG("%g", idx)  FLOW_LOG("%g", total)  FLOW_LOG("%g", label)
    after    FLOW_LOG("%d", idx)  FLOW_LOG("%d", total)  FLOW_LOG("%s", label)

The same body in a plain function has always generated %d, %d, %s. Only
capability methods were affected, and every one of them was.

On arm64 that prints a double read out of an int slot, so a capability method
printing its arguments printed garbage. Spotted in flow-scikit, where
ConsoleProgress_on_fit_start printed two of its three arguments wrongly and
clang had been reporting it as nine -Wformat warnings the build did not
surface.

The fix mirrors what _gen_function already does: save the scope, register
each parameter with the overload resolver and the type table, generate, then
restore in a finally so an error inside the body cannot leak the scope.

Effects and capabilities are a headline feature, so this is worth a test that
pins both halves: the formats themselves, and that a plain function and a
capability method with identical bodies generate identical output.

Unit 1517 passed, tier-2 687/687.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@godofecht
godofecht merged commit 58fb997 into main Aug 19, 2026
19 checks passed
@godofecht
godofecht deleted the fix/capability-method-parameter-types branch August 19, 2026 07:11
godofecht added a commit that referenced this pull request Aug 19, 2026
…uth (#565)

Two of the limitations recorded on the showcase were not limitations, and the
third described a failure mode the compiler no longer has.

Capabilities are not stateless. Capability methods have no `self`, which is
true and worth saying, but the note went further and said Flow has no mutable
globals, so a handler "cannot accumulate state (e.g. a real collecting
test-spy or a metrics counter)". Both of those work:

    let mut call_count: i32 = 0
    capability CountingLogger {
        effect Log,
        function info(msg: string) -> void { call_count = call_count + 1 },
    }
    -> handler ran 3 times

A spy collecting numbers into a static array works too. The real limit is the
type: module statics must be a primitive, a fixed array of primitives, or
ptr<T>, so a handler accumulating strings or structs has nowhere to put them.
That is now what the page says, with issue #562 for the underlying question.

Dynamic capability objects no longer fail the way the note describes. It said
the `capability EffectName` parameter style "type-checks and generates C
syntax-cleanly" and then silently falls back to zeroed defaults. It does not:
strict type checking rejects it, and under --lenient it emits C referencing an
undeclared identifier. Still bad, but loudly rather than silently, and worth
saying accurately. Issue #561.

The type-inference workaround is removed. `let m: string = msg` existed
because capability method bodies did not know their own parameter types, so a
string parameter printed as a float. Fixed in #560, so the example now passes
the parameter straight to printf and still verifies.

A stated limitation that is not real costs readers work, which is why these
are worth correcting rather than leaving.

Effects showcase: 27 of 28 blocks verified, the last one an intentional
pattern illustration. Unit 1517 passed, links clean.

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant