提交 2c47cc06 编写于 作者: A AlekseyTs 提交者: GitHub

Merge pull request #14434 from AlekseyTs/Issue14364_rc

SemanticModel shouldn’t expose ParameterSymbols parented to an EventSymbol
......@@ -394,7 +394,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Private Sub CheckEventTypeCompliance(symbol As EventSymbol)
Dim type = symbol.Type
If type.TypeKind = TypeKind.Delegate AndAlso type.IsImplicitlyDeclared Then
If type.TypeKind = TypeKind.Delegate AndAlso type.IsImplicitlyDeclared AndAlso TryCast(type, NamedTypeSymbol)?.AssociatedSymbol Is symbol Then
Debug.Assert(symbol.DelegateReturnType.SpecialType = SpecialType.System_Void)
CheckParameterCompliance(symbol.DelegateParameters, symbol.ContainingType)
ElseIf ShouldReportNonCompliantType(type, symbol.ContainingType, symbol) Then
......
......@@ -1063,7 +1063,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Case SymbolKind.Method
Return GetParameterSymbol(DirectCast(symbol, MethodSymbol).Parameters, parameter)
Case SymbolKind.Event
Return GetParameterSymbol(DirectCast(symbol, EventSymbol).DelegateParameters, parameter)
Dim eventSymbol As EventSymbol = DirectCast(symbol, EventSymbol)
Dim type = TryCast(eventSymbol.Type, NamedTypeSymbol)
If type?.AssociatedSymbol Is eventSymbol Then
Return GetParameterSymbol(type.DelegateInvokeMethod.Parameters, parameter)
End If
Return Nothing
Case SymbolKind.Property
Return GetParameterSymbol(DirectCast(symbol, PropertySymbol).Parameters, parameter)
Case SymbolKind.NamedType
......
......@@ -128,10 +128,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Dim sourceSymbol = TryCast(symbol, SourceEventSymbol)
If sourceSymbol IsNot Nothing AndAlso sourceSymbol.IsTypeInferred Then
If format.MemberOptions.IncludesOption(SymbolDisplayMemberOptions.IncludeParameters) Then
Dim invoke = DirectCast(sourceSymbol.Type, SynthesizedEventDelegateSymbol).DelegateInvokeMethod
AddPunctuation(SyntaxKind.OpenParenToken)
AddParametersIfRequired(isExtensionMethod:=False, parameters:=StaticCast(Of IParameterSymbol).From(invoke.Parameters))
AddParametersIfRequired(isExtensionMethod:=False, parameters:=StaticCast(Of IParameterSymbol).From(sourceSymbol.DelegateParameters))
AddPunctuation(SyntaxKind.CloseParenToken)
End If
End If
......
......@@ -216,9 +216,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Dim types = _containingType.GetTypeMembers(Me.Name & EVENT_DELEGATE_SUFFIX)
Debug.Assert(Not types.IsDefault)
If Not types.IsEmpty Then
type = types(0)
Else
type = Nothing
For Each candidate In types
If candidate.AssociatedSymbol Is Me Then
type = candidate
Exit For
End If
Next
If type Is Nothing Then
' if we still do not know the type, get a temporary one (it is not a member of the containing class)
type = New SynthesizedEventDelegateSymbol(Me._syntaxRef, _containingType)
End If
......
......@@ -103,7 +103,17 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Public NotOverridable Overrides ReadOnly Property IsImplicitlyDeclared As Boolean
Get
Return (GetMatchingPropertyParameter() IsNot Nothing) OrElse (Me.ContainingSymbol.IsImplicitlyDeclared)
If Me.ContainingSymbol.IsImplicitlyDeclared Then
If TryCast(Me.ContainingSymbol, MethodSymbol)?.MethodKind = MethodKind.DelegateInvoke AndAlso
Not Me.ContainingType.AssociatedSymbol?.IsImplicitlyDeclared Then
Return False
End If
Return True
End If
Return (GetMatchingPropertyParameter() IsNot Nothing)
End Get
End Property
......
......@@ -1324,7 +1324,8 @@ BC30001: Statement is not valid in a namespace.
Assert.NotNull(paramSymbol1)
Assert.Equal("Percent", paramSymbol1.Name)
Assert.Equal("System.Single", paramSymbol1.Type.ToTestDisplayString())
Assert.Equal("Event N1.Test.Percent(Percent As System.Single)", paramSymbol1.ContainingSymbol.ToTestDisplayString())
Assert.Equal("Event N1.Test.Percent(Percent As System.Single)", paramSymbol1.ContainingType.AssociatedSymbol.ToTestDisplayString())
Assert.Equal("Sub N1.Test.PercentEventHandler.Invoke(Percent As System.Single)", paramSymbol1.ContainingSymbol.ToTestDisplayString())
End Sub
......
......@@ -2240,5 +2240,168 @@ BC30590: Event 'E' cannot be found.
</expected>)
End Sub
<WorkItem(14364, "https://github.com/dotnet/roslyn/issues/14364")>
<Fact()>
Public Sub SemanticModelOnParameters_01()
Dim compilation = CompilationUtils.CreateCompilationWithMscorlib(
<compilation>
<file name="a.vb"><![CDATA[
Class A
Public Event E1(x As Integer)
End Class]]></file>
</compilation>, options:=TestOptions.DebugDll)
compilation.AssertTheseDiagnostics(<expected></expected>)
Dim tree = compilation.SyntaxTrees.Single()
Dim x = tree.GetRoot().DescendantNodes().OfType(Of ParameterSyntax)().Single().Identifier
Dim model = compilation.GetSemanticModel(tree)
Dim xSym = model.GetDeclaredSymbol(x)
Assert.Equal("x As System.Int32", xSym.ToTestDisplayString())
Assert.False(xSym.IsImplicitlyDeclared)
Assert.Equal("x As Integer", xSym.DeclaringSyntaxReferences.Single().GetSyntax().ToString())
Dim e1EventHandler = compilation.GetTypeByMetadataName("A+E1EventHandler").DelegateInvokeMethod
Assert.Same(e1EventHandler.ContainingType, DirectCast(e1EventHandler.ContainingType.AssociatedSymbol, EventSymbol).Type)
Assert.True(e1EventHandler.IsImplicitlyDeclared)
Assert.True(e1EventHandler.ContainingType.IsImplicitlyDeclared)
Assert.Same(e1EventHandler, xSym.ContainingSymbol)
Assert.Same(xSym, e1EventHandler.Parameters.First())
End Sub
<WorkItem(14364, "https://github.com/dotnet/roslyn/issues/14364")>
<Fact()>
Public Sub SemanticModelOnParameters_02()
Dim compilation = CompilationUtils.CreateCompilationWithMscorlib(
<compilation>
<file name="a.vb"><![CDATA[
Interface I1
Delegate Sub D (z As Integer)
Event E1 As D
End Interface
Class A
Implements I1
Public Event E1(x As Integer) Implements I1.E1
End Class]]></file>
</compilation>, options:=TestOptions.DebugDll)
compilation.AssertTheseDiagnostics(<expected></expected>)
Dim a = compilation.GetTypeByMetadataName("A")
Dim e1 = a.GetMember(Of EventSymbol)("E1")
Assert.Equal("I1.D", e1.Type.ToTestDisplayString())
Dim tree = compilation.SyntaxTrees.Single()
Dim x = tree.GetRoot().DescendantNodes().OfType(Of ParameterSyntax)().ElementAt(1).Identifier
Dim model = compilation.GetSemanticModel(tree)
Dim xSym = model.GetDeclaredSymbol(x)
Assert.Null(xSym)
End Sub
<WorkItem(14364, "https://github.com/dotnet/roslyn/issues/14364")>
<Fact()>
Public Sub SemanticModelOnParameters_03()
Dim compilation = CompilationUtils.CreateCompilationWithMscorlib(
<compilation>
<file name="a.vb"><![CDATA[
Interface I1
End Interface
Class A
Implements I1
Public Event E1(x As Integer) Implements I1.E1
End Class]]></file>
</compilation>, options:=TestOptions.DebugDll)
compilation.AssertTheseDiagnostics(
<expected>
BC30401: 'E1' cannot implement 'E1' because there is no matching event on interface 'I1'.
Public Event E1(x As Integer) Implements I1.E1
~~~~~
</expected>)
Dim a = compilation.GetTypeByMetadataName("A")
Dim e1 = a.GetMember(Of EventSymbol)("E1")
Assert.Equal("Event A.E1(x As System.Int32)", e1.ToTestDisplayString())
Assert.Equal("A.E1EventHandler", e1.Type.ToTestDisplayString())
Assert.True(e1.Type.IsDelegateType())
Assert.DoesNotContain(e1.Type, a.GetMembers())
Assert.Same(a, e1.Type.ContainingType)
Assert.True(e1.Type.IsImplicitlyDeclared)
Dim tree = compilation.SyntaxTrees.Single()
Dim x = tree.GetRoot().DescendantNodes().OfType(Of ParameterSyntax)().Single().Identifier
Dim model = compilation.GetSemanticModel(tree)
Dim xSym = model.GetDeclaredSymbol(x)
Assert.Equal("x As System.Int32", xSym.ToTestDisplayString())
Assert.False(xSym.IsImplicitlyDeclared)
Assert.Equal("x As Integer", xSym.DeclaringSyntaxReferences.Single().GetSyntax().ToString())
Assert.True(xSym.ContainingSymbol.IsImplicitlyDeclared)
Assert.Same(e1.Type, xSym.ContainingType)
Assert.Same(xSym, xSym.ContainingType.DelegateInvokeMethod.Parameters.First())
End Sub
<WorkItem(14364, "https://github.com/dotnet/roslyn/issues/14364")>
<Fact()>
Public Sub SemanticModelOnParameters_04()
Dim compilation = CompilationUtils.CreateCompilationWithMscorlib(
<compilation>
<file name="a.vb"><![CDATA[
Interface I1
End Interface
Class A
Implements I1
Public Event E1(x As Integer) Implements I1.E1
class E1EventHandler
End Class
End Class]]></file>
</compilation>, options:=TestOptions.DebugDll)
compilation.AssertTheseDiagnostics(
<expected>
BC30401: 'E1' cannot implement 'E1' because there is no matching event on interface 'I1'.
Public Event E1(x As Integer) Implements I1.E1
~~~~~
</expected>)
Dim e1EventHandler = compilation.GetTypeByMetadataName("A+E1EventHandler")
Assert.False(e1EventHandler.IsImplicitlyDeclared)
Assert.Equal("A.E1EventHandler", e1EventHandler.ToTestDisplayString())
Assert.Null(e1EventHandler.AssociatedSymbol)
Dim a = compilation.GetTypeByMetadataName("A")
Dim e1 = a.GetMember(Of EventSymbol)("E1")
Assert.Equal("Event A.E1(x As System.Int32)", e1.ToTestDisplayString())
Assert.Equal("A.E1EventHandler", e1.Type.ToTestDisplayString())
Assert.True(e1.Type.IsDelegateType())
Assert.DoesNotContain(e1.Type, a.GetMembers())
Assert.Same(a, e1.Type.ContainingType)
Assert.True(e1.Type.IsImplicitlyDeclared)
Dim tree = compilation.SyntaxTrees.Single()
Dim x = tree.GetRoot().DescendantNodes().OfType(Of ParameterSyntax)().Single().Identifier
Dim model = compilation.GetSemanticModel(tree)
Dim xSym = model.GetDeclaredSymbol(x)
Assert.Equal("x As System.Int32", xSym.ToTestDisplayString())
Assert.False(xSym.IsImplicitlyDeclared)
Assert.Equal("x As Integer", xSym.DeclaringSyntaxReferences.Single().GetSyntax().ToString())
Assert.True(xSym.ContainingSymbol.IsImplicitlyDeclared)
Assert.Same(e1.Type, xSym.ContainingType)
Assert.Same(xSym, xSym.ContainingType.DelegateInvokeMethod.Parameters.First())
End Sub
End Class
End Namespace
......@@ -183,7 +183,7 @@ End Class
End Function
<WorkItem(553324, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/553324")>
<WpfFact, Trait(Traits.Feature, Traits.Features.FindReferences)>
<WpfFact(Skip:="https://github.com/dotnet/roslyn/issues/14428"), Trait(Traits.Feature, Traits.Features.FindReferences)>
Public Async Function TestEventParameterCascading() As Task
Dim input =
<Workspace>
......
......@@ -50,7 +50,8 @@ public class B { };
TestRoundTrip(GetDeclaredSymbols(compilation), compilation);
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/14364")]
[Fact]
[WorkItem(14364, "https://github.com/dotnet/roslyn/issues/14364")]
public void TestVBParameterizedEvent()
{
var source = @"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册