question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Numeric literals are not handled properly

See original GitHub issue

RoslynQuoter 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:open
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
svickcommented, May 30, 2021

@KirillOsenkov It’s much better, but still not fully fixed. The generated code now 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.0)),
                Token(SyntaxKind.CommaToken),
                LiteralExpression(
                    SyntaxKind.NumericLiteralExpression,
                    Literal(42f)),
                Token(SyntaxKind.CommaToken),
                LiteralExpression(
                    SyntaxKind.NumericLiteralExpression,
                    Literal(42m)),
                Token(SyntaxKind.CommaToken),
                LiteralExpression(
                    SyntaxKind.NumericLiteralExpression,
                    Literal(42.0m)),
                Token(SyntaxKind.CommaToken),
                LiteralExpression(
                    SyntaxKind.NumericLiteralExpression,
                    Literal(42.00m))})))
.NormalizeWhitespace()

This produces:

new object[]{42, 42, 42F, 42M, 42.0M, 42.00M}

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.

1reaction
svickcommented, Nov 27, 2017

I suspect that using the SyntaxFactory.Literal(string text, SomeType value) overloads is going to work.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found