Skip to content

generate unchecked((int)0x91234567) instead of int.MinValue + 0x11234567 #1115

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

Conversation

TymurGubayev
Copy link
Contributor

Problem

#1064
For &H80040E19 we get int.MinValue + 0x00040E19. With this PR, we'd get unchecked((int)0x80040E19) instead, thus preserving the hex literal value.

Solution

Corresponding code in LiteralConversion.cs was adjusted.

Additionally, I had to change CsWouldBeSimplifiedIncorrectly to return true for CastExpressionSyntax with Parent that's a CheckedExpressionSyntax, because otherwise it would've caused CS0266: "Cannot implicitly convert type 'uint' to 'int'".
Note this rule is a bit too aggressive:

int i = unchecked(0x80040E19); //we prevent error CS0266 here
uint u = unchecked(0x80040E19); //but we also prevent this from happening

(and many more cases with different types)

I'm reasonable sure it doesn't matter though: this PR introduces the very first call to SyntaxFactory.CheckedExpression.

@TymurGubayev
Copy link
Contributor Author

the unit test that failed is unrelated, with following difference:

[System.Runtime.CompilerServices.CompilerGenerated()]
[global::System.Runtime.CompilerServices.CompilerGenerated()]

I had it locally once, and I think it's because of the modal dialog Debug.Fail wants to show (CachedReflectedDelegates.cs, line 60).

@TymurGubayev
Copy link
Contributor Author

watched the unit test error some more, it just happens sporadically from time to time.

@GrahamTheCoder
Copy link
Member

Interesting on the debug fall, I've never caught the root cause of that transient issue

@GrahamTheCoder GrahamTheCoder merged commit fc0a1e6 into icsharpcode:master Jul 20, 2024
2 checks passed
@TymurGubayev TymurGubayev deleted the feature/UseUncheckedFoxSomeHexLiterals branch July 22, 2024 13:37
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