Skip to content

Conversation

@som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Sep 11, 2025

Fixes #16072
Fixes #18020

Warn to guard against colon fusion in object x_: and def x_: String.

Indent warning is made awesome.

@som-snytt
Copy link
Contributor Author

som-snytt commented Sep 11, 2025

Note that it's less helpful if scanner issues the indent warning "out of order".

-- Warning: tests/warn/i16072.scala:4:2 --------------------------------------------------------------------------------4 |  def x = 1 // warn too far right
  |  ^
  |  Line is indented too far to the right, or a `{` or `:` is missing
-- [E222] Syntax Warning: tests/warn/i16072.scala:3:7 ------------------------------------------------------------------3 |object Hello_: // warn colon in name without backticks because the body is empty
  |       ^^^^^^^
  |       name `Hello_:` should be enclosed in backticks

Must follow up givens and enums.

@som-snytt som-snytt force-pushed the issue/16072-trailing-colon-template-name branch 2 times, most recently from 28e072e to d32f88d Compare September 12, 2025 07:56
@som-snytt
Copy link
Contributor Author

My first swing from Dec 2022 was more direct:

     def checkNextNotIndented(): Unit =
-      if in.isNewLine then
+      if testNextNotIndented() then
+        warning(em"Line is indented too far to the right, or a `{` or `:` is missing", in.next.offset)
+
+    def testNextNotIndented(): Boolean =
+      in.isNewLine && {
         val nextIndentWidth = in.indentWidth(in.next.offset)
-        if in.currentRegion.indentWidth < nextIndentWidth then
-          warning(em"Line is indented too far to the right, or a `{` or `:` is missing", in.next.offset)
+        in.currentRegion.indentWidth < nextIndentWidth
+      }

+    // Catch inadvertent fusion of punctuation as operator char (name_:)
+    private def checkSuspiciousColon(name: Name, offset: Offset): Unit =
+      if name.endsWith(":") && testNextNotIndented() then
+        warning(em"name ends in `:` before indented line", offset-1)
+

     def classDef(start: Offset, mods: Modifiers): TypeDef = atSpan(start, nameStart) {
-      classDefRest(start, mods, ident().toTypeName)
+      val name = ident()
+      checkSuspiciousColon(name, nameStart)
+      classDefRest(start, mods, name.toTypeName)
     }

@som-snytt
Copy link
Contributor Author

I wonder how wedded folks are to forms of quote. I like the idea of backticks because of markdown, but the char literal printed by showToken seems more natural. I would accept backticks for user identifiers, but token ticks for tokens.

Welcome to Scala 3.8.0-RC1-bin-SNAPSHOT-nonbootstrapped-git-d394833 (23.0.2, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> locally {
     |     val x = 42
     |   val y = 27
     | }
1 warning found
-- [E223] Syntax Warning: ------------------------------------------------------
3 |  val y = 27
  |  ^
  |  Line is indented too far to the left, or a '}' is missing
  |
  | longer explanation available when compiling with `-explain`

scala>
➜  snips scala-cli repl --server=false -S 3.7.2
Welcome to Scala 3.7.2 (23.0.2, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> locally {
     |     val x = 42
     |   val y = 27
     | }
1 warning found
-- Warning: --------------------------------------------------------------------
3 |  val y = 27
  |  ^
  |  Line is indented too far to the left, or a `}` is missing

@som-snytt som-snytt force-pushed the issue/16072-trailing-colon-template-name branch from d32f88d to 5e4d1c6 Compare September 20, 2025 01:18
This is to guard the edge case `object X_:` where the user
may or may not have intended colon syntax. The next line does
not tell us, since it may be indented yet not nested. Therefore,
any empty template with a suspicious name will warn. Non-empty
templates are given a pass even if written `object X_: :`.
When the user accidentally writes `val x_: Int` where the colon
belongs to the identifier as an operator suffix, tell them so.
@som-snytt
Copy link
Contributor Author

Why was the information-rich comment too verbose?

See below.

Why was the ticket worth tackling?

If it surprises keynmol, it can surprise any of us. The syntax is a mild language wart but easy enough to compensate for (by linting). (Arguably, people should be persuaded not to give underscore special meaning as a leading or trailing character in a name. For all the reduction in underscore semantics, it is still fundamental to operator names; it is not farfetched to suggest it should be discouraged for other snake purposes, in the absence of backticks, just like embedded dollar.) (Compare making underscore no longer available as an identifier.)

How I fixed it

My first attempt was to consider any such name, or such a name followed by indentation. For context, however, it's simpler to query whether the template has a body; I think #16072 (comment) by abgruszecki suggested it to me.

The improvement for def f String = "" was suggested on the ticket by dwijnand.

Why is this PR worth reviewing?

There is no syntax change, but only a warning for the two gotchas. The change is confined to the parser.

If I spend another hour on it, I'll add dwijnand's idea to split the unintended identifier in def s_: String = "" (with a warning).

What's the worse that could happen?

TBD

@som-snytt som-snytt changed the title Warn if empty template name has trailing colon Warn if name of empty template has trailing colon Nov 25, 2025
@som-snytt
Copy link
Contributor Author

som-snytt commented Nov 25, 2025

Indent warning is made awesome --> "awesomaticized".

Rebased. Updated check file for change in error id, but dropped the last commit as previous research.

@som-snytt som-snytt force-pushed the issue/16072-trailing-colon-template-name branch from 5e4d1c6 to 524ee92 Compare November 25, 2025 15:23
@som-snytt som-snytt marked this pull request as ready for review November 25, 2025 15:59
@som-snytt som-snytt marked this pull request as draft November 25, 2025 17:22
@som-snytt som-snytt force-pushed the issue/16072-trailing-colon-template-name branch from 524ee92 to 7874d12 Compare November 25, 2025 17:23
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.

Confusing parsing error when defining a val named _root_ with a type Surprising parsing behaviour for objects ending with _

1 participant