From cde81d70e5c07bd40dc8da7365f94ff8b1fdad42 Mon Sep 17 00:00:00 2001 From: Bruce Payette Date: Thu, 21 Jun 2018 15:30:04 -0700 Subject: [PATCH 1/2] Fix for #7128 Methods with return type [object] should return null for an empty result --- .../engine/lang/scriptblock.cs | 5 ++++- .../Classes/Scripting.Classes.BasicParsing.Tests.ps1 | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/lang/scriptblock.cs b/src/System.Management.Automation/engine/lang/scriptblock.cs index 2b6d72ae0c2..1622c056218 100644 --- a/src/System.Management.Automation/engine/lang/scriptblock.cs +++ b/src/System.Management.Automation/engine/lang/scriptblock.cs @@ -519,7 +519,10 @@ internal T InvokeAsMemberFunctionT(object instance, object[] args) invocationInfo: null, propagateAllExceptionsToTop: true, args: args); - Diagnostics.Assert(result.Count == 1, "Code generation ensures we return the correct type"); + if (result.Count == 0) + { + return default(T); + } return (T)result[0]; } diff --git a/test/powershell/Language/Classes/Scripting.Classes.BasicParsing.Tests.ps1 b/test/powershell/Language/Classes/Scripting.Classes.BasicParsing.Tests.ps1 index 5185aa38b22..83617bd25e3 100644 --- a/test/powershell/Language/Classes/Scripting.Classes.BasicParsing.Tests.ps1 +++ b/test/powershell/Language/Classes/Scripting.Classes.BasicParsing.Tests.ps1 @@ -116,6 +116,16 @@ Describe 'Positive Parse Properties Tests' -Tags "CI" { class C9b { [System.Collections.Generic.List[C9b]] f() { return [C9b]::new() } } $c9b = [C9b]::new().f() It "Expected a System.Collections.Generic.List[C9b] returned" { $c9b -is [System.Collections.Generic.List[C9b]] | Should -BeTrue } + It 'Methods returning object should return $null if no output was produced' { + class Foo { + [object] Bar1() { return & {} } + static [object] Bar2() { return & {} } + } + # Test instance method + [Foo]::new().Bar1() | Should -BeNullOrEmpty + # Test static method + [foo]::Bar2() | Should -BeNullOrEmpty + } } It 'Positive ParseProperty Attributes Test' { From a1fb00a73f8ce1ec2520dc51a813859a75fe561a Mon Sep 17 00:00:00 2001 From: Bruce Payette Date: Fri, 22 Jun 2018 14:26:06 -0700 Subject: [PATCH 2/2] added a comment and fixed a codeflow issue --- .../engine/lang/scriptblock.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/System.Management.Automation/engine/lang/scriptblock.cs b/src/System.Management.Automation/engine/lang/scriptblock.cs index 1622c056218..d4492f47c7c 100644 --- a/src/System.Management.Automation/engine/lang/scriptblock.cs +++ b/src/System.Management.Automation/engine/lang/scriptblock.cs @@ -519,10 +519,18 @@ internal T InvokeAsMemberFunctionT(object instance, object[] args) invocationInfo: null, propagateAllExceptionsToTop: true, args: args); + + // This is needed only for the case where the + // method returns [object]. If the argument to 'return' + // is a pipeline that emits nothing then result.Count will + // be zero so we catch that and "convert" it to null. Note that + // the return statement is still required in the method, it + // just recieves nothing from it's argument. if (result.Count == 0) { return default(T); } + return (T)result[0]; }