Skip to content

Compiler hangs under some situations using Coercible #3857

@Thimoteus

Description

@Thimoteus

Description

In some situations using coerce will cause the compiler to hang, whereas pattern matching will not.

To Reproduce

spago.dhall:

{ name = "my-project"
, dependencies = [ "prelude", "safe-coerce" ]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
}

packages.dhall after the first comment:

let upstream =
      https://github.com/purescript/package-sets/releases/download/psc-0.13.6-20200423/packages.dhall sha256:c180a06bb5444fd950f8cbdd6605c644fd246deb397e62572b8f4a6b9dbcaf22

let overrides =
    { prelude =
        upstream.prelude // { version = "c932361d008379958f14ca8cc2fe32e06cc2647d" }
    }

let additions =
    { safe-coerce =
        { dependencies = [ "unsafe-coerce" ]
        , repo =
            "https://github.com/purescript/purescript-safe-coerce.git"
        , version = "v0.0.2"
        }
    }

in  upstream // overrides // additions

Failing code, placed inside src/Main.purs:

module Main where

import Prelude

import Safe.Coerce (coerce)

data Rep a
  = Leaf a
  | Link Int (Rep a) (Rep a)

data Rank

foreign import data Z :: Rank
foreign import data S :: Rank -> Rank

newtype Ranked :: Rank -> Type -> Type
newtype Ranked n a = Ranked a

type Tree n a = Ranked n (Rep a)

linkRep :: forall a. Rep a -> Rep a -> Rep a
linkRep t1 t2 = Link (rankRep t1 + 1) t1 t2

rankRep :: forall a. Rep a -> Int
rankRep =
  case _ of
    Leaf _ -> 0
    Link n _ _ -> n

badLink :: forall a n. Tree n a -> Tree n a -> Tree (S n) a
badLink t1 t2 = coerce (linkRep (coerce t1) (coerce t2))

goodLink :: forall a n. Tree n a -> Tree n a -> Tree (S n) a
goodLink (Ranked t1) (Ranked t2) = coerce (linkRep t1 t2)

Then run spago build.

Expected behavior

badLink and goodLink should both not cause the compiler to hang, but only goodLink ends.

Additional context

The compiler also maxes out my ram and cpu core usage.

PureScript version

0.13.6 [development build; commit: 63e90fe DIRTY]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions