提交 7e1a61be 编写于 作者: A AlekseyTs

VB: Prevent SemanticModel from crashing on an invalid syntax used as a type parameter. Fixes #436.

***NO_CI***
 (changeset 1386442)
上级 3c135233
......@@ -293,7 +293,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End If
Case Else
Throw ExceptionUtilities.UnexpectedValue(typeSyntax.Kind)
' An error case
End Select
Next
End If
......@@ -309,11 +309,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Dim parent As VisualBasicSyntaxNode = name.Parent
' Type parameter
If name.Kind = SyntaxKind.IdentifierName AndAlso parent IsNot Nothing AndAlso parent.Kind = SyntaxKind.TypeArgumentList Then
Dim identifier = DirectCast(name, IdentifierNameSyntax)
If parent IsNot Nothing AndAlso parent.Kind = SyntaxKind.TypeArgumentList Then
Dim ordinal As Integer = DirectCast(parent, TypeArgumentListSyntax).Arguments.IndexOf(name)
Return ImmutableArray.Create(Of Symbol)(
New CrefTypeParameterSymbol(ordinal, identifier.Identifier.ValueText, identifier))
If name.Kind = SyntaxKind.IdentifierName Then
Dim identifier = DirectCast(name, IdentifierNameSyntax)
Return ImmutableArray.Create(Of Symbol)(New CrefTypeParameterSymbol(ordinal, identifier.Identifier.ValueText, identifier))
End If
' An error case
Return ImmutableArray.Create(Of Symbol)(New CrefTypeParameterSymbol(ordinal, StringConstants.NamedSymbolErrorName, name))
End If
' Names considered to be checked for color-color case are Identifier or Generic names which
......@@ -835,7 +840,9 @@ lAgain:
typeParameterSymbols(i) = created
Case Else
Throw ExceptionUtilities.UnexpectedValue(typeSyntax.Kind)
' An error case
created = New CrefTypeParameterSymbol(i, StringConstants.NamedSymbolErrorName, typeSyntax)
typeParameterSymbols(i) = created
End Select
typeParameters(created.Name) = created
......
......@@ -25,7 +25,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Private ReadOnly _name As String
Private ReadOnly _syntaxReference As SyntaxReference
Public Sub New(ordinal As Integer, name As String, syntax As IdentifierNameSyntax)
Public Sub New(ordinal As Integer, name As String, syntax As TypeSyntax)
Me._ordinal = ordinal
Me._name = name
Me._syntaxReference = syntax.GetReference()
......
......@@ -12090,5 +12090,125 @@ xmlDoc)
End Sub
#End Region
<WorkItem(1087447, "DevDiv"), WorkItem(436, "CodePlex")>
<Fact>
Public Sub Bug1087447_01()
Dim compilation = CompileCheckDiagnosticsAndXmlDocument(
<compilation name="EmptyCref">
<file name="a.vb">
<![CDATA[
''' <summary>
''' <see cref="C(Of Integer).f()"/>
''' </summary>
Class C(Of T)
Sub f()
End Sub
End Class
]]>
</file>
</compilation>,
<error><![CDATA[
BC42309: XML comment has a tag with a 'cref' attribute 'C(Of Integer).f()' that could not be resolved.
''' <see cref="C(Of Integer).f()"/>
~~~~~~~~~~~~~~~~~~~~~~~~
]]></error>,
<xml>
<![CDATA[
<?xml version="1.0"?>
<doc>
<assembly>
<name>
EmptyCref
</name>
</assembly>
<members>
<member name="T:C`1">
<summary>
<see cref="!:C(Of Integer).f()"/>
</summary>
</member>
</members>
</doc>
]]>
</xml>)
Dim tree = compilation.SyntaxTrees(0)
Dim model = compilation.GetSemanticModel(tree)
Dim node1 = tree.GetRoot().DescendantNodes(descendIntoTrivia:=True).OfType(Of IdentifierNameSyntax)().Where(Function(n) n.Identifier.ValueText = "f").Single()
Dim symbolInfo1 = model.GetSymbolInfo(node1.Parent)
Assert.Equal("Sub C(Of ?).f()", symbolInfo1.Symbol.ToTestDisplayString())
Dim node = tree.GetRoot().DescendantNodes(descendIntoTrivia:=True).OfType(Of TypeSyntax)().Where(Function(n) n.ToString() = "Integer").Single()
Dim symbolInfo = model.GetSymbolInfo(node)
Assert.Equal("?", symbolInfo.Symbol.ToTestDisplayString())
End Sub
<WorkItem(1087447, "DevDiv"), WorkItem(436, "CodePlex")>
<Fact>
Public Sub Bug1087447_02()
Dim compilation = CompileCheckDiagnosticsAndXmlDocument(
<compilation name="EmptyCref">
<file name="a.vb">
<![CDATA[
''' <summary>
''' <see cref="C(Of System.Int32).f()"/>
''' </summary>
Class C(Of T)
Sub f()
End Sub
End Class
]]>
</file>
</compilation>,
<error><![CDATA[
BC42309: XML comment has a tag with a 'cref' attribute 'C(Of System.Int32).f()' that could not be resolved.
''' <see cref="C(Of System.Int32).f()"/>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
]]></error>,
<xml>
<![CDATA[
<?xml version="1.0"?>
<doc>
<assembly>
<name>
EmptyCref
</name>
</assembly>
<members>
<member name="T:C`1">
<summary>
<see cref="!:C(Of System.Int32).f()"/>
</summary>
</member>
</members>
</doc>
]]>
</xml>)
Dim tree = compilation.SyntaxTrees(0)
Dim model = compilation.GetSemanticModel(tree)
Dim node1 = tree.GetRoot().DescendantNodes(descendIntoTrivia:=True).OfType(Of IdentifierNameSyntax)().Where(Function(n) n.Identifier.ValueText = "f").Single()
Dim symbolInfo1 = model.GetSymbolInfo(node1.Parent)
Assert.Equal("Sub C(Of ?).f()", symbolInfo1.Symbol.ToTestDisplayString())
Dim node = tree.GetRoot().DescendantNodes(descendIntoTrivia:=True).OfType(Of TypeSyntax)().Where(Function(n) n.ToString() = "System.Int32").Single()
Dim symbolInfo = model.GetSymbolInfo(node)
Assert.Equal("?", symbolInfo.Symbol.ToTestDisplayString())
End Sub
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册