Skip to content

Improve Error Reporting: Forgetting | in anonymous record gives a confusing error message. #8127

@isaacabraham

Description

@isaacabraham

What

Whilst it's an expected pattern to move from anonymous records to nominal records, we've noticed that we sometimes find it useful to go the other direction - from a full nominal record to an anonymous record with a type alias.

Unfortunately, doing this leads to an error message that is especially misleading - and leads to lots of confusion (I'm talking sometimes 5 minutes of chasing our tails until we realise what the problem is).

This code compiles fine:

type Person = { Name : string }
let x : Person = { Name = "Test" }

Now assume we change Person to an type-aliased anonymous record:

type Person = {| Name : string |} // just add two |s in here..
let x : Person = { Name = "Test" } // Error: The record label `Name` is not defined.

Why

Whilst it's totally reasonable to give a compiler error at this point, the error is misleading - it suggests that the Name field doesn't exist: The first thing a developer does at this point is look at the type alias and say "Yes, the Name does exist! There's a problem with the compiler!".

The developer might completely overlook the fact that they are missing the | from the RHS of the second line. Also consider that this second line might exist in another file, or even project, miles away from the alias definition.

How

Detect if you're comparing or assigning a nominal record to an aliased record (and, ideally, compare all the field names). Then give an error message such as:

You are attempting to assign a record to an anonymous record. Did you forget to add vertical pipes to the record definition e.g. {| Field = value... |}?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions