Skip to content

Commit 26d4886

Browse files
authored
Fix compilation failure from regex source generator with some lazy loops (#118132)
Because of some possible complicated control flow, the compiler thinks these variables are not deterministically initialized.
1 parent 5196f3e commit 26d4886

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3852,7 +3852,8 @@ void EmitLazy(RegexNode node)
38523852
{
38533853
startingPos = ReserveName("lazyloop_starting_pos");
38543854
sawEmpty = ReserveName("lazyloop_empty_seen");
3855-
writer.WriteLine($"int {startingPos} = pos, {sawEmpty} = 0; // the lazy loop may match empty iterations");
3855+
additionalDeclarations.Add($"int {startingPos} = 0, {sawEmpty} = 0;");
3856+
writer.WriteLine($"{startingPos} = {sawEmpty} = 0; // the lazy loop may match empty iterations");
38563857
}
38573858

38583859
// If the min count is 0, start out by jumping right to what's after the loop. Backtracking

src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/Regex.Match.Tests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ public static IEnumerable<object[]> Match_MemberData()
280280
yield return (@"(abcd*?)+e", "abcde", RegexOptions.None, 0, 5, true, "abcde");
281281
yield return (@"(abcd*)+?e", "abcde", RegexOptions.None, 0, 5, true, "abcde");
282282
yield return (@"(abcd*?)+?e", "abcde", RegexOptions.None, 0, 5, true, "abcde");
283+
yield return (@"(?:m(?:((e)?)??)|a)\b", "you m you", RegexOptions.None, 0, 9, true, "m");
284+
yield return (@"(?:m(?:((e)?)??)|a)\b", "you me you", RegexOptions.None, 0, 10, true, "me");
285+
yield return (@"(?:m(?:((e)?)??)|a)\b", "you a you", RegexOptions.None, 0, 9, true, "a");
286+
yield return (@"(?:m(?:((e)?)??)|a)\b", "you and you", RegexOptions.None, 0, 11, false, "");
287+
yield return (@"(?:m(?:|(e)?)|a)\b", "you m you", RegexOptions.None, 0, 9, true, "m");
288+
yield return (@"(?:m(?:|(e)?)|a)\b", "you me you", RegexOptions.None, 0, 10, true, "me");
289+
yield return (@"(?:m(?:|(e)?)|a)\b", "you a you", RegexOptions.None, 0, 9, true, "a");
290+
yield return (@"(?:m(?:|(e)?)|a)\b", "you and you", RegexOptions.None, 0, 11, false, "");
283291

284292
// Testing selected FindOptimizations finds the right prefix
285293
yield return (@"(^|a+)bc", " aabc", RegexOptions.None, 0, 5, true, "aabc");

0 commit comments

Comments
 (0)