Skip to content

Commit e7aec4c

Browse files
committed
Fix: Prevent inlining const arrays.
1 parent 306ac17 commit e7aec4c

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

ShaderShrinker/Shrinker.Parser/Optimizations/InlineConstantVariablesExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static void InlineConstantVariables(this SyntaxNode rootNode)
3131
.Where(o => o.Token is AlphaNumToken)
3232
.ToList();
3333

34-
foreach (var definition in constDeclNode.Definitions.Where(o => o.IsSimpleAssignment()).ToList())
34+
foreach (var definition in constDeclNode.Definitions.Where(o => o.IsSimpleAssignment() && !o.IsArray).ToList())
3535
{
3636
var usages = potentialUsage
3737
.Where(o => o.Token.Content.StartsWithVarName(definition.Name) &&

ShaderShrinker/UnitTests/ShrinkerTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,23 @@ public void CheckConstArrayAssignmentsAreNotInlined()
20302030
Assert.That(rootNode.ToCode().ToSimple(), Is.EqualTo(Code));
20312031
}
20322032

2033+
[Test]
2034+
public void CheckConstArraysAreNotInlined()
2035+
{
2036+
const string Code = "float f(float[7]a, int n) { return a[n]; } float main() { const float arr[2] = float[](0., 10.); return f(arr, 1); }";
2037+
2038+
var lexer = new Lexer();
2039+
lexer.Load(Code);
2040+
2041+
var options = CustomOptions.None();
2042+
options.InlineConstantVariables = true;
2043+
var rootNode = new Parser(lexer)
2044+
.Parse()
2045+
.Simplify(options);
2046+
2047+
Assert.That(rootNode.ToCode().ToSimple(), Is.EqualTo(Code));
2048+
}
2049+
20332050
[Test]
20342051
public void CheckNonConstArrayAssignmentsCanBeMadeConst()
20352052
{

0 commit comments

Comments
 (0)