Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MattEdwardsWaggleBee opened this issue Mar 17, 2025 · 2 comments
Open
Labels
Milestone

Comments

@MattEdwardsWaggleBee
Copy link
Contributor

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);
}
@MattEdwardsWaggleBee
Copy link
Contributor Author

Added PR #456

@dadhi
Copy link
Owner

dadhi commented Mar 18, 2025

@MattEdwardsWaggleBee Thanks for bringing it. I've just finished the full rewrite of the TryEmitConvert.
Will merge your PR (double thanks) and see how it goes.

@dadhi dadhi added the bug label Mar 18, 2025
@dadhi dadhi added this to the v5.0.3 milestone Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants