Powershell: Assignment with method invocation the LHS doesn't throw

Created on 20 Jul 2020  路  4Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

'test'.Substring(1) = 'something'

Expected behavior

At line:1 char:1
+ 'test'.Substring(1) = 'something'
+ ~~~~~~~~~~~~~~~~~~~
The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as
a variable or a property.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidLeftHandSide

Actual behavior

Nothing, it works like if the LHS was $null.

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.0-preview.5
PSEdition                      Core
GitCommitId                    7.1.0-preview.5
OS                             Microsoft Windows 10.0.18362
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Tbh I don't really want this fixed because it frees me up to maybe implement some ref return stuff in PSLambda. That's not really a valid reason though, so reporting anyway.


My guess is this is because MemberExpressionAst implements ISupportsAssignment, but just doesn't check if it happens to be an InvokeMemberExpressionAst instance.

Issue-Question WG-Engine

Most helpful comment

This kind of brings up a bigger discussion that might be better off in a new issue but:

  1. Should CLR ref return members be allowed on the LHS of an expression
  2. Should CLR ref return members return the referenced value itself when used outside the LHS of an assignment

Though the above questions are made a bit more complicated by the fact that there's very limited support for them in the SLE.Expression API.

All 4 comments

As I recall, this is intentional support for assignment to byref return values which, while not common, are used in some important VBA scenarios.

@lzybkr can you give an example of how that would look? I'm having trouble picturing what that would be useful for 馃槀

Oh right like $excelWorkSheet.Cells.Item(1, 1) = 'text'. So then I guess the question is should it throw if the com binder isn't used?

Or should support just be added to the CLR binder? As long as the invocation is on the LHS and not saved it should be doable.

This kind of brings up a bigger discussion that might be better off in a new issue but:

  1. Should CLR ref return members be allowed on the LHS of an expression
  2. Should CLR ref return members return the referenced value itself when used outside the LHS of an assignment

Though the above questions are made a bit more complicated by the fact that there's very limited support for them in the SLE.Expression API.

Was this page helpful?
0 / 5 - 0 ratings