dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.92k stars 786 forks source link

Cannot use operators defined in internal types. #16762

Open teo-tsirpanis opened 8 months ago

teo-tsirpanis commented 8 months ago

I discovered that the compiler will not let me use an operator in an internal type, even from an internal function. I'm not sure if it's intentional, so opening it here as a bug.

Repro steps

open System

module internal M =
    type C(value: int) =
        static member inline (||+) (x: C, y: C) = 0

let inline internal f (x1: M.C) (x2: M.C) = x1 ||+ x2

Expected behavior

The snippet compiles successfully.

Actual behavior

error FS0043: The member or object constructor 'op_BarBarPlus' is not public. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.

Known workarounds

If I make the module public the snippet will compile successfully. I can do it in my use case (an F# library distributed in source form) and explicitly document that the types may become internal at any time.

Related information

Provide any related information (optional):

KevinRansom commented 7 months ago

This seems like the wrong behaviour.

The only way to make this compile is to decorate the module as public. The error message identifies the operator as private, and yet the only active scope limiter is the module which is decorated as internal.

image

KevinRansom commented 7 months ago

@dsyme clearly F# has had this behaviour for ever, do we really want to restrict operator type member visibility to public members.

image

dsyme commented 6 months ago

There's an approved language design suggestion for this, and https://github.com/dotnet/fsharp/pull/6805 addressed the issue, but that PR is quite old now.