Skip to content

Commit

Permalink
Fix nullable reference type warnings in RegexGenerator (#68516)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Apr 25, 2022
1 parent ffe71b9 commit b42adad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))}" :
Expand Down Expand Up @@ -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.
Expand All @@ -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";
Expand Down Expand Up @@ -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");
Expand All @@ -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" });

Expand Down Expand Up @@ -3323,7 +3323,7 @@ void EmitLazy(RegexNode node)
EmitUncaptureUntil(StackPop());
}
EmitStackPop(iterationMayBeEmpty ?
new[] { sawEmpty, startingPos, "pos" } :
new[] { sawEmpty!, startingPos!, "pos" } :
new[] { "pos" });
SliceInputSpan();

Expand Down Expand Up @@ -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 });
}

Expand All @@ -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 });
}

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -3928,7 +3928,7 @@ void EmitLoop(RegexNode node)
Goto(originalDoneLabel);
}
EmitStackPop(iterationMayBeEmpty ?
new[] { "pos", startingPos } :
new[] { "pos", startingPos! } :
new[] { "pos" });
if (expressionHasCaptures)
{
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ static bool IsAllowedKind(SyntaxKind kind) =>
/// <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; }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit b42adad

Please sign in to comment.