提交 5a22f4bf 编写于 作者: C Charles Stoner

Check accessor before associated symbol

上级 0de01e0f
......@@ -6510,40 +6510,101 @@ internal sealed class C1 : I1
[Fact]
public void TestObsoleteAndPropertyAccessors()
{
var source =
var source0 =
@"using System;
[Obsolete] class A { }
[Obsolete] class B { }
class C
namespace Windows.Foundation.Metadata
{
public sealed class DeprecatedAttribute : Attribute
{
public DeprecatedAttribute(System.String message, DeprecationType type, System.UInt32 version)
{
}
}
public enum DeprecationType
{
Deprecate = 0,
Remove = 1
}
}";
var source1 =
@"using Windows.Foundation.Metadata;
[Deprecated(null, DeprecationType.Deprecate, 0)] class A { }
[Deprecated(null, DeprecationType.Deprecate, 0)] class B { }
[Deprecated(null, DeprecationType.Deprecate, 0)] class C { }
class D
{
object P { get { return new A(); } }
[Obsolete] object Q { get { return new B(); } }
[Deprecated(null, DeprecationType.Deprecate, 0)] object Q { get { return new B(); } }
object R { [Deprecated(null, DeprecationType.Deprecate, 0)] get { return new C(); } }
}";
var comp = CreateCompilationWithMscorlibAndSystemCore(source);
var comp = CreateStandardCompilation(new[] { Parse(source0), Parse(source1) });
comp.VerifyDiagnostics(
// (6,33): warning CS0612: 'A' is obsolete
// (9,17): error CS1667: Attribute 'Windows.Foundation.Metadata.DeprecatedAttribute' is not valid on property or event accessors. It is only valid on 'assembly, module, class, struct, enum, constructor, method, property, indexer, field, event, interface, parameter, delegate, return, type parameter' declarations.
// object R { [Deprecated(null, DeprecationType.Deprecate, 0)] get { return new C(); } }
Diagnostic(ErrorCode.ERR_AttributeNotOnAccessor, "Deprecated").WithArguments("Windows.Foundation.Metadata.DeprecatedAttribute", "assembly, module, class, struct, enum, constructor, method, property, indexer, field, event, interface, parameter, delegate, return, type parameter").WithLocation(9, 17),
// (7,33): warning CS0612: 'A' is obsolete
// object P { get { return new A(); } }
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "A").WithArguments("A").WithLocation(6, 33));
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "A").WithArguments("A").WithLocation(7, 33),
// (9,82): warning CS0612: 'C' is obsolete
// object R { [Deprecated(null, DeprecationType.Deprecate, 0)] get { return new C(); } }
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "C").WithArguments("C").WithLocation(9, 82));
}
[Fact]
public void TestObsoleteAndEventAccessors()
{
var source =
var source0 =
@"using System;
[Obsolete] class A { }
[Obsolete] class B { }
class C
namespace Windows.Foundation.Metadata
{
public sealed class DeprecatedAttribute : Attribute
{
public DeprecatedAttribute(System.String message, DeprecationType type, System.UInt32 version)
{
}
}
public enum DeprecationType
{
Deprecate = 0,
Remove = 1
}
}";
var source1 =
@"using System;
using Windows.Foundation.Metadata;
[Deprecated(null, DeprecationType.Deprecate, 0)] class A { }
[Deprecated(null, DeprecationType.Deprecate, 0)] class B { }
[Deprecated(null, DeprecationType.Deprecate, 0)] class C { }
class D
{
event EventHandler E { add { } remove { M(new A()); } }
[Obsolete] event EventHandler F { add { } remove { M(new B()); } }
event EventHandler E
{
add { }
remove { M(new A()); }
}
[Deprecated(null, DeprecationType.Deprecate, 0)] event EventHandler F
{
add { }
remove { M(new B()); }
}
event EventHandler G
{
add { }
[Deprecated(null, DeprecationType.Deprecate, 0)] remove { M(new C()); }
}
static void M(object o) { }
}";
var comp = CreateCompilationWithMscorlibAndSystemCore(source);
var comp = CreateStandardCompilation(new[] { Parse(source0), Parse(source1) });
comp.VerifyDiagnostics(
// (6,51): warning CS0612: 'A' is obsolete
// event EventHandler E { add { } remove { M(new A()); } }
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "A").WithArguments("A").WithLocation(6, 51));
// (21,10): error CS1667: Attribute 'Windows.Foundation.Metadata.DeprecatedAttribute' is not valid on property or event accessors. It is only valid on 'assembly, module, class, struct, enum, constructor, method, property, indexer, field, event, interface, parameter, delegate, return, type parameter' declarations.
// [Deprecated(null, DeprecationType.Deprecate, 0)] remove { M(new C()); }
Diagnostic(ErrorCode.ERR_AttributeNotOnAccessor, "Deprecated").WithArguments("Windows.Foundation.Metadata.DeprecatedAttribute", "assembly, module, class, struct, enum, constructor, method, property, indexer, field, event, interface, parameter, delegate, return, type parameter"),
// (11,24): warning CS0612: 'A' is obsolete
// remove { M(new A()); }
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "A").WithArguments("A"),
// (21,73): warning CS0612: 'C' is obsolete
// [Deprecated(null, DeprecationType.Deprecate, 0)] remove { M(new C()); }
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "C").WithArguments("C").WithLocation(21, 73));
}
[Fact]
......
......@@ -46,11 +46,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
''' </returns>
Private Shared Function GetObsoleteContextState(symbol As Symbol, forceComplete As Boolean) As ThreeState
While symbol IsNot Nothing
' For property or event accessors, check the associated property or event instead.
If symbol.IsAccessor() Then
symbol = DirectCast(symbol, MethodSymbol).AssociatedSymbol
End If
If forceComplete Then
symbol.ForceCompleteObsoleteAttribute()
End If
......@@ -60,7 +55,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Return state
End If
symbol = symbol.ContainingSymbol
' For property or event accessors, check the associated property or event instead.
If symbol.IsAccessor() Then
symbol = DirectCast(symbol, MethodSymbol).AssociatedSymbol
Else
symbol = symbol.ContainingSymbol
End If
End While
Return ThreeState.False
......
......@@ -719,24 +719,43 @@ End Class
<compilation>
<file><![CDATA[
Imports System
<Obsolete>
Class A
Namespace Windows.Foundation.Metadata
Public NotInheritable Class DeprecatedAttribute
Inherits Attribute
Public Sub New(message As String, type As DeprecationType, version As UInteger)
End Sub
End Class
Public Enum DeprecationType
Deprecate
Remove
End Enum
End Namespace
]]>
</file>
<file><![CDATA[
Imports Windows.Foundation.Metadata
<Deprecated(Nothing, DeprecationType.Deprecate, 0)>Class A
End Class
<Obsolete>
Class B
<Deprecated(Nothing, DeprecationType.Deprecate, 0)>Class B
End Class
Class C
<Deprecated(Nothing, DeprecationType.Deprecate, 0)>Class C
End Class
Class D
ReadOnly Property P As Object
Get
Return New A()
End Get
End Property
<Obsolete>
ReadOnly Property Q As Object
<Deprecated(Nothing, DeprecationType.Deprecate, 0)>ReadOnly Property Q As Object
Get
Return New B()
End Get
End Property
ReadOnly Property R As Object
<Deprecated(Nothing, DeprecationType.Deprecate, 0)>Get
Return New C()
End Get
End Property
End Class
]]>
</file>
......@@ -751,13 +770,29 @@ End Class
<compilation>
<file><![CDATA[
Imports System
<Obsolete>
Class A
Namespace Windows.Foundation.Metadata
Public NotInheritable Class DeprecatedAttribute
Inherits Attribute
Public Sub New(message As String, type As DeprecationType, version As UInteger)
End Sub
End Class
Public Enum DeprecationType
Deprecate
Remove
End Enum
End Namespace
]]>
</file>
<file><![CDATA[
Imports System
Imports Windows.Foundation.Metadata
<Deprecated(Nothing, DeprecationType.Deprecate, 0)>Class A
End Class
<Obsolete>
Class B
<Deprecated(Nothing, DeprecationType.Deprecate, 0)>Class B
End Class
Class C
<Deprecated(Nothing, DeprecationType.Deprecate, 0)>Class C
End Class
Class D
Custom Event E As EventHandler
AddHandler(value As EventHandler)
End AddHandler
......@@ -767,8 +802,7 @@ Class C
RaiseEvent
End RaiseEvent
End Event
<Obsolete>
Custom Event F As EventHandler
<Deprecated(Nothing, DeprecationType.Deprecate, 0)>Custom Event F As EventHandler
AddHandler(value As EventHandler)
End AddHandler
RemoveHandler(value As EventHandler)
......@@ -777,6 +811,15 @@ Class C
RaiseEvent
End RaiseEvent
End Event
Custom Event G As EventHandler
AddHandler(value As EventHandler)
End AddHandler
<Deprecated(Nothing, DeprecationType.Deprecate, 0)>RemoveHandler(value As EventHandler)
M(New C())
End RemoveHandler
RaiseEvent
End RaiseEvent
End Event
Shared Sub M(o As Object)
End Sub
End Class
......@@ -784,7 +827,8 @@ End Class
</file>
</compilation>
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
Diagnostic(ERRID.WRN_UseOfObsoleteSymbolNoMessage1, "A").WithArguments("A").WithLocation(13, 19))
Diagnostic(ERRID.ERR_ObsoleteInvalidOnEventMember, "<Deprecated(Nothing, DeprecationType.Deprecate, 0)>RemoveHandler(value As EventHandler)").WithArguments("Windows.Foundation.Metadata.DeprecatedAttribute").WithLocation(31, 9),
Diagnostic(ERRID.WRN_UseOfObsoleteSymbolNoMessage1, "A").WithArguments("A").WithLocation(14, 19))
End Sub
<Fact>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册