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

Decimal constant improvements #1236

Merged

Conversation

ashmind
Copy link
Contributor

@ashmind ashmind commented Jul 25, 2018

This covers two main things:

  1. Added support for decimal optional parameters, e.g. M(decimal d = 1m). Since those are stored as [DecimalConstant(...)] and not standard constants, previously they would not decompile correctly.

  2. Added support for new setting DecimalConstants. If set to false, decimal constants and optional parameters will explicitly expose [DecimalConstant(...)] instead of pretending to be a literal. This is required by SharpLab since I want to expose inner workings as close as possible.

Both changes include unit-tests.

(ref: ashmind/SharpLab#316)

@ashmind ashmind changed the title Bugfix for decimal optional arguments, Decimal constant improvements Jul 25, 2018
Added option to uglify presentation of decimal constants (show `[DecimalConstant(...)]`).
@ashmind ashmind force-pushed the feature/decimal-constant-improvements branch from 2999383 to d15fe0f Compare July 25, 2018 12:43
Copy link
Member

@dgrunwald dgrunwald left a comment

Choose a reason for hiding this comment

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

I didn't realize there was a difference between these methods, and that C# can call both without specifying an argument:

public static void Test1(CancellationToken ct = default) { }
public static void Test2([Optional] CancellationToken ct) { }

Given this difference, the introduction of HasConstantValueInSignature makes sense.

The PR looks good to me (except for the thread-safety issues). Thanks!

@@ -42,7 +42,7 @@ sealed class MetadataField : IField
object constantValue;
IType type;
bool isVolatile; // initialized together with this.type
byte decimalConstant; // 0=no, 1=yes, 2=unknown
bool? isDecimalConstant;
Copy link
Member

Choose a reason for hiding this comment

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

This change is not thread-safe.
In the rare case where multiple threads are concurrently initializing isDecimalConstant, we need accesses to be atomic.
But Nullable<T> is a struct and may have torn reads/writes.

@@ -37,6 +37,8 @@ sealed class MetadataParameter : IParameter

// lazy-loaded:
string name;
bool? hasConstantValueInSignature;
bool? isDecimalConstant;
Copy link
Member

Choose a reason for hiding this comment

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

Same thread-safety issues here.

@ashmind
Copy link
Contributor Author

ashmind commented Jul 26, 2018

@dgrunwald Thanks, fixed -- didn't ever consider torn reads before, something to keep in mind.
Not sure if I should have gone with an enum here -- please let me know.

@dgrunwald dgrunwald merged commit e92201c into icsharpcode:master Jul 28, 2018
ElektroKill added a commit to dnSpyEx/ILSpy that referenced this pull request Jul 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants