Skip to content

Commit 137a37b

Browse files
Change Null coalescing assigned operator to ??=
1 parent cfd73f9 commit 137a37b

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed

src/System.Management.Automation/engine/parser/Compiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ internal Expression ReduceAssignment(ISupportsAssignment left, TokenKind tokenKi
794794
case TokenKind.MultiplyEquals: et = ExpressionType.Multiply; break;
795795
case TokenKind.DivideEquals: et = ExpressionType.Divide; break;
796796
case TokenKind.RemainderEquals: et = ExpressionType.Modulo; break;
797-
case TokenKind.QuestionEquals: return GetCoalesceExpression(right, av, tokenKind);
797+
case TokenKind.QuestionQuestionEquals: return GetCoalesceExpression(right, av, tokenKind);
798798
}
799799

800800
var exprs = new List<Expression>();
@@ -810,7 +810,7 @@ private Expression GetCoalesceExpression(Expression rhs, IAssignableValue leftAs
810810
var temps = new List<ParameterExpression>();
811811
var leftExpr = leftAssignableValue.GetValue(this, exprs, temps);
812812

813-
if (tokenKind == TokenKind.QuestionEquals)
813+
if (tokenKind == TokenKind.QuestionQuestionEquals)
814814
{
815815
exprs.Add(leftAssignableValue.SetValue(this, Expression.MakeBinary(ExpressionType.Coalesce,leftExpr,rhs)));
816816
return Expression.Block(temps, exprs);

src/System.Management.Automation/engine/parser/token.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ public enum TokenKind
417417
QuestionMark = 100,
418418

419419
/// <summary>The null conditional assignment operator '?='.</summary>
420-
QuestionEquals = 101,
420+
QuestionQuestionEquals = 101,
421421

422422
/// <summary>The null coalesce operator '??'.</summary>
423423
QuestionQuestion = 102,
@@ -860,7 +860,7 @@ public static class TokenTraits
860860
/* Shr */ TokenFlags.BinaryOperator | TokenFlags.BinaryPrecedenceComparison | TokenFlags.CanConstantFold,
861861
/* Colon */ TokenFlags.SpecialOperator | TokenFlags.DisallowedInRestrictedMode,
862862
/* QuestionMark */ TokenFlags.TernaryOperator | TokenFlags.DisallowedInRestrictedMode,
863-
/* QuestionEquals */ TokenFlags.AssignmentOperator,
863+
/* QuestionQuestionEquals */ TokenFlags.AssignmentOperator,
864864
/* QuestionQuestion */ TokenFlags.BinaryOperator,
865865
/* Reserved slot 5 */ TokenFlags.None,
866866
/* Reserved slot 6 */ TokenFlags.None,
@@ -1057,7 +1057,7 @@ public static class TokenTraits
10571057
/* Shl */ "-shl",
10581058
/* Shr */ "-shr",
10591059
/* Colon */ ":",
1060-
/* QuestionEquals */ "?=",
1060+
/* QuestionQuestionEquals */ "??=",
10611061
/* QuestionQuestion */ "??",
10621062
/* Reserved slot 4 */ string.Empty,
10631063
/* Reserved slot 5 */ string.Empty,

src/System.Management.Automation/engine/parser/tokenizer.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4996,18 +4996,25 @@ internal Token NextToken()
49964996
case '?' when InExpressionMode():
49974997
c1 = PeekChar();
49984998

4999-
if (c1 == '=')
5000-
{
5001-
SkipChar();
5002-
return CheckOperatorInCommandMode(c, c1, TokenKind.QuestionEquals);
5003-
}
5004-
50054999
if (c1 == '?')
50065000
{
50075001
SkipChar();
5002+
5003+
c1 = PeekChar();
5004+
5005+
if (c1 == '=')
5006+
{
5007+
SkipChar();
5008+
return CheckOperatorInCommandMode(c, c1, TokenKind.QuestionQuestionEquals);
5009+
}
5010+
50085011
return CheckOperatorInCommandMode(c, c1, TokenKind.QuestionQuestion);
50095012
}
50105013

5014+
5015+
5016+
5017+
50115018
return this.NewToken(TokenKind.QuestionMark);
50125019

50135020
case '\0':

test/powershell/Language/Operators/NullConditional.Tests.ps1

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Describe 'NullConditionalOperations' -tag 'CI' {
55

6-
Context "Null conditional assignment operator ?=" {
6+
Context "Null conditional assignment operator ??=" {
77
BeforeAll {
88
$someGuid = New-Guid
99

@@ -21,43 +21,43 @@ Describe 'NullConditionalOperations' -tag 'CI' {
2121
It 'Variable doesnot exist' {
2222
Remove-Variable variableDoesNotExist -ErrorAction SilentlyContinue -Force
2323

24-
$variableDoesNotExist ?= 1
24+
$variableDoesNotExist ??= 1
2525
$variableDoesNotExist | Should -Be 1
2626

27-
$variableDoesNotExist ?= 2
27+
$variableDoesNotExist ??= 2
2828
$variableDoesNotExist | Should -Be 1
2929
}
3030

3131
It 'Variable exists and is null' {
3232
$variableDoesNotExist = $null
3333

34-
$variableDoesNotExist ?= 2
34+
$variableDoesNotExist ??= 2
3535
$variableDoesNotExist | Should -Be 2
3636
}
3737

3838
It 'Validate types - <name> can be set' -TestCases $typesTests {
3939
param ($name, $valueToSet)
4040

4141
$x = $null
42-
$x ?= $valueToSet
42+
$x ??= $valueToSet
4343
$x | Should -Be $valueToSet
4444
}
4545

4646
It 'Validate hashtable can be set' {
4747
$x = $null
48-
$x ?= @{ 1 = '1' }
48+
$x ??= @{ 1 = '1' }
4949
$x.Keys | Should -Be @(1)
5050
}
5151

5252
It 'Validate lhs is returned' {
5353
$x = 100
54-
$x ?= 200
54+
$x ??= 200
5555
$x | Should -Be 100
5656
}
5757

5858
It 'Error case' {
5959
$e = $null
60-
$null = [System.Management.Automation.Language.Parser]::ParseInput('1 ?= 100', [ref] $null, [ref] $e)
60+
$null = [System.Management.Automation.Language.Parser]::ParseInput('1 ??= 100', [ref] $null, [ref] $e)
6161
$e[0].ErrorId | Should -BeExactly 'InvalidLeftHandSide'
6262
}
6363
}
@@ -102,11 +102,11 @@ Describe 'NullConditionalOperations' -tag 'CI' {
102102

103103
Context 'Combined usage of null conditional operators' {
104104

105-
It '?? and ?= used together' {
106-
$x ?= 100 ?? 200
105+
It '?? and ??= used together' {
106+
$x ??= 100 ?? 200
107107
$x | Should -Be 100
108108

109-
$y ?= 100 ?? 200
109+
$y ??= 100 ?? 200
110110
$y | Should -Be 100
111111
}
112112
}

0 commit comments

Comments
 (0)