Skip to content

fix: keep negative constants and unnamed enum values as one operand - #1042

Merged
DocSvartz merged 1 commit into
MapsterMapper:developmentfrom
vtxe02023274:fix/negative-constant-operands
Dec 2, 2026
Merged

DocSvartz merged 1 commit into
MapsterMapper:developmentfrom
vtxe02023274:fix/negative-constant-operands

Conversation

@vtxe02023274

Copy link
Copy Markdown

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:

config.NewConfig<Src, Dst>()
    .Map(d => d.Day, s => (DayOfWeek)(-1))
    .Map(d => d.Text, s => (-5).ToString());

generates

Day = (DayOfWeek)-1,     // CS0075: DayOfWeek minus 1
Text = -5.ToString(),    // CS0023: negates the string

Other cases from ToScript() on development:

Expression Before After
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:

  • ShouldGroup treats a negative numeric constant and an unnamed enum value as unary, so they are grouped as a call target and under Negate.
  • Under a cast, an operand that starts with a sign (-x, +x, --x, -5) is grouped. Keyword casts such as (long)-5 were valid, they now read (long)(-5).
  • A negative unnamed enum value is written (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: TestNegativeConstantsStayOneOperand fails 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.

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).
@DocSvartz
DocSvartz merged commit 57069e6 into MapsterMapper:development Dec 2, 2026
2 checks passed
@DocSvartz

Copy link
Copy Markdown
Contributor

@vtxe02023274 Thanks! added in v10.0.15-pre06

@DocSvartz

Copy link
Copy Markdown
Contributor

Tests: TestNegativeConstantsStayOneOperand fails 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.

Hi, @vtxe02023274. I recommend added tests directly in Mapster.Tool.Tests. It has helper for validate compiling the generated mappers code text.

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