From 9cdf9819e1a9df27b85b1912feaa9d2f79ffdb08 Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Sat, 25 Feb 2023 18:18:04 +0100 Subject: [PATCH 1/2] Fix parsing for array literals in index expressions in method calls --- .../engine/parser/Parser.cs | 13 ++++++++++++- test/powershell/Language/Parser/Parsing.Tests.ps1 | 8 ++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/parser/Parser.cs b/src/System.Management.Automation/engine/parser/Parser.cs index e1788d5996f..1d359cd0409 100644 --- a/src/System.Management.Automation/engine/parser/Parser.cs +++ b/src/System.Management.Automation/engine/parser/Parser.cs @@ -7939,7 +7939,18 @@ private ExpressionAst ElementAccessRule(ExpressionAst primaryExpression, Token l // G primary-expression '[' new-lines:opt expression new-lines:opt ']' SkipNewlines(); - ExpressionAst indexExpr = ExpressionRule(); + bool oldDisableCommaOperator = _disableCommaOperator; + _disableCommaOperator = false; + ExpressionAst indexExpr = null; + try + { + indexExpr = ExpressionRule(); + } + finally + { + _disableCommaOperator = oldDisableCommaOperator; + } + if (indexExpr == null) { // ErrorRecovery: hope we see a closing bracket. If we don't, we'll pretend we saw diff --git a/test/powershell/Language/Parser/Parsing.Tests.ps1 b/test/powershell/Language/Parser/Parsing.Tests.ps1 index 7b6cf0b69f8..24445e8c471 100644 --- a/test/powershell/Language/Parser/Parsing.Tests.ps1 +++ b/test/powershell/Language/Parser/Parsing.Tests.ps1 @@ -664,3 +664,11 @@ Describe "Parsing using statement with alias and linebreak and comma" -Tag CI { } } } + +It "Should correctly parse array literals for index expressions in method calls" { + $tks = $null + $ers = $null + $Script = '[string]::join(" ", (0, 1, 2)[0, 1])' + $result = [System.Management.Automation.Language.Parser]::ParseInput($Script, [ref]$tks, [ref]$ers) + $result.EndBlock.Statements[0].PipelineElements[0].Expression.Arguments[1].Index.Elements.Count | Should -Be 2 +} From 6a814b4c8166eb319f2149c465fa59a668044c20 Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Tue, 16 May 2023 22:19:46 +0200 Subject: [PATCH 2/2] Add additional check to test. --- test/powershell/Language/Parser/Parsing.Tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/test/powershell/Language/Parser/Parsing.Tests.ps1 b/test/powershell/Language/Parser/Parsing.Tests.ps1 index 4f50c2d2f82..2bd717becda 100644 --- a/test/powershell/Language/Parser/Parsing.Tests.ps1 +++ b/test/powershell/Language/Parser/Parsing.Tests.ps1 @@ -671,6 +671,7 @@ It "Should correctly parse array literals for index expressions in method calls" $Script = '[string]::join(" ", (0, 1, 2)[0, 1])' $result = [System.Management.Automation.Language.Parser]::ParseInput($Script, [ref]$tks, [ref]$ers) $result.EndBlock.Statements[0].PipelineElements[0].Expression.Arguments[1].Index.Elements.Count | Should -Be 2 + $ers.Count | Should -Be 0 } It "Should correctly parse array types that are used as arguments without brackets in generic type" {