Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/System.Management.Automation/engine/lang/scriptblock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,18 @@ internal T InvokeAsMemberFunctionT<T>(object instance, object[] args)
invocationInfo: null,
propagateAllExceptionsToTop: true,
args: args);
Diagnostics.Assert(result.Count == 1, "Code generation ensures we return the correct type");

// 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];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could explicitly check result type. I believe it make sense to use testcase for two return types (object and int/string) to ensure that default(T) works as expected.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only time this code path is hit is if the method returns [object]. All of the other cases are already handled elsewhere. And the object returned is null. How do you propose checking it's type?

Copy link
Collaborator

@iSazonov iSazonov Jun 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it was my misunderstanding of default(T).

Should it be Should -BeNull?

# Test static method
[foo]::Bar2() | Should -BeNullOrEmpty
}
}

It 'Positive ParseProperty Attributes Test' {
Expand Down