Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stash and restore original culture in CultureNormalizer #76713

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4873,6 +4873,7 @@ public void HandlerConversionPreferredOverStringForNonConstant(string expression
var code = @"
CultureInfoNormalizer.Normalize();
C.M(" + expression + @");
CultureInfoNormalizer.Reset();
class C
{
public static void M(CustomHandler b)
Expand All @@ -4896,7 +4897,7 @@ public static void M(string s)

verifier.VerifyIL(@"<top-level-statements-entry-point>", @"
{
// Code size 55 (0x37)
// Code size 60 (0x3c)
.maxstack 4
.locals init (CustomHandler V_0)
IL_0000: call ""void CultureInfoNormalizer.Normalize()""
Expand All @@ -4919,7 +4920,8 @@ .locals init (CustomHandler V_0)
IL_002f: pop
IL_0030: ldloc.0
IL_0031: call ""void C.M(CustomHandler)""
IL_0036: ret
IL_0036: call ""void CultureInfoNormalizer.Reset()""
IL_003b: ret
}
");

Expand All @@ -4928,7 +4930,7 @@ .locals init (CustomHandler V_0)

verifier.VerifyIL(@"<top-level-statements-entry-point>", expression.Contains('+') ? @"
{
// Code size 37 (0x25)
// Code size 42 (0x2a)
.maxstack 2
IL_0000: call ""void CultureInfoNormalizer.Normalize()""
IL_0005: ldstr ""{0,2:f}""
Expand All @@ -4938,20 +4940,22 @@ .maxstack 2
IL_0015: ldstr ""Literal""
IL_001a: call ""string string.Concat(string, string)""
IL_001f: call ""void C.M(string)""
IL_0024: ret
IL_0024: call ""void CultureInfoNormalizer.Reset()""
IL_0029: ret
}
"
: @"
{
// Code size 27 (0x1b)
// Code size 32 (0x20)
.maxstack 2
IL_0000: call ""void CultureInfoNormalizer.Normalize()""
IL_0005: ldstr ""{0,2:f}Literal""
IL_000a: ldc.i4.1
IL_000b: box ""int""
IL_0010: call ""string string.Format(string, object)""
IL_0015: call ""void C.M(string)""
IL_001a: ret
IL_001a: call ""void CultureInfoNormalizer.Reset()""
IL_001f: ret
}
");
}
Expand Down Expand Up @@ -5422,6 +5426,7 @@ public void LambdaReturnInference_02(string expression)
using System;
CultureInfoNormalizer.Normalize();
C.M(() => " + expression + @");
CultureInfoNormalizer.Reset();

class C
{
Expand Down Expand Up @@ -5669,6 +5674,7 @@ public void LambdaReturnInference_06(string expression)
if (b) return default(CustomHandler);
else return " + expression + @";
});
CultureInfoNormalizer.Reset();

static class C
{
Expand Down Expand Up @@ -5922,6 +5928,7 @@ public void TernaryTypes_02(string expression)
CultureInfoNormalizer.Normalize();
var x = (bool)(object)false ? default(CustomHandler) : " + expression + @";
Console.WriteLine(x);
CultureInfoNormalizer.Reset();

public partial struct CustomHandler
{
Expand All @@ -5934,7 +5941,7 @@ public partial struct CustomHandler

verifier.VerifyIL("<top-level-statements-entry-point>", !expression.Contains('+') ? @"
{
// Code size 56 (0x38)
// Code size 61 (0x3d)
.maxstack 2
.locals init (CustomHandler V_0)
IL_0000: call ""void CultureInfoNormalizer.Normalize()""
Expand All @@ -5952,12 +5959,13 @@ .locals init (CustomHandler V_0)
IL_002c: ldloc.0
IL_002d: call ""string CustomHandler.op_Implicit(CustomHandler)""
IL_0032: call ""void System.Console.WriteLine(string)""
IL_0037: ret
IL_0037: call ""void CultureInfoNormalizer.Reset()""
IL_003c: ret
}
"
: @"
{
// Code size 66 (0x42)
// Code size 71 (0x47)
.maxstack 2
.locals init (CustomHandler V_0)
IL_0000: call ""void CultureInfoNormalizer.Normalize()""
Expand All @@ -5977,7 +5985,8 @@ .locals init (CustomHandler V_0)
IL_0036: ldloc.0
IL_0037: call ""string CustomHandler.op_Implicit(CustomHandler)""
IL_003c: call ""void System.Console.WriteLine(string)""
IL_0041: ret
IL_0041: call ""void CultureInfoNormalizer.Reset()""
IL_0046: ret
}
");
}
Expand Down Expand Up @@ -6190,6 +6199,7 @@ public void SwitchTypes_02(string expression)
CultureInfoNormalizer.Normalize();
var x = (bool)(object)false switch { true => default(CustomHandler), false => " + expression + @" };
Console.WriteLine(x);
CultureInfoNormalizer.Reset();

public partial struct CustomHandler
{
Expand All @@ -6202,7 +6212,7 @@ public partial struct CustomHandler

verifier.VerifyIL("<top-level-statements-entry-point>", !expression.Contains('+') ? @"
{
// Code size 59 (0x3b)
// Code size 64 (0x40)
.maxstack 2
.locals init (string V_0,
CustomHandler V_1)
Expand All @@ -6224,12 +6234,13 @@ .locals init (string V_0,
IL_0033: stloc.0
IL_0034: ldloc.0
IL_0035: call ""void System.Console.WriteLine(string)""
IL_003a: ret
IL_003a: call ""void CultureInfoNormalizer.Reset()""
IL_003f: ret
}
"
: @"
{
// Code size 69 (0x45)
// Code size 74 (0x4a)
.maxstack 2
.locals init (string V_0,
CustomHandler V_1)
Expand All @@ -6253,7 +6264,8 @@ .locals init (string V_0,
IL_003d: stloc.0
IL_003e: ldloc.0
IL_003f: call ""void System.Console.WriteLine(string)""
IL_0044: ret
IL_0044: call ""void CultureInfoNormalizer.Reset()""
IL_0049: ret
}
");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3633,6 +3633,7 @@ public void LambdaReturnInference_02(string expression)
using System;
CultureInfoNormalizer.Normalize();
C.M(() => " + expression + @");
CultureInfoNormalizer.Reset();

class C
{
Expand Down Expand Up @@ -3902,6 +3903,7 @@ public void LambdaReturnInference_06(string expression)
if (b) return default(CustomHandler);
else return " + expression + @";
});
CultureInfoNormalizer.Reset();

static class C
{
Expand Down Expand Up @@ -4178,6 +4180,7 @@ public void TernaryTypes_02(string expression)
CultureInfoNormalizer.Normalize();
var x = (bool)(object)false ? default(CustomHandler) : " + expression + @";
Console.WriteLine(x);
CultureInfoNormalizer.Reset();

public partial struct CustomHandler
{
Expand All @@ -4190,7 +4193,7 @@ public partial struct CustomHandler

verifier.VerifyIL("<top-level-statements-entry-point>", !expression.Contains('+') ? @"
{
// Code size 56 (0x38)
// Code size 61 (0x3d)
.maxstack 2
.locals init (CustomHandler V_0)
IL_0000: call ""void CultureInfoNormalizer.Normalize()""
Expand All @@ -4208,12 +4211,13 @@ .locals init (CustomHandler V_0)
IL_002c: ldloc.0
IL_002d: call ""string CustomHandler.op_Implicit(CustomHandler)""
IL_0032: call ""void System.Console.WriteLine(string)""
IL_0037: ret
IL_0037: call ""void CultureInfoNormalizer.Reset()""
IL_003c: ret
}
"
: @"
{
// Code size 66 (0x42)
// Code size 71 (0x47)
.maxstack 2
.locals init (CustomHandler V_0)
IL_0000: call ""void CultureInfoNormalizer.Normalize()""
Expand All @@ -4233,7 +4237,8 @@ .locals init (CustomHandler V_0)
IL_0036: ldloc.0
IL_0037: call ""string CustomHandler.op_Implicit(CustomHandler)""
IL_003c: call ""void System.Console.WriteLine(string)""
IL_0041: ret
IL_0041: call ""void CultureInfoNormalizer.Reset()""
IL_0046: ret
}
");
}
Expand Down Expand Up @@ -4443,6 +4448,7 @@ public void SwitchTypes_02(string expression)
CultureInfoNormalizer.Normalize();
var x = (bool)(object)false switch { true => default(CustomHandler), false => " + expression + @" };
Console.WriteLine(x);
CultureInfoNormalizer.Reset();

public partial struct CustomHandler
{
Expand All @@ -4455,7 +4461,7 @@ public partial struct CustomHandler

verifier.VerifyIL("<top-level-statements-entry-point>", !expression.Contains('+') ? @"
{
// Code size 59 (0x3b)
// Code size 64 (0x40)
.maxstack 2
.locals init (string V_0,
CustomHandler V_1)
Expand All @@ -4477,12 +4483,13 @@ .locals init (string V_0,
IL_0033: stloc.0
IL_0034: ldloc.0
IL_0035: call ""void System.Console.WriteLine(string)""
IL_003a: ret
IL_003a: call ""void CultureInfoNormalizer.Reset()""
IL_003f: ret
}
"
: @"
{
// Code size 69 (0x45)
// Code size 74 (0x4a)
.maxstack 2
.locals init (string V_0,
CustomHandler V_1)
Expand All @@ -4506,7 +4513,8 @@ .locals init (string V_0,
IL_003d: stloc.0
IL_003e: ldloc.0
IL_003f: call ""void System.Console.WriteLine(string)""
IL_0044: ret
IL_0044: call ""void CultureInfoNormalizer.Reset()""
IL_0049: ret
}
");
}
Expand Down
8 changes: 8 additions & 0 deletions src/Compilers/Test/Utilities/CSharp/CSharpTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2544,10 +2544,18 @@ internal static string GetInterpolatedStringCustomHandlerType(string name, strin
var cultureInfoHandler = @"
public class CultureInfoNormalizer
{
private static CultureInfo originalCulture;

public static void Normalize()
333fred marked this conversation as resolved.
Show resolved Hide resolved
{
originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
}

public static void Reset()
{
CultureInfo.CurrentCulture = originalCulture;
}
}
";

Expand Down