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- using System ;
6- using System . Collections . Generic ;
7- using System . Collections . Immutable ;
5+ #nullable enable
6+
87using System . Diagnostics ;
98using System . Linq ;
109using System . Text ;
11- using Microsoft . CodeAnalysis . Collections ;
12- using Microsoft . CodeAnalysis . CSharp . Symbols ;
13- using Microsoft . CodeAnalysis . CSharp . Syntax ;
1410using Microsoft . CodeAnalysis . PooledObjects ;
15- using Microsoft . CodeAnalysis . Text ;
16- using Roslyn . Utilities ;
1711
1812namespace Microsoft . CodeAnalysis . CSharp
1913{
@@ -35,8 +29,9 @@ public static string ToCSharpString(this TypedConstant constant)
3529 return "{" + string . Join ( ", " , constant . Values . Select ( v => v . ToCSharpString ( ) ) ) + "}" ;
3630 }
3731
38- if ( constant . Kind == TypedConstantKind . Type || constant . TypeInternal . SpecialType == SpecialType . System_Object )
32+ if ( constant . Kind == TypedConstantKind . Type || constant . TypeInternal ! . SpecialType == SpecialType . System_Object )
3933 {
34+ Debug . Assert ( constant . Value is object ) ;
4035 return "typeof(" + constant . Value . ToString ( ) + ")" ;
4136 }
4237
@@ -46,6 +41,7 @@ public static string ToCSharpString(this TypedConstant constant)
4641 return DisplayEnumConstant ( constant ) ;
4742 }
4843
44+ Debug . Assert ( constant . ValueInternal is object ) ;
4945 return SymbolDisplay . FormatPrimitive ( constant . ValueInternal , quoteStrings : true , useHexadecimalNumbers : false ) ;
5046 }
5147
@@ -55,7 +51,8 @@ private static string DisplayEnumConstant(TypedConstant constant)
5551 Debug . Assert ( constant . Kind == TypedConstantKind . Enum ) ;
5652
5753 // Create a ConstantValue of enum underlying type
58- SpecialType splType = ( ( INamedTypeSymbol ) constant . Type ) . EnumUnderlyingType . SpecialType ;
54+ SpecialType splType = ( ( INamedTypeSymbol ) constant . Type ! ) . EnumUnderlyingType ! . SpecialType ;
55+ Debug . Assert ( constant . ValueInternal is object ) ;
5956 ConstantValue valueConstant = ConstantValue . Create ( constant . ValueInternal , splType ) ;
6057
6158 string typeName = constant . Type . ToDisplayString ( SymbolDisplayFormat . QualifiedNameOnlyFormat ) ;
@@ -71,25 +68,27 @@ private static string DisplayEnumConstant(TypedConstant constant)
7168
7269 private static string DisplayUnsignedEnumConstant ( TypedConstant constant , SpecialType specialType , ulong constantToDecode , string typeName )
7370 {
71+ Debug . Assert ( constant . Kind == TypedConstantKind . Enum ) ;
72+
7473 // Specified valueConstant might have an exact matching enum field
7574 // or it might be a bitwise Or of multiple enum fields.
7675 // For the later case, we keep track of the current value of
7776 // bitwise Or of possible enum fields.
7877 ulong curValue = 0 ;
7978
8079 // Initialize the value string to empty
81- PooledStringBuilder pooledBuilder = null ;
82- StringBuilder valueStringBuilder = null ;
80+ PooledStringBuilder ? pooledBuilder = null ;
81+ StringBuilder ? valueStringBuilder = null ;
8382
8483 // Iterate through all the constant members in the enum type
85- var members = constant . Type . GetMembers ( ) ;
84+ var members = constant . Type ! . GetMembers ( ) ;
8685 foreach ( var member in members )
8786 {
8887 var field = member as IFieldSymbol ;
8988
90- if ( ( object ) field != null && field . HasConstantValue )
89+ if ( field is object && field . HasConstantValue )
9190 {
92- ConstantValue memberConstant = ConstantValue . Create ( field . ConstantValue , specialType ) ;
91+ ConstantValue memberConstant = ConstantValue . Create ( field . ConstantValue ! , specialType ) ; // use MemberNotNull when available https://github.com/dotnet/roslyn/issues/41964
9392 ulong memberValue = memberConstant . UInt64Value ;
9493
9594 // Do we have an exact matching enum field
@@ -140,29 +139,34 @@ private static string DisplayUnsignedEnumConstant(TypedConstant constant, Specia
140139 }
141140
142141 // Unable to decode the enum constant, just display the integral value
143- return constant . ValueInternal . ToString ( ) ;
142+ Debug . Assert ( constant . ValueInternal is object ) ;
143+ var result = constant . ValueInternal . ToString ( ) ;
144+ Debug . Assert ( result is object ) ;
145+ return result ;
144146 }
145147
146148 private static string DisplaySignedEnumConstant ( TypedConstant constant , SpecialType specialType , long constantToDecode , string typeName )
147149 {
150+ Debug . Assert ( constant . Kind == TypedConstantKind . Enum ) ;
151+
148152 // Specified valueConstant might have an exact matching enum field
149153 // or it might be a bitwise Or of multiple enum fields.
150154 // For the later case, we keep track of the current value of
151155 // bitwise Or of possible enum fields.
152156 long curValue = 0 ;
153157
154158 // Initialize the value string to empty
155- PooledStringBuilder pooledBuilder = null ;
156- StringBuilder valueStringBuilder = null ;
159+ PooledStringBuilder ? pooledBuilder = null ;
160+ StringBuilder ? valueStringBuilder = null ;
157161
158162 // Iterate through all the constant members in the enum type
159- var members = constant . Type . GetMembers ( ) ;
163+ var members = constant . Type ! . GetMembers ( ) ;
160164 foreach ( var member in members )
161165 {
162166 var field = member as IFieldSymbol ;
163- if ( ( object ) field != null && field . HasConstantValue )
167+ if ( field is object && field . HasConstantValue )
164168 {
165- ConstantValue memberConstant = ConstantValue . Create ( field . ConstantValue , specialType ) ;
169+ ConstantValue memberConstant = ConstantValue . Create ( field . ConstantValue ! , specialType ) ; // use MemberNotNull when available https://github.com/dotnet/roslyn/issues/41964
166170 long memberValue = memberConstant . Int64Value ;
167171
168172 // Do we have an exact matching enum field
@@ -213,7 +217,10 @@ private static string DisplaySignedEnumConstant(TypedConstant constant, SpecialT
213217 }
214218
215219 // Unable to decode the enum constant, just display the integral value
216- return constant . ValueInternal . ToString ( ) ;
220+ Debug . Assert ( constant . ValueInternal is object ) ;
221+ var result = constant . ValueInternal . ToString ( ) ;
222+ Debug . Assert ( result is object ) ;
223+ return result ;
217224 }
218225 }
219226}
0 commit comments