Repository navigation
Emit exact array accessors for nonvector element storage #609
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
Merged
dadhi
merged 3 commits into
dadhi:master
from
jogibear9988:fix/nonvector-arrays-standalone
Nov 9, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
test/FastExpressionCompiler.UnitTests/NonVectorArrayRegressionTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using S=System.Linq.Expressions.Expression; | ||
| #if LIGHT_EXPRESSION | ||
| namespace FastExpressionCompiler.LightExpression.UnitTests; | ||
| #else | ||
| namespace FastExpressionCompiler.UnitTests; | ||
| #endif | ||
| public struct NonVectorArrayRegressionTests : ITestX | ||
| { | ||
| public void Run(TestRun t) { | ||
| Reads_use_actual_lower_bounds(t); Writes_and_compounds_use_actual_lower_bounds(t); | ||
| Struct_elements_remain_addressable(t); Ref_arguments_preserve_array_element_storage(t); | ||
| Ignored_reads_still_validate_array_and_bounds(t); Receiver_addresses_survive_exception_arguments(t); | ||
| } | ||
| public struct Cell { public int Value; public int Increment(int n) => Value+=n; } | ||
| public static int Increment(ref int n) => ++n; | ||
| public static void Replace(ref string value) => value="changed"; | ||
| public static void ReplaceObject(ref object value) => value=new object(); | ||
| static Array Make(Type t, int lower) => Array.CreateInstance(t,new[]{3},new[]{lower}); | ||
| public void Reads_use_actual_lower_bounds(TestContext t) { | ||
| Check(t,42); Check(t,"hello"); Check(t,(int?)7); Check(t,(string)null); Check(t,new Cell{Value=9}); | ||
| } | ||
| static void Check<T>(TestContext t,T value) { | ||
| foreach(var lower in new[]{-2,1}) { | ||
| var array=Make(typeof(T),lower);array.SetValue(value,lower); | ||
| foreach(var binary in new[]{false,true}) { | ||
| var a=S.Parameter(typeof(Array));var i=S.Parameter(typeof(int));var converted=S.Convert(a,array.GetType()); | ||
| var access=binary?(System.Linq.Expressions.Expression)S.ArrayIndex(converted,i):S.ArrayAccess(converted,i); | ||
| var e=S.Lambda<Func<Array,int,T>>(access,a,i);var f=e.FromSysExpression().CompileFast(true); | ||
| if(!t.IsNotNull(f))return;t.AreEqual(value,f(array,lower)); | ||
| #if !NETFRAMEWORK | ||
| // Framework's System compiler emits unverifiable IL for these non-vector array reads. | ||
| t.AreEqual(e.Compile()(array,lower),f(array,lower)); | ||
| #endif | ||
| } | ||
| } | ||
| } | ||
| public void Writes_and_compounds_use_actual_lower_bounds(TestContext t) { | ||
| foreach(var lower in new[]{-2,0,1}) { | ||
| var array=Make(typeof(int),lower);var a=S.Parameter(typeof(Array));var i=S.Parameter(typeof(int)); | ||
| var access=S.ArrayAccess(S.Convert(a,array.GetType()),i); | ||
| var e=S.Lambda<Func<Array,int,int>>(S.Block(S.Assign(access,S.Constant(5)),S.AddAssign(access,S.Constant(7)),S.PreIncrementAssign(access),S.PostDecrementAssign(access)),a,i); | ||
| var f=e.FromSysExpression().CompileFast(true);if(!t.IsNotNull(f))return; | ||
| t.AreEqual(13,f(array,lower));t.AreEqual(12,array.GetValue(lower)); | ||
| } | ||
| } | ||
| public void Struct_elements_remain_addressable(TestContext t) { | ||
| foreach(var lower in new[]{-2,0,1}) { | ||
| var array=Make(typeof(Cell),lower);var a=S.Parameter(typeof(Array));var i=S.Parameter(typeof(int)); | ||
| var access=S.ArrayAccess(S.Convert(a,array.GetType()),i); | ||
| var e=S.Lambda<Func<Array,int,int>>(S.Block(S.Assign(S.Field(access,nameof(Cell.Value)),S.Constant(40)),S.Call(access,typeof(Cell).GetMethod(nameof(Cell.Increment)),S.Constant(2))),a,i); | ||
| var f=e.FromSysExpression().CompileFast(true);if(!t.IsNotNull(f))return; | ||
| t.AreEqual(42,f(array,lower));t.AreEqual(42,((Cell)array.GetValue(lower)).Value); | ||
| } | ||
| } | ||
| public void Ref_arguments_preserve_array_element_storage(TestContext t) { | ||
| foreach(var lower in new[]{-2,0,1}) { | ||
| var ints=Make(typeof(int),lower);ints.SetValue(40,lower);var a=S.Parameter(typeof(Array));var i=S.Parameter(typeof(int)); | ||
| var access=S.ArrayAccess(S.Convert(a,ints.GetType()),i); | ||
| var f=S.Lambda<Func<Array,int,int>>(S.Call(typeof(NonVectorArrayRegressionTests).GetMethod(nameof(Increment)),access),a,i).FromSysExpression().CompileFast(true); | ||
| if(!t.IsNotNull(f))return;t.AreEqual(41,f(ints,lower));t.AreEqual(41,ints.GetValue(lower)); | ||
| var strings=Make(typeof(string),lower);strings.SetValue("old",lower); | ||
| var textAccess=S.ArrayAccess(S.Convert(a,strings.GetType()),i); | ||
| var set=S.Lambda<Action<Array,int>>(S.Call(typeof(NonVectorArrayRegressionTests).GetMethod(nameof(Replace)),textAccess),a,i).FromSysExpression().CompileFast(true); | ||
| if(!t.IsNotNull(set))return;set(strings,lower);t.AreEqual("changed",strings.GetValue(lower)); | ||
| var objectArrayType=lower==0?typeof(object[]):typeof(object).MakeArrayType(1); | ||
| var covariantAccess=S.ArrayAccess(S.Convert(a,objectArrayType),i); | ||
| var covariant=S.Lambda<Action<Array,int>>(S.Call(typeof(NonVectorArrayRegressionTests).GetMethod(nameof(ReplaceObject)),covariantAccess),a,i).FromSysExpression().CompileFast(true); | ||
| if(!t.IsNotNull(covariant))return;t.Throws<ArrayTypeMismatchException>(()=>covariant(strings,lower)); | ||
| } | ||
| } | ||
| public void Ignored_reads_still_validate_array_and_bounds(TestContext t) { | ||
| foreach(var lower in new[]{-2,0,1}) foreach(var binary in new[]{false,true}) { | ||
| var array=Make(typeof(int),lower);var a=S.Parameter(typeof(Array));var i=S.Parameter(typeof(int));var converted=S.Convert(a,array.GetType()); | ||
| var access=binary?(System.Linq.Expressions.Expression)S.ArrayIndex(converted,i):S.ArrayAccess(converted,i); | ||
| var f=S.Lambda<Action<Array,int>>(S.Block(access,S.Empty()),a,i).FromSysExpression().CompileFast(true); | ||
| if(!t.IsNotNull(f))return;f(array,lower);t.Throws<IndexOutOfRangeException>(()=>f(array,lower-1));t.Throws<NullReferenceException>(()=>f(null,lower)); | ||
| } | ||
| } | ||
| public void Receiver_addresses_survive_exception_arguments(TestContext t) { | ||
| foreach(var lower in new[]{-2,1}) { | ||
| var array=Make(typeof(Cell),lower);var a=S.Parameter(typeof(Array)); | ||
| var element=S.ArrayAccess(S.Convert(a,array.GetType()),S.Constant(lower)); | ||
| var rhs=S.TryFinally(S.Constant(3),S.Empty()); | ||
| var tree=S.Lambda<Func<Array,int>>(S.Call(element,typeof(Cell).GetMethod(nameof(Cell.Increment)),rhs),a); | ||
| var fast=tree.FromSysExpression().CompileFast(true);if(!t.IsNotNull(fast))return; | ||
| t.AreEqual(3,fast(array));t.AreEqual(6,fast(array));t.AreEqual(6,((Cell)array.GetValue(lower)).Value); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.