@@ -41,7 +41,7 @@ public partial class RegexGenerator
41
41
} ;
42
42
43
43
/// <summary>Generates the code for one regular expression class.</summary>
44
- private static ( string , ImmutableArray < Diagnostic > ) EmitRegexType ( RegexType regexClass )
44
+ private static ( string , ImmutableArray < Diagnostic > ) EmitRegexType ( RegexType regexClass , Compilation compilation )
45
45
{
46
46
var sb = new StringBuilder ( 1024 ) ;
47
47
var writer = new IndentedTextWriter ( new StringWriter ( sb ) ) ;
@@ -82,7 +82,7 @@ private static (string, ImmutableArray<Diagnostic>) EmitRegexType(RegexType rege
82
82
generatedName += ComputeStringHash ( generatedName ) . ToString ( "X" ) ;
83
83
84
84
// Generate the regex type
85
- ImmutableArray < Diagnostic > diagnostics = EmitRegexMethod ( writer , regexClass . Method , generatedName ) ;
85
+ ImmutableArray < Diagnostic > diagnostics = EmitRegexMethod ( writer , regexClass . Method , generatedName , compilation ) ;
86
86
87
87
while ( writer . Indent != 0 )
88
88
{
@@ -149,7 +149,7 @@ static bool ExceedsMaxDepthForSimpleCodeGeneration(RegexNode node, int allowedDe
149
149
}
150
150
151
151
/// <summary>Generates the code for a regular expression method.</summary>
152
- private static ImmutableArray < Diagnostic > EmitRegexMethod ( IndentedTextWriter writer , RegexMethod rm , string id )
152
+ private static ImmutableArray < Diagnostic > EmitRegexMethod ( IndentedTextWriter writer , RegexMethod rm , string id , Compilation compilation )
153
153
{
154
154
string patternExpression = Literal ( rm . Pattern ) ;
155
155
string optionsExpression = Literal ( rm . Options ) ;
@@ -174,6 +174,8 @@ private static ImmutableArray<Diagnostic> EmitRegexMethod(IndentedTextWriter wri
174
174
return ImmutableArray . Create ( Diagnostic . Create ( DiagnosticDescriptors . LimitedSourceGeneration , rm . MethodSyntax . GetLocation ( ) ) ) ;
175
175
}
176
176
177
+ bool allowUnsafe = compilation . Options is CSharpCompilationOptions { AllowUnsafe : true } ;
178
+
177
179
writer . WriteLine ( $ "new { id } ();") ;
178
180
writer . WriteLine ( ) ;
179
181
writer . WriteLine ( $ " private { id } ()") ;
@@ -231,6 +233,10 @@ private static ImmutableArray<Diagnostic> EmitRegexMethod(IndentedTextWriter wri
231
233
writer . Indent -= 4 ;
232
234
writer . WriteLine ( $ " }}") ;
233
235
writer . WriteLine ( ) ;
236
+ if ( allowUnsafe )
237
+ {
238
+ writer . WriteLine ( $ " [global::System.Runtime.CompilerServices.SkipLocalsInit]") ;
239
+ }
234
240
writer . WriteLine ( $ " protected override void Go()") ;
235
241
writer . WriteLine ( $ " {{") ;
236
242
writer . Indent += 4 ;
0 commit comments