Make a variable assignment in a ParenExpression to return the variable value (#17174)

This commit is contained in:
Dongbo Wang
2022-04-22 10:55:38 -07:00
committed by GitHub
parent db1c8658fb
commit b898da8842
3 changed files with 27 additions and 1 deletions
@@ -244,6 +244,14 @@ namespace System.Management.Automation
}
}
/// <summary>
/// Gets the value without triggering debugger check.
/// </summary>
internal virtual object GetValueRaw()
{
return _value;
}
/// <summary>
/// Gets or sets the value of the variable.
/// </summary>
@@ -796,6 +804,11 @@ namespace System.Management.Automation
}
}
internal override object GetValueRaw()
{
return _tuple.GetValue(_tupleSlot);
}
internal override void SetValueRaw(object newValue, bool preserveValueTypeSemantics)
{
if (preserveValueTypeSemantics)
@@ -46,6 +46,13 @@ namespace System.Management.Automation
: GetAttributeCollection(attributeAsts);
var = new PSVariable(variablePath.UnqualifiedPath, value, ScopedItemOptions.None, attributes);
if (attributes.Count > 0)
{
// When there are any attributes, it's possible the value was converted/transformed.
// Use 'GetValueRaw' here so the debugger check won't be triggered.
value = var.GetValueRaw();
}
// Marking untrusted values for assignments in 'ConstrainedLanguage' mode is done in
// SessionStateScope.SetVariable.
sessionState.SetVariable(variablePath, var, false, origin);
@@ -81,7 +88,7 @@ namespace System.Management.Automation
null,
Metadata.InvalidValueFailure,
var.Name,
((value != null) ? value.ToString() : "$null"));
(value != null) ? value.ToString() : "$null");
throw e;
}
@@ -29,4 +29,10 @@ Describe "Scripting.Followup.Tests" -Tags "CI" {
## $arraylist.Clear() should be executed
$arraylist.Count | Should -Be 0
}
## fix https://github.com/PowerShell/PowerShell/issues/17165
It "([bool] `$var = 42) should return the varaible value" {
([bool]$var = 42).GetType().FullName | Should -Be "System.Boolean"
. { ([bool]$var = 42).GetType().FullName } | Should -Be "System.Boolean"
}
}