Skip to content

Out-of-bounds indexed access works differently with collections than with scalars #6823

Closed

Description

Follow-up from #6357 and #6436.

PSv3 brought unified handling of scalars and collections, allowing indexing even into scalars; e.g., $a = 1; $a[0]

However, using an out-of-bounds index results in different behavior depending on whether the value being accessed is a scalar or a collection:

  • scalars: [System.Management.Automation.Internal.AutomationNull]::Value
  • collections: $null

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 -eq 0
$null -eq $scalar[1] -and @($scalar[1]).Count -eq 0

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)

Activity

  1. added
    Issue-Question
    WG-Engine
    on May 6, 2018
  2. added
    Breaking-Change
    on May 13, 2018
  3. BrucePay commented on May 13, 2018

    @BrucePay
    Collaborator

    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.)

  4. mklement0 commented on May 14, 2018

    @mklement0
    ContributorAuthor

    That's helpful background information, Bruce Payette (@BrucePay).


    Leaving backward-compatibility issues aside:

    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.

    In short: probably a Bucket 3: Unlikely Grey Area change (though that is just a guess).

  5. BrucePay commented on Jun 1, 2018

    @BrucePay
    Collaborator

    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.

  6. mklement0 commented on Jun 1, 2018

    @mklement0
    ContributorAuthor

    Bruce Payette (@BrucePay):

    Still - it would be nice to be consistent.

    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).

  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  8. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  9. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  10. added
    Resolution-No Activity
    on Nov 16, 2023
  11. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking-ChangeIssue-QuestionResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions