mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Fix up default value for parameters with the in modifier (#26785)
This commit is contained in:
@@ -7201,6 +7201,13 @@ namespace System.Management.Automation.Language
|
||||
var argValue = parameters[i].DefaultValue;
|
||||
if (argValue == null)
|
||||
{
|
||||
if (parameterType.IsByRef)
|
||||
{
|
||||
// When the default value is null for a ByRef parameter (e.g. an optional `in` parameter
|
||||
// using `default`), expression trees cannot create Expression.Default for the T& type.
|
||||
// In that case we switch to the element type and use Default(TElement) instead.
|
||||
parameterType = parameterType.GetElementType();
|
||||
}
|
||||
argExprs[i] = Expression.Default(parameterType);
|
||||
}
|
||||
else if (!parameters[i].HasDefaultValue && parameterType != typeof(object) && argValue == Type.Missing)
|
||||
|
||||
@@ -27,6 +27,12 @@ public class TestClass
|
||||
public static string StaticWithOptionalExpected() => StaticWithOptional();
|
||||
public static string StaticWithOptional([Optional] string value) => value;
|
||||
|
||||
public static int PrimitiveTypeWithInDefault(in int value = default) => value;
|
||||
|
||||
public static Guid ValueTypeWithInDefault(in Guid value = default) => value;
|
||||
|
||||
public static string RefTypeWithInDefault(in string value = default) => value;
|
||||
|
||||
public object InstanceWithDefaultExpected() => InstanceWithDefault();
|
||||
public object InstanceWithDefault(object value = null) => value;
|
||||
|
||||
@@ -101,6 +107,21 @@ public class TestClassCstorWithOptional
|
||||
$actual | Should -Be $expected
|
||||
}
|
||||
|
||||
It "Binds to static method with primitive type with in modifier and default argument" {
|
||||
$actual = [CLRBindingTests.TestClass]::PrimitiveTypeWithInDefault()
|
||||
$actual | Should -Be 0
|
||||
}
|
||||
|
||||
It "Binds to static method with value type with in modifier and default argument" {
|
||||
$actual = [CLRBindingTests.TestClass]::ValueTypeWithInDefault()
|
||||
$actual | Should -Be ([Guid]::Empty)
|
||||
}
|
||||
|
||||
It "Binds to static method with ref type with in modifier and default argument" {
|
||||
$actual = [CLRBindingTests.TestClass]::RefTypeWithInDefault()
|
||||
$null -eq $actual | Should -BeTrue
|
||||
}
|
||||
|
||||
It "Binds to instance method with default argument" {
|
||||
$c = [CLRBindingTests.TestClass]::new()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user