Skip to content

Improve the error message for when a property-name / hashtable-key token cannot be parsed as such #6959

Closed

Description

Following up from #6946:

A token to the right of . when accessing an object property or hashtable entry is parsed as an expression if it doesn't start with a letter.

If that token is ultimately not a valid expression, the error message erroneously suggests that no token was specified, which is confusing:

$obj = [pscustomobject] @{ '1a' = 'one A' }
PS> $obj.1a  # !! Breaks; must use $obj.'1a', due to property name starting with a *digit*
At line:1 char:6
+ $obj.1a
+      ~
Missing property name after reference operator.

Given that the fact that 1a is not treated as a string - and instead evaluated as an expression that breaks - is not obvious, providing specific guidance in the error message along the following lines would be helpful:

At line:1 char:6
+ $obj.1a
+      ~
Invalid property-name expression; use quotes for literal interpretation.

Environment data

Written as of:

PowerShell Core v6.1.0-preview.2

Activity

  1. iSazonov commented on May 31, 2018

    @iSazonov
    Collaborator

    Is it necessary to use quotes in the scenario? Maybe can fix this?

  2. added
    WG-Engine
    Issue-Discussion
    on May 31, 2018
  3. mklement0 commented on May 31, 2018

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov):

    The linked issue - #6946 - discusses a fix.

    In short:

    If we didn't have a backward-compatibility concern, my vote would definitely be to fix it.

    The problem is that for backward compatibility we must retain the behavior of at least first attempting to interpret a property name / hashtable key that starts with a digit as an expression, for the sake of accessing hashtables with numeric keys.

    A real-world example is $Matches.0 to access the hashtable entry with integer key 0.

    We could fall back to string interpretation if the expression turns out not to be valid, however; e.g.:

    # Examples of surprising behaviors we CANNOT change:
    $obj.1e2  # 1e2 is interpreted as [double] 100, so as a *property* access this is the same as: $obj.'100'
    $obj.1d # -> $obj.'1'
    $obj.1l # -> $obj.'1'
    $obj.1kb # -> $obj.'1024'
    
    # This we could fix by falling back to string, because 1a is not valid as an expression.
    $obj.1a  

    Given that there are cases where unexpected numeric interpretation can kick in, which results in quiet failure (relative to the user's intent), the question is whether implementing fall-back-to-string behavior is ultimately more confusing than helpful:

    In the absence of knowing the underlying logic, it would be hard to infer what's going on; e.g., with fall-back-to-string in place, the user may be puzzled why .1a works, but .1d doesn't (in the sense of .'1d').

    Providing guidance via the error message - i.e., use quoting if you want to force interpretation as a string - ultimately promotes safer habits.

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

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

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

  7. added
    Resolution-No Activity
    on Nov 16, 2023
  8. 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

    Issue-DiscussionResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions