From 04b93af4ae333013e4fe00013a196268d314cb00 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 3 Apr 2023 09:36:36 -0700 Subject: [PATCH] Enable type conversion of `AutomationNull` to `$null` for assignment (#19415) --- .../engine/LanguagePrimitives.cs | 6 ++++++ .../Language/Scripting/Scripting.Followup.Tests.ps1 | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/System.Management.Automation/engine/LanguagePrimitives.cs b/src/System.Management.Automation/engine/LanguagePrimitives.cs index b1c7cab0ac..ac3c1c1a70 100644 --- a/src/System.Management.Automation/engine/LanguagePrimitives.cs +++ b/src/System.Management.Automation/engine/LanguagePrimitives.cs @@ -4691,6 +4691,12 @@ namespace System.Management.Automation } } + // treat AutomationNull.Value as null for consistency + if (propValue == AutomationNull.Value) + { + propValue = null; + } + property.Value = propValue; } else diff --git a/test/powershell/Language/Scripting/Scripting.Followup.Tests.ps1 b/test/powershell/Language/Scripting/Scripting.Followup.Tests.ps1 index 888c43b357..fff4993ffe 100644 --- a/test/powershell/Language/Scripting/Scripting.Followup.Tests.ps1 +++ b/test/powershell/Language/Scripting/Scripting.Followup.Tests.ps1 @@ -30,6 +30,18 @@ Describe "Scripting.Followup.Tests" -Tags "CI" { $arraylist.Count | Should -Be 0 } + It 'AutomationNull should be same as null for type conversion' { + $result = pwsh -noprofile -command { + class Example { + [string[]]$LogMessage + } + + [Example] @{ LogMessage = [System.Management.Automation.Internal.AutomationNull]::Value } + } + + $result.LogMessage | Should -BeNullOrEmpty + } + ## fix https://github.com/PowerShell/PowerShell/issues/17165 It "([bool] `$var = 42) should return the varaible value" { ([bool]$var = 42).GetType().FullName | Should -Be "System.Boolean"