Skip to content

Need to expand type synonyms during role inference #3871

@hdgarrood

Description

@hdgarrood

Description

Type synonyms are currently treated by the type checker as having all of their arguments being "phantom", i.e. anything happening under a type synonym is ignored. This can lead to invalid coercions; for example:

module Main where
import Safe.Coerce (coerce)
newtype Age = Age Int
data Maybe a = Just a | Nothing
type SMaybe a = Maybe a
data G a = G (SMaybe a)
-- This is okay
testA :: G Age -> G Int
testA = coerce
-- This is not
testB :: G Age -> G String
testB = coerce

Instead, the compiler should expand type synonyms as necessary during role inference.

To Reproduce

Use a parameterised type synonym within a data type declaration.

Expected behavior

The type checker should expand type synonyms while inferring roles so that the inferred roles are the same as if the data type had been written without any reference to type synonyms. (We already have a linting step to ensure that we don't have loops in type synonym expansion, so this should be safe, right?)

PureScript version

master

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions