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

Fix access to hoisted variables from reductions #104360

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void EnterScope(object node)
// User-created blocks will never hit this case; only our
// internally reduced nodes will.
//
scope = new CompilerScope(node, false) { NeedsClosure = _scope.NeedsClosure };
scope = new CompilerScope(node, false) { NeedsClosure = true };
}

_scope = scope.Enter(this, _scope);
Expand Down
38 changes: 38 additions & 0 deletions src/libraries/System.Linq.Expressions/tests/Switch/SwitchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,44 @@ public void SwitchOnStringEqualsMethod(bool useInterpreter)
}
}

[Theory, ClassData(typeof(CompilationTypes))]
public void SwitchOnStringWithAccessToLambdaHoistedVariables(bool useInterpreter)
{
ParameterExpression arg0 = Expression.Parameter(typeof(string), "value");

// f1 = (Func<int> f) => f();
ParameterExpression arg1 = Expression.Parameter(typeof(Func<int>), "f");
Expression<Func<Func<int>, int>> expr1 =
Expression.Lambda<Func<Func<int>, int>>(
body: Expression.Invoke(arg1),
parameters: new[] { arg1 });

// f2 = () => value.Length;
Expression<Func<int>> expr2 =
Expression.Lambda<Func<int>>(
Expression.Property(arg0, typeof(string).GetProperty("Length")));

// f0 = (string value) => value switch { "1" => 1, ..., "7" => 7, _ => f1(f2) };
Expression<Func<string, int>> expr0 =
Expression.Lambda<Func<string, int>>(
body: Expression.Switch(
switchValue: arg0,
defaultBody: Expression.Invoke(expr1, expr2),
cases: Enumerable.Range(1, 7).Select(index =>
Expression.SwitchCase(Expression.Constant(index), Expression.Constant(index.ToString()))).ToArray()),
parameters: new[] { arg0 });

Func<string, int> f0 = expr0.Compile(useInterpreter);
Assert.Equal(1, f0("1"));
Assert.Equal(2, f0("2"));
Assert.Equal(3, f0("3"));
Assert.Equal(4, f0("4"));
Assert.Equal(5, f0("5"));
Assert.Equal(6, f0("6"));
Assert.Equal(7, f0("7"));
Assert.Equal(2, f0("10"));
}

[Fact]
public void ToStringTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ public void TypeEqualConstant(Type type, bool useInterpreter)
Assert.False(isNullOfType());
}

[Theory, ClassData(typeof(CompilationTypes))]
public void TypeEqualConstantWithAccessToLambdaHoistedVariables(bool useInterpreter)
{
ParameterExpression arg0 = Expression.Parameter(typeof(object), "value");

// f1 = () => value;
Expression<Func<object>> expr1 = Expression.Lambda<Func<object>>(arg0);

// f0 = (object value) => (() => value) is int;
Expression<Func<object, bool>> expr0 = Expression.Lambda<Func<object, bool>>(
Expression.TypeEqual(
expr1,
typeof(int)),
parameters: new[] { arg0 });
Func<object, bool> f0 = expr0.Compile(useInterpreter);
Assert.False(f0(null));
}

[Fact]
public void ToStringTest()
{
Expand Down
Loading