Add ForeignPtr type and builtins for finalizing external resources - #6192
Merged
Conversation
Contributor
Author
|
Share tests pass (aside from a couple expected failures). Benchmarks look good. I must not completely understand the new checking framework to get past CI, though. |
Contributor
|
We're working out some issues with it, sorry. I'll try making it pass. |
pchiusano
approved these changes
Mar 24, 2026
pchiusano
left a comment
Member
There was a problem hiding this comment.
LGTM assuming conflicts are fixed and CI passes. Can you prep a PR to base after this gets merged, too?
Closed
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a
ForeignPtrtype and various related builtins for managing foreign resources in conjunction with garbage collection. This is based on GHC's support for this feature.ForeignPtrscan be created in two waysPtr awith a finalizer, which is useful for memory allocated and returned from foreign functionsallocatefunctions. These are similar to the existing functions forPtr, except they come equipped with finalizers that automatically release the memory (I think actually the memory is automatically collected because it's pinned memory in the Haskell heap, but that doesn't really matter)There are also two types of finalizers available
Func (Ptr a ->{IO} ()). These are foreign functions loaded from a DLL via a new builtin that returns a 'function pointer' rather than a callable unison function.'{IO} ()These can't actually be combined, because GHC doesn't allow it. However, anything you could do with the first could be done by the second by calling the foreign function in the thunk.
I didn't add any builtins for dereferencing the function pointers, since the only use we really have for them right now are these finalizers. There is enough information in the representation to get out a callable function if that becomes desirable in the future.
Tests of the new features have been added in transcripts. I didn't exhaustively test all the allocation builtins, but they're implemented uniformly, so testing one (which I did) should be sufficient.
I also had to tweak one of the git hook scripts to actually be able to push this, so that's included.
I have most of the docs written up for inclusion in base if/once this gets merged and released.
Haven't run share tests and benchmarks yet. I'll get on that.