Skip to content

IWSAM can result in a runtime verification exception #19184

@TIHan

Description

@TIHan

This could be looked at as a bug or a feature request; feel free to change the label.

The following F# code will compile (but with warnings because IWSAM):

type ITest =

    static abstract Doot : int

type Test() =

    interface ITest with
        static member Doot = 5

let test<'T when 'T :> ITest>(x: 'T) =
    'T.Doot

let t = Test(): ITest
System.Console.WriteLine(test(t))

When executed, it will raise a System.Security.VerificationException:
Method Program.test: type argument 'Program+ITest' violates the constraint of type parameter 'T'.

The reason is because the call to test with the type argument ITest; ITest.Doot does not have a most specific implementation.

Expected behavior

It should raise a compiler error, like it does in C#:

ITest t = new Test();
Console.WriteLine(test(t));

static int test<T>(T obj) where T : ITest
{
    return T.Doot();
}


interface ITest
{
    static abstract int Doot();
}

class Test : ITest
{
    public static int Doot()
    {
        return 5;
    }
}

The interface 'ITest' cannot be used as type argument. Static member 'ITest.Doot()' does not have a most specific implementation in the interface.

Actual behavior

Compiles. When executed, runtime verification exception is raised.

Known workarounds

Just don't use it.

Related Information
.NET 10 and VS2026

Thoughts

I looked at https://github.com/fsharp/fslang-design/blob/main/FSharp-7.0/FS-1124-interfaces-with-static-abstract-members.md but it did not mention this type of scenario. Perhaps a minor oversight? To be fair, I missed this scenario in Oly.

Raising a compiler error here should be straight-forward to do.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-SRTPbugs in SRTP inference, resolution, witness passing, code genBug

    Type

    Projects

    Status

    New

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions