Numeric literals are not handled properly
See original GitHub issueRoslynQuoter doesn’t generate the correct code for numeric literals. For example, consider this expression:
new object[] { 42, 42.0, 42f, 42m, 42.0m, 42.00m }
Those six values all have different syntax and are also different values. But the code generated by RQ is:
ArrayCreationExpression(
ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword))).WithRankSpecifiers(
SingletonList<ArrayRankSpecifierSyntax>(
ArrayRankSpecifier(
SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression())))))
.WithInitializer(
InitializerExpression(
SyntaxKind.ArrayInitializerExpression,
SeparatedList<ExpressionSyntax>(
new SyntaxNodeOrToken[]
{
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(42)),
Token(SyntaxKind.CommaToken),
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(42)),
Token(SyntaxKind.CommaToken),
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(42)),
Token(SyntaxKind.CommaToken),
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(42)),
Token(SyntaxKind.CommaToken),
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(42.0)),
Token(SyntaxKind.CommaToken),
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(42.00))
}))).NormalizeWhitespace()
Evaluating this results in:
new object[]{42, 42, 42, 42, 42, 42}
I think that RQ should generate code that at least preserves semantics. (Ideally, it should preserve the exact syntax used, but I think that’s less important.)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Inconsistency parsing numeric literals according to C++ ...
Reading through the C++17 standard, it seems to me that there is an inconsistency between pp-number as handled by the preprocessor and numeric ......
Read more >about Numeric Literals - PowerShell
Integer literals can be written in decimal, hexadecimal, or binary notation. Hexadecimal literals are prefixed with 0x and binary literals ...
Read more >Early error on '0' followed by '8' or '9' in numeric literals ...
We're talking about something different here, legacy decimal integer literals starting with 0 and containing 8 or 9. As far as I know,...
Read more >Python Variables, Constants and Literals (With Examples)
In this tutorial, we will learn about Python variables, constants, literals ... Numeric literals can belong to 3 different numerical types: Integer ,...
Read more >Literals in Programming Languages
This paper covers the history and use of literals (or constants) in programming languages, from the beginning of programming to the present ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@KirillOsenkov It’s much better, but still not fully fixed. The generated code now is:
This produces:
So the only remaining problematic case is
double
, which should be fixed if https://github.com/dotnet/roslyn/issues/23403 is fixed. I guess this means you can now either close this issue as a duplicate of the Roslyn issue, or keep it open if you think workaround in RQ would be worth it.I suspect that using the
SyntaxFactory.Literal(string text, SomeType value)
overloads is going to work.