Skip to content

Fix the interpreted version of Expression<>.Compile() for setting field on value types #116901

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

manofstick
Copy link
Contributor

@manofstick manofstick commented Jun 22, 2025

A bug was discovered where Expression<>.Compile(true) would return a different result to Expression<>.Compile(false). The result in the case of the interpreter meant that fields of a value type embedded within a reference type could not be set, as the interpreter was setting the fields on a boxed version of the value type.

The following unit test demonstrates this functionality:

        class Class<T>
        {
            public T Item;
        }

        struct Struct<T>
        {
            public T Item;
        }

        [Theory]
        [ClassData(typeof(CompilationTypes))]
        public static void AssignToNestedValueTypesClassStructIntTest(bool useInterpreter)
        {
            var outer =
                Expression.Parameter(typeof(Class<Struct<int>>));

            Expression<Action<Class<Struct<int>>>> e =
                Expression.Lambda<Action<Class<Struct<int>>>>(
                    Expression.Assign(
                        Expression.Field(
                            Expression.Field(
                                outer,
                                "Item"
                            ),
                            "Item"
                        ),
                        Expression.Constant(42)
                    ),
                    [outer]
                );

            Action<Class<Struct<int>>> f = e.Compile(useInterpreter);

            Class<Struct<int>> src = new Class<Struct<int>>();

            Assert.Equal(0, src.Item.Item);

            f(src);

            Assert.Equal(42, src.Item.Item); // fails for interpreted version
        }

This PR fixes this issue by utilizing TypedReference to access the fields. A special type FieldData was added into the InterpretedFrame's Data stack which is then used to determine if the slot represents a value type field access. Externally what was exposed as an object array is now wrapped, and access unwraps to the actual object upon access.

The FieldOperations class is the only class that is required to handle the added FieldData type.

NOTE: Tests for the System.Linq.Expressions library had been ignored in debug builds (due to speed reasons) but I have commented out that in order that they are executed. So presumably that change would be reversed before this PR is pulled into the mainline...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Linq.Expressions community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant