Skip to content

Commit 4ae86a3

Browse files
committed
Fix array creation implicit size variable conversion
- Fix issues when you pass a non-int32 variable into an array creation expression, reported by Ureishi
1 parent 4042459 commit 4ae86a3

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

Assets/UdonSharp/Editor/UdonSharpASTVisitor.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,26 @@ public override void VisitArrayCreationExpression(ArrayCreationExpressionSyntax
476476
}
477477
else
478478
{
479+
SymbolDefinition capturedRank;
480+
479481
using (ExpressionCaptureScope rankCapture = new ExpressionCaptureScope(visitorContext, null))
480482
{
481483
Visit(node.Type.RankSpecifiers[0]);
482-
arrayRankSymbol = rankCapture.ExecuteGet();
484+
capturedRank = rankCapture.ExecuteGet();
485+
}
486+
487+
if (capturedRank.symbolCsType == typeof(int))
488+
{
489+
arrayRankSymbol = capturedRank;
490+
}
491+
else
492+
{
493+
using (ExpressionCaptureScope convertScope = new ExpressionCaptureScope(visitorContext, null))
494+
{
495+
arrayRankSymbol = visitorContext.topTable.CreateUnnamedSymbol(typeof(int), SymbolDeclTypeFlags.Internal);
496+
convertScope.SetToLocalSymbol(arrayRankSymbol);
497+
convertScope.ExecuteSet(capturedRank, true);
498+
}
483499
}
484500
}
485501

Assets/UdonSharp/Tests/TestScripts/Core/ArrayTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ public void ExecuteTests()
8686

8787
tester.TestAssertion("Unity VideoPlayer array", unityVideoPlayerArray[0] != null && unityVideoPlayerArray[1] != null);
8888
tester.TestAssertion("Base VRCStation array", baseVideoPlayerArray[0] != null && baseVideoPlayerArray[1] != null);
89+
90+
// Test implicit array rank conversions
91+
float[] myFloatArr = new float[20U];
92+
tester.TestAssertion("Array size implicit conversion 1", myFloatArr.Length == 20);
93+
94+
uint arrSize = 4;
95+
myFloatArr = new float[arrSize];
96+
tester.TestAssertion("Array size implicit conversion 2", myFloatArr.Length == 4);
97+
98+
byte arrSize2 = 30;
99+
myFloatArr = new float[arrSize2];
100+
101+
tester.TestAssertion("Array size implicit conversion 3", myFloatArr.Length == 30);
89102
}
90103
}
91104
}

0 commit comments

Comments
 (0)