Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/parser/SemanticChecks.cs (C#)

Code before:
                    //   $a = [ordered]10
                    _parser.ReportError(convertExpressionAst.Extent,
                        nameof(ParserStrings.OrderedAttributeOnlyOnHashLiteralNode),
                        ParserStrings.OrderedAttributeOnlyOnHashLiteralNode,
                        convertExpressionAst.Type.TypeName.FullName);
                }

                // Skip any further type checks on [ordered],
                // type resolution failure is costly and unnecessary
                return AstVisitAction.Continue;
            }

            Type targetType = convertExpressionAst.Type.TypeName.GetReflectionType();

            if (typeof(PSReference) == targetType)
            {
                // Check for [ref][ref]
                ExpressionAst child = convertExpressionAst.Child;
                bool multipleRefs = false;
                while (true)
                {
                    var childAttrExpr = child as AttributedExpressionAst;
                    if (childAttrExpr != null)
                    {
                        var childConvert = childAttrExpr as ConvertExpressionAst;
                        if (childConvert != null && !IsOrderedAttribute(childConvert.Type) && typeof(PSReference) == childConvert.Type.TypeName.GetReflectionType())
                        {
                            multipleRefs = true;
                            _parser.ReportError(childConvert.Type.Extent,
                                                nameof(ParserStrings.ReferenceNeedsToBeByItselfInTypeSequence),
                                                ParserStrings.ReferenceNeedsToBeByItselfInTypeSequence);
                        }

                        child = childAttrExpr.Child;
                        continue;
                    }

                    break;
                }

                // Check for [int][ref], but don't add an extra error for [ref][ref].
                var parent = convertExpressionAst.Parent as AttributedExpressionAst;
                while (parent != null)
                {
                    var parentConvert = parent as ConvertExpressionAst;
                    if (parentConvert != null && !multipleRefs)
                    {
                        if (typeof(PSReference) == parentConvert.Type.TypeName.GetReflectionType())
                        {
                            break;
                        }

Review comment (performance):
Let's not make any changes here. We are not trying to optimize all cases where `[ordered]xxx` may appear, but the most common ones, which are the cases of simply `[ordered]<expr>` -- it is already covered above.

Code after:
                        ParserStrings.OrderedAttributeOnlyOnHashLiteralNode,
                        convertExpressionAst.Type.TypeName.FullName);
                }

                // Currently, the type name '[ordered]' is handled specially in PowerShell.
                // When used in a conversion expression, it's only allowed on a hashliteral node, and it's
                // always interpreted as an initializer for a case-insensitive
                // 'System.Collections.Specialized.OrderedDictionary' by the compiler.
                // So, we can return early from here.
                return AstVisitAction.Continue;
            }

            if (typeof(PSReference) == convertExpressionAst.Type.TypeName.GetReflectionType())
            {
                // Check for [ref][ref]
                ExpressionAst child = convertExpressionAst.Child;
                bool multipleRefs = false;
                while (true)
                {
                    var childAttrExpr = child as AttributedExpressionAst;
                    if (childAttrExpr != null)
                    {
                        var childConvert = childAttrExpr as ConvertExpressionAst;
                        if (childConvert != null && typeof(PSReference) == childConvert.Type.TypeName.GetReflectionType())
                        {
                            multipleRefs = true;
                            _parser.ReportError(childConvert.Type.Extent,
                                                nameof(ParserStrings.ReferenceNeedsToBeByItselfInTypeSequence),
                                                ParserStrings.ReferenceNeedsToBeByItselfInTypeSequence);
                        }

                        child = childAttrExpr.Child;
                        continue;
                    }

                    break;
                }

                // Check for [int][ref], but don't add an extra error for [ref][ref].
                var parent = convertExpressionAst.Parent as AttributedExpressionAst;
                while (parent != null)
                {
                    var parentConvert = parent as ConvertExpressionAst;
                    if (parentConvert != null && !multipleRefs)
                    {
                        if (typeof(PSReference) == parentConvert.Type.TypeName.GetReflectionType())
                        {
                            break;
                        }

                        // Don't complain if on the lhs of an assign, there is a different error message, and