Skip to content

Commit

Permalink
Cover the gap left by AttributeTargets.Method
Browse files Browse the repository at this point in the history
  • Loading branch information
jnm2 committed Apr 29, 2020
1 parent 3153060 commit 3255afa
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -766,12 +766,19 @@ private void DecodeDllImportAttribute(ref DecodeWellKnownAttributeArguments<Attr

private void DecodeModuleInitializerAttribute(DecodeWellKnownAttributeArguments<AttributeSyntax, CSharpAttributeData, AttributeLocation> arguments)
{
Debug.Assert(arguments.AttributeSyntaxOpt is object);

if (MethodKind != MethodKind.Ordinary)
{
// Ignore cases where there will already be a warning due to the AttributeTargets.Method usage
// restriction on the attribute definition in the framework.
if (MethodKind != MethodKind.Constructor && MethodKind != MethodKind.StaticConstructor)
{
arguments.Diagnostics.Add(ErrorCode.ERR_ModuleInitializerMethodMustBeOrdinary, arguments.AttributeSyntaxOpt.Location);
}
return;
}

Debug.Assert(arguments.AttributeSyntaxOpt is object);
Debug.Assert(ContainingType is object);

if (isInaccessible(this) || containingTypes(this).Any(isInaccessible))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,6 @@ namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Symbols
{
public sealed partial class ModuleInitializersTests
{
[Fact]
public void IgnoredOnLocalFunction()
{
string source = @"
using System.Runtime.CompilerServices;
class C
{
internal static void M()
{
[ModuleInitializer]
static void LocalFunction() { }
}
}
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } }
";
var verifier = CompileAndVerify(source, parseOptions: s_parseOptions);

verifier.VerifyMemberInIL("<Module>..cctor", expected: false);
}

[Fact]
public void IgnoredOnReturnValue()
{
Expand Down Expand Up @@ -141,63 +119,6 @@ class C
public C() { }
}
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } }
";
var verifier = CompileAndVerify(source, parseOptions: s_parseOptions);

verifier.VerifyMemberInIL("<Module>..cctor", expected: false);
}

[Fact]
public void IgnoredOnDestructor()
{
string source = @"
using System.Runtime.CompilerServices;
class C
{
[ModuleInitializer]
~C() { }
}
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } }
";
var verifier = CompileAndVerify(source, parseOptions: s_parseOptions);

verifier.VerifyMemberInIL("<Module>..cctor", expected: false);
}

[Fact]
public void IgnoredOnOperator()
{
string source = @"
using System.Runtime.CompilerServices;
class C
{
[ModuleInitializer]
public static C operator -(C p) => p;
}
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } }
";
var verifier = CompileAndVerify(source, parseOptions: s_parseOptions);

verifier.VerifyMemberInIL("<Module>..cctor", expected: false);
}

[Fact]
public void IgnoredOnConversionOperator()
{
string source = @"
using System.Runtime.CompilerServices;
class C
{
[ModuleInitializer]
public static explicit operator int(C p) => default;
}
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } }
";
var verifier = CompileAndVerify(source, parseOptions: s_parseOptions);
Expand All @@ -217,30 +138,6 @@ class C
public event System.Action E;
}
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } }
";
var verifier = CompileAndVerify(source, parseOptions: s_parseOptions);

verifier.VerifyMemberInIL("<Module>..cctor", expected: false);
}

[Fact]
public void IgnoredOnEventAccessors()
{
string source = @"
using System.Runtime.CompilerServices;
class C
{
public event System.Action E
{
[ModuleInitializer]
add { }
[ModuleInitializer]
remove { }
}
}
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } }
";
var verifier = CompileAndVerify(source, parseOptions: s_parseOptions);
Expand All @@ -260,30 +157,6 @@ class C
public int P { get; set; }
}
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } }
";
var verifier = CompileAndVerify(source, parseOptions: s_parseOptions);

verifier.VerifyMemberInIL("<Module>..cctor", expected: false);
}

[Fact]
public void IgnoredOnPropertyAccessors()
{
string source = @"
using System.Runtime.CompilerServices;
class C
{
public int P
{
[ModuleInitializer]
get;
[ModuleInitializer]
set;
}
}
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } }
";
var verifier = CompileAndVerify(source, parseOptions: s_parseOptions);
Expand All @@ -303,30 +176,6 @@ class C
public int this[int p] => p;
}
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } }
";
var verifier = CompileAndVerify(source, parseOptions: s_parseOptions);

verifier.VerifyMemberInIL("<Module>..cctor", expected: false);
}

[Fact]
public void IgnoredOnIndexerAccessors()
{
string source = @"
using System.Runtime.CompilerServices;
class C
{
public int this[int p]
{
[ModuleInitializer]
get => p;
[ModuleInitializer]
set { }
}
}
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } }
";
var verifier = CompileAndVerify(source, parseOptions: s_parseOptions);
Expand Down
Loading

0 comments on commit 3255afa

Please sign in to comment.