Skip to content

Commit 603ae98

Browse files
committed
Fix foreach loops breaking in recursive methods when used with an expression iterator
- When using a foreach loop in a method marked with [RecursiveMethod] while iterating on an expression result, the expression result was not being pushed to the recursive stack. This could cause unexpected behavior/errors in some cases. Reported by GlitchyDev
1 parent d61ba6d commit 603ae98

4 files changed

Lines changed: 44 additions & 1 deletion

File tree

Assets/UdonSharp/Editor/UdonSharpASTVisitor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,12 @@ public override void VisitForEachStatement(ForEachStatementSyntax node)
22062206
throw new System.Exception("foreach loop must iterate an array type");
22072207
}
22082208

2209+
if (visitorContext.isRecursiveMethod &&
2210+
((arraySymbol.declarationType & SymbolDeclTypeFlags.Internal) != 0))
2211+
{
2212+
arraySymbol.declarationType |= SymbolDeclTypeFlags.NeedsRecursivePush;
2213+
}
2214+
22092215
if (node.Type.IsVar)
22102216
{
22112217
if (!isTransformIterator)

Assets/UdonSharp/Tests/TestScripts/FlowControl/RecursionTest.asset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ MonoBehaviour:
1717
udonAssembly:
1818
assemblyError:
1919
sourceCsScript: {fileID: 11500000, guid: 41aa655b07e37a84292568dd0dc1aac5, type: 3}
20+
behaviourSyncMode: 0
2021
behaviourIDHeapVarName: __refl_const_intnl_udonTypeID
2122
compileErrors: []
2223
hasInteractEvent: 0

Assets/UdonSharp/Tests/TestScripts/FlowControl/RecursionTest.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ public string CombineStringsNested(int count, string a, string b)
176176
return string.Concat(a, CombineStringsNested(count - 1, CombineStringsNested(count - 1, b, a), CombineStringsNested(count - 1, a, b)), CombineStringsNested(count - 1, CombineStringsNested(count - 1, a, b), CombineStringsNested(count - 1, b, a)), "c");
177177
}
178178

179+
Transform[] GetChildrenTransforms(Transform parent)
180+
{
181+
Transform[] children = new Transform[parent.childCount];
182+
183+
for (int i = 0; i < children.Length; ++i)
184+
{
185+
children[i] = parent.GetChild(i);
186+
}
187+
188+
return children;
189+
}
190+
179191
[RecursiveMethod]
180192
int CountChildren(Transform transformToCount)
181193
{
@@ -187,6 +199,28 @@ int CountChildren(Transform transformToCount)
187199
return childCount;
188200
}
189201

202+
[RecursiveMethod]
203+
int CountChildrenForeachExpression(Transform transformToCount)
204+
{
205+
int childCount = transformToCount.childCount;
206+
207+
foreach (Transform child in GetChildrenTransforms(transformToCount))
208+
childCount += CountChildrenForeachExpression(child);
209+
210+
return childCount;
211+
}
212+
213+
[RecursiveMethod]
214+
int CountChildrenForeachAccessExpression(Transform transformToCount)
215+
{
216+
int childCount = transformToCount.childCount;
217+
218+
foreach (Transform child in transformToCount.gameObject.transform)
219+
childCount += CountChildrenForeachAccessExpression(child);
220+
221+
return childCount;
222+
}
223+
190224
int externChildCount;
191225

192226
[RecursiveMethod]
@@ -280,6 +314,8 @@ public void ExecuteTests()
280314
tester.TestAssertion("Nested call recursion", CombineStringsNested(3, "a", "b") == "abaccbcccabccacccccbaccbccccccabccacccbaccbcccccabccaccccccc");
281315

282316
tester.TestAssertion("Count children recursively foreach", CountChildren(transform) == 20);
317+
tester.TestAssertion("Count children recursively foreach expression", CountChildrenForeachExpression(transform) == 20);
318+
tester.TestAssertion("Count children recursively foreach access", CountChildrenForeachAccessExpression(transform) == 20);
283319

284320
externChildCount = 0;
285321
CountChildrenExternalCount(transform);

Assets/UdonSharp/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.19.11
1+
v0.20.0

0 commit comments

Comments
 (0)