Skip to content

TypeAs should return null when it cannot cast to a nullable #455

Closed
@MattEdwardsWaggleBee

Description

@MattEdwardsWaggleBee

When I run an expression casting a long to a Nullable<int>, I get "System.NullReferenceException : Object reference not set to an instance of an object. at (ArrayClosure)"

Here is a sample test, I can take a better look tomorrow and move it to a branch.

[Test]
public void Original_case()
{
    var x = Parameter(typeof(int?), "x");

    var expr = Lambda<Func<int?>>(
        Block(
            [x],
            Assign(x, TypeAs(Constant(12345L), typeof(int?)))
        )
    );

    expr.PrintCSharp();

    var fs = expr.CompileSys();
    fs.PrintIL();

    var sr = fs();
    Asserts.AreEqual(null, sr);

    var ff = expr.CompileFast(false);
    ff.PrintIL();

    var fr = ff();
    Asserts.AreEqual(null, fr);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions