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

There seems to be something wrong with unsafePartial #11

Closed
i-am-the-slime opened this issue Dec 1, 2019 · 4 comments
Closed

There seems to be something wrong with unsafePartial #11

i-am-the-slime opened this issue Dec 1, 2019 · 4 comments
Labels
bug Something isn't working

Comments

@i-am-the-slime
Copy link

i-am-the-slime commented Dec 1, 2019

I was trying to compile purescript-unicode. I had to add some FFI and that was fine. However I got weird errors when it got to compiling/running this piece of code:

bsearch   a . a -> Array a -> Int -> (a -> a -> Ordering) -> Maybe a
bsearch a array size compare = go 0 size
    where
    go i k | i > k = Nothing
           | otherwise = let j = floor (toNumber (i + k) / 2.0)
                             b = unsafePartial (Array.unsafeIndex array j)
                         in case compare a b of
                              EQ -> Just b
                              GT -> go (j + 1) k
                              _  -> go i (j - 1)

The error being:

build project.localhost/purescript-native/output/Main: cannot load project.localhost/purescript-native/output/array: cannot find module providing package project.localhost/purescript-native/output/array

If I rename the variable to anything other than array the error now complains about the new name, e.g.:

build project.localhost/purescript-native/output/Main: cannot load project.localhost/purescript-native/output/otherName: cannot find module providing package project.localhost/purescript-native/output/otherName

The beginning of Data_Char_Unicode_Internal.purs is this:

// Code generated by psgo. DO NOT EDIT.

package Data_Char_Unicode_Internal

import . "github.com/purescript-native/go-runtime"

import (
    "project.localhost/purescript-native/output/Data.Show"
    "project.localhost/purescript-native/output/Data.Maybe"
    "project.localhost/purescript-native/output/Data.Boolean"
    "project.localhost/purescript-native/output/Data.Int"
    "project.localhost/purescript-native/output/otherName"
    "project.localhost/purescript-native/output/Data.Ordering"
    "project.localhost/purescript-native/output/Data.Array"
    "project.localhost/purescript-native/output/Data.Eq"
    "project.localhost/purescript-native/output/Data.Functor"
)

Note the suspicious

    "project.localhost/purescript-native/output/otherName"

When I change the code to not use unsafeIndex but this:

b = fromMaybe' (\_ -> unsafeCrashWith "Array index out of bounds") (Array.index otherName j)

the build succeeds.

I don't know what this might be but I'm hopeful you might know!

@andyarvanitis andyarvanitis added the bug Something isn't working label Dec 2, 2019
@andyarvanitis
Copy link
Owner

andyarvanitis commented Dec 2, 2019

Yeah, I'm able to reproduce it and see the bad codegen. It looks like a simple bug in the unsafeIndex inliner that's easy to fix. I'll have a closer look in a bit.

andyarvanitis added a commit to andyarvanitis/purescript-native that referenced this issue Dec 4, 2019
@andyarvanitis
Copy link
Owner

andyarvanitis commented Dec 4, 2019

I've finished testing the fix, and it's checked in as well as in the "2019-12-03" binary release.

Also let me know if you see a "missing function 'Index'" error (it would be at the go build stage). If you do, see if doing

$ go mod tidy && go clean -modcache

fixes it. Thanks for trying it and for the bug report.

@i-am-the-slime
Copy link
Author

I will probably try tomorrow!

@i-am-the-slime
Copy link
Author

I think this worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants