未验证 提交 b42adadd 编写于 作者: S Stephen Toub 提交者: GitHub

Fix nullable reference type warnings in RegexGenerator (#68516)

上级 ffe71b9a
......@@ -917,7 +917,7 @@ void EmitLiteralAfterAtomicLoop()
Debug.Assert(target.LoopNode.Kind is RegexNodeKind.Setloop or RegexNodeKind.Setlazy or RegexNodeKind.Setloopatomic);
Debug.Assert(target.LoopNode.N == int.MaxValue);
writer.Write($"// The pattern begins with an atomic loop for {DescribeSet(target.LoopNode.Str)}, followed by ");
writer.Write($"// The pattern begins with an atomic loop for {DescribeSet(target.LoopNode.Str!)}, followed by ");
writer.WriteLine(
target.Literal.String is not null ? $"the string {Literal(target.Literal.String)}." :
target.Literal.Chars is not null ? $"one of the characters {Literal(new string(target.Literal.Chars))}" :
......@@ -1027,7 +1027,7 @@ private static void EmitTryMatchAtCurrentPosition(IndentedTextWriter writer, Reg
// Helper to define names. Names start unadorned, but as soon as there's repetition,
// they begin to have a numbered suffix.
var usedNames = new Dictionary<string, int>();
Dictionary<string, int> usedNames = new();
// Every RegexTree is rooted in the implicit Capture for the whole expression.
// Skip the Capture node. We handle the implicit root capture specially.
......@@ -1039,8 +1039,8 @@ private static void EmitTryMatchAtCurrentPosition(IndentedTextWriter writer, Reg
// In some cases, we need to emit declarations at the beginning of the method, but we only discover we need them later.
// To handle that, we build up a collection of all the declarations to include, track where they should be inserted,
// and then insert them at that position once everything else has been output.
var additionalDeclarations = new HashSet<string>();
var additionalLocalFunctions = new Dictionary<string, string[]>();
HashSet<string> additionalDeclarations = new();
Dictionary<string, string[]> additionalLocalFunctions = new();
// Declare some locals.
string sliceSpan = "slice";
......@@ -3217,7 +3217,7 @@ void EmitLazy(RegexNode node)
// additional checks if we can prove that the loop can never match empty, which we can do by computing
// the minimum length of the child; only if it's 0 might iterations be empty.
bool iterationMayBeEmpty = child.ComputeMinLength() == 0;
string startingPos = null, sawEmpty = null;
string? startingPos = null, sawEmpty = null;
if (iterationMayBeEmpty)
{
startingPos = ReserveName("lazyloop_starting_pos");
......@@ -3243,8 +3243,8 @@ void EmitLazy(RegexNode node)
// iterations, this state needs to be stored on to the backtracking stack.
int entriesPerIteration = 1/*pos*/ + (iterationMayBeEmpty ? 2/*startingPos+sawEmpty*/ : 0) + (expressionHasCaptures ? 1/*Crawlpos*/ : 0);
EmitStackPush(
expressionHasCaptures && iterationMayBeEmpty ? new[] { "pos", startingPos, sawEmpty, "base.Crawlpos()" } :
iterationMayBeEmpty ? new[] { "pos", startingPos, sawEmpty } :
expressionHasCaptures && iterationMayBeEmpty ? new[] { "pos", startingPos!, sawEmpty!, "base.Crawlpos()" } :
iterationMayBeEmpty ? new[] { "pos", startingPos!, sawEmpty! } :
expressionHasCaptures ? new[] { "pos", "base.Crawlpos()" } :
new[] { "pos" });
......@@ -3323,7 +3323,7 @@ void EmitLazy(RegexNode node)
EmitUncaptureUntil(StackPop());
}
EmitStackPop(iterationMayBeEmpty ?
new[] { sawEmpty, startingPos, "pos" } :
new[] { sawEmpty!, startingPos!, "pos" } :
new[] { "pos" });
SliceInputSpan();
......@@ -3373,7 +3373,7 @@ void EmitLazy(RegexNode node)
if (rm.Analysis.IsInLoop(node))
{
EmitStackPush(iterationMayBeEmpty ?
new[] { iterationCount, startingPos, sawEmpty } :
new[] { iterationCount, startingPos!, sawEmpty! } :
new[] { iterationCount });
}
......@@ -3392,7 +3392,7 @@ void EmitLazy(RegexNode node)
if (rm.Analysis.IsInLoop(node))
{
EmitStackPop(iterationMayBeEmpty ?
new[] { sawEmpty, startingPos, iterationCount } :
new[] { sawEmpty!, startingPos!, iterationCount } :
new[] { iterationCount });
}
......@@ -3818,9 +3818,9 @@ void EmitLoop(RegexNode node)
// here we still need the stack, because each iteration of _this_ loop may have its own state, e.g. we
// need to know where each iteration began so when backtracking we can jump back to that location.
EmitStackPush(
expressionHasCaptures && iterationMayBeEmpty ? new[] { "base.Crawlpos()", startingPos, "pos" } :
expressionHasCaptures && iterationMayBeEmpty ? new[] { "base.Crawlpos()", startingPos!, "pos" } :
expressionHasCaptures ? new[] { "base.Crawlpos()", "pos" } :
iterationMayBeEmpty ? new[] { startingPos, "pos" } :
iterationMayBeEmpty ? new[] { startingPos!, "pos" } :
new[] { "pos" });
writer.WriteLine();
......@@ -3928,7 +3928,7 @@ void EmitLoop(RegexNode node)
Goto(originalDoneLabel);
}
EmitStackPop(iterationMayBeEmpty ?
new[] { "pos", startingPos } :
new[] { "pos", startingPos! } :
new[] { "pos" });
if (expressionHasCaptures)
{
......@@ -4020,7 +4020,7 @@ void EmitLoop(RegexNode node)
// Store the loop's state
EmitStackPush(iterationMayBeEmpty ?
new[] { startingPos, iterationCount } :
new[] { startingPos!, iterationCount } :
new[] { iterationCount });
// Skip past the backtracking section
......@@ -4032,7 +4032,7 @@ void EmitLoop(RegexNode node)
string backtrack = ReserveName("LoopBacktrack");
MarkLabel(backtrack, emitSemicolon: false);
EmitStackPop(iterationMayBeEmpty ?
new[] { iterationCount, startingPos } :
new[] { iterationCount, startingPos! } :
new[] { iterationCount });
// We're backtracking. Check the timeout.
......
......@@ -238,7 +238,7 @@ private static bool IsSemanticTargetForGeneration(SemanticModel semanticModel, M
/// <summary>A regex method.</summary>
internal sealed record RegexMethod(RegexType DeclaringType, MethodDeclarationSyntax MethodSyntax, string MethodName, string Modifiers, string Pattern, RegexOptions Options, int? MatchTimeout, RegexTree Tree, AnalysisResults Analysis)
{
public string GeneratedName { get; set; }
public string? GeneratedName { get; set; }
public bool IsDuplicate { get; set; }
}
......
......@@ -47,7 +47,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
// - Diagnostic in the case of a failure that should end the compilation
// - (RegexMethod regexMethod, string runnerFactoryImplementation, Dictionary<string, string[]> requiredHelpers) in the case of valid regex
// - (RegexMethod regexMethod, string reason, Diagnostic diagnostic) in the case of a limited-support regex
IncrementalValueProvider<ImmutableArray<object?>> codeOrDiagnostics =
IncrementalValueProvider<ImmutableArray<object>> codeOrDiagnostics =
context.SyntaxProvider
// Find all MethodDeclarationSyntax nodes attributed with RegexGenerator and gather the required information.
......@@ -94,11 +94,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
// and raise all of the created diagnostics.
context.RegisterSourceOutput(codeOrDiagnostics.Combine(compilationDataProvider), static (context, compilationDataAndResults) =>
{
ImmutableArray<object?> results = compilationDataAndResults.Left;
ImmutableArray<object> results = compilationDataAndResults.Left;
// Report any top-level diagnostics.
bool allFailures = true;
foreach (object? result in results)
foreach (object result in results)
{
if (result is Diagnostic d)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册