Skip to content

ValueFromRemainingArguments attribute should not be allowed on multiple parameters #4629

Description

By definition, using attribute ValueFromRemainingArguments on a parameter:

  • only makes sense on one parameter

  • which should either be unconstrained, or declared as an array.

Both At least the former condition should be enforced at parse time, because functions that violate these principles fail on invocation in the typical use case.
[As noted below, the must-be-unconstrained-or-array condition cannot be reliably enforced without preventing exotic, but legitimate use cases.]

Steps to reproduce

Define the following functions:

# Define *multiple* `ValueFromRemainingArguments` parameters, which makes no sense.
function Foo1 {
  [CmdletBinding()]
  param(
    [Parameter(ValueFromRemainingArguments)]
    $Rest1,
    [Parameter(ValueFromRemainingArguments)]
    $Rest2
  ) 
  'hi1'
}

# Define a `ValueFromRemainingArguments` parameter with a *scalar type*, which 
# generally makes no sense, because the whole idea behind the attribute is to collect
# *multiple* arguments.
function Foo2 {
  [CmdletBinding()]
  param(
    [Parameter(ValueFromRemainingArguments)]
    [int] $Rest
  )
}

Then, assuming you can define them (which you shouldn't be able to), invoke them:

Foo1 a
Foo2 1 2

Expected behavior

Parsing the function definitions should fail, because they violate the idea behind the ValueFromRemainingArguments attribute.

Actual behavior

# Foo1 was successfully defined, but cannot be invoked with positional arguments.
Foo1 : Parameter set cannot be resolved using the specified named parameters.

# Foo2 was successfully defined, but fails with more than 1 [int] argument.
Foo2 : Cannot process argument transformation on parameter 'Rest'. Cannot convert the "System.Collections.Generic.List`1[System.Object]" value of type 
"System.Collections.Generic.List`1[[System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]" to type "System.Int32".

In fact, Foo2 even fails with a single [int] argument, because a ValueFromRemainingArguments parameter currently unconditionally returns a [List[object]] instance, even if only a single argument is bound - see #4625

Environment data

PowerShell Core v6.0.0-beta.5 on macOS 10.12.6
PowerShell Core v6.0.0-beta.5 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.5 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.483 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions