diff --git a/src/System.Management.Automation/engine/parser/Compiler.cs b/src/System.Management.Automation/engine/parser/Compiler.cs index 9972e7ff1f8..390adadc84c 100644 --- a/src/System.Management.Automation/engine/parser/Compiler.cs +++ b/src/System.Management.Automation/engine/parser/Compiler.cs @@ -5477,6 +5477,8 @@ public object VisitInvokeMemberExpression(InvokeMemberExpressionAst invokeMember public object VisitArrayExpression(ArrayExpressionAst arrayExpressionAst) { Expression values = null; + ExpressionAst pureExprAst = null; + var subExpr = arrayExpressionAst.SubExpression; if (subExpr.Traps == null) { @@ -5485,10 +5487,10 @@ public object VisitArrayExpression(ArrayExpressionAst arrayExpressionAst) var pipelineBase = subExpr.Statements[0] as PipelineBaseAst; if (pipelineBase != null) { - var exprAst = pipelineBase.GetPureExpression(); - if (exprAst != null) + pureExprAst = pipelineBase.GetPureExpression(); + if (pureExprAst != null) { - values = Compile(exprAst); + values = Compile(pureExprAst); } } } @@ -5500,16 +5502,12 @@ public object VisitArrayExpression(ArrayExpressionAst arrayExpressionAst) } values = values ?? CaptureAstResults(subExpr, CaptureAstContext.Enumerable); - if (values.Type.IsArray) + if (pureExprAst is ArrayLiteralAst) { - // If the result is already an array, don't wrap the array. + // If the pure expression is ArrayLiteralAst, just return the result. return values; } - if (values.Type == typeof(List)) - { - return Expression.Call(values, CachedReflectionInfo.ObjectList_ToArray); - } - if (values.Type.GetTypeInfo().IsPrimitive || values.Type == typeof(string)) + if (values.Type.IsPrimitive || values.Type == typeof(string)) { // Slight optimization - no need for a dynamic site. We could special case other // types as well, but it's probably not worth it. diff --git a/test/powershell/Language/Scripting/Array.Tests.ps1 b/test/powershell/Language/Scripting/Array.Tests.ps1 new file mode 100644 index 00000000000..c66ce5fc216 --- /dev/null +++ b/test/powershell/Language/Scripting/Array.Tests.ps1 @@ -0,0 +1,75 @@ +Describe "ArrayExpression Tests" -Tags "CI" { + It "@([object[]](1,2,3)) should return a 3-element array of object[]" { + $result = @([object[]](1,2,3)) + $result.GetType().FullName | Should Be "System.Object[]" + $result.Length | Should Be 3 + } + + It "@([int[]](1,2,3)) should return a 3-element array of object[]" { + $result = @([int[]](1,2,3)) + $result.GetType().FullName | Should Be "System.Object[]" + $result.Length | Should Be 3 + } + + It "@([object[]]`$null) should return a 1-element(`$null) array of object[]" { + $result = @([object[]]$null) + $result.GetType().FullName | Should Be "System.Object[]" + $result.Length | Should Be 1 + $result[0] | Should Be $null + } + + It "@([int[]]`$null) should return a 1-element(`$null) array of object[]" { + $result = @([int[]]$null) + $result.GetType().FullName | Should Be "System.Object[]" + $result.Length | Should Be 1 + $result[0] | Should Be $null + } + + It "@([object[]][System.Management.Automation.Internal.AutomationNull]::Value) should return a 1-element(`$null) array of object[]" { + $result = @([object[]][System.Management.Automation.Internal.AutomationNull]::Value) + $result.GetType().FullName | Should Be "System.Object[]" + $result.Length | Should Be 1 + $result[0] | Should Be $null + } + + It "@([int[]][System.Management.Automation.Internal.AutomationNull]::Value) should return a 1-element(`$null) array of object[]" { + $result = @([int[]][System.Management.Automation.Internal.AutomationNull]::Value) + $result.GetType().FullName | Should Be "System.Object[]" + $result.Length | Should Be 1 + $result[0] | Should Be $null + } + + It "@(`$null) should return a 1-element(`$null) array of object[]" { + $result = @($null) + $result.GetType().FullName | Should Be "System.Object[]" + $result.Length | Should Be 1 + $result[0] | Should Be $null + } + + It "@([System.Management.Automation.Internal.AutomationNull]::Value) should return an empty array of object[]" { + $result = @([System.Management.Automation.Internal.AutomationNull]::Value) + $result.GetType().FullName | Should Be "System.Object[]" + $result.Length | Should Be 0 + } + + It "@([object[]]`$a) should return a new array" { + $a = 1,2,3 + $result = @([object[]]$a) + $result.GetType().FullName | Should Be "System.Object[]" + $result.Length | Should Be 3 + } + + It "@([int[]]`$a) should return a new array" { + $a = 1,2,3 + $result = @([int[]]$a) + $result.GetType().FullName | Should Be "System.Object[]" + $result.Length | Should Be 3 + } + + It "@([System.Collections.Generic.List[object]]`$null) should return a 1-element(`$null) array of object[]" { + $result = @([System.Collections.Generic.List[object]]$null) + $result.GetType().FullName | Should Be "System.Object[]" + $result.Length | Should Be 1 + $result[0] | Should Be $null + } +}