Skip to content

Compiler allows non-functions to be bound to active pattern names #17190

Closed

Description

@brianrourkeboll

There are a couple of ways in which it is possible to define "active patterns" that are (from the F# perspective) values, not functions. While the tooling treats them as active patterns, it is impossible to actually use them as patterns in any pattern-matching position.

Expected behavior

The compiler disallows defining a multi-case active pattern that is not a function:

let (|P|Q|) = Choice<unit, unit>.Choice1Of2 ()
// error FS1209: Active pattern '|P|Q|' is not a function

I would expect the same for single-case and partial active patterns.

Actual behavior

(|P|)

A value bound to what looks like a single-case active pattern name is actually compiled as a property with a getter (when a module-bound value):

let (|P|) = 3
// val (|P|) : int = 3

image

(|P|_|)

An option or value option value bound to a partial active pattern name is compiled to the exact same .NET method as it would be if it were defined as a function, but from the F# perspective it is a value (or a type function, even though you cannot give it explicit type arguments), not a function, although the tooling again treats it as an active pattern.

let (|P|_|) = None
// val (|P|_|) : 'a option

image

let (|P|_|) = ValueNone
// val (|P|_|) : 'a voption

image

Unfortunately, updating the compiler to disallow these scenarios would be a breaking change — someone could be passing values so defined around qua values (f (|P|), let r = (|P|) + (|Q|), etc.) — although I doubt anyone actually intentionally binds values to active pattern names like this in normal code.

Activity

  1. added this to the Backlog milestone on May 20, 2024
  2. edgarfgp commented on May 20, 2024

    @edgarfgp
    Contributor

    A warning behind a lang feature like we have done in other cases might work here ?

  3. brianrourkeboll commented on May 20, 2024

    @brianrourkeboll
    ContributorAuthor

    A warning behind a lang feature like we have done in other cases might work here ?

    Yeah, I think that would probably be the best we could do.

  4. brianrourkeboll commented on May 20, 2024

    @brianrourkeboll
    ContributorAuthor

    It turns out that members with active pattern names are also allowed (SharpLab) and are not treated as patterns at all by the tooling:

    [<AutoOpen>]
    type Foo =
        static member (|P|) = 3
        static member (|Q|_|) = 3
        static member (|R|_|) = None
        static member (|S|_|) = ValueNone
        static member (|T|U|) = 3

    image

    Anonymous records, too:

    let x = {| (|P|) = 3 |}

    (Parens are gone in tooling, though)

    image

    And constraint calls:

    let inline f x = (^a : (member (|P|) : int) x)
    f {| (|P|) = 3 |}

    And lambdas:

    let f : int -> int = fun (|P|) -> (|P|) + (|P|)

    Or any other pattern-matching context:

    match 3 with
    | (|P|) -> (|P|) + (|P|)

    Also unions:

    [<RequireQualifiedAccess>]
    type T =
        | (|P|) of int
        | (|Q|)

    I guess it's a similar phenomenon to the one that allows you to bind operator names to values:

    let y = {| (||) = 3 |}
    match (+) with (|++|) -> 2 |++| 2
    
    let f (>*^*<) x y = x >*^*< y
    match (+) 3 with (~+) & (~-) -> +(-(2)) + -(9) // 20.
    

    They're all just being lexed as IDENTs (or parenIdents), and then there's inconsistent treatment later on...

  5. abelbraaksma commented on May 21, 2024

    @abelbraaksma
    Contributor

    Yes. from F#'s standpoint, these are just longident, and while they have special meaning in some scenarios, they are not illegal. Likewise, if you do have an active pattern, you can just use it as a HOF function argument, or in a function call.

    I vaguely remember bringing this up ages ago, but I cannot remember the resolution at the time. We could (should?) issue a warning perhaps, instead of an error?

  6. added
    Impact-Low
    Area-Compiler-Checking
    and removed on Jun 24, 2024
  7. edgarfgp commented on Jul 7, 2024

    @edgarfgp
    Contributor

    I think we should prevent users from using active patterns that are not functions.

  8. self-assigned this
    on Aug 12, 2024
  9. edgarfgp commented on Aug 13, 2024

    @edgarfgp
    Contributor

    @vzarytovskii Would be ok to create a new language preview flag e.g. ImprovedActivePatternReporting and fix in multiple PR's some of the cases showed in this issue ?

  10. vzarytovskii commented on Aug 13, 2024

    @vzarytovskii
    Member

    @vzarytovskii Would be ok to create a new language preview flag e.g. ImprovedActivePatternReporting and fix in multiple PR's some of the cases showed in this issue ?

    I suppose, but I will still need to re-read the whole thread to freshen my memory

  11. edgarfgp commented on Aug 13, 2024

    @edgarfgp
    Contributor

    @vzarytovskii I think we can start with

    let (|P|) = 3 // This compiles now. But should show `FS1209: Active pattern '|P|' is not a function` 

    Why:

    let (|P|) = 3 is not a function and can not even be used in match

    Screenshot 2024-08-13 at 13 30 37
  12. added
    Area-Compiler-PatternMatching
    on Feb 5, 2026
  13. T-Gro commented on May 18, 2026

    @T-Gro
    Member
    🤖 Copilot implementation hints

    Fix

    One-line addition in src/Compiler/Checking/PostInferenceChecks.fs at CheckBinding (~line 2093).

    doesActivePatternHaveFreeTypars already raises FS1209 for multi-case APs when the type is not a function. The same check is missing for single-case (|P|) and partial (|P|_|) APs.

    In the match TryGetActivePatternInfo vref with block, add:

    | Some _ ->
        if not (isFunTy g v.TauType) then
            errorR(Error(FSComp.SR.activePatternIdentIsNotFunctionTyped(v.LogicalName), v.Range))

    Important: Use v.TauType, not v.Type — using v.Type produces false positives on generic partial APs like let (|EqualTo|_|) y x = x = y.

    This is technically a breaking change — gate behind langversion:preview or next if required.

    Instructions

    • Use TDD: write tests asserting let (|P|) = 3 and let (|P|_|) = None produce FS1209, while let (|P|_|) x = false still compiles.
    • Run: dotnet test tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj --filter "ActivePattern" -c Debug
    • After code change, invoke the expert-review skill to review your work.
    • Minimal surgical fix. No bloated comments.
    • Run dotnet fantomas . on changed files.
  14. removed their assignment
    on May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area-Compiler-CheckingArea-Compiler-PatternMatchingBugImpact-Lowhelp wanted

Type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions