fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Confusing message about extension method `CopyTo` being available for any type

Open ForNeVeR opened this issue 5 years ago • 1 comments

Repro steps

  1. Create the following F# program:
    open System
    type Foo() = class end
    [<EntryPoint>]
    let main _ =
      let foo = Foo()
      let span = Span<int>.Empty
      foo.CopyTo(span)
    
  2. Try to compile it for netcoreapp3.1 target framework

Expected behavior

Compiler should tell me that the method in question (Foo.CopyTo(Span)) doesn't exist.

Actual behavior

Compiler reports the following:

Program.fs(7,3): error FS0041: No overloads match for method 'CopyTo'.

Known type of argument: Span<int>

Available overloads:
 - (extension) Foo.CopyTo<'T>(destination: Memory<'T>) : unit
 - (extension) Foo.CopyTo<'T>(destination: Span<'T>) : unit

Well, it somewhat does tell me that the method doesn't exists, but it uses a very confusing notation: how's an overload Foo.CopyTo<'T> available? And if it does, then why doesn't it get used here?

I suspect that the compiler actually picks a pair of corresponding System.MemoryExtensions.CopyTo methods which are only available for T[]?, but renders them in a confusing way.

Consider how confusing it was when I've tried to call it on BitArray (which has its own CopyTo method which doesn't work for Spans). I was first fascinated to know that BitArray.CopyTo<'T>(destination: Span<'T>) exists, but then very confused on how to call it.

Known workarounds

No.

Related information

Provide any related information (optional):

  • Operating system: Windows 10
  • .NET Runtime kind:
    $ dotnet --info
    .NET Core SDK (reflecting any global.json):
     Version:   3.1.302
     Commit:    41faccf259
    
    Runtime Environment:
     OS Name:     Windows
     OS Version:  10.0.19041
     OS Platform: Windows
     RID:         win10-x64
     Base Path:   C:\Program Files\dotnet\sdk\3.1.302\
    
    Host (useful for support):
      Version: 3.1.6
      Commit:  3acd9b0cd1
    
    .NET Core SDKs installed:
      2.1.808 [C:\Program Files\dotnet\sdk]
      2.2.207 [C:\Program Files\dotnet\sdk]
      3.1.302 [C:\Program Files\dotnet\sdk]
    
  • Editing Tools: initially I've noticed the same confusing text in Rider

ForNeVeR avatar Jul 30 '20 14:07 ForNeVeR

Agreed this needs improvement

but renders them in a confusing way

The rendering of extension methods in these error messages should definitely include the member extended

dsyme avatar Aug 08 '20 09:08 dsyme