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

'SqlExpr' can be unsafely coerced using the safe 'coerce' function #270

Closed
arthurxavierx opened this issue Jun 30, 2021 · 5 comments · Fixed by #413
Closed

'SqlExpr' can be unsafely coerced using the safe 'coerce' function #270

arthurxavierx opened this issue Jun 30, 2021 · 5 comments · Fixed by #413

Comments

@arthurxavierx
Copy link
Contributor

It's now possible to write

let
  fromNull :: SqlExpr (Maybe (Entity e)) -> SqlExpr (Entity e)
  fromNull = coerce

  myNonNullableEntity = fromNull (myNullableEntity :: SqlExpr (Maybe (Entity SomeEntity)))

which could cause unexpected runtime errors.

@belevy
Copy link
Collaborator

belevy commented Jun 30, 2021

Is there a way to prevent this outside of the library code? This is a feature in the internal code, we get free coercion.

Basically coerce is not part of our contract so if someone makes a runtime error using it I don't really care.

@parsonsmatt
Copy link
Collaborator

Hm.

We could have:

newtype SqlExpr a 
type role SqlExpr nominal

veryUnsafeCoerceSqlExpr :: SqlExpr a -> SqlExpr b
veryUnsafeCoerceSqlExpr = unsafeCoerce

unsafeCoerce is safe in this sense, since we're explicitly blocking the normal coerce behavior that would otherwise be used. Clients of the library need to write the scary name. And we can still use it internally. Is this an acceptable compromise?

@belevy
Copy link
Collaborator

belevy commented Jun 30, 2021

that seems fine, we should probably be using veryUnsafeCoerceSqlExpr everywhere anyways though I think we lose the fmap (f . coerce) == coerce . fmap f rewrite rule

@parsonsmatt
Copy link
Collaborator

I think an INLINE pragma on veryUnsafeCoerceSqlExpr would work to get the rewrites firing again

@parsonsmatt
Copy link
Collaborator

Fixed in #413

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 a pull request may close this issue.

3 participants