File tree 2 files changed +34
-0
lines changed
src/System.Management.Automation/engine/parser
test/powershell/Language/Scripting
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -920,6 +920,13 @@ public override AstVisitAction VisitConvertExpression(ConvertExpressionAst conve
920
920
ParserStrings . OrderedAttributeOnlyOnHashLiteralNode ,
921
921
convertExpressionAst . Type . TypeName . FullName ) ;
922
922
}
923
+
924
+ // Currently, the type name '[ordered]' is handled specially in PowerShell.
925
+ // When used in a conversion expression, it's only allowed on a hashliteral node, and it's
926
+ // always interpreted as an initializer for a case-insensitive
927
+ // 'System.Collections.Specialized.OrderedDictionary' by the compiler.
928
+ // So, we can return early from here.
929
+ return AstVisitAction . Continue ;
923
930
}
924
931
925
932
if ( typeof ( PSReference ) == convertExpressionAst . Type . TypeName . GetReflectionType ( ) )
Original file line number Diff line number Diff line change @@ -41,4 +41,31 @@ Describe "Scripting.Followup.Tests" -Tags "CI" {
41
41
$obj.ForEach (' p' , 32 ) | Should - BeNullOrEmpty
42
42
$obj.p | Should - Be 32
43
43
}
44
+
45
+ It " Test the special type name 'ordered'" {
46
+ class ordered {
47
+ [hashtable ] $Member
48
+ ordered([hashtable ] $hash ) {
49
+ $this.Member = $hash
50
+ }
51
+ }
52
+
53
+ # # `<expr> -as\-is [ordered]` resolves 'ordered' as a normal type name.
54
+ $hash = @ { key = 2 }
55
+ $result = $hash -as [ordered ]
56
+ $result.GetType ().FullName | Should - BeExactly ([ordered ].FullName)
57
+ $result -is [ordered ] | Should - BeTrue
58
+ $result.Member [' key' ] | Should - Be 2
59
+ $result.Member.Count | Should - Be 1
60
+
61
+ # # `[ordered]$hash` causes parsing error.
62
+ $err = $null
63
+ $null = [System.Management.Automation.Language.Parser ]::ParseInput(' [ordered]$hash' , [ref ]$null , [ref ]$err )
64
+ $err.Count | Should - Be 1
65
+ $err [0 ].ErrorId | Should - BeExactly ' OrderedAttributeOnlyOnHashLiteralNode'
66
+
67
+ # # `[ordered]@{ key = 1 }` creates 'OrderedDictionary'
68
+ $result = [ordered ]@ { key = 1 }
69
+ $result | Should - BeOfType ' System.Collections.Specialized.OrderedDictionary'
70
+ }
44
71
}
You can’t perform that action at this time.
0 commit comments