Skip to content

Align @[effect] with §16.3d (declared effect contracts) #402

Closed

Description

@ehartford

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 a pub boundary; "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_bits are scalars, Parser.w:52-53). The spec's dst: write colon spelling is not accepted (parser requires TK_EQ, Parser.w:611). Unknown effect names map to bit 0 and vanish silently (Parser.w:621-627, 645: only eff_bits != 0 records anything).
  • Attachment: the pin is stored only when an NK_FN_DECL is built (Parser.w:1136-1140 → fn_effect_pin_params/fn_effect_pin_bits, declared at src/Ast.w:456-457, copied by src/ComptimeTransform.w:166-168). The NK_EXTERN_FN path (Parser.w:1192) never reads pending_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 via set_sig_param_effect (SemaCheck.w:918), with &T/*T params clamped to READ|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 ignores pub (runs on any bodied fn) and silently does nothing if the pinned param name matches no parameter (pin_pi stays -1, SemaCheck.w:935-940).
  • Call-site enforcement family (src/SemaCheck.w:8171-8197): sig_param_effect drives §3.8 call-mode diagnostics — EFF_CONSUME|EFF_ESCAPE_VALUE marks the arg moved and requires move x/copy x for non-Copy args (SemaCheck.w:8178-8188); EFF_ESCAPE_VIEW forbids 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])]; tests behav_effect_pin.w, err_pin_ceiling.w.

Implementation plan

  1. 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 existing lib/std/thread.w usage (or migrate that file to : in the same commit — preferred, it's one site).
    • Replace scalar pending_effect_param/bits with parallel Vec[i32]s (pending_effect_params, pending_effect_bits_vec), reset in skip_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.
  2. Attach to extern declarations: in the extern-fn parse path (around Parser.w:1149-1192), store pins on the NK_EXTERN_FN node in the same fn_effect_pin_* maps (extend map storage to lists: e.g. fn_effect_pin_starts/counts into an extras-style side table in Ast.w state, since multiple params now need recording per node).
  3. 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 and set_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).
  4. 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_bits for the pinned param (compare over the five §16.3d-recognized bits; EFF_RAW_PTR_VALIDITY is 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. Update behav_effect_pin.w/err_pin_ceiling.w accordingly.
  5. 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 only pub boundaries 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.
  6. 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 without move is an error; with move x compiles (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)] on fn f(p: i32) → //! expect-error: names unknown parameter.
  • test/compile_errors/err_effect_pin_mismatch_floor.w: pin escape_value on 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) and behav_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.w externs).

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] on extern declarations populates sig_param_effects and 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:24 usage migrated/working; no application-code requirement introduced anywhere.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

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

  1. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    Implemented in c96c852. @[effect] now supports the §16.3d multi-parameter param: effect spelling, 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, and with build :test-green.

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