Skip to content

[3.0 regression] AreEqual/AreNotEqual with nullable structs and implicit casts don't compile any more #1550

@cremor

Description

@cremor

Describe the bug

After updating MSTest.TestFramework to 3.0.2 unit tests that use Assert.AreEqual/AreNotEqual with a nullable struct parameter that has an implicit cast operator to the other parameter type implemented don't compile any more.

Steps To Reproduce

  1. Create a new test project. (It will be created with MSTest.TestFramework 2.2.10.)
  2. Add the following code.
public struct MyValue
{
    public int Value;

    public static implicit operator int(MyValue v) => v.Value;
}

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var v = new MyValue();
        MyValue? nullable = null;

        // Still compiles without errors.
        Assert.AreEqual(0, v);
        Assert.AreNotEqual(1, v);
        Assert.AreEqual(null, nullable);
        Assert.AreNotEqual(v, nullable);

        // Doesn't compile any more.
        Assert.AreNotEqual(1, nullable);
        Assert.AreEqual(0, nullable); // Note: This fails. I don't know if there is a useful use-case for this.
    }
}
  1. Try to compile the project, see that it compiles just fine.
  2. Update MSTest.TestFramework to 3.0.2
  3. See compile errors

Expected behavior

Code should compile without errors.

Actual behavior

Code has compile errors:
CS0411 The type arguments for method 'Assert.AreNotEqual(T?, T?)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Additional context

The Assert.AreEqual case might not make sense. But at least the Assert.AreNotEqual case has valid use-cases and should work.

It seems like the reason for this is #254/#1429 because version 2.2.10 used this overload.
Cc @whoisj @Evangelink

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions