diff --git a/src/Compilers/VisualBasic/Portable/Parser/ParseExpression.vb b/src/Compilers/VisualBasic/Portable/Parser/ParseExpression.vb index c29530db61927..3ed53d00f42c2 100644 --- a/src/Compilers/VisualBasic/Portable/Parser/ParseExpression.vb +++ b/src/Compilers/VisualBasic/Portable/Parser/ParseExpression.vb @@ -1310,14 +1310,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ' Lines: 16304 - 16304 ' ParenthesizedArgumentList .Parser::ParseParenthesizedArguments( [ _Inout_ bool& ErrorInConstruct ] ) Friend Function ParseParenthesizedArguments(Optional RedimOrNewParent As Boolean = False, Optional attributeListParent As Boolean = False) As ArgumentListSyntax - Debug.Assert(CurrentToken.Kind = SyntaxKind.OpenParenToken, "should be at tkLParen.") - Dim arguments As CodeAnalysis.Syntax.InternalSyntax.SeparatedSyntaxList(Of ArgumentSyntax) = Nothing Dim openParen As PunctuationSyntax = Nothing Dim closeParen As PunctuationSyntax = Nothing - Debug.Assert(CurrentToken.Kind = SyntaxKind.OpenParenToken) - TryGetTokenAndEatNewLine(SyntaxKind.OpenParenToken, openParen) + ' Try to get the opening parenthesis, create a missing one if not found + If Not TryGetTokenAndEatNewLine(SyntaxKind.OpenParenToken, openParen) Then + openParen = InternalSyntaxFactory.MissingPunctuation(SyntaxKind.OpenParenToken) + openParen = ReportSyntaxError(openParen, ERRID.ERR_ExpectedLparen) + End If Dim unexpected As GreenNode = Nothing arguments = ParseArguments(unexpected, RedimOrNewParent, attributeListParent) diff --git a/src/Compilers/VisualBasic/Test/Syntax/Syntax/SyntaxFactoryTests.vb b/src/Compilers/VisualBasic/Test/Syntax/Syntax/SyntaxFactoryTests.vb index bb08faf2730a6..01213dda49746 100644 --- a/src/Compilers/VisualBasic/Test/Syntax/Syntax/SyntaxFactoryTests.vb +++ b/src/Compilers/VisualBasic/Test/Syntax/Syntax/SyntaxFactoryTests.vb @@ -134,5 +134,30 @@ Integer Dim typeName = SyntaxFactory.ParseTypeName("", options:=parseOptions) Assert.Same(parseOptions, typeName.SyntaxTree.Options) End Sub + + + Public Shared Sub TestParseArgumentListWithoutParentheses() + ' This should not throw NullReferenceException or assertion failure + ' when input lacks parentheses + Dim result = SyntaxFactory.ParseArgumentList("42") + + ' Should still produce a valid ArgumentListSyntax with errors + Assert.NotNull(result) + Assert.True(result.ContainsDiagnostics) + + ' Should have an error for missing opening parenthesis + Dim errors = result.GetDiagnostics().ToArray() + Assert.True(errors.Length > 0, "Expected diagnostic errors for missing parentheses") + + ' Verify that the parse produces a valid tree with missing tokens + Assert.NotNull(result.OpenParenToken) + Assert.True(result.OpenParenToken.IsMissing) + + ' Test with parentheses - should work correctly + Dim resultWithParens = SyntaxFactory.ParseArgumentList("(42)") + Assert.NotNull(resultWithParens) + Assert.Equal("(42)", resultWithParens.ToString()) + Assert.False(resultWithParens.ContainsDiagnostics) + End Sub End Class End Namespace