Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/System.Management.Automation/engine/parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,25 @@ private ITypeName CompleteArrayTypeName(ITypeName elementType, TypeName typeForA
token = NextToken();
} while (token.Kind == TokenKind.Comma);

// The dimensions for an array must be less than or equal to 32.
// Search the doc for 'Type.MakeArrayType(int rank)' for more details.
if (dim > 32)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we get better UX in PSRL if we move the error report in cycle above?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. We'd need to be very careful to ensure we continue to consume , tokens until we hit a ] or something unexpected if we want to do that.

This way is a bit simpler to follow, and I think the only UX difference we'd really get would be that maybe we'd be able to pinpoint exactly the point at which there are too many commas, rather than reporting an error for the whole type token.

I don't know if that really gives much to aid the user, though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no difference in UX in PSRL. PSRL will just change the last token of the user prompt to red color (not always, depending on the user's custom prompt function) in the event of a parsing error.

maybe we'd be able to pinpoint exactly the point at which there are too many commas, rather than reporting an error for the whole type token.

I think it's clearer to give user the exact number of dimensions the array is declared with. Moving the error report in the loop will only detect that it's more than 32 now, but won't get you the exact number of dimensions.

{
// If the next token is right bracket, we swallow it to make it easier to parse the rest of script.
// Otherwise, we unget the token for the subsequent parsing to consume.
if (token.Kind != TokenKind.RBracket)
{
UngetToken(token);
}

ReportError(
ExtentOf(firstTokenAfterLBracket, lastComma),
nameof(ParserStrings.ArrayHasTooManyDimensions),
ParserStrings.ArrayHasTooManyDimensions,
arg: dim);
break;
}

if (token.Kind != TokenKind.RBracket)
{
// ErrorRecovery: just pretend we saw a ']'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ Possible matches are</value>
<data name="CannotIndexWithByRefLikeReturnType" xml:space="preserve">
<value>Unable to index into an object of type "{0}" with the ByRef-like return type "{1}". ByRef-like types are not supported in PowerShell.</value>
</data>
<data name="ArrayHasTooManyDimensions" xml:space="preserve">
<value>The array has too many dimensions: {0}. The number of dimensions for an array must be less than or equal to 32.</value>
</data>
<data name="ArrayAssignmentFailed" xml:space="preserve">
<value>Array assignment to [{0}] failed: {1}.</value>
</data>
Expand Down
18 changes: 18 additions & 0 deletions test/powershell/Language/Parser/Parsing.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,21 @@ Describe "Keywords 'default', 'hidden', 'in', 'static' Token parsing" -Tags CI {
. $Keyword | Should -BeExactly $Keyword
}
}

Describe "Parsing array that has too many dimensions" -Tag CI {
It "ParseError for '<Script>'" -TestCases @(
@{ Script = '[int[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,'; ErrorId = @('ArrayHasTooManyDimensions', 'EndSquareBracketExpectedAtEndOfAttribute'); StartOffset = @(5, 37); EndOffset = @(37, 37) }
@{ Script = '[int[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]'; ErrorId = @('ArrayHasTooManyDimensions', 'EndSquareBracketExpectedAtEndOfAttribute'); StartOffset = @(5, 38); EndOffset = @(37, 38) }
@{ Script = '[int[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]]'; ErrorId = @('ArrayHasTooManyDimensions'); StartOffset = @(5); EndOffset = @(37) }
) {
param($Script, $ErrorId, $StartOffset, $EndOffset)

$errs = Get-ParseResults -src $Script
$errs.Count | Should -Be $ErrorId.Count
for ($i = 0; $i -lt $errs.Count; $i++) {
$errs[$i].ErrorId | Should -BeExactly $ErrorId[$i]
$errs[$i].Extent.StartScriptPosition.Offset | Should -Be $StartOffset[$i]
$errs[$i].Extent.EndScriptPosition.Offset | Should -Be $EndOffset[$i]
}
}
}