Skip to content

Commit a523b84

Browse files
authored
Fix nullable annotations of SymbolDisplay.FormatPrimitive (#79869)
* Fix nullable annotations of `SymbolDisplay.FormatPrimitive` * Remove unnecessary breaking change doc * Mark as removed instead of removing
1 parent 80ddff5 commit a523b84

File tree

5 files changed

+25
-20
lines changed

5 files changed

+25
-20
lines changed

src/Compilers/CSharp/Portable/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ override Microsoft.CodeAnalysis.CSharp.Syntax.ExtensionBlockDeclarationSyntax.Se
5353
override Microsoft.CodeAnalysis.CSharp.Syntax.ExtensionBlockDeclarationSyntax.TypeParameterList.get -> Microsoft.CodeAnalysis.CSharp.Syntax.TypeParameterListSyntax?
5454
override Microsoft.CodeAnalysis.CSharp.Syntax.ExtensionMemberCrefSyntax.Accept(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor! visitor) -> void
5555
override Microsoft.CodeAnalysis.CSharp.Syntax.ExtensionMemberCrefSyntax.Accept<TResult>(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor<TResult>! visitor) -> TResult?
56+
*REMOVED*static Microsoft.CodeAnalysis.CSharp.SymbolDisplay.FormatPrimitive(object! obj, bool quoteStrings, bool useHexadecimalNumbers) -> string!
57+
static Microsoft.CodeAnalysis.CSharp.SymbolDisplay.FormatPrimitive(object? obj, bool quoteStrings, bool useHexadecimalNumbers) -> string?
5658
static Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ExtensionBlockDeclaration() -> Microsoft.CodeAnalysis.CSharp.Syntax.ExtensionBlockDeclarationSyntax!
5759
static Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ExtensionBlockDeclaration(Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.CSharp.Syntax.AttributeListSyntax!> attributeLists, Microsoft.CodeAnalysis.SyntaxTokenList modifiers, Microsoft.CodeAnalysis.CSharp.Syntax.TypeParameterListSyntax? typeParameterList, Microsoft.CodeAnalysis.CSharp.Syntax.ParameterListSyntax? parameterList, Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.CSharp.Syntax.TypeParameterConstraintClauseSyntax!> constraintClauses, Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.CSharp.Syntax.MemberDeclarationSyntax!> members) -> Microsoft.CodeAnalysis.CSharp.Syntax.ExtensionBlockDeclarationSyntax!
5860
static Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ExtensionBlockDeclaration(Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.CSharp.Syntax.AttributeListSyntax!> attributeLists, Microsoft.CodeAnalysis.SyntaxTokenList modifiers, Microsoft.CodeAnalysis.SyntaxToken keyword, Microsoft.CodeAnalysis.CSharp.Syntax.TypeParameterListSyntax? typeParameterList, Microsoft.CodeAnalysis.CSharp.Syntax.ParameterListSyntax? parameterList, Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.CSharp.Syntax.TypeParameterConstraintClauseSyntax!> constraintClauses, Microsoft.CodeAnalysis.SyntaxToken openBraceToken, Microsoft.CodeAnalysis.SyntaxList<Microsoft.CodeAnalysis.CSharp.Syntax.MemberDeclarationSyntax!> members, Microsoft.CodeAnalysis.SyntaxToken closeBraceToken, Microsoft.CodeAnalysis.SyntaxToken semicolonToken) -> Microsoft.CodeAnalysis.CSharp.Syntax.ExtensionBlockDeclarationSyntax!

src/Compilers/CSharp/Portable/SymbolDisplay/ObjectDisplay.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System;
6+
using System.Diagnostics.CodeAnalysis;
87
using System.Globalization;
98
using System.Reflection;
109
using Microsoft.CodeAnalysis.PooledObjects;
@@ -35,7 +34,7 @@ internal static class ObjectDisplay
3534
/// <see cref="long"/>, <see cref="ulong"/>, <see cref="double"/>, <see cref="float"/>, <see cref="decimal"/>,
3635
/// and <c>null</c>.
3736
/// </remarks>
38-
public static string FormatPrimitive(object obj, ObjectDisplayOptions options)
37+
public static string? FormatPrimitive(object? obj, ObjectDisplayOptions options)
3938
{
4039
if (obj == null)
4140
{
@@ -138,7 +137,7 @@ internal static string FormatLiteral(bool value)
138137
/// Returns true if the character should be replaced and sets
139138
/// <paramref name="replaceWith"/> to the replacement text.
140139
/// </summary>
141-
private static bool TryReplaceChar(char c, out string replaceWith)
140+
private static bool TryReplaceChar(char c, [NotNullWhen(returnValue: true)] out string? replaceWith)
142141
{
143142
replaceWith = null;
144143
switch (c)
@@ -332,7 +331,7 @@ internal static string FormatLiteral(char c, ObjectDisplayOptions options)
332331
builder.Append(quote);
333332
}
334333

335-
string replaceWith;
334+
string? replaceWith;
336335
if (escapeNonPrintable && TryReplaceChar(c, out replaceWith))
337336
{
338337
builder.Append(replaceWith);
@@ -355,7 +354,7 @@ internal static string FormatLiteral(char c, ObjectDisplayOptions options)
355354
return pooledBuilder.ToStringAndFree();
356355
}
357356

358-
internal static string FormatLiteral(sbyte value, ObjectDisplayOptions options, CultureInfo cultureInfo = null)
357+
internal static string FormatLiteral(sbyte value, ObjectDisplayOptions options, CultureInfo? cultureInfo = null)
359358
{
360359
if (options.IncludesOption(ObjectDisplayOptions.UseHexadecimalNumbers))
361360
{
@@ -369,7 +368,7 @@ internal static string FormatLiteral(sbyte value, ObjectDisplayOptions options,
369368
}
370369
}
371370

372-
internal static string FormatLiteral(byte value, ObjectDisplayOptions options, CultureInfo cultureInfo = null)
371+
internal static string FormatLiteral(byte value, ObjectDisplayOptions options, CultureInfo? cultureInfo = null)
373372
{
374373
if (options.IncludesOption(ObjectDisplayOptions.UseHexadecimalNumbers))
375374
{
@@ -381,7 +380,7 @@ internal static string FormatLiteral(byte value, ObjectDisplayOptions options, C
381380
}
382381
}
383382

384-
internal static string FormatLiteral(short value, ObjectDisplayOptions options, CultureInfo cultureInfo = null)
383+
internal static string FormatLiteral(short value, ObjectDisplayOptions options, CultureInfo? cultureInfo = null)
385384
{
386385
if (options.IncludesOption(ObjectDisplayOptions.UseHexadecimalNumbers))
387386
{
@@ -395,7 +394,7 @@ internal static string FormatLiteral(short value, ObjectDisplayOptions options,
395394
}
396395
}
397396

398-
internal static string FormatLiteral(ushort value, ObjectDisplayOptions options, CultureInfo cultureInfo = null)
397+
internal static string FormatLiteral(ushort value, ObjectDisplayOptions options, CultureInfo? cultureInfo = null)
399398
{
400399
if (options.IncludesOption(ObjectDisplayOptions.UseHexadecimalNumbers))
401400
{
@@ -407,7 +406,7 @@ internal static string FormatLiteral(ushort value, ObjectDisplayOptions options,
407406
}
408407
}
409408

410-
internal static string FormatLiteral(int value, ObjectDisplayOptions options, CultureInfo cultureInfo = null)
409+
internal static string FormatLiteral(int value, ObjectDisplayOptions options, CultureInfo? cultureInfo = null)
411410
{
412411
if (options.IncludesOption(ObjectDisplayOptions.UseHexadecimalNumbers))
413412
{
@@ -419,7 +418,7 @@ internal static string FormatLiteral(int value, ObjectDisplayOptions options, Cu
419418
}
420419
}
421420

422-
internal static string FormatLiteral(uint value, ObjectDisplayOptions options, CultureInfo cultureInfo = null)
421+
internal static string FormatLiteral(uint value, ObjectDisplayOptions options, CultureInfo? cultureInfo = null)
423422
{
424423
var pooledBuilder = PooledStringBuilder.GetInstance();
425424
var sb = pooledBuilder.Builder;
@@ -442,7 +441,7 @@ internal static string FormatLiteral(uint value, ObjectDisplayOptions options, C
442441
return pooledBuilder.ToStringAndFree();
443442
}
444443

445-
internal static string FormatLiteral(long value, ObjectDisplayOptions options, CultureInfo cultureInfo = null)
444+
internal static string FormatLiteral(long value, ObjectDisplayOptions options, CultureInfo? cultureInfo = null)
446445
{
447446
var pooledBuilder = PooledStringBuilder.GetInstance();
448447
var sb = pooledBuilder.Builder;
@@ -465,7 +464,7 @@ internal static string FormatLiteral(long value, ObjectDisplayOptions options, C
465464
return pooledBuilder.ToStringAndFree();
466465
}
467466

468-
internal static string FormatLiteral(ulong value, ObjectDisplayOptions options, CultureInfo cultureInfo = null)
467+
internal static string FormatLiteral(ulong value, ObjectDisplayOptions options, CultureInfo? cultureInfo = null)
469468
{
470469
var pooledBuilder = PooledStringBuilder.GetInstance();
471470
var sb = pooledBuilder.Builder;
@@ -488,28 +487,28 @@ internal static string FormatLiteral(ulong value, ObjectDisplayOptions options,
488487
return pooledBuilder.ToStringAndFree();
489488
}
490489

491-
internal static string FormatLiteral(double value, ObjectDisplayOptions options, CultureInfo cultureInfo = null)
490+
internal static string FormatLiteral(double value, ObjectDisplayOptions options, CultureInfo? cultureInfo = null)
492491
{
493492
var result = value.ToString("R", GetFormatCulture(cultureInfo));
494493

495494
return options.IncludesOption(ObjectDisplayOptions.IncludeTypeSuffix) ? result + "D" : result;
496495
}
497496

498-
internal static string FormatLiteral(float value, ObjectDisplayOptions options, CultureInfo cultureInfo = null)
497+
internal static string FormatLiteral(float value, ObjectDisplayOptions options, CultureInfo? cultureInfo = null)
499498
{
500499
var result = value.ToString("R", GetFormatCulture(cultureInfo));
501500

502501
return options.IncludesOption(ObjectDisplayOptions.IncludeTypeSuffix) ? result + "F" : result;
503502
}
504503

505-
internal static string FormatLiteral(decimal value, ObjectDisplayOptions options, CultureInfo cultureInfo = null)
504+
internal static string FormatLiteral(decimal value, ObjectDisplayOptions options, CultureInfo? cultureInfo = null)
506505
{
507506
var result = value.ToString(GetFormatCulture(cultureInfo));
508507

509508
return options.IncludesOption(ObjectDisplayOptions.IncludeTypeSuffix) ? result + "M" : result;
510509
}
511510

512-
private static CultureInfo GetFormatCulture(CultureInfo cultureInfo)
511+
private static CultureInfo GetFormatCulture(CultureInfo? cultureInfo)
513512
{
514513
return cultureInfo ?? CultureInfo.InvariantCulture;
515514
}

src/Compilers/CSharp/Portable/SymbolDisplay/SymbolDisplay.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ private static ArrayBuilder<SymbolDisplayPart> PopulateDisplayParts(
297297
/// <see cref="long"/>, <see cref="ulong"/>, <see cref="double"/>, <see cref="float"/>, <see cref="decimal"/>,
298298
/// and <c>null</c>.
299299
/// </remarks>
300-
public static string FormatPrimitive(object obj, bool quoteStrings, bool useHexadecimalNumbers)
300+
public static string? FormatPrimitive(object? obj, bool quoteStrings, bool useHexadecimalNumbers)
301301
{
302302
var options = ObjectDisplayOptions.EscapeNonPrintableCharacters;
303303
if (quoteStrings)

src/Compilers/CSharp/Portable/Symbols/TypedConstantExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public static string ToCSharpString(this TypedConstant constant)
4040
}
4141

4242
Debug.Assert(constant.ValueInternal is object);
43-
return SymbolDisplay.FormatPrimitive(constant.ValueInternal, quoteStrings: true, useHexadecimalNumbers: false);
43+
var result = SymbolDisplay.FormatPrimitive(constant.ValueInternal, quoteStrings: true, useHexadecimalNumbers: false);
44+
Debug.Assert(result != null);
45+
return result;
4446
}
4547

4648
// Decode the value of enum constant

src/Compilers/CSharp/Portable/Utilities/ValueSetFactory.CharTC.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ char INumericTC<char>.Next(char value)
5050

5151
string INumericTC<char>.ToString(char c)
5252
{
53-
return ObjectDisplay.FormatPrimitive(c, ObjectDisplayOptions.EscapeNonPrintableCharacters | ObjectDisplayOptions.UseQuotes);
53+
var result = ObjectDisplay.FormatPrimitive(c, ObjectDisplayOptions.EscapeNonPrintableCharacters | ObjectDisplayOptions.UseQuotes);
54+
Debug.Assert(result != null);
55+
return result;
5456
}
5557

5658
char INumericTC<char>.Prev(char value)

0 commit comments

Comments
 (0)