Skip to content

Warn dubious negative literal syntax - #24163

Merged
som-snytt merged 1 commit into
scala:mainfrom
som-snytt:topic/warn-minus-literal
May 12, 2026
Merged

som-snytt merged 1 commit into
scala:mainfrom
som-snytt:topic/warn-minus-literal

Conversation

@som-snytt

@som-snytt som-snytt commented Oct 10, 2025 •

Copy link
Copy Markdown
Contributor

Warns about - 42 (the misconception that space is significant);
and -42.abs (the misconception that it parses the same as -x.abs, i.e. -(42.abs)).

Patches are emitted for -rewrite.

Tweak prefixExpr to exclude subsequent expression on the next line.

Tweak simplePattern to exclude backquoted hyphen as unary minus.

Fixes #7910
Fixes #24162

@som-snytt

Copy link
Copy Markdown
Contributor Author

Resuscitates #12459

Addresses the question at https://contributors.scala-lang.org/t/functional-syntax/5062/1

Could use code actions.

@som-snytt

Copy link
Copy Markdown
Contributor Author

The examples are at the far edge of edge cases. Maybe there are bigger warts to justify -Wsyntax?

@som-snytt som-snytt closed this Oct 27, 2025
@som-snytt som-snytt reopened this May 4, 2026
@som-snytt

Copy link
Copy Markdown
Contributor Author

Better idea is no option, definitely no error ID. Maybe a bare warning but definitely a rewrite. If it's a syntax quirk that no one ought to ever exploit, it could be an error to write it like that.

@SolalPirelli

Copy link
Copy Markdown
Contributor

I think no option, always warn makes sense. Probably also easier to get the PR merged that way (especially if the option would use a very generic term like -Wsyntax, that seems... bikesheddable)

@SolalPirelli

Copy link
Copy Markdown
Contributor

(happy to review after a rebase)

@som-snytt
som-snytt force-pushed the topic/warn-minus-literal branch from 9826103 to 6f6d667 Compare May 12, 2026 05:29
@som-snytt som-snytt changed the title Warn negative literal parsing under -Wsyntax Warn dubious negative literal syntax May 12, 2026
@som-snytt
som-snytt marked this pull request as ready for review May 12, 2026 05:33
@Gedochao
Gedochao requested a review from SolalPirelli May 12, 2026 06:42

@SolalPirelli SolalPirelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice!

if (op.name == nme.raw.MINUS && isNumericLit)
simpleExprRest(literal(start), location, canApply = true)
if isNegatedNumber then
simpleExprRest(literal(start = in.skipToken()), location, canApply = true)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't fully understand why we skip a token here. Isn't start the location of the minus sign, per the Scaladoc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, literal restores the - op by testing the positions. I'm sorry I changed the name to start, I think that was when I was reading the code. I don't know or remember why the tokens are joined in parser and not in scanner, which joins other things (case class). But it certainly avoids a strval that is "- 42", while incurring the current problem.

Oh, I guess it's useful to call it start for the span to patch.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah OK so it's literal that has odd semantics, this here makes sense then, thanks

ch match
case 'x' | 'X' => base = 16; nextChar(); checkNumberChar()
case 'b' | 'B' => base = 2; nextChar(); checkNumberChar()
case _ => base = 10; putChar('0')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: no indentation for a match?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was doing that for a while; I guess it still looks natural to me.

Inline fetchLeadingZero in getNumber

Use isNegatedNumber to exclude backtick minus

`- 42.abs` must remove inner patch to apply `(-42)`.
@som-snytt
som-snytt force-pushed the topic/warn-minus-literal branch from 6f6d667 to cc07985 Compare May 12, 2026 13:02
@som-snytt

Copy link
Copy Markdown
Contributor Author

If we had hashtags, this would be resolving #frivoloustickets.

@SolalPirelli

Copy link
Copy Markdown
Contributor

We might get hashtags soon :p

@SolalPirelli
SolalPirelli enabled auto-merge (squash) May 12, 2026 13:07
@som-snytt
som-snytt disabled auto-merge May 12, 2026 13:08
@som-snytt
som-snytt enabled auto-merge (squash) May 12, 2026 13:08
@som-snytt

Copy link
Copy Markdown
Contributor Author

I was composing a message... in case I have to read it when reverting.

@som-snytt
som-snytt merged commit 21dd6a6 into scala:main May 12, 2026
46 checks passed
@som-snytt
som-snytt deleted the topic/warn-minus-literal branch May 12, 2026 14:09
@mbovel

mbovel commented May 14, 2026 •

Copy link
Copy Markdown
Member

It seems that breaks Scala parallel collections.
At least it broke the version I have in the benchmarks: lampepfl/scala3-benchmarks@68578f4.

It seems quite an impactful change.
Might be worth running the open community build on this.

@WojciechMazur WojciechMazur added this to the 3.9.0 milestone May 26, 2026
bishabosha pushed a commit to dotty-staging/dotty that referenced this pull request Jun 5, 2026
Warns about `- 42` (the misconception that space is significant);
and `-42.abs` (the misconception that it parses the same as `-x.abs`,
i.e. `-(42.abs)`).

Patches are emitted for `-rewrite`.

Tweak `prefixExpr` to exclude subsequent expression on the next line.

Tweak `simplePattern` to exclude backquoted hyphen as unary minus.

Fixes scala#7910 
Fixes scala#24162
@xuwei-k

xuwei-k commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

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.

Backquote quirk in pattern Unary identifier has undue expectation of subsequent expression

5 participants