-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
from
som-snytt:issue/16072-trailing-colon-template-name
Draft
Warn if empty template name has trailing colon #23907
som-snytt
wants to merge
6
commits into
scala:main
from
som-snytt:issue/16072-trailing-colon-template-name
+172
−30
Conversation
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
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
September 11, 2025 22:53
26646c6
to
53d4ff7
Compare
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
September 12, 2025 07:56
28e072e
to
d32f88d
Compare
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.
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
September 20, 2025 01:18
d32f88d
to
5e4d1c6
Compare
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.
Uh oh!
There was an error while loading. Please reload this page.
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