Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Regression] Compiler is unable to compile specific flavor of type check in patterns #16447

Closed
En3Tho opened this issue Dec 15, 2023 · 2 comments · Fixed by #17113
Closed

[Regression] Compiler is unable to compile specific flavor of type check in patterns #16447

En3Tho opened this issue Dec 15, 2023 · 2 comments · Fixed by #17113
Assignees
Labels
Area-Compiler-Syntax lexfilter, indentation and parsing Bug Impact-High (Internal MS Team use only) Describes an issue with extreme impact on existing code. Regression
Milestone

Comments

@En3Tho
Copy link
Contributor

En3Tho commented Dec 15, 2023

Please provide a succinct description of the issue.

I've found a case where compiler is not able to compile the code it used to compile before.
This particular flavor of pattern macthing is used to help compiler figure out some overload complexities

Repro steps

Provide the steps required to reproduce the problem:

open System

module Repro =
    let fail1() =
        let x: Result<unit, exn> = Error (NullReferenceException())
        match x with
        | Error (_: exn & :? System.NullReferenceException) -> // Unexpected symbol ':?' in pattern (FS0010)
            printfn "NullRef!"
        | _ ->
            ()

    let fail2() =
        let x: Result<unit, exn> = Error (Exception())
        match x with
        | Error (_: exn & (:? System.NullReferenceException)) -> // Constraint intersection syntax may only be used with flexible types, e.g. '#IDisposable & #ISomeInterface'. (FS3572)
            printfn "NullRef"
        | _ ->
            printfn "Not a NullRef"

Repro.fail1()
Repro.fail2()

Net6/7 output: (compiles and runs)

NullRef!
Not a NullRef

Net8 fails to build the app with errors specified in the code as comments

@github-actions github-actions bot added this to the Backlog milestone Dec 15, 2023
@En3Tho En3Tho changed the title Compiler is unable to compile specific flavor of type check in patterns [BreakingChange] Compiler is unable to compile specific flavor of type check in patterns Dec 15, 2023
@brianrourkeboll
Copy link
Contributor

brianrourkeboll commented Dec 15, 2023

Interesting. I suppose it is because & was not valid in the type syntax before, and so only the part between : and & was being parsed as a type, i.e., exn; & … and so on were being parsed as separate patterns. Now that & is valid in the type syntax (after #15413), the parser no longer stops at & after the : and instead tries keep parsing the subsequent patterns (:? … or (:? …)) as part of the type.

Parenthesizing the typed pattern itself _ : exn & …(_ : exn) & … allows your example to compile in F# 8:

module Repro =
    let fail1() =
        let x: Result<unit, exn> = Error (NullReferenceException())
        match x with
        | Error ((_: exn) & :? System.NullReferenceException) -> // Works in F# 8 with parens around the typed pattern.
            printfn "NullRef!"
        | _ ->
            ()

    let fail2() =
        let x: Result<unit, exn> = Error (Exception())
        match x with
        | Error ((_: exn) & (:? System.NullReferenceException)) -> // Works in F# 8 with parens around the typed pattern.
            printfn "NullRef"
        | _ ->
            printfn "Not a NullRef"

Repro.fail1()
Repro.fail2()

But you are right about this being a regression.

@En3Tho
Copy link
Contributor Author

En3Tho commented Dec 15, 2023

@brianrourkeboll Thank you for finding a workaround

@En3Tho En3Tho changed the title [BreakingChange] Compiler is unable to compile specific flavor of type check in patterns [Regression] Compiler is unable to compile specific flavor of type check in patterns Dec 16, 2023
@0101 0101 added Regression Area-Compiler-Syntax lexfilter, indentation and parsing Impact-High (Internal MS Team use only) Describes an issue with extreme impact on existing code. and removed Needs-Triage labels Jan 8, 2024
@psfinaki psfinaki self-assigned this Feb 1, 2024
@psfinaki psfinaki modified the milestones: Backlog, February-2024 Feb 1, 2024
@vzarytovskii vzarytovskii modified the milestones: February-2024, March-2024 Mar 4, 2024
@psfinaki psfinaki modified the milestones: March-2024, April-2024 Apr 4, 2024
@psfinaki psfinaki modified the milestones: April-2024, May-2024 May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compiler-Syntax lexfilter, indentation and parsing Bug Impact-High (Internal MS Team use only) Describes an issue with extreme impact on existing code. Regression
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants