-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix Decimal literal formatting in source generators #93160
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Globalization; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
|
|
||
| namespace SourceGenerators; | ||
|
|
||
| internal static class CSharpSyntaxUtilities | ||
| { | ||
| // Standard format for double and single on non-inbox frameworks to ensure value is round-trippable. | ||
| public const string DoubleFormatString = "G17"; | ||
| public const string SingleFormatString = "G9"; | ||
|
|
||
| // Format a literal in C# format -- works around https://github.com/dotnet/roslyn/issues/58705 | ||
| public static string FormatLiteral(object? value, TypeRef type) | ||
ericstj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| if (value == null) | ||
| { | ||
| return $"default({type.FullyQualifiedName})"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: wouldn't it be preferable to just use
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am seeing we used
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We used this same |
||
| } | ||
|
|
||
| switch (value) | ||
| { | ||
| case string @string: | ||
| return SymbolDisplay.FormatLiteral(@string, quote: true); ; | ||
| case char @char: | ||
| return SymbolDisplay.FormatLiteral(@char, quote: true); | ||
| case double.NegativeInfinity: | ||
| return "double.NegativeInfinity"; | ||
| case double.PositiveInfinity: | ||
| return "double.PositiveInfinity"; | ||
| case double.NaN: | ||
ericstj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return "double.NaN"; | ||
| case double @double: | ||
| return $"{@double.ToString(DoubleFormatString, CultureInfo.InvariantCulture)}D"; | ||
| case float.NegativeInfinity: | ||
| return "float.NegativeInfinity"; | ||
| case float.PositiveInfinity: | ||
| return "float.PositiveInfinity"; | ||
| case float.NaN: | ||
| return "float.NaN"; | ||
| case float @float: | ||
| return $"{@float.ToString(SingleFormatString, CultureInfo.InvariantCulture)}F"; | ||
| case decimal @decimal: | ||
| // we do not need to specify a format string for decimal as it's default is round-trippable on all frameworks. | ||
| return $"{@decimal.ToString(CultureInfo.InvariantCulture)}M"; | ||
ericstj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case bool @bool: | ||
| return @bool ? "true" : "false"; | ||
| default: | ||
| // Assume this is a number. | ||
| return FormatNumber(); | ||
| } | ||
|
|
||
| string FormatNumber() => $"({type.FullyQualifiedName})({Convert.ToString(value, CultureInfo.InvariantCulture)})"; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.