Skip to content

Returning nothing (Automation.Null) from an [object]-typed custom-class method breaks #7128

@mklement0

Description

@mklement0

Steps to reproduce

class Foo { [object] Bar() { return & {} } }; (new Foo).Bar()

Note that & {} outputs "nothing", i.e., the "null collection" [System.Management.Automation.Internal.AutomationNull]::Value

Expected behavior

Given that PowerShell allows casting any value to [object] - including $null and [System.Management.Automation.Internal.AutomationNull]::Value, the operation should succeed and the [System.Management.Automation.Internal.AutomationNull]::Value) should be passed through.

@BrucePay's take on the desired behavior differs, however (taken from this SO answer of his):

We should still generate an error but it should be "non-void methods must return a value" or some such

Actual behavior

The following error occurs:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: Index
...

An analysis of the cause, courtesy of @BrucePay's previously linked answer:

[...] the code that invokes the scriptblock expects there to be an object in the result List i.e. result.Count == 1. There are no objects in the pipeline case so you get an index-out-of-range error. [...]
BTW - the code in question is here.

Environment data

PowerShell Core v6.1.0-preview.2 on macOS 10.13.4
PowerShell Core v6.1.0-preview.2 on Ubuntu 16.04.4 LTS
PowerShell Core v6.1.0-preview.2 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)
Windows PowerShell v5.1.17134.48 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)

Metadata

Metadata

Assignees

Labels

Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions