-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Codegen for module initializers feature (#43281)
- Loading branch information
Showing
23 changed files
with
441 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/Compilers/CSharp/Test/Symbol/Symbols/ModuleInitializersTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Reflection; | ||
using Microsoft.CodeAnalysis.CSharp.Symbols; | ||
using Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE; | ||
using Microsoft.CodeAnalysis.CSharp.Test.Utilities; | ||
using Microsoft.CodeAnalysis.Test.Utilities; | ||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Symbols | ||
{ | ||
[CompilerTrait(CompilerFeature.ModuleInitializers)] | ||
public sealed class ModuleInitializersTests : CSharpTestBase | ||
{ | ||
private static readonly CSharpParseOptions s_parseOptions = TestOptions.RegularPreview; | ||
|
||
[Fact] | ||
public static void LastLanguageVersionNotSupportingModuleInitializersIs8() | ||
{ | ||
var source = | ||
@"using System.Runtime.CompilerServices; | ||
|
||
class C | ||
{ | ||
[ModuleInitializer] | ||
internal static void M() { } | ||
} | ||
|
||
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } } | ||
"; | ||
var compilation = CreateCompilation(source, parseOptions: TestOptions.Regular8); | ||
|
||
compilation.VerifyDiagnostics( | ||
// (5,6): error CS8652: The feature 'module initializers' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. | ||
// [ModuleInitializer] | ||
Diagnostic(ErrorCode.ERR_FeatureInPreview, "ModuleInitializer").WithArguments("module initializers").WithLocation(5, 6) | ||
); | ||
} | ||
|
||
[Fact] | ||
public static void FirstLanguageVersionSupportingModuleInitializersIs9() | ||
{ | ||
var source = | ||
@"using System.Runtime.CompilerServices; | ||
|
||
class C | ||
{ | ||
[ModuleInitializer] | ||
internal static void M() { } | ||
} | ||
|
||
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } } | ||
"; | ||
var compilation = CreateCompilation(source, parseOptions: TestOptions.RegularPreview); | ||
|
||
compilation.VerifyDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
public void ModuleTypeStaticConstructorIsNotEmittedWhenNoMethodIsMarkedWithModuleInitializerAttribute() | ||
{ | ||
string source = @" | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
class C | ||
{ | ||
internal static void M() => Console.WriteLine(""C.M""); | ||
} | ||
|
||
class Program | ||
{ | ||
static void Main() => Console.WriteLine(""Program.Main""); | ||
} | ||
|
||
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } } | ||
"; | ||
|
||
CompileAndVerify( | ||
source, | ||
parseOptions: s_parseOptions, | ||
options: new CSharpCompilationOptions(OutputKind.ConsoleApplication, metadataImportOptions: MetadataImportOptions.All), | ||
symbolValidator: module => | ||
{ | ||
var rootModuleType = (TypeSymbol)module.GlobalNamespace.GetMember("<Module>"); | ||
Assert.Null(rootModuleType.GetMember(".cctor")); | ||
}, | ||
expectedOutput: @" | ||
Program.Main"); | ||
} | ||
|
||
[Fact] | ||
public void ModuleTypeStaticConstructorCallsMethodMarkedWithModuleInitializerAttribute() | ||
{ | ||
string source = @" | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
class C | ||
{ | ||
[ModuleInitializer] | ||
internal static void M() => Console.WriteLine(""C.M""); | ||
} | ||
|
||
class Program | ||
{ | ||
static void Main() => Console.WriteLine(""Program.Main""); | ||
} | ||
|
||
namespace System.Runtime.CompilerServices { class ModuleInitializerAttribute : System.Attribute { } } | ||
"; | ||
|
||
CompileAndVerify( | ||
source, | ||
parseOptions: s_parseOptions, | ||
options: TestOptions.DebugExe.WithMetadataImportOptions(MetadataImportOptions.All), | ||
symbolValidator: module => | ||
{ | ||
var rootModuleType = (TypeSymbol)module.GlobalNamespace.GetMember("<Module>"); | ||
var staticConstructor = (PEMethodSymbol)rootModuleType.GetMember(".cctor"); | ||
|
||
Assert.NotNull(staticConstructor); | ||
Assert.Equal(MethodKind.StaticConstructor, staticConstructor.MethodKind); | ||
|
||
var expectedFlags = | ||
MethodAttributes.Private | ||
| MethodAttributes.Static | ||
| MethodAttributes.SpecialName | ||
| MethodAttributes.RTSpecialName | ||
| MethodAttributes.HideBySig; | ||
|
||
Assert.Equal(expectedFlags, staticConstructor.Flags); | ||
}, | ||
expectedOutput: @" | ||
C.M | ||
Program.Main"); | ||
} | ||
} | ||
} |
Oops, something went wrong.