Skip to content

feat(codegen): surface &T / &mut T as SharedBorrow / ExclBorrow (closes #221)#255

Open
hyperpolymath wants to merge 1 commit into
mainfrom
feat/codegen-borrow-ownership-kind
Open

feat(codegen): surface &T / &mut T as SharedBorrow / ExclBorrow (closes #221)#255
hyperpolymath wants to merge 1 commit into
mainfrom
feat/codegen-borrow-ownership-kind

Conversation

@hyperpolymath
Copy link
Copy Markdown
Owner

Summary

  • Extends Ty::Borrow and ExprKind::Borrow with mutable: bool (mirrored on the Surface* enums); parser recognises &mut via a mut_marker atomic rule + mut keyword.
  • Adds ty_to_ownership_kind helper that maps &T → SharedBorrow, &mut T → ExclBorrow, linear Ref/String → Linear, else Unrestricted. Replaces UserFnInfo.param_linear: Vec<bool> with param_kinds: Vec<OwnershipKind>.
  • emit_ownership_section broadens its filter from "any linear" to "any non-Unrestricted" so shared-borrow-only signatures still emit an entry.
  • s-expr IR encoder uses borrow vs borrow-mut discriminator atoms to preserve mutability through the IR round-trip.

Closes the producer-side gap flagged in both 2026-05-30 reviews of typed-wasm proposal 0001 — SharedBorrow + ExclBorrow were defined in ownership.rs but never emitted, so the verifier's L7 alias-exclusion check fired zero times against ephapax-emitted modules.

Test plan

  • cargo test --workspace green (all suites)
  • test_parse_mut_borrow&mut x parses with mutable: true
  • ownership_section_emits_shared_borrow_for_borrow_param
  • ownership_section_emits_excl_borrow_for_mut_borrow_param
  • Existing ownership round-trip + linear-param tests unchanged and green
  • Example examples/linear/16-shared-vs-exclusive-borrow.eph demonstrates both polarities

Out of scope

  • Mutability is currently passed unchecked from parser → AST → codegen; the linear/affine checkers walk borrow children but don't yet enforce alias-exclusion at compile time. The verifier-side L7 check (typed-wasm) now has signal to enforce; ephapax-side static enforcement is a follow-up.

Closes #221.

🤖 Generated with Claude Code

#221)

Extends the AST and parser to distinguish shared from exclusive
borrows, then wires the distinction through codegen so the
`typedwasm.ownership` custom section emits all four OwnershipKind
variants instead of just `Linear` + `Unrestricted`.

## What changes

- `Ty::Borrow` and `ExprKind::Borrow` (in ephapax-syntax) gain a
  `mutable: bool` field; the corresponding `SurfaceTy`/`SurfaceExprKind`
  variants gain it too.
- Parser grammar adds an optional `"mut"` marker after `&` for both
  borrow types and borrow expressions; `mut` joins the keyword list.
- Codegen replaces `UserFnInfo.param_linear: Vec<bool>` with
  `param_kinds: Vec<OwnershipKind>` populated via a new
  `ty_to_ownership_kind` helper: `&T → SharedBorrow`,
  `&mut T → ExclBorrow`, linear `Ref`/`String → Linear`, else
  `Unrestricted`. `emit_ownership_section` consumes the kinds
  directly; the filter is broadened from "any linear" to "any
  non-Unrestricted" so shared-borrow-only signatures still emit
  an entry.
- s-expr IR encoding uses `borrow` vs `borrow-mut` discriminator
  atoms to preserve mutability through the IR round-trip.

## Tests

- `ownership_section_emits_shared_borrow_for_borrow_param` — `&T` →
  `SharedBorrow`.
- `ownership_section_emits_excl_borrow_for_mut_borrow_param` —
  `&mut T` → `ExclBorrow`.
- `test_parse_mut_borrow` — `&mut x` parses with `mutable: true`.
- Existing ownership round-trip + linear-param tests unchanged
  and green.

## Example

- `examples/linear/16-shared-vs-exclusive-borrow.eph` — minimal
  shared-vs-exclusive contrast.

## Closes typed-wasm gap

This is the producer-side counterpart to typed-wasm's L7
alias-exclusion enforcement; previously ephapax-emitted modules
never produced `ExclBorrow`, so the verifier saw only 2 of 4
`OwnershipKind` variants from this producer. Both reviewers of
proposal 0001 (2026-05-30) called out the gap.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@hyperpolymath hyperpolymath enabled auto-merge (squash) May 30, 2026 23:01
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.

codegen: surface Ty::Borrow into OwnershipKind::SharedBorrow / ExclBorrow at emission

1 participant