Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Warn if empty template name has trailing colon #23907

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

Draft
som-snytt wants to merge 6 commits into scala:main
base: main
Choose a base branch
Loading
from som-snytt:issue/16072-trailing-colon-template-name

Conversation

Copy link
Contributor

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

Fixes #16072
Fixes #18020

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

Copy link
Contributor Author

som-snytt commented Sep 11, 2025
edited
Loading

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 from 26646c6 to 53d4ff7 Compare September 11, 2025 22:53
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_: :`.
@som-snytt som-snytt force-pushed the issue/16072-trailing-colon-template-name branch from 28e072e to d32f88d Compare September 12, 2025 07:56
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)
 }

When the user accidentally writes `val x_: Int` where the colon
belongs to the identifier as an operator suffix, tell them so.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
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

AltStyle によって変換されたページ (->オリジナル) /