Skip to content

Commit b898da8

Browse files
authored
Make a variable assignment in a ParenExpression to return the variable value (PowerShell#17174)
1 parent db1c865 commit b898da8

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/System.Management.Automation/engine/ShellVariable.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@ internal void DebuggerCheckVariableWrite()
244244
}
245245
}
246246

247+
/// <summary>
248+
/// Gets the value without triggering debugger check.
249+
/// </summary>
250+
internal virtual object GetValueRaw()
251+
{
252+
return _value;
253+
}
254+
247255
/// <summary>
248256
/// Gets or sets the value of the variable.
249257
/// </summary>
@@ -796,6 +804,11 @@ public override object Value
796804
}
797805
}
798806

807+
internal override object GetValueRaw()
808+
{
809+
return _tuple.GetValue(_tupleSlot);
810+
}
811+
799812
internal override void SetValueRaw(object newValue, bool preserveValueTypeSemantics)
800813
{
801814
if (preserveValueTypeSemantics)

src/System.Management.Automation/engine/runtime/Operations/VariableOps.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ internal static object SetVariableValue(VariablePath variablePath, object value,
4646
: GetAttributeCollection(attributeAsts);
4747
var = new PSVariable(variablePath.UnqualifiedPath, value, ScopedItemOptions.None, attributes);
4848

49+
if (attributes.Count > 0)
50+
{
51+
// When there are any attributes, it's possible the value was converted/transformed.
52+
// Use 'GetValueRaw' here so the debugger check won't be triggered.
53+
value = var.GetValueRaw();
54+
}
55+
4956
// Marking untrusted values for assignments in 'ConstrainedLanguage' mode is done in
5057
// SessionStateScope.SetVariable.
5158
sessionState.SetVariable(variablePath, var, false, origin);
@@ -81,7 +88,7 @@ internal static object SetVariableValue(VariablePath variablePath, object value,
8188
null,
8289
Metadata.InvalidValueFailure,
8390
var.Name,
84-
((value != null) ? value.ToString() : "$null"));
91+
(value != null) ? value.ToString() : "$null");
8592

8693
throw e;
8794
}

test/powershell/Language/Scripting/Scripting.Followup.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ Describe "Scripting.Followup.Tests" -Tags "CI" {
2929
## $arraylist.Clear() should be executed
3030
$arraylist.Count | Should -Be 0
3131
}
32+
33+
## fix https://github.com/PowerShell/PowerShell/issues/17165
34+
It "([bool] `$var = 42) should return the varaible value" {
35+
([bool]$var = 42).GetType().FullName | Should -Be "System.Boolean"
36+
. { ([bool]$var = 42).GetType().FullName } | Should -Be "System.Boolean"
37+
}
3238
}

0 commit comments

Comments
 (0)