提交 3e855782 编写于 作者: G Gen Lu

Fix crash in `EventAssignmentExpression.EventInstance` in VB

Which is caused by invalid code.
上级 993b6248
......@@ -935,6 +935,14 @@ public class MemberReferenceAnalyzer : DiagnosticAnalyzer
DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public static readonly DiagnosticDescriptor InvalidEventDescriptor = new DiagnosticDescriptor(
"InvalidEvent",
"Invalid Event",
"A EventAssignmentExpression with invalid event found.",
"Testing",
DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public static readonly DiagnosticDescriptor HandlerAddedDescriptor = new DiagnosticDescriptor(
"HandlerAdded",
"Handler Added",
......@@ -975,7 +983,14 @@ public class MemberReferenceAnalyzer : DiagnosticAnalyzer
DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(EventReferenceDescriptor, HandlerAddedDescriptor, HandlerRemovedDescriptor, PropertyReferenceDescriptor, FieldReferenceDescriptor, MethodBindingDescriptor);
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(EventReferenceDescriptor,
HandlerAddedDescriptor,
HandlerRemovedDescriptor,
PropertyReferenceDescriptor,
FieldReferenceDescriptor,
MethodBindingDescriptor,
InvalidEventDescriptor);
public sealed override void Initialize(AnalysisContext context)
{
......@@ -991,6 +1006,15 @@ public sealed override void Initialize(AnalysisContext context)
{
IEventAssignmentExpression eventAssignment = (IEventAssignmentExpression)operationContext.Operation;
operationContext.ReportDiagnostic(Diagnostic.Create(eventAssignment.Adds ? HandlerAddedDescriptor : HandlerRemovedDescriptor, operationContext.Operation.Syntax.GetLocation()));
if (eventAssignment.Event == null)
{
if (eventAssignment.EventInstance == null && eventAssignment.IsInvalid)
{
// report inside after checking for null to make sure it does't crash.
operationContext.ReportDiagnostic(Diagnostic.Create(InvalidEventDescriptor, eventAssignment.Syntax.GetLocation()));
}
}
},
OperationKind.EventAssignmentExpression);
......
......@@ -1545,7 +1545,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Public ReadOnly Property EventInstance As IOperation Implements IEventAssignmentExpression.EventInstance
Get
If [Event].IsStatic Then
If [Event] Is Nothing OrElse [Event].IsStatic Then
Return Nothing
End If
......
......@@ -1724,5 +1724,84 @@ End Module
Diagnostic(ForLoopConditionCrashVBTestAnalyzer.ForLoopConditionCrashDescriptor.Id, "Boo").WithLocation(41, 24),
Diagnostic(ForLoopConditionCrashVBTestAnalyzer.ForLoopConditionCrashDescriptor.Id, "10").WithLocation(19, 40))
End Sub
<WorkItem(9012, "https://github.com/dotnet/roslyn/issues/9012")>
<Fact>
Public Sub InvalidEventInstanceVisualBasic()
Dim source = <compilation>
<file name="c.vb">
<![CDATA[
Imports System
Imports System.Collections.Generic
Module Program
Sub Main(args As String())
AddHandler Function(ByVal x) x
End Sub
End Module
Class TestClass
Event TestEvent As Action
Shared Sub Test(receiver As TestClass)
AddHandler receiver?.TestEvent, AddressOf Main
End Sub
Shared Sub Main()
End Sub
End Class
Module Module1
Sub Main()
Dim x = {Iterator sub() yield, new object}
Dim y = {Iterator sub() yield 1, Iterator sub() yield, new object}
Dim z = {Sub() AddHandler, New Object}
g0(Iterator sub() Yield)
g1(Iterator Sub() Yield, 5)
End Sub
Sub g0(ByVal x As Func(Of IEnumerator))
End Sub
Sub g1(ByVal x As Func(Of IEnumerator), ByVal y As Integer)
End Sub
Iterator Function f() As IEnumerator
Yield
End Function
End Module
]]>
</file>
</compilation>
Dim comp = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(source)
comp.VerifyDiagnostics(
Diagnostic(ERRID.ERR_ExpectedComma, "").WithLocation(6, 39),
Diagnostic(ERRID.ERR_ExpectedExpression, "").WithLocation(6, 39),
Diagnostic(ERRID.ERR_ExpectedExpression, "").WithLocation(24, 38),
Diagnostic(ERRID.ERR_ExpectedExpression, "").WithLocation(25, 62),
Diagnostic(ERRID.ERR_ExpectedExpression, "").WithLocation(26, 34),
Diagnostic(ERRID.ERR_ExpectedExpression, "").WithLocation(27, 32),
Diagnostic(ERRID.ERR_ExpectedExpression, "").WithLocation(28, 32),
Diagnostic(ERRID.ERR_ExpectedExpression, "").WithLocation(37, 14),
Diagnostic(ERRID.ERR_TooFewGenericArguments1, "IEnumerator").WithArguments("System.Collections.Generic.IEnumerator(Of Out T)").WithLocation(31, 31),
Diagnostic(ERRID.ERR_TooFewGenericArguments1, "IEnumerator").WithArguments("System.Collections.Generic.IEnumerator(Of Out T)").WithLocation(33, 31),
Diagnostic(ERRID.ERR_TooFewGenericArguments1, "IEnumerator").WithArguments("System.Collections.Generic.IEnumerator(Of Out T)").WithLocation(36, 30),
Diagnostic(ERRID.ERR_AddOrRemoveHandlerEvent, "receiver?.TestEvent").WithLocation(15, 20),
Diagnostic(ERRID.ERR_AddOrRemoveHandlerEvent, "Function(ByVal x) x").WithLocation(6, 20),
Diagnostic(ERRID.ERR_BadIteratorReturn, "sub").WithLocation(24, 27),
Diagnostic(ERRID.ERR_BadIteratorReturn, "sub").WithLocation(25, 27),
Diagnostic(ERRID.ERR_BadIteratorReturn, "sub").WithLocation(25, 51),
Diagnostic(ERRID.ERR_BadIteratorReturn, "sub").WithLocation(27, 21),
Diagnostic(ERRID.ERR_BadIteratorReturn, "Sub").WithLocation(28, 21),
Diagnostic(ERRID.HDN_UnusedImportStatement, "Imports System.Collections.Generic").WithLocation(2, 1))
comp.VerifyAnalyzerDiagnostics({New MemberReferenceAnalyzer}, Nothing, Nothing, False,
Diagnostic(MemberReferenceAnalyzer.HandlerAddedDescriptor.Id, "AddHandler, New Object").WithLocation(26, 24),
Diagnostic(MemberReferenceAnalyzer.InvalidEventDescriptor.Id, "AddHandler, New Object").WithLocation(26, 24),
Diagnostic(MemberReferenceAnalyzer.HandlerAddedDescriptor.Id, "AddHandler receiver?.TestEvent, AddressOf Main").WithLocation(15, 9),
Diagnostic(MemberReferenceAnalyzer.InvalidEventDescriptor.Id, "AddHandler receiver?.TestEvent, AddressOf Main").WithLocation(15, 9),
Diagnostic(MemberReferenceAnalyzer.HandlerAddedDescriptor.Id, "AddHandler Function(ByVal x) x").WithLocation(6, 9),
Diagnostic(MemberReferenceAnalyzer.InvalidEventDescriptor.Id, "AddHandler Function(ByVal x) x").WithLocation(6, 9))
End Sub
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册