C#: Fix numeric literal type attribution to reflect suffix#7187
Merged
knutwannheden merged 1 commit intomainfrom Mar 28, 2026
Merged
Conversation
Numeric literals with type suffixes (e.g., 0L, 1.0F, 1.0D) were all attributed as JavaType.Primitive(Int) because GetPrimitiveType hardcoded NumericLiteralExpression to Int. Now uses Roslyn's semantic model to resolve the actual type for numeric literals, correctly producing Long, Float, Double, etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Numeric literals with type suffixes (e.g.,
0L,1.0F,2.0D) were all attributed asJavaType.Primitive(Int)becauseGetPrimitiveTypehardcodedNumericLiteralExpressiontoInt, ignoring the actual type. This forced downstream recipes likeMarkLocalVariableAsConst(in recipes-csharp) to implement a workaround (InferTypeFromLiteralSuffix) that manually parsed suffixes fromValueSource— which itself had bugs (e.g., interpreting.in string literals asdouble).Summary
SemanticModel.GetTypeInfo()via_typeMapping?.Type(node)to resolve the correct type for numeric literals, falling back to the hardcodedIntonly when no semantic model is availableParse()andFindFirst<T>()test helpers toRewriteTestfor tests that need to inspect AST node properties beyond round-trip printing0L,0l,42,1.0f,1.0F,1.0d,1.0D, and1.0with their expected primitive kindsTest plan
NumericLiteralTypeAttributiontest cases passLiteralTestspass (existing + new)