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

byref analysis not working with type abbreviations #7934

Closed
pihai opened this issue Dec 4, 2019 · 3 comments
Closed

byref analysis not working with type abbreviations #7934

pihai opened this issue Dec 4, 2019 · 3 comments
Assignees
Labels
Milestone

Comments

@pihai
Copy link

pihai commented Dec 4, 2019

Consider the following example involving a ReadOnlySpan and a type abbreviation:

open System

type Bytes = ReadOnlySpan<byte>

type Foo() =
           
    //member this.foo1 (data: ReadOnlySpan<byte>) =
    //    let x = 
    //        if false then
    //            failwith "" // Error FS0412: A type instantiation involves a byref type. This is not permitted by the rules of Common IL
            
    //        else
    //            data
    //    x

    //member this.foo2 (data: ReadOnlySpan<byte>) =
    //    let x = 
    //        if false then
    //            failwithf "" // Error FS0412: A type instantiation involves a byref type. This is not permitted by the rules of Common IL
    //        else
    //            data
    //    x

    member this.foo3 (data: Bytes) =
        let x = 
            if false then // true or false doesn't matter
                failwith ""
            else
                data
        x

    member this.foo4 (data: Bytes) =
        let x = 
            if false then
                failwithf ""
            else
                data
        x

[<EntryPoint>]
let main argv =
    let span = ReadOnlySpan<_>(Array.empty)
    let result = Foo().foo3(span)
    let result = Foo().foo4(span)
    0

Repro steps

As described in #5776 the functions foo1 and foo2 give a compiler error. However, if I use a type abbreviation instead of the actual name of the byref-type ReadOnlySpan the compiler errors disappear and the program can be compiled. The funtion foo3 even works despite it shouldn't, at least I suppose. foo4 raises a TypeLoadException:

System.TypeLoadException: 'The generic type 'Microsoft.FSharp.Core.PrintfFormat`5' was used with an invalid instantiation in assembly 'FSharp.Core, Version=4.7.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.'

Expected behavior

None of the above functions should compile. I'm not sure why foo3 works at runtime anyway.

Actual behavior

A type abbreviation kind of prevents the byref analysis which leads to bad surprises at runtime.

Known workarounds

Don't use type abbreviations.

Related information

Provide any related information (optional):

  • Windows 10
  • .NET Core 3.1
  • Visual Studio 16.4
@cartermp cartermp added this to the Backlog milestone Dec 4, 2019
@cartermp
Copy link
Contributor

cartermp commented Dec 4, 2019

link #6133

(related, but not the same issue)

@TIHan
Copy link
Contributor

TIHan commented Dec 10, 2019

Yes, this should fail at compile time. Thank you for making an issue. It should be an easy fix.

I'm not sure why foo3 works at runtime anyway.

This is because failwith is inline and doesn't use a byref-like type as a type argument in its internals.

@TIHan TIHan self-assigned this Dec 10, 2019
@cartermp
Copy link
Contributor

Fixed by #7953

@cartermp cartermp modified the milestones: Backlog, 16.5 Jan 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants