Skip to content

Having a function with inref parameter crashes assembly scanner #1140

@xperiandri

Description

@xperiandri

After I added such code to the project, that is being scanned:

[<AutoOpen>]
module StringBuffer =

    open System
    open System.Text

    type StringBuffer = StringBuilder -> unit

    module StringBuilderOperations =

        let yieldSpan (chars: Span<char>) (b: StringBuilder) =
            for i = 0 to chars.Length - 1 do
                Printf.bprintf b "%c" chars.[i]

        let yieldReadOnlySpan (chars: ReadOnlySpan<char>) (b: StringBuilder) =
            for i = 0 to chars.Length - 1 do
                Printf.bprintf b "%c" chars.[i]

        let yieldMemory (chars: Memory<char>) (b : StringBuilder) =
            for i = 0 to chars.Length - 1 do
                Printf.bprintf b "%c" chars.Span.[i]

        let yieldReadOnlyMemory (chars: ReadOnlyMemory<char>) (b : StringBuilder) =
            for i = 0 to chars.Length - 1 do
                Printf.bprintf b "%c" chars.Span.[i]

        let yieldStringSeq (strings: #seq<string>) (b: StringBuilder) =
            for s in strings do
                Printf.bprintf b "%s" s

    open StringBuilderOperations

    type StringBufferBuilder () =
        member inline _.Yield (txt: string) = fun (b: StringBuilder) -> Printf.bprintf b "%s" txt
        member inline _.Yield (c: char) = fun (b: StringBuilder) -> Printf.bprintf b "%c" c
        member inline _.Yield (b: byte) = fun (sb: StringBuilder) -> Printf.bprintf sb "%02x " b
        member inline _.Yield (span: inref<Span<char>>) = yieldSpan span
        member inline _.Yield (span: inref<ReadOnlySpan<char>>) = yieldReadOnlySpan span
        member inline _.Yield (chars: Memory<char>) = yieldMemory chars
        member inline _.Yield (chars: ReadOnlyMemory<char>) = yieldReadOnlyMemory chars
        member inline _.YieldFrom (strings: #seq<string>) = yieldStringSeq strings

        member inline _.YieldFrom (f: StringBuffer) = f
        member _.Combine (f, g) = fun (b: StringBuilder) -> f b; g b
        member _.Delay f = fun (b: StringBuilder) -> (f()) b
        member _.Zero () = ignore

        member _.For (xs: 'a seq, f: 'a -> StringBuffer) =
            fun (b: StringBuilder) ->
                use e = xs.GetEnumerator ()
                while e.MoveNext() do
                    (f e.Current) b

        member _.While (p: unit -> bool, f: StringBuffer) =
            fun (b: StringBuilder) -> while p () do f b

        member _.Run (f: StringBuffer) =
            let b = StringBuilder()
            do f b
            b.ToString()

    let stringBuffer = new StringBufferBuilder ()

I see System.Reflection.ReflectionTypeLoadException: 'Unable to load one or more of the requested types. A ByRef or ByRef-like type cannot be used as the type for an instance field in a non-ByRef-like type.'

The root cause are these lines:

        member inline _.Yield (span: inref<Span<char>>) = yieldSpan span
        member inline _.Yield (span: inref<ReadOnlySpan<char>>) = yieldReadOnlySpan span

Is there a workaround for this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions