Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToCSharpString outputs floats without a type suffix #76061

Open
DanJBower opened this issue Nov 24, 2024 · 2 comments
Open

ToCSharpString outputs floats without a type suffix #76061

DanJBower opened this issue Nov 24, 2024 · 2 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@DanJBower
Copy link

Version Used:

<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />

Steps to Reproduce:

  1. Use ToCSharpString on any TypedConstant that represents a float

Expected Behavior:

It produces a string that represents the float and could be used to initialise a new float in C#, i.e., 0.5f

Actual Behavior:

It produces a string without a type suffix, i.e., 0.5

Use case:

I'm creating a source generator that generates source code based on NamedArguments in AttributeData. When the named argument's value is retrieved, I use arg[x].Value.ToCSharpString() to turn the value into a string to place in the generated code and the string being generated for floats is causing compile errors

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 24, 2024
@CyrusNajmabadi
Copy link
Member

ToCSharpString doesn't guarantee the natural type in the expression matches the original. It just represents legal c# syntax.

@DanJBower
Copy link
Author

I wouldn't expect it to match the original (like if the original expression was complex where multiple floats were added together or something), but I was expecting it to produce compilable code. Would 0.5 be considered legal C# syntax for a typed constant representing a float, as wouldn't it always be interpreted as a double?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants