Skip to content

FFI: keep the safety of a foreign import in the AST - #576

Merged
augustss merged 2 commits into
augustss:masterfrom
haskell-miso:ffi-safety
Nov 11, 2026
Merged

augustss merged 2 commits into
augustss:masterfrom
haskell-miso:ffi-safety

Conversation

@dmjio

@dmjio dmjio commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Summary

This is the split-out you suggested in #561: the safety annotation of a foreign import (unsafe, safe or interruptible) is now recorded in the AST. Nothing uses it yet, so the generated code is unchanged.

Changes

  • MicroHs.Expr: a new type

    data Safety = Unsafe | Safe | Interruptible

    and ForImp carries it:

    ForImp CallConv Safety (Maybe String) Ident EType

    The pretty printer shows it.

  • MicroHs.Parse: pSafety returns the annotation. When there is none, the result is Safe, which is the default in the Haskell 2010 report (section 8.4.3).

  • MicroHs.TypeCheck, MicroHs.Desugar: the pattern matches on ForImp take the extra field. The type checker passes it along and the desugarer ignores it.

Notes

Testing

  • make runtest passes, and hlint reports no hints for the changed files.
  • The compiler bootstraps to a fixpoint. generated/mhs.c is regenerated in a separate commit.
  • I have not built it with GHC locally, so the CI run here is the first check of that.

Thank you for the review and the guidance on #561.

dmjio and others added 2 commits November 11, 2026 15:45
A foreign import can be marked unsafe, safe or interruptible.  Add a
Safety type and record the annotation in ForImp, with Safe when none
is given, as in the Haskell report, and show it in the pretty printer.
Nothing uses it yet, so the generated code is unchanged.

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
@augustss
augustss merged commit 64f9116 into augustss:master Nov 11, 2026
13 checks passed
@dmjio
dmjio deleted the ffi-safety branch November 12, 2026 05:47
dmjio added a commit to haskell-miso/MicroHs that referenced this pull request Nov 12, 2026
Resolve conflicts with augustss#573 and augustss#576: ForImp now carries the Safety
field, and tests/Makefile has both testforimpforall and testffiname.
generated/mhs.c is regenerated from the merged source.

Co-Authored-By: Claude <noreply@anthropic.com>
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.

2 participants