Repository navigation
Description
Summary
Align the existing @[effect] attribute with §16.3d (new in v7.1): required contract information on bodiless declarations (extern, intrinsic stubs), an optional checked pin on bodied functions, and never application-code ceremony. Today the attribute parses and a pin check exists, but the spec's primary use case — @[effect] on extern declarations driving call-site move/escape enforcement — is silently dropped by the parser, only one parameter can be annotated, the spec's param: effect spelling doesn't parse, unknown effect names are silently ignored, and the pin check silently widens instead of erroring on disagreement.
Spec
§16.3d @[effect] — Declared Effect Contracts:
@[effect(dst: write, src: read)]
extern "C" fn memcpy(dst: *mut c_void, src: *const c_void, n: usize) -> *mut c_void
@[effect(handle: consume)]
extern "C" fn close_handle(handle: *mut c_void)
- Recognized names:
read,write,consume,escape_value,escape_view. - Rule 1: required information only where no body exists (extern, intrinsics); "Bindings without it stay on the raw surface for the affected parameters."
- Rule 2: on a bodied function,
@[effect]may optionally pin the inferred summary at apubboundary; "if inference disagrees with the pin, that is a compile error — the pin is a checked contract, not an override." - Rule 3: library-author surface only (stdlib, FFI bindings, §16.3c overlays); application code never needs it (§1.7 anti-ceremony).
Current state
- Parsing (
src/Parser.w:600-649):@[effect(param = effect)]or= [a, b]. Exactly one parameter per attribute (pending_effect_param/pending_effect_bitsare scalars, Parser.w:52-53). The spec'sdst: writecolon spelling is not accepted (parser requiresTK_EQ, Parser.w:611). Unknown effect names map to bit 0 and vanish silently (Parser.w:621-627, 645: onlyeff_bits != 0records anything). - Attachment: the pin is stored only when an
NK_FN_DECLis built (Parser.w:1136-1140→fn_effect_pin_params/fn_effect_pin_bits, declared atsrc/Ast.w:456-457, copied bysrc/ComptimeTransform.w:166-168). TheNK_EXTERN_FNpath (Parser.w:1192) never readspending_effect_*—@[effect]on an extern declaration is silently discarded, which is the §16.3d-required case. - Inference + pin check (
src/SemaCheck.w:904-950): per-param effects are inferred from the body and written viaset_sig_param_effect(SemaCheck.w:918), with &T/*T params clamped toREAD|ESCAPE_VIEW|RAW_PTR_VALIDITY(SemaCheck.w:910-916). The pin check (SemaCheck.w:930-950) implements floor merge — pin bits not inferred are silently OR-ed into the exported summary (SemaCheck.w:943-945) — plus a ceiling error when inference exceeds the pin (SemaCheck.w:946-950). The silent floor merge is an override, which §16.3d forbids. The check also ignorespub(runs on any bodied fn) and silently does nothing if the pinned param name matches no parameter (pin_pistays -1, SemaCheck.w:935-940). - Call-site enforcement family (
src/SemaCheck.w:8171-8197):sig_param_effectdrives §3.8 call-mode diagnostics —EFF_CONSUME|EFF_ESCAPE_VALUEmarks the arg moved and requiresmove x/copy xfor non-Copy args (SemaCheck.w:8178-8188);EFF_ESCAPE_VIEWforbids move/copy (8190-8197);propagate_call_param_effect(SemaCheck.w:4960) makes effects transitive. Because extern sigs never get effects set (no body → no inference; pins dropped), none of this fires for extern calls today. - Effect bits:
EFF_READ=1, WRITE=2, CONSUME=4, ESCAPE_VALUE=8, ESCAPE_VIEW=16, RAW_PTR_VALIDITY=32(src/Sema.w:263-264). Existing usage:lib/std/thread.w:24@[effect(worker = [read, escape_value])]; testsbehav_effect_pin.w,err_pin_ceiling.w.
Implementation plan
- Parser: multi-param + spec spelling + strict names (Parser.w:600-649):
- Loop over comma-separated
name: effect/name: [a, b]entries; accept=as a deprecated alias for the existinglib/std/thread.wusage (or migrate that file to:in the same commit — preferred, it's one site). - Replace scalar
pending_effect_param/bitswith parallelVec[i32]s (pending_effect_params,pending_effect_bits_vec), reset inskip_attributes(Parser.w:463-464). - Unknown effect name →
emit_error("unknown effect 'x' in @[effect]; expected read, write, consume, escape_value, or escape_view")instead of silently producing 0.
- Loop over comma-separated
- Attach to extern declarations: in the extern-fn parse path (around Parser.w:1149-1192), store pins on the
NK_EXTERN_FNnode in the samefn_effect_pin_*maps (extend map storage to lists: e.g.fn_effect_pin_starts/countsinto an extras-style side table inAst.wstate, since multiple params now need recording per node). - Sema: bodiless declarations consume the contract — in
Sema.collect_extern_fn(src/SemaDecl.w:1236-1306), after the sig is built, resolve each pinned param name to its index andset_sig_param_effect(sig_idx, pi, bits). Error if a pinned name matches no parameter:@[effect] names unknown parameter 'x'(also fix the same silent no-match in the bodied path, SemaCheck.w:935-940). With effects set, the existing SemaCheck.w:8171-8197 family enforces consume/escape at extern call sites with zero new code. Note: declared effects are §3.8 contract metadata only — they do not lift §16.11 unsafe requirements on raw bindings (ci_function_requires_raw_abi, SemaDecl.w:478, is unaffected). - Pin-vs-inference consistency (bodied fns) — replace the silent floor merge (SemaCheck.w:943-945) with the §16.3d contract check: after clamping, if
inferred != pin_bitsfor the pinned param (compare over the five §16.3d-recognized bits;EFF_RAW_PTR_VALIDITYis internal and excluded from pins), emit@[effect] pin on 'x' does not match inferred effects (pinned: write; inferred: read); the pin is a checked contract — fix the body or the pin. Keep the existing ceiling message for the exceeds case if a friendlier split is wanted, but no direction may silently win. Updatebehav_effect_pin.w/err_pin_ceiling.waccordingly. - Decide and test the pub scoping of rule 2: §16.3d frames pins as
pub-boundary surface. Decision proposed: accept pins on non-pub bodied functions with identical checking (a checked assertion is harmless and useful in compiler/stdlib internals), and document that onlypubboundaries are encouraged. If the maintainer prefers strict reading, emit a warning on non-pub pins instead. Either way, add the test that pins the decision. - Reject
@[effect]where it is meaningless: on type decls, lets, etc. (currently the attribute scanner ignores placement) — at minimum the unknown-parameter error from step 3 covers fn-likes; add a parse-time error if the pending pins are non-empty when a non-fn declaration is built.
Test plan
test/spec/spec_ss16_3d_effect_extern.w:@[effect(handle: consume)] extern "C" fn close_handle(handle: *mut c_void)+ a non-Copy wrapper arg → call withoutmoveis an error; withmove xcompiles (mirror of SemaCheck.w:8186 diagnostic). Multi-param@[effect(dst: write, src: read)]on a memcpy-shaped extern parses and round-trips.test/compile_errors/err_effect_unknown_name.w:@[effect(p: wriet)]→//! expect-error: unknown effect.test/compile_errors/err_effect_unknown_param.w:@[effect(q: read)]onfn f(p: i32)→//! expect-error: names unknown parameter.test/compile_errors/err_effect_pin_mismatch_floor.w: pinescape_valueon a param the body only reads →//! expect-check-fail:pin-mismatch text (this is the behavior change from silent merge).- Keep/adjust
err_pin_ceiling.w(body exceeds pin) andbehav_effect_pin.w(exact match passes). - Non-pub bodied pin test capturing the step-5 decision.
- Extern-without-effect regression: un-annotated extern pointer params keep current behavior (raw surface; no move enforcement) — no new errors on existing code (
lib/std/libc.wexterns).
Acceptance criteria
- Spec spelling
@[effect(dst: write, src: read)]parses; multiple parameters per attribute supported; unknown effect names and unknown parameter names are compile errors. -
@[effect]onexterndeclarations populatessig_param_effectsand drives the SemaCheck.w:8171-8197 call-mode diagnostics (consume/escape_value require move/copy; escape_view forbids them). - Pin on a bodied function is a checked contract: any disagreement between pin and inferred summary (either direction) is a compile error; no silent merge.
- Declared effects do not weaken §16.11 unsafe/raw classification.
-
lib/std/thread.w:24usage migrated/working; no application-code requirement introduced anywhere. -
with build && with build :fixpoint && with build :testpass.
Dependencies
- Remove the move x/copy x acknowledgment requirement for consuming arguments #372 (removal of move/copy acknowledgment for consuming args — it rewrites the very SemaCheck.w:8178-8188 diagnostics this issue wires extern effects into; coordinate so the extern path inherits whatever call-mode rules Remove the move x/copy x acknowledgment requirement for consuming arguments #372 settles on).
- Curated libc contract overlay for c_import modeled surfaces #379 (contract overlays declare effects through the same metadata).
- Represent unsafe function pointer and callback types #370 (unsafe callback types — value-level counterpart).
Traceability
Spec reference: §16.3d (new in v7.1). Original context: @[effect] implemented (src/Parser.w:600-649) but unspecified; v7.1 scopes it to bodiless declarations + optional checked pub pins, never required in application code.
Activity
ehartford commentedon Jun 13, 2026
Implemented in c96c852.
@[effect]now supports the §16.3d multi-parameterparam: effectspelling, validates effect and parameter names, attaches contracts to extern declarations, enforces bodied-function pins as exact checked contracts, and preserves raw/manual extern unsafe requirements. Added positive and negative coverage for extern contracts, unknown names/params, exact pin mismatches, extern consume propagation, and unsafe preservation. Verification passed:with build,with build :fixpoint,with build :test, andwith build :test-green.