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

Fix bugs with generation for ctor param default values #62798

Merged
merged 3 commits into from
Jan 6, 2022

Conversation

layomia
Copy link
Contributor

@layomia layomia commented Dec 14, 2021

Fixes source gen issue where invalid code is generated by JSON source-gen when supporting default ctor param values, preventing successful app compilation.
Fixes #62766.
Candidate for 6.0.x servicing fix.

cc @333fred

@layomia layomia added this to the 7.0.0 milestone Dec 14, 2021
@layomia layomia self-assigned this Dec 14, 2021
@ghost
Copy link

ghost commented Dec 14, 2021

Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes source gen issue where invalid code is generated by JSON source-gen when supporting default ctor param values, preventing successful app compilation.
Fixes #62766.
Candidate for 6.0.x servicing fix.

cc @333fred

Author: layomia
Assignees: layomia
Labels:

area-System.Text.Json

Milestone: 7.0.0

Copy link
Member

@steveharter steveharter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some questions, but otherwise LGTM.

@layomia layomia force-pushed the CtorBug branch 3 times, most recently from c435ff0 to 100156a Compare December 22, 2021 20:34
return ToCSharpKeyword(@bool);
default:
// Assume this is a number.
return $"({typeRef})({value.ToString()})";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to use invariant culture? What if the current culture is one that uses something other than - for a negative sign?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use invariant culture and avoid generating invalid C#. I plan to figure out some testing for this scenario in a follow up.

cc @ericstj on whether the dotnet/arcade implementations has similar localization concerns - https://github.com/dotnet/arcade/blob/0cd94b1d02c03377d99f3739beb191591f6abee5/src/Microsoft.Cci.Extensions/Writers/CSharp/CSDeclarationWriter.Attributes.cs#L313

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @safern. It does special case certain types and use invariant culture, but not all (including int). It's possible it has the issue but we don't notice since it's only used when folks run genapi which tends to be done by devs on english systems who review the diff it generates.

I'm a bit surprised we have to write this much ourselves. Does Roslyn have an API to give us the formatted default value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see @stephentoub pointed this out here: #62798 (comment)

Nice! @safern take note, GenAPI could use this.

@layomia layomia merged commit 98c5328 into dotnet:main Jan 6, 2022
@layomia
Copy link
Contributor Author

layomia commented Jan 6, 2022

/backport to release/6.0

@github-actions
Copy link
Contributor

github-actions bot commented Jan 6, 2022

Started backporting to release/6.0: https://github.com/dotnet/runtime/actions/runs/1665097854

@github-actions
Copy link
Contributor

github-actions bot commented Jan 6, 2022

@layomia backporting to release/6.0 failed, the patch most likely resulted in conflicts:

$ git am --3way --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix bugs with generation for ctor param default values
.git/rebase-apply/patch:109: trailing whitespace.
        
warning: 1 line adds whitespace errors.
Using index info to reconstruct a base tree...
M	src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
M	src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Double.cs
M	src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Float.cs
M	src/libraries/System.Text.Json/tests/Common/ConstructorTests/ConstructorTests.ParameterMatching.cs
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/System.Text.Json/tests/Common/ConstructorTests/ConstructorTests.ParameterMatching.cs
CONFLICT (content): Merge conflict in src/libraries/System.Text.Json/tests/Common/ConstructorTests/ConstructorTests.ParameterMatching.cs
Auto-merging src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Float.cs
Auto-merging src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Double.cs
Auto-merging src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 Fix bugs with generation for ctor param default values
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

layomia added a commit to layomia/dotnet_runtime that referenced this pull request Jan 7, 2022
* Fix bugs with generation for ctor param default values

* Address review feedback

* Address feedback
danmoseley pushed a commit that referenced this pull request Jan 8, 2022
* Fix bugs with generation for ctor param default values

* Address review feedback

* Address feedback
@ghost ghost locked as resolved and limited conversation to collaborators Feb 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

System.Text.Json generator does not generate valid code for empty string default parameter constructors
5 participants