Skip to content

"as" pattern with an identifier picks a literal #18315

Open
@IS4Code

Description

@IS4Code

There has been a change 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

[<Literal>]
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 exhibits the expected behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-PatternMatchingpattern compilation, active patterns, performance, codegenBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.Needs-design

    Type

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions