Repository navigation
Description
Implement implicit parameters and with name(expr): contexts (§7.3a, §9.1a)
Summary
Implicit parameters let cross-cutting values (device/stream/allocator handles, std.context.Context) flow to callees from an enclosing with name(expr): scope without positional plumbing. The core landed in commit a0a54f6 ("F6 — Automatic implicit parameter resolution") and is exercised by test/behavior/behav_implicit_params.w. This issue now tracks the verified remainder: the implicit+default declaration error, closure capture of contexts (currently a silent miscompile), implicit fill on method calls (currently an LLVM verify error), plus tests locking in the already-working duplicate-implicit-type error and auto-ref lookup.
Spec
§7.3a Form 3a: Implicit Context (docs/with-specification.md:2558-2607), §9.1a (docs/with-specification.md:3063-3105), std.context (lib/std/context.w).
fn sin(x: &Array, ctx: implicit &Context) -> Array: ...
with context(default_device()):
let y = sin(x) // ctx resolved implicitly
let w = sin(x, ctx: other) // explicit argument overrides implicit
Normative rules (§7.3a, §9.1a):
- Resolution order: positional → named → implicit → defaults.
- Implicit resolution is lexical and type-based; innermost
with name(expr):wins; the identifier inwith name(expr):is descriptive only. - Auto-ref applies during implicit lookup (
Contextand&Contextcompose). - A function may not declare two
implicitparameters of the same type. - An
implicitparameter may not also have a default value. - Closures capture the implicit contexts visible at their definition site, like ordinary lexical bindings.
Current state
Landed and verified working (commit a0a54f6):
- Parser:
implicitis a soft keyword on parameter types (src/Parser.w:6905-6906), recorded asFN_PARAM_FLAG_IMPLICIT(src/Ast.w:232, accessorfn_param_is_implicit:268). with name(expr):parses asNK_WITH_IMPLICIT;Sema.check_with_implicit(src/SemaCheck.w:7434) pushes onto theimplicit_binding_types/implicit_binding_symsstacks (src/Sema.w:460-461) and pops at scope exit — giving innermost-shadowing lexical scoping.check_callfills unfilled implicit params from the stack, innermost-first, bytypes_compatiblematch, encoding the resolution as a negative marker0 - bind_symin the resolved-args table (named-arg path src/SemaCheck.w:7897-7917; plain-call path :7933-7990). Defaults are filled after implicits — resolution order matches the spec.- MIR:
lower_calldecodes the marker and pusheslower_var(impl_sym, 0, 0)(src/MirLower.w:5554-5561). - Missing-context diagnostic exists: "implicit parameter not provided; add a 'with' binding of the matching type" (src/SemaCheck.w:8145).
- Duplicate-implicit-type error already exists: "function has multiple implicit parameters of the same type" (src/SemaDecl.w:1166-1173) — verified firing. Needs only a compile-error test.
- Auto-ref during lookup works for plain calls — verified end-to-end:
fn f(x: i32, ctx: implicit &Ctx)resolves fromwith c(Ctx {...}):and runs correctly. Needs tests, not implementation. - behav_implicit_params.w covers automatic resolution, explicit named override, and nested shadowing.
Verified gaps (the remainder):
- implicit+default not rejected.
fn f(x: i32, ctx: implicit Ctx = Ctx { m: 1 })passeschecktoday (verified). §9.1a says this must be an error. - Closure capture of contexts is a silent miscompile. Verified:
with c(Ctx { m: 3 }): let cl = v => f(v); apply(cl)compiles and runs but produces a wrong value (the implicit arg reads garbage). Root cause: sema resolves the marker0 - bind_symagainst the lexically-live stack, but the closure's capture analysis only sees AST idents — the context binding never enters the closure's capture set (set_closure_capture_summary, src/Sema.w:2774), solower_var(impl_sym, ...)inside the closure body resolves to a nonexistent local. - Method calls with implicit params break codegen. Verified:
fn S.scale(self: &S, ctx: implicit Ctx)+s.scale()under a context binding passes sema but fails with "LLVM verify error: Incorrect number of arguments passed to called function!" — the implicit-fill passes live only incheck_call's function path; the method-call lowering never receives resolved args.
Implementation plan
- implicit+default declaration error — src/SemaDecl.w, in the same param loop that detects duplicate implicit types (:1157-1173): if
fn_param_is_implicit(p_flags) != 0andself.ast.get_fn_param_default(param_start, pi) != 0, emit error"implicit parameter may not have a default value"on the param node. (Overlap: Named-args rule gaps: implicit+default rejection, placeholder+named rejection (§9.1a, §9.4) #456 lists "implicit+default rejection" too — implement once, here, and note it in Named-args rule gaps: implicit+default rejection, placeholder+named rejection (§9.1a, §9.4) #456.) - Method-call implicit fill — extend the implicit third-pass to the method-call check path (the call sites that compute
sig_idx/param_offsetfor receiver calls;check_call'sparam_offsetplumbing already models receivers, so the fix is to run the same fill +store_resolved_call_argsfor method calls, and to make the method-call MIR lowering honorhas_resolved_call_argsthe waylower_calldoes at src/MirLower.w:5554). Until then sema accepts what codegen can't lower — a correctness hole that must not survive this issue. - Closure capture of contexts — two coordinated changes:
a. Sema: when a call inside a closure body resolves an implicit param tobind_sym, recordbind_symas a captured symbol of the enclosing closure (feed it into the same machinery that buildsclosure_capture_summary_*, src/Sema.w:515-517/2774), so escape checks and capture layout include it. Capture is by value at definition site (§7.3a: "just like ordinary lexical bindings"); sincecheck_with_implicitpops the binding at scope exit, the captured copy is what survives.
b. MirLower: when lowering the negative marker inside a closure,lower_varmust find the binding through the closure environment (it will, once (a) puts the symbol in the capture set — verifylower_var's capture lookup path handles it).
Edge cases: nested closures (capture propagates outward through each level); a closure defined outside any context that calls an implicit-param function must still get the "implicit parameter not provided" error at the closure's definition site, not garbage. - Ephemeral interaction:
std.context.Contextisephemeral(lib/std/context.w:31). A closure capturing an ephemeral context must obey the existing escaping-closure-captures-ephemeral rejection (err_escaping_bound_closure_captures_ref.w family). Add this to the capture summary's effect bits so an escaping closure capturing a Context is rejected, while a non-escaping one works. - Tests (below), including locking in already-working behavior.
No rt/Link/seed surface is touched; normal with build + fixpoint applies.
Test plan
- test/compile_errors/err_implicit_param_default.w —
//! expect-check-fail: implicit parameter may not have a default value; fn withctx: implicit Ctx = .... - test/compile_errors/err_implicit_param_duplicate_type.w —
//! expect-check-fail: function has multiple implicit parameters of the same type; twoimplicit Ctxparams (locks in existing SemaDecl.w:1172 behavior). - test/compile_errors/err_implicit_param_missing_context.w — call with no enclosing
with name(expr):→ "implicit parameter not provided; add a 'with' binding of the matching type". - test/behavior/behav_implicit_autoref.w —
//! expect-stdout: ok:ctx: implicit &Ctxresolved from a by-value context binding; also actx: implicit Ctxparam with field reads, both directions through one call chain. - test/behavior/behav_implicit_closure_capture.w —
//! expect-stdout: ok: closure defined insidewith c(Ctx { m: 3 }):calling an implicit-param fn; invoked (a) inside the scope, (b) passed to a function and invoked there; nested with-scopes shadowing across closure definition sites (definition-site capture, not call-site). - test/behavior/behav_implicit_method_call.w —
//! expect-stdout: ok:fn S.scale(self: &S, ctx: implicit Ctx)called ass.scale()under a context binding (currently an LLVM verify error — this test is the regression gate for step 2). - test/spec/spec_ss07_3a_implicit_context.w — port the §7.3a examples:
trace_id(ctx: implicit Context)withwith active(default_context()):from lib/std/context.w; explicit-override; innermost-shadowing (extends behav_implicit_params.w with the std.context shape). - Negative: closure capturing ephemeral Context escaping its scope → reuse the escaping-closure diagnostic (file under test/compile_errors/).
Acceptance criteria
-
implicit+ default value is a compile error with the exact message above. - Duplicate implicit types remain a compile error (test exists).
- Method calls with implicit params work end-to-end (no LLVM verify error).
- Closures capture visible contexts at definition site and produce correct values; no silent miscompile path remains.
- Auto-ref lookup covered by passing tests in both
Ctx/&Ctxdirections. - Resolution order positional → named → implicit → defaults covered by a test mixing all four.
-
with build && with build :fixpoint && with build :testpass (andwith build :test-greenrecorded).
Dependencies
- Named-args rule gaps: implicit+default rejection, placeholder+named rejection (§9.1a, §9.4) #456 (Named-args rule gaps) explicitly lists implicit+default rejection — implement once, cross-reference.
- Implement named arguments (§9.1a) #397 (named arguments) — substantially landed; the named-override path is shared.
- Closure-capture work overlaps Bug: closure capture modes diverge from §12.4 #420 (closure capture modes diverge from §12.4) and the summary machinery named in Make ephemerality provenance-tracked and modular #362.
- Crux device/stream/allocator threading is the downstream consumer (original context).
Activity
ehartford commentedon Jun 10, 2026
Scope correction from the requirements campaign (2026-06-10): implicit parameters +
with name(expr):resolution are substantially IMPLEMENTED (landed a0a54f6;behav_implicit_params.w,behav_std_context.wcover resolution order, shadowing, named override, type-driven lookup). Remaining under this issue: duplicate-implicit-type error, implicit+default error, auto-ref during lookup, closure-capture verification.ehartford commentedon Jun 14, 2026
Implemented in 56d6fce. Completed implicit context resolution: implicit+default declarations now diagnose with the spec wording, method calls fill implicit/default args through the resolved-arg path, and closure environments include sema-resolved implicit captures so definition-site context capture no longer silently miscompiles. Added positive and negative coverage for duplicate implicit types, missing contexts, auto-ref lookup, closure capture, method calls, std.context §7.3a behavior, and escaping ephemeral Context capture. Verification passed: build, fixpoint, full :test, and explicit :test-green.