-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Patch indentation when removing braces (and other bug fixes in -indent -rewrite
)
#17522
base: main
Are you sure you want to change the base?
Conversation
e355923
to
017c518
Compare
a415bdc
to
c7a3eb5
Compare
If the first indentation of the region is greater than the indentation of the enclosing region, we use it to indent the whole region. Otherwise we use the incremented indentation of the enclosing region. ```scala def foo = { x // we replicate indentation of x downward in region y } ``` ```scala def foo = { x // indentation of x is incorrect, we increment enclosing indentation y } ``` A bigger indentation than the required one is permitted except just after a closing brace. ```scala def bar = { x .toString // permitted indentation def foo = { } bar // must be unindented, to not fall into the body of foo } ``` And other bug fixes (see scala#17522)
@@ -1545,7 +1553,7 @@ object Scanners { | |||
|
|||
/* Initialization: read first char, then first token */ | |||
nextChar() | |||
nextToken() | |||
(this: @unchecked).nextToken() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initialization of ToIndentParser
is unsafe because the ctor of Parser
calls in.nextToken()
where in
is a ToIndentScanner
and can access uninitialized fields in ToIndentParser
. In practice the if token != EMPTY
in ToIndentScanner.nextToken
prevents this from happening.
|
||
import ast.untpd._ | ||
|
||
class ScriptParser(source: SourceFile)(using Context) extends Parser(source) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that ScriptParser
is no so much useful anymore and I would like to avoid adding a ToIndentScriptParser
.
@adpi2 Some tests are still failing. |
Remove ScriptParsers
We cannot remove braces after a new line, to prevent from removing braces in stat seq.
The CI is green again. |
ping @odersky? |
Primary goal
The goal of this PR is to ensure the
-ident
rewrite does not break the compilation, by patching the indentation where needed. It fixes #17456.For instance, if we consider this piece of code:
It used to be rewritten to:
After the PR it is rewritten to:
They are many other patterns of compilation breakages involving indentation: in control structure, in pattern matching, in argument passing.
The PR consists mostly in two parts:
Some important considerations are:
should stay unchanged. The rewrite is not a formatting tool, it tries to do as few changes as possible.
The associated tests are indent-3-spaces.scala and indent-mix-tab-space.
Secondary goals
While working on this I found many other bugs or unintended behaviors that I fixed along the way.
Disabling rewrite in Outline Parser
The outline parser, that parses the source path, should not emit any patch.
Preservation of comments
The indent rewrite should not remove any comments.
should be rewritten to
The associated test is indent-comments.scala.
Back-quoting operators in brace-less syntax
foo >=> { bar }
is rewritten to
foo `>=>`: bar
Do not remove braces after
}
should not be rewritten to:
but left unchanged.
Do not remove braces of blocks in sequence of statements
Fix #10986
should not be rewritten to:
In general we cannot remove the braces of a standalone block in a sequence of statements.
Back-quoting
end
ident inend match {
Otherwise it is interpreted as an end marker.
Tests and results
Added tests
Rewritting dotty to indent
The output of running the rewrite in the
scala3-library
andscala3-compiler
can be found in #17618.In summary, it changed 13.381 lines and removed 10.034 lines. It removed more than 10.000 pairs of
{}
. In the other 3.350 lines there are some} else {
rewritten toelse
, some} // comment
rewritten to// comment
but also some fixed indentations.Here are a few noticeable patches:
Running the rewrite in the community build
Here is the current status of running the rewrite in the entire community build (in #17617):
Community Build A
-no-ident
was used)a test fails because of source lines
Community Build B
Community Build C
Why does the rewrite fails in some community build projects?
-no-indent
it is not possible to rewrite it to-indent
.-new-syntax -rewrite
before running-indent -rewrite
. Some exotic patterns that are valid with the new control structure syntax are not with the combination of the old and brace-less syntax. For instance:This is valid:
This is valid:
But this is not valid: