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

IsString rules #521

Closed
wants to merge 1 commit into from
Closed

IsString rules #521

wants to merge 1 commit into from

Conversation

Bodigrim
Copy link
Contributor

@Bodigrim Bodigrim commented Jun 21, 2022

Closes #140 in line with design suggested at #140 (comment)

The idea is that users were never meant to use IsString.fromString directly, this method should be invoked only by {-# LANGUAGE OverloadedStrings #-} magic. This PR effectively enforces this limitation.

  • In ghci IsString.fromString = packCharsSafe. This is a partial function,
    throwing errors on characters out of Latin1 range, but partiality is not a big
    deal for interactive sessions.
  • For compiled programs:
    • For ASCII literals IsString.fromString = packChars = unsafePackLiteral,
      no runtime overhead and Core remains exactly the same as before.
    • For Unicode literals (and literals with '\0') IsString.fromString = packCharsSafe.
      This incurs runtime overhead, but such thunk is evaluated only once in its life.
    • For dynamic strings (user input, etc.) IsString.fromString = error.
      This is to make runtime behaviour more robust (if it crashes, it crashes for
      any input) and avoids a danger of routines, which were tested only for ASCII
      inputs but suddenly receive Unicode and silently lose data.

@Bodigrim Bodigrim requested a review from sjakobi June 21, 2022 23:38
@Bodigrim Bodigrim added this to the 0.12.0.0 milestone Jun 21, 2022
@Bodigrim Bodigrim marked this pull request as draft June 22, 2022 00:18
@sjakobi
Copy link
Member

sjakobi commented Jun 22, 2022

The main problem that I'm expecting with this approach is that programs compiled with -O0 or -fno-enable-rewrite-rules may behave in unexpected ways. For example, test suite runs with -O0 might fail.

* In ghci IsString.fromString = packCharsSafe. This is a partial function,
  throwing erros on characters out of Latin1 range, but partiality is not a big
  deal for interactive sessions.
* For compiled programs:
  * For ASCII literals IsString.fromString = packChars = unsafePackLiteral,
    no runtime overhead and Core remains exactly the same as before.
  * For Unicode literals (and literals with '\0') IsString.fromString = packCharsSafe.
    This incurs runtime overhead, but such thunk is evaluated only once in its life.
  * For dynamic strings such as user input IsString.fromString = error.
    This is to make runtime behaviour more robust (if it crashes, it crashes for
    any input) and avoids a danger of routines, which were tested only for ASCII
    inputs but suddenly receive Unicode and silently lose data.
@clyring clyring removed this from the 0.12.0.0 milestone Jul 7, 2023
@Bodigrim
Copy link
Contributor Author

I kinda lost my plot here: CI fails, and I don't really remember what, why and how. We might be able to find better approaches in future, so in the meantime closing.

@Bodigrim Bodigrim closed this Jul 29, 2023
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.

Surprising behavior of ByteString literals via IsString
3 participants