Skip to content

Regression in compiling lambdas with ref struct parameters #490

@tomasherceg

Description

@tomasherceg

I found a bug in handling ref struct parameters. It appeared in 4.0.0 and is present even in the latest stable version 5.3.0.
Interestingly, the same code works correctly in 3.3.*.

Basically, I am generating a function like this (Utf8JsonReader is ref struct):

(ref Utf8JsonReader reader) => reader.TokenType == JsonTokenType.Null ? default(int) : reader.GetInt32();

Here is the repro:

var param = Parameter(typeof(Utf8JsonReader).MakeByRefType());
var body = Condition(
    Equal(Property(param, "TokenType"), Constant(JsonTokenType.Null)),
    Default(typeof(int)),
    Call(param, "GetInt32", Type.EmptyTypes)
);
var lambda = Lambda<TestDelegate>(body, param);
var func = lambda.CompileFast();

// make reader and advance to the Null token
var reader = new Utf8JsonReader("null"u8);
reader.Read();
Assert.Equal(JsonTokenType.Null, reader.TokenType);

// the compiled function should return default(int), yet it calls reader.GetInt32 instead
func(ref reader);

Metadata

Metadata

Assignees

Labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions