Repository navigation
Conversation
The translator wrote a negative literal or an unnamed enum value without parentheses, so the generated C# read as something else: - Negate of a negative constant became --5 (a decrement). - A call on a negative constant became -5.ToString(), which negates the result. - An unnamed negative enum value or a cast of a negated operand to a non-keyword type became (DayOfWeek)-2, which C# reads as DayOfWeek minus 2 (CS0075). - A call on an unnamed enum value became (DayOfWeek)9.ToString(). Treat these constants like unary expressions when deciding to group, group a sign-led operand under a cast, and write a negative unnamed enum value as (T)(-2).
Contributor
|
@vtxe02023274 Thanks! added in v10.0.15-pre06 |
Contributor
Hi, @vtxe02023274. I recommend added tests directly in |
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.
The translator writes a negative literal or an unnamed enum value without parentheses, so the generated C# means something else or does not compile. With Mapster.Tool this shows up in generated mappers:
generates
Other cases from
ToScript()on development:Negate(Constant(-5))--5(decrement, CS1059)-(-5)Call(Constant(-5L), "CompareTo", 1L)-5l.CompareTo(1l)(-5l).CompareTo(1l)Constant((DayOfWeek)(-2))(DayOfWeek)-2(DayOfWeek)(-2)Call(Constant((DayOfWeek)9), "ToString")(DayOfWeek)9.ToString()((DayOfWeek)9).ToString()Convert(Negate(x), typeof(DayOfWeek))(DayOfWeek)-x(DayOfWeek)(-x)Change:
ShouldGrouptreats a negative numeric constant and an unnamed enum value as unary, so they are grouped as a call target and underNegate.-x,+x,--x,-5) is grouped. Keyword casts such as(long)-5were valid, they now read(long)(-5).(T)(-2). The assertion added in fix: write negative int, short and enum constants with the invariant culture #1040 expected)-2, it now expects)(-2).x - -5,-x,(DayOfWeek)x,DayOfWeek.Monday.ToString()are unchanged.Tests:
TestNegativeConstantsStayOneOperandfails on development (--5) and passes with the change. ExpressionDebugger.Tests (net6.0, net7.0, net48), Mapster.Tests and Mapster.Tool.Tests pass. I also compiled the generated mapper above with Roslyn: 4 errors before, none after.