dotnet / dotnet/fsharp

"as" pattern with an identifier picks a literal

Open
#18,315 4 comments 1 reaction 0 assignees View on GitHub
Area-Compiler-PatternMatching Bug Impact-Medium Needs-design
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

There has been a [change](https://github.com/fsharp/fslang-design/blob/main/FSharp-6.0/FS-1105-Non-variable-patterns-to-the-right-of-as-patterns.md) in F# 6 that introduces arbitrary patterns to the right of `as`. However, this is also a breaking change in case an identifier is used that starts with an uppercase letter, which was an already valid syntax before F# 6 to create a new variable.

**Repro steps**

```fs
[]
let Co = 2
match 1 with
| 1 as Co -> printf "1"
| _ -> printf "other"
```

**Expected behavior**

The match should succeed, bind `1` to a new variable `Co`, and print "1".

**Actual behavior**

"other" is printed, since it interprets the first pattern as `1 as 2`.

**Known workarounds**

Not using identifiers after `as` that start on an uppercase letter.

**Related information**

I am not sure if this behaviour is by design or not, since the RFC that introduces it does not discuss this case. However, I believe it is not by design, since the feature is marked as *not* being a breaking change. If the breaking change stays in, warnings should be issued since the interpretation of the code differs.

Environment: https://sharplab.io/#v2:DYLgZgzgPg2gPAGQJYBcCmAnAhsAfAXQFgAoYNFAAgGEB7CgXgoCYSBbLFAYwAsKBGCgHdU3ElH4UsEanQC0uCgAcMSAHYowFAER8tYigH0K8pSvWatNFN0xagA=

Trying this in an [older version of the compiler](https://tryfsharp.fsbolero.io/) exhibits the expected behaviour.

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.