Steps to reproduce
function foo { param([ValidateNotNull()] $bar) & {'OK'}.GetNewClosure() }
foo
Expected behavior
Function call foo should succeed, and string 'OK' should be output.
Actual behavior
The .GetNewClosure() call fails with the following error message:
Exception calling "GetNewClosure" with "0" argument(s): "The attribute cannot be added because variable bar with value would no longer be valid."
That is, an attempt is made to include the unbound -bar parameter value - the $bar variable - in the closure, which apparently then defaults to $null, which violates its validation attribute.
Passing a valid -bar value makes the problem go away; e.g., foo -bar ''.
I suspect this is a bug: If the function itself treats $bar in the absence of a -bar parameter value as nonexistent, so should .GetNewClosure().
The problem seems closely related to this bug report on uservoice.com, however, it has been marked as fixed in v5.
An example of how the problem might affect a real-world scenario can be found here.
Environment data
PowerShell v6.0.0-alpha (v6.0.0-alpha.15) on Darwin Kernel Version 16.4.0: Thu Dec 22 22:53:21 PST 2016; root:xnu-3789.41.3~3/RELEASE_X86_64
PowerShell v5.1.14393.693 on Microsoft Windows 10 Pro (64-bit; v10.0.14393)