Skip to content

nativeptr in interfaces leads to runtime errors #14508

@hyazinthh

Description

@hyazinthh

Implementing a generic interface that contains a member returning nativeptr<'T> leads to a TypeLoadException with message

Signature of the body and declaration in a method implementation do not match.

when using that type. Curiously, implementing the interface in a generic type works as expected. I'm not sure if I'm doing something completely unreasonable here, but I'd expect a compiler error at least.

This does not seem to be related to .NET 7.

Repro steps

ConsoleApp1.zip

type IFoo<'T when 'T : unmanaged> =
    abstract member Pointer : nativeptr<'T>

type Broken() =
    member x.Pointer : nativeptr<int> = Unchecked.defaultof<_>

    interface IFoo<int> with
        member x.Pointer = x.Pointer

type Working<'T when 'T : unmanaged>() =
    member x.Pointer : nativeptr<'T> = Unchecked.defaultof<_>

    interface IFoo<'T> with
        member x.Pointer = x.Pointer

[<EntryPoint>]
let main argv =
    // Works
    let a = Working<int>()

    // Throws System.TypeLoadException: 'Signature of the body and declaration in a method implementation do not match.
    let b = Broken()

    0

Expected behavior

Should run or produce a compile-time error if not valid.

Actual behavior

Throws an TypeLoadException at runtime.

Related information

  • Windows 10
  • .NET 6 (and 7 tested on a colleagues machine)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-CodeGenIlxGen, ilwrite and things at the backendBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

    Type

    No fields configured for Bug.

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions