Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Confusing message about extension method CopyTo being available for any type #9838

@ForNeVeR

Description

@ForNeVeR

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Diagnosticsmistakes and possible improvements to diagnosticsBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

    Type

    No fields configured for Bug.

    Projects

    Status
    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions