Skip to content

Commit a16fead

Browse files
iSazonovlzybkr
authored andcommitted
Fix error position reporting on classes (#3103)
Interactive hosts expect an `IncompleteParseException` to signal that more input is expected. When detecting errors, the parser can report 2 positions: * where the error should be reported * where the error was detected Typically these are the same, so most error reporting methods have a single parameter. For missing braces, the pattern is supposed to be to report the error after the opening brace, but the error is typically detected at the end of the file. There were a few places where we were not consistent in reporting such errors, this PR corrects those places.
1 parent ca5fc9c commit a16fead

6 files changed

Lines changed: 16 additions & 12 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3909,7 +3909,7 @@ private StatementAst ClassDefinitionRule(List<AttributeBaseAst> customAttributes
39093909
if (rCurly.Kind != TokenKind.RCurly)
39103910
{
39113911
UngetToken(rCurly);
3912-
ReportIncompleteInput(lastExtent, () => ParserStrings.MissingEndCurlyBrace);
3912+
ReportIncompleteInput(After(lCurly), rCurly.Extent, () => ParserStrings.MissingEndCurlyBrace);
39133913
}
39143914
else
39153915
{
@@ -4269,7 +4269,7 @@ private StatementAst EnumDefinitionRule(List<AttributeBaseAst> customAttributes,
42694269
if (rCurly.Kind != TokenKind.RCurly)
42704270
{
42714271
UngetToken(rCurly);
4272-
ReportIncompleteInput(After(lastExtent), () => ParserStrings.MissingEndCurlyBrace);
4272+
ReportIncompleteInput(After(lCurly), rCurly.Extent, () => ParserStrings.MissingEndCurlyBrace);
42734273
}
42744274

42754275
var startExtent = customAttributes != null && customAttributes.Count > 0
@@ -6344,8 +6344,8 @@ private ExpressionAst HashExpressionRule(Token atCurlyToken, bool parsingSchemaE
63446344
// Note - the error handling function inspects the error message body to extra the ParserStrings property name. It uses this value as the errorid.
63456345
var errorMessageExpression = parsingSchemaElement ?
63466346
(Expression<Func<string>>)(() => ParserStrings.IncompletePropertyAssignmentBlock)
6347-
: (Expression<Func<string>>)(() => ParserStrings.IncompleteHashLiteral);
6348-
ReportIncompleteInput(keyValuePairs.Any() ? After(keyValuePairs.Last().Item2) : After(atCurlyToken), rCurly.Extent, errorMessageExpression);
6347+
: (Expression<Func<string>>)(() => ParserStrings.MissingEndCurlyBrace);
6348+
ReportIncompleteInput(After(atCurlyToken), rCurly.Extent, errorMessageExpression);
63496349
endExtent = Before(rCurly);
63506350
}
63516351
else
@@ -6394,7 +6394,7 @@ private KeyValuePair GetKeyValuePair(bool parsingSchemaElement)
63946394
? (() => (ParserStrings.MissingEqualsInPropertyAssignmentBlock))
63956395
: (Expression<Func<string>>)(() => ParserStrings.MissingEqualsInHashLiteral);
63966396
ReportError(errorExtent, errorMessageExpression);
6397-
SyncOnError(true, TokenKind.RCurly, TokenKind.Semi, TokenKind.NewLine);
6397+
SyncOnError(false, TokenKind.RCurly, TokenKind.Semi, TokenKind.NewLine);
63986398
return new KeyValuePair(key, new ErrorStatementAst(errorExtent));
63996399
}
64006400

src/System.Management.Automation/resources/ParserStrings.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,6 @@ Possible matches are</value>
347347
<data name="MissingExpressionInNamedArgument" xml:space="preserve">
348348
<value>Missing statement after '=' in named argument.</value>
349349
</data>
350-
<data name="IncompleteHashLiteral" xml:space="preserve">
351-
<value>The hash literal was incomplete.</value>
352-
</data>
353350
<data name="MissingPropertyTerminator" xml:space="preserve">
354351
<value>Missing ';' or end-of-line in property definition.</value>
355352
</data>

test/powershell/Language/Classes/Scripting.Classes.BasicParsing.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ Describe 'Positive Parse Properties Tests' -Tags "CI" {
251251
Describe 'Negative Parsing Tests' -Tags "CI" {
252252
ShouldBeParseError 'class' MissingNameAfterKeyword 5
253253
ShouldBeParseError 'class foo' MissingTypeBody 9
254-
ShouldBeParseError 'class foo {' MissingEndCurlyBrace 10
254+
ShouldBeParseError 'class foo {' MissingEndCurlyBrace 11
255255
ShouldBeParseError 'class foo { [int] }' IncompleteMemberDefinition 17
256256
ShouldBeParseError 'class foo { $private: }' InvalidVariableReference 12
257257
ShouldBeParseError 'class foo { [int]$global: }' InvalidVariableReference 17
@@ -307,6 +307,8 @@ Describe 'Negative Parsing Tests' -Tags "CI" {
307307
ShouldBeParseError 'class C { static [int]$i; [void] foo() {$i = 10} }' MissingTypeInStaticPropertyAssignment 40
308308

309309
ShouldBeParseError 'class C : B' MissingTypeBody 11
310+
311+
ShouldBeParseError 'Class foo { q(){} w(){}' MissingEndCurlyBrace 11
310312
}
311313

312314
Describe 'Negative methods Tests' -Tags "CI" {

test/powershell/Language/Classes/scripting.Classes.using.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ New-Object Foo
257257
It "report an error on incomplete using input" {
258258
$err = Get-ParseResults "using module @{ModuleName = 'FooWithManifest'; FooWithManifest = 1." # missing closing bracket
259259
$err.Count | Should Be 2
260-
$err[0].ErrorId | Should Be 'IncompleteHashLiteral'
260+
$err[0].ErrorId | Should Be 'MissingEndCurlyBrace'
261261
$err[1].ErrorId | Should Be 'RequiresModuleInvalid'
262262
}
263263

test/powershell/Language/Classes/scripting.enums.tests.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Describe 'Basic enum errors' -Tags "CI" {
8585
ShouldBeParseError 'enum foo' MissingTypeBody 8
8686
ShouldBeParseError 'enum foo {' MissingEndCurlyBrace 10
8787
ShouldBeParseError 'enum foo { x = }' ExpectedValueExpression 14
88-
ShouldBeParseError 'enum foo { x =' ExpectedValueExpression,MissingEndCurlyBrace 14,14
88+
ShouldBeParseError 'enum foo { x =' ExpectedValueExpression,MissingEndCurlyBrace 14,10
8989
ShouldBeParseError 'enum foo {} enum foo {}' MemberAlreadyDefined 12
9090
ShouldBeParseError 'enum foo { x; x }' MemberAlreadyDefined 14 -SkipAndCheckRuntimeError
9191
ShouldBeParseError 'enum foo { X; x }' MemberAlreadyDefined 14 -SkipAndCheckRuntimeError
@@ -94,4 +94,5 @@ Describe 'Basic enum errors' -Tags "CI" {
9494
ShouldBeParseError 'enum foo { e = [int]::MaxValue + 1 }' EnumeratorValueTooLarge 15 -SkipAndCheckRuntimeError
9595
ShouldBeParseError 'enum foo { e = $foo }' EnumeratorValueMustBeConstant 15 -SkipAndCheckRuntimeError
9696
ShouldBeParseError 'enum foo { e = "hello" }' CannotConvertValue 15 -SkipAndCheckRuntimeError
97+
ShouldBeParseError 'enum foo { a;b;c;' MissingEndCurlyBrace 10
9798
}

test/powershell/Language/Parser/Parsing.Tests.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,8 @@ Describe 'expressions parsing' -Tags "CI" {
299299
ShouldBeParseError '[ref][ref]$x' ReferenceNeedsToBeByItselfInTypeSequence 5
300300
ShouldBeParseError '[int][ref]$x' ReferenceNeedsToBeLastTypeInTypeConversion 5
301301
ShouldBeParseError '[int][ref]$x = 42' ReferenceNeedsToBeByItselfInTypeConstraint 5
302-
}
302+
}
303+
304+
Describe 'Hash Expression parsing' -Tags "CI" {
305+
ShouldBeParseError '@{ a=1;b=2;c=3;' MissingEndCurlyBrace 2
306+
}

0 commit comments

Comments
 (0)