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

Operator implicit/explicit produces InvalidProgram #451

Open
Xemrox opened this issue Feb 24, 2025 · 2 comments
Open

Operator implicit/explicit produces InvalidProgram #451

Xemrox opened this issue Feb 24, 2025 · 2 comments
Assignees
Labels
Milestone

Comments

@Xemrox
Copy link

Xemrox commented Feb 24, 2025

Hi there,

another small report with testcase. Throws a System.InvalidProgramException : Common Language Runtime detected an invalid program.

public struct SampleType
{
    public bool? Value { get; init; }

    public SampleType(bool? value) { Value = value; }

    public static implicit operator bool?(SampleType left) => (left.Value is not null && left.Value is bool b) ? b : null;
    public static explicit operator bool(SampleType left) => left.Value is not null && left.Value is bool b && b;
}

[Fact]
public void Operator_Implicit_Fast()
{
    var attributeType = typeof(SampleType);
    var ctorMethodInfo = attributeType.GetConstructors()[0];

    var newExpression = Expression.New(ctorMethodInfo, [Expression.Constant(null, typeof(bool?))]);

    var conversion1 = Expression.Convert(newExpression, typeof(bool));
    var conversion2 = Expression.Convert(newExpression, typeof(bool?));

    var lambda1 = Expression.Lambda<Func<bool>>(conversion1);
    var lambda2 = Expression.Lambda<Func<bool?>>(conversion2);
    var sample1 = lambda1.Compile()(); // <- OK
    var sample2 = lambda2.Compile()(); // <- OK
    var sample_fast1 = lambda1.CompileFast()(); // <- OK
    var sample_fast2 = lambda2.CompileFast()(); // <- throws exception
}
@Xemrox Xemrox changed the title Operator implicit produces InvalidProgram Operator implicit/explicit produces InvalidProgram Feb 24, 2025
@Xemrox
Copy link
Author

Xemrox commented Feb 24, 2025

It makes no difference if its implicit operator or explicit operator

@dadhi
Copy link
Owner

dadhi commented Mar 3, 2025

@Xemrox Hi, thanks for the reporting. I will check.

@dadhi dadhi self-assigned this Mar 3, 2025
@dadhi dadhi added the bug label Mar 3, 2025
@dadhi dadhi added this to the v5.0.3 milestone Mar 3, 2025
dadhi added a commit that referenced this issue Mar 4, 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