dotnet / dotnet/fsharp

Some self-excluding type constraint combinations are allowed, wrong error when they aren't

Open
#9,777 9 comments 0 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

If you use a constraint combination that isn't allowed, you always get the same error, twice:

> Program.fs(38,76): error FS0043: The constraints 'struct' and 'not struct' are inconsistent
> Program.fs(38,76): error FS0043: The constraints 'struct' and 'not struct' are inconsistent

![image](https://user-images.githubusercontent.com/16015770/88439475-a2d43600-ce0b-11ea-9e20-d06467640e94.png)

Repro:

```f#
type Del<'T when 'T: comparison and 'T: delegate> =
member _.Z = 1
```

Some others are allowed, but cannot be instantiated:

```f#
type EnumNull<'T when 'T:enum and 'T:null>()
type StructNull<'T when 'T:'T:struct and 'T:null>()
type DelEnum<'T when 'T: enum and 'T: delegate>
type UnmDel<'T when 'T: unmanaged and 'T: delegate> =
// etc
```

As was mentioned on Slack, some of these are used with SRTP dirty tricks (see [slack discussion](https://fsharp.slack.com/archives/C1R50TKEU/p1595595515248400) with @NinoFloris and an example: https://github.com/fsprojects/FSharpPlus/blob/7e32d52e9881ff40b5440f5ca886b56e6dd4a4c4/src/FSharpPlus/Control/Alternative.fs#L28)

### Proposal: fix

I propose we fix this:

* Fix the error to show the actual conflicting / inconsistent constraints
* Fix the error to show only once
* Make others that shouldn't be allowed (i.e., that cannot be combined in any type) a warning, and add it as default to `warnings-as-errors` (to prevent this from becoming a backward compat issue, similar to how we added FS3239 by default to new projects).

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.