@@ -30,7 +30,7 @@ public static ExpressionSyntax GetLiteralExpression(object value, string textFor
30
30
var ( maybeTextForUser , maybeFullExpression ) = ConvertNumericLiteralValueText ( textForUser ?? value . ToString ( ) , value ) ;
31
31
if ( maybeFullExpression != null ) return maybeFullExpression ;
32
32
textForUser = maybeTextForUser ;
33
-
33
+
34
34
switch ( value )
35
35
{
36
36
case byte b :
@@ -68,7 +68,7 @@ public static ExpressionSyntax GetLiteralExpression(object value, string textFor
68
68
}
69
69
70
70
private static LiteralExpressionSyntax NumericLiteral ( SyntaxToken literal ) => SyntaxFactory . LiteralExpression ( CSSyntaxKind . NumericLiteralExpression , literal ) ;
71
-
71
+
72
72
/// <summary>
73
73
/// See LiteralConversions.GetLiteralExpression
74
74
/// These are all the literals where the type will already be correct from the literal declaration
@@ -149,17 +149,35 @@ private static (string textForUser, ExpressionSyntax MaybeFullExpression) Conver
149
149
string hexValue = textForUser . Substring ( 2 ) ;
150
150
textForUser = "0x" + hexValue ;
151
151
152
- int parsedHexValue = int . Parse ( hexValue , NumberStyles . HexNumber , CultureInfo . InvariantCulture ) ;
152
+ bool replaceWithUncheckedExpr = false ;
153
+ LiteralExpressionSyntax hexValueExpr = default ;
154
+ CSSyntaxKind csSyntaxKind = CSSyntaxKind . None ;
155
+
156
+ if ( value is long ) {
157
+ long parsedHexValue = long . Parse ( hexValue , NumberStyles . HexNumber , CultureInfo . InvariantCulture ) ;
158
+ if ( parsedHexValue < 0L ) {
159
+ hexValueExpr = NumericLiteral ( SyntaxFactory . Literal ( textForUser , parsedHexValue ) ) ;
160
+ csSyntaxKind = CSSyntaxKind . LongKeyword ;
161
+ replaceWithUncheckedExpr = true ;
162
+ }
163
+ } else if ( value is int ) {
164
+ int parsedHexValue = int . Parse ( hexValue , NumberStyles . HexNumber , CultureInfo . InvariantCulture ) ;
165
+ if ( parsedHexValue < 0 ) {
166
+ hexValueExpr = NumericLiteral ( SyntaxFactory . Literal ( textForUser , parsedHexValue ) ) ;
167
+ csSyntaxKind = CSSyntaxKind . IntKeyword ;
168
+ replaceWithUncheckedExpr = true ;
169
+ }
170
+ }
153
171
154
172
// This is a very special case where for 8 digit hex strings, C# interprets them as unsigned ints, but VB interprets them as ints
155
173
// This can lead to a compile error if assigned to an int in VB. So in a case like 0x91234567, we generate `unchecked((int)0x91234567)`
156
174
// This way the value looks pretty close to before and remains a compile time constant
157
- if ( parsedHexValue < 0 ) {
158
- var hexValueExpr = NumericLiteral ( SyntaxFactory . Literal ( textForUser , parsedHexValue ) ) ;
175
+ // The same applies to 16 digit hex strings that are converted to long
176
+ if ( replaceWithUncheckedExpr ) {
159
177
var checkedExpr = SyntaxFactory . CheckedExpression (
160
178
CSSyntaxKind . UncheckedExpression ,
161
179
SyntaxFactory . CastExpression (
162
- SyntaxFactory . PredefinedType ( SyntaxFactory . Token ( CSSyntaxKind . IntKeyword ) ) ,
180
+ SyntaxFactory . PredefinedType ( SyntaxFactory . Token ( csSyntaxKind ) ) ,
163
181
hexValueExpr ) ) ;
164
182
return ( null , checkedExpr ) ;
165
183
}
@@ -172,20 +190,20 @@ private static (string textForUser, ExpressionSyntax MaybeFullExpression) Conver
172
190
}
173
191
174
192
if ( value switch {
175
- ulong _ => "UL" ,
176
- long _ => "L" ,
177
- uint _ => "U" ,
178
- int _ => "" ,
179
- ushort _ => "" ,
180
- short _ => "" ,
181
- double _ => "d" ,
182
- float _ => "f" ,
183
- decimal _ => "m" ,
184
- _ => default
185
- } is { } suffix ) {
193
+ ulong _ => "UL" ,
194
+ long _ => "L" ,
195
+ uint _ => "U" ,
196
+ int _ => "" ,
197
+ ushort _ => "" ,
198
+ short _ => "" ,
199
+ double _ => "d" ,
200
+ float _ => "f" ,
201
+ decimal _ => "m" ,
202
+ _ => default
203
+ } is { } suffix ) {
186
204
textForUser += suffix ;
187
205
}
188
206
189
207
return ( textForUser , null ) ;
190
208
}
191
- }
209
+ }
0 commit comments