Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Lift ByteArray breaking with RebindableSyntax #286

Closed
TeofilC opened this issue Sep 20, 2024 · 6 comments
Closed

Avoid Lift ByteArray breaking with RebindableSyntax #286

TeofilC opened this issue Sep 20, 2024 · 6 comments
Labels
withdrawn Withdrawn by proposer

Comments

@TeofilC
Copy link

TeofilC commented Sep 20, 2024

haskell/text#534 fixes a bug in the Lift Text instance namely the way numeric values were lifted meant that in the presence of RebindableSyntax, which might override the meaning of numeric literals, the code would break.

The Lift ByteArray instance in base1 is vulnerable to the same issue.

https://gitlab.haskell.org/ghc/ghc/-/merge_requests/13270 fixes this but leads to a change in behaviour. If a user were to inspect the output of the lift call they would see a largely equivalent but distinct AST.

It's highly unlikely that this would break anyone's code. ByteArray was only recently added to base, and most users of lift do not inspect its output.

Here is the relevant part of the diff for context:

  lift (ByteArray b) =
-    [| addrToByteArray $(lift len)
+   -- we cannot use Lift Int here because of #25267 and #25272
+   [| addrToByteArray $(pure . LitE . IntPrimL $ fromIntegral len)
                       $(pure . LitE . BytesPrimL $ Bytes ptr 0 (fromIntegral len))
    |]

Before this we got something like addrToByteArray 10 ... now we get addrToByteArray 10# ... (and the type of the non-exported function addrToByteArray changed to accommodate this)

Footnotes

  1. The Lift ByteArray instance used to live in template-haskell but had to be moved to base when all the wired-in definitions from template-haskell were moved to ghc-internal. An orphan instance in template-haskell would have no longer been guaranteed to be in scope for users and could have led to breakage. See: this comment

@Bodigrim
Copy link
Collaborator

How did it happen that instance Lift ByteArray was moved from template-haskell into base? Clearly this is a change of API (previoiusly base did not export an instance and now it does), but I don't recall a CLC proposal to do so.

(I'm not being formal for no reason here: dealing with TH AST all over the base is a maintenance burden, and this proposal is just a herald of what to come)

@tomjaguarpaw
Copy link
Member

How did it happen that instance Lift ByteArray was moved from template-haskell into base?

It happened here: https://gitlab.haskell.org/ghc/ghc/-/commit/228dcae6a0efbe5289add5330c46f581780dd96c

I guess the point is that if Lift goes into ghc-internal then we have to put instance Lift ByteArray next to the definition of ByteArray (unless we want an orphan instance).

@TeofilC
Copy link
Author

TeofilC commented Sep 21, 2024

The lack of a CLC proposal was an oversight from me. As you can see it was quite a big MR and I overlooked that this could do with a proposal. I can write one up if you'd like. I'll definitely keep it in mind for the future in any case.

I'm not being formal for no reason here: dealing with TH AST all over the base is a maintenance burden, and this proposal is just a herald of what to come

Thats a good point. I think ByteArray is a bit of an exception though. Most instances are just derived ones.

@Bodigrim
Copy link
Collaborator

The lack of a CLC proposal was an oversight from me. As you can see it was quite a big MR and I overlooked that this could do with a proposal. I can write one up if you'd like.

Yes, please. Also make sure that there are changelog entries and @since annotations.

@TeofilC
Copy link
Author

TeofilC commented Sep 22, 2024

The lack of a CLC proposal was an oversight from me. As you can see it was quite a big MR and I overlooked that this could do with a proposal. I can write one up if you'd like.

Yes, please. Also make sure that there are changelog entries and @since annotations.

See: #287

@TeofilC
Copy link
Author

TeofilC commented Sep 27, 2024

I'm pausing this for now as I'm no longer sure if this is the best fix to the underlying issue. I'll revisit this in a few months

@TeofilC TeofilC closed this as completed Sep 27, 2024
@Bodigrim Bodigrim added the withdrawn Withdrawn by proposer label Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
withdrawn Withdrawn by proposer
Projects
None yet
Development

No branches or pull requests

3 participants