Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Management.Automation.Internal;
using System.Management.Automation.Language;
using System.Management.Automation.Runspaces;
using System.Numerics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -3307,6 +3308,11 @@ private static string ConvertNumericToString(object valueToConvert,
return sgl.ToString(SinglePrecision, numberFormat);
}

if (valueToConvert is BigInteger b)
{
return b.ToString(numberFormat);
}

return (string)Convert.ChangeType(valueToConvert, resultType, CultureInfo.InvariantCulture.NumberFormat);
}
catch (Exception e)
Expand Down Expand Up @@ -4380,7 +4386,8 @@ internal static ConversionRank GetConversionRank(Type fromType, Type toType)
typeof(Int16), typeof(Int32), typeof(Int64),
typeof(UInt16), typeof(UInt32), typeof(UInt64),
typeof(sbyte), typeof(byte),
typeof(Single), typeof(double), typeof(decimal)
typeof(Single), typeof(double), typeof(decimal),
typeof(BigInteger)
};

private static Type[] s_integerTypes = new Type[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4171,7 +4171,9 @@ public object VisitCommand(CommandAst commandAst)
private Expression GetCommandArgumentExpression(CommandElementAst element)
{
var constElement = element as ConstantExpressionAst;
if (constElement != null && LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(constElement.StaticType)))
if (constElement != null
&& (LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(constElement.StaticType))
|| constElement.StaticType == typeof(System.Numerics.BigInteger)))
{
var commandArgumentText = constElement.Extent.Text;
if (!commandArgumentText.Equals(constElement.Value.ToString(), StringComparison.Ordinal))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,9 @@ private void ConvertCommand(CommandAst commandAst, bool isTrustedInput)
{
var constantExprAst = ast as ConstantExpressionAst;
object argument;
if (constantExprAst != null && LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(constantExprAst.StaticType)))
if (constantExprAst != null
&& (LanguagePrimitives.IsNumeric(LanguagePrimitives.GetTypeCode(constantExprAst.StaticType))
|| constantExprAst.StaticType == typeof(System.Numerics.BigInteger)))
{
var commandArgumentText = constantExprAst.Extent.Text;
argument = constantExprAst.Value;
Expand Down
41 changes: 41 additions & 0 deletions test/powershell/Language/Parser/ParameterBinding.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,44 @@ Describe "Custom type conversion in parameter binding" -Tags 'Feature' {
}
}
}

Describe 'Roundtrippable Conversions for Bare-string Numeric Literals passed to [string] Parameters' -Tags CI {

BeforeAll {
$TestValues = @(
@{ Argument = "34uy" }
@{ Argument = "48y" }
@{ Argument = "8s" }
@{ Argument = "49us" }
@{ Argument = "26" }
@{ Argument = "28u" }
@{ Argument = "24l" }
@{ Argument = "32ul" }
@{ Argument = "20d" }
@{ Argument = "6n" }
)

function Test-SimpleStringValue([string] $Value) { $Value }
function Test-AdvancedStringValue {
[CmdletBinding()]
param(
[string]
$Value
)

$Value
}
}

It 'should correctly convert <Argument> back to string in simple functions' -TestCases $TestValues {
param($Argument)

Invoke-Expression "Test-SimpleStringValue -Value $Argument" | Should -BeExactly $Argument
}

It 'should correctly convert <Argument> back to string in advanced functions' -TestCases $TestValues {
param($Argument)

Invoke-Expression "Test-AdvancedStringValue -Value $Argument" | Should -BeExactly $Argument
}
}