You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Out-of-bounds indexed access works differently with collections than with scalars #6823
Breaking-Changebreaking change that may affect usersIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
What is the reason for this discrepancy?
Can it be eliminated?
Steps to reproduce
# Define a single-element array variable and a scalar variable.$array=,1; $scalar=1# Access out-of-bounds index 1 on both# and test if the value is a null collection ([System.Management.Automation.Internal.AutomationNull]::Value)$null-eq$array[1] -and@($array[1]).Count -eq0$null-eq$scalar[1] -and@($scalar[1]).Count -eq0
Expected behavior
False
False
That is, (scalar) $null should be returned in both cases.
Actual behavior
False
True
That is, in the scalar case [System.Management.Automation.Internal.AutomationNull]::Value was returned.
Environment data
PowerShell Core v6.0.2 on macOS 10.13.4
PowerShell Core v6.0.2 on Ubuntu 16.04.4 LTS
PowerShell Core v6.0.2 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)
Windows PowerShell v5.1.16299.251 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)
In scalar operations, this difference is is effectively unnoticeable since AutomationNull gets converted to $null in a value context:
PS[1] (154) > (1)[1] -eq $null
True
However in an array operation, AutomationNull is treated like an empty array so this 1,2,3 + (1)[1] results in an array of three elements and this 1,2,3 + @(1)[1] results in an array with 4 elements.
I'm on the fence as to which is the "better" behaviour (note: strict mode prevents out-of-bound indexing but with different errors for arrays (out-of-bounds) and scalars ('Unable to index into an object of type System.Int32.'))
Also, if this affects many people, we don't want to change things due to the risk of breaking them. And if it doesn't affect anyone then it doesn't matter whether we change it or not (other than the 'ick' factor of course.)
I'm on the fence as to which is the "better" behaviour
While it's fundamentally tricky to speculate about the properties of nonexistent entities, I think it makes more sense to think of the elements of an array as scalars by default (rather than assuming nesting of arrays).
Since $null is the scalar "not-a-value", it makes sense to me to default to that and to confine [System.Management.Automation.Internal.AutomationNull]::Value - the "null collection" , the "no-values-to-enumerate" value - to situations where collections are expected.
As for backward compatibility:
Also, if this affects many people, we don't want to change things due to the risk of breaking them.
At least to me it's hard to see how someone would have found value in the current behavior and therefore relied on it, especially given its inconsistency.
Michael Klement (@mklement0) Yes - bucket is 3 probably right. Which also means that fixing it won't impact people much either way - no breaks but little benefit. Still - it would be nice to be consistent.
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
Breaking-Changebreaking change that may affect usersIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
BrucePay commentedon May 13, 2018
In scalar operations, this difference is is effectively unnoticeable since
AutomationNullgets converted to$nullin a value context:However in an array operation,
AutomationNullis treated like an empty array so this1,2,3 + (1)[1]results in an array of three elements and this1,2,3 + @(1)[1]results in an array with 4 elements.I'm on the fence as to which is the "better" behaviour (note: strict mode prevents out-of-bound indexing but with different errors for arrays (out-of-bounds) and scalars ('Unable to index into an object of type System.Int32.'))
Also, if this affects many people, we don't want to change things due to the risk of breaking them. And if it doesn't affect anyone then it doesn't matter whether we change it or not (other than the 'ick' factor of course.)
mklement0 commentedon May 14, 2018
That's helpful background information, Bruce Payette (@BrucePay).
Leaving backward-compatibility issues aside:
While it's fundamentally tricky to speculate about the properties of nonexistent entities, I think it makes more sense to think of the elements of an array as scalars by default (rather than assuming nesting of arrays).
Since
$nullis the scalar "not-a-value", it makes sense to me to default to that and to confine[System.Management.Automation.Internal.AutomationNull]::Value- the "null collection" , the "no-values-to-enumerate" value - to situations where collections are expected.As for backward compatibility:
At least to me it's hard to see how someone would have found value in the current behavior and therefore relied on it, especially given its inconsistency.
In short: probably a Bucket 3: Unlikely Grey Area change (though that is just a guess).
BrucePay commentedon Jun 1, 2018
Michael Klement (@mklement0) Yes - bucket is 3 probably right. Which also means that fixing it won't impact people much either way - no breaks but little benefit. Still - it would be nice to be consistent.
mklement0 commentedon Jun 1, 2018
Bruce Payette (@BrucePay):
Indeed, and the corollaries of that are:
no head-scratching by new users stumbling upon the behavior.
no subtle bugs in future code (perhaps unlikely, but definitely possible; e.g., expecting
$var[0], $var[1] | % { "[$_]" }to always output 2 objects).microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.