dotnet / dotnet/fsharp

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

Open
#8,127 3 comments 3 reactions 0 assignees View on GitHub
Area-Diagnostics Feature Request
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

# 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:

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

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

```fsharp
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... |}?
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.