Skip to content

Fix #7421: Insert missing <outdent> tokens where a region ends #7429

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

Merged
merged 4 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ object Parsers {
if in.isNewLine then in.nextToken() else accept(SEMI)

def acceptStatSepUnlessAtEnd(altEnd: Token = EOF): Unit =
in.observeOutdented()
if (!isStatSeqEnd)
in.token match {
case EOF =>
Expand Down
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,13 @@ object Scanners {
token = INDENT
end observeIndented

/** Insert an <outdent> token if next token closes an indentation region */
def observeOutdented(): Unit = currentRegion match
case r: Indented if !r.isOutermost && closingRegionTokens.contains(token) =>
currentRegion = r.enclosing
insert(OUTDENT, offset)
case _ =>

/** - Join CASE + CLASS => CASECLASS, CASE + OBJECT => CASEOBJECT, SEMI + ELSE => ELSE, COLON + <EOL> => COLONEOL
* - Insert missing OUTDENTs at EOF
*/
Expand Down
2 changes: 2 additions & 0 deletions docs/docs/reference/other-new-features/indentation-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ There are two rules:
If the indentation width of the token on the next line is still less than the new current indentation width, step (2) repeats. Therefore, several `<outdent>` tokens
may be inserted in a row.

An `<outdent>` is also inserted if the next statement following a statement sequence starting with an `<indent>` closes an indentation region, i.e. is one of `then`, `else`, `do`, `catch`, `finally`, `yield`, `}` or `case`.

It is an error if the indentation width of the token following an `<outdent>` does not
match the indentation of some previous line in the enclosing indentation region. For instance, the following would be rejected.
```scala
Expand Down
6 changes: 1 addition & 5 deletions tests/neg/spaces-vs-tabs.check
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@
| Previous indent : 2 tabs
| Latest indent : 1 tab, 2 spaces
-- Error: tests/neg/spaces-vs-tabs.scala:8:1 ---------------------------------------------------------------------------
8 | else () // error // error
8 | else () // error
| ^
| Incompatible combinations of tabs and spaces in indentation prefixes.
| Previous indent : 2 tabs
| Latest indent : 1 space
-- [E040] Syntax Error: tests/neg/spaces-vs-tabs.scala:8:6 -------------------------------------------------------------
8 | else () // error // error
| ^
| ';' expected, but '(' found
-- Error: tests/neg/spaces-vs-tabs.scala:14:2 --------------------------------------------------------------------------
14 | else 2 // error // error
| ^
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/spaces-vs-tabs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Test
println(2) // error
println(3) // error
println(4) // error
else () // error // error
else () // error

object Test2

Expand Down
17 changes: 17 additions & 0 deletions tests/pos/i7421.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
object Main {

def main(args: Array[String]): Unit = {
if (args.length > 0) then
{
println(args)
} else {
println("Hello world!")
}
if (args.length > 0)
{
println(args)
} else {
println("Hello world!")
}
}
}