Skip to content

Enforce indentation off-side rule #10691

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

Closed
wants to merge 3 commits into from
Closed
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
129 changes: 83 additions & 46 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,21 @@ object Parsers {
finally staged = saved
}

private var strictIndent = false
private def withStrictIndent[T](body: => T): T = {
val saved = strictIndent
strictIndent = true
try body
finally strictIndent = saved
}

private def withoutStrictIndent[T](body: => T): T = {
val saved = strictIndent
strictIndent = false
try body
finally strictIndent = saved
}

/* ---------- TREE CONSTRUCTION ------------------------------------------- */

/** Convert tree to formal parameter list
Expand Down Expand Up @@ -1281,16 +1296,21 @@ object Parsers {
in.sourcePos())
patch(source, Span(in.offset), " ")

def possibleTemplateStart(isNew: Boolean = false): Unit =
def possibleTemplateStart[A](isNew: Boolean = false)(rest: => A): A =
in.observeColonEOL()
if in.token == COLONEOL || in.token == WITHEOL then
if in.lookahead.isIdent(nme.end) then in.token = NEWLINE
val indented =
if in.token == COLONEOL || in.token == WITHEOL then
if in.lookahead.isIdent(nme.end) then in.token = NEWLINE
else
in.nextToken()
if in.token != INDENT && in.token != LBRACE then
syntaxErrorOrIncomplete(i"indented definitions expected, ${in}")
true
else
in.nextToken()
if in.token != INDENT && in.token != LBRACE then
syntaxErrorOrIncomplete(i"indented definitions expected, ${in}")
else
newLineOptWhenFollowedBy(LBRACE)
newLineOptWhenFollowedBy(LBRACE)
in.next.token == LBRACE
if indented then withStrictIndent(rest)
else rest

def checkEndMarker[T <: Tree](stats: ListBuffer[T]): Unit =

Expand Down Expand Up @@ -2309,19 +2329,22 @@ object Parsers {
def newExpr(): Tree =
val start = in.skipToken()
def reposition(t: Tree) = t.withSpan(Span(start, in.lastOffset))
possibleTemplateStart()
val parents =
if in.isNestedStart then Nil
else constrApps(commaOK = false)
colonAtEOLOpt()
possibleTemplateStart(isNew = true)
parents match {
case parent :: Nil if !in.isNestedStart =>
reposition(if (parent.isType) ensureApplied(wrapNew(parent)) else parent)
case _ =>
New(reposition(templateBodyOpt(emptyConstructor, parents, Nil)))
possibleTemplateStart() {
val parents =
if in.isNestedStart then Nil
else constrApps(commaOK = false)
colonAtEOLOpt()
possibleTemplateStart(isNew = true) {
parents match {
case parent :: Nil if !in.isNestedStart =>
reposition(if (parent.isType) ensureApplied(wrapNew(parent)) else parent)
case _ =>
New(reposition(templateBodyOpt(emptyConstructor, parents, Nil)))
}
}
}


/** ExprsInParens ::= ExprInParens {`,' ExprInParens}
*/
def exprsInParensOpt(): List[Tree] =
Expand Down Expand Up @@ -3660,12 +3683,13 @@ object Parsers {
tokenSeparated(COMMA, () => convertToTypeId(qualId()))
}
else Nil
possibleTemplateStart()
if (isEnum) {
val (self, stats) = withinEnum(templateBody())
Template(constr, parents, derived, self, stats)
possibleTemplateStart() {
if (isEnum) {
val (self, stats) = withinEnum(templateBody())
Template(constr, parents, derived, self, stats)
}
else templateBodyOpt(constr, parents, derived)
}
else templateBodyOpt(constr, parents, derived)
}

/** TemplateOpt = [Template]
Expand All @@ -3675,12 +3699,13 @@ object Parsers {
if in.token == EXTENDS || isIdent(nme.derives) then
template(constr)
else
possibleTemplateStart()
if in.isNestedStart then
template(constr)
else
checkNextNotIndented()
Template(constr, Nil, Nil, EmptyValDef, Nil)
possibleTemplateStart() {
if in.isNestedStart then
template(constr)
else
checkNextNotIndented()
Template(constr, Nil, Nil, EmptyValDef, Nil)
}

/** TemplateBody ::= [nl] `{' TemplateStatSeq `}'
* EnumBody ::= [nl] ‘{’ [SelfType] EnumStat {semi EnumStat} ‘}’
Expand All @@ -3705,10 +3730,11 @@ object Parsers {
/** with Template, with EOL <indent> interpreted */
def withTemplate(constr: DefDef, parents: List[Tree]): Template =
if in.token != WITHEOL then accept(WITH)
possibleTemplateStart() // consumes a WITHEOL token
val (self, stats) = templateBody()
Template(constr, parents, Nil, self, stats)
.withSpan(Span(constr.span.orElse(parents.head.span).start, in.lastOffset))
possibleTemplateStart() { // consumes a WITHEOL token
val (self, stats) = templateBody()
Template(constr, parents, Nil, self, stats)
.withSpan(Span(constr.span.orElse(parents.head.span).start, in.lastOffset))
}

/* -------- STATSEQS ------------------------------------------- */

Expand Down Expand Up @@ -3778,6 +3804,7 @@ object Parsers {
def templateStatSeq(): (ValDef, List[Tree]) = checkNoEscapingPlaceholders {
var self: ValDef = EmptyValDef
val stats = new ListBuffer[Tree]
var firstStatIndent: Option[IndentWidth] = None
if (isExprIntro && !isDefIntro(modifierTokens)) {
val first = expr1()
if (in.token == ARROW) {
Expand All @@ -3793,13 +3820,22 @@ object Parsers {
in.nextToken()
}
else {
val indent = in.indentWidth(in.offset)
if (firstStatIndent.isEmpty) firstStatIndent = Some(indent)
else ()
stats += first
acceptStatSepUnlessAtEnd(stats)
}
}
var exitOnError = false
while (!isStatSeqEnd && !exitOnError) {
setLastStatOffset()
val indent = in.indentWidth(in.offset)
if (firstStatIndent.isEmpty) firstStatIndent = Some(indent)
else if (strictIndent && firstStatIndent != Some(indent))
syntaxErrorOrIncomplete(s"unexpected indent: found $indent expected ${firstStatIndent.get}")
else ()

if (in.token == IMPORT)
stats ++= importClause(IMPORT, mkImport())
else if (in.token == EXPORT)
Expand Down Expand Up @@ -3919,18 +3955,19 @@ object Parsers {
else
val pkg = qualId()
var continue = false
possibleTemplateStart()
if in.token == EOF then
ts += makePackaging(start, pkg, List())
else if in.isNestedStart then
ts += inDefScopeBraces(makePackaging(start, pkg, topStatSeq()), rewriteWithColon = true)
continue = true
else
acceptStatSep()
ts += makePackaging(start, pkg, topstats())
if continue then
acceptStatSepUnlessAtEnd(ts)
ts ++= topStatSeq()
possibleTemplateStart() {
if in.token == EOF then
ts += makePackaging(start, pkg, List())
else if in.isNestedStart then
ts += inDefScopeBraces(makePackaging(start, pkg, topStatSeq()), rewriteWithColon = true)
continue = true
else
acceptStatSep()
ts += makePackaging(start, pkg, topstats())
if continue then
acceptStatSepUnlessAtEnd(ts)
ts ++= topStatSeq()
}
}
else
ts ++= topStatSeq(outermost = true)
Expand Down
9 changes: 8 additions & 1 deletion compiler/test/dotty/tools/DottyTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import dotc.printing.Texts._
import dotc.reporting.ConsoleReporter
import dotc.core.Decorators._
import dotc.ast.tpd
import dotc.Compiler
import dotc.{CompilationUnit,Compiler}
import dotc.util.SourceFile

import dotc.core.Phases.Phase

Expand All @@ -38,6 +39,12 @@ trait DottyTest extends ContextEscapeDetection {
override def clearCtx() = {
ctx = null
}
def resetCtx(sourceFile: SourceFile) = {
clearCtx()
val c = initialCtx
c.setCompilationUnit(CompilationUnit(sourceFile, mustExist = false))
ctx = c
}

protected def initializeCtx(fc: FreshContext): Unit = {
fc.setSetting(fc.settings.encoding, "UTF8")
Expand Down
45 changes: 45 additions & 0 deletions compiler/test/dotty/tools/dotc/parsing/DottyScannerTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dotty.tools
package dotc
package parsing

import dotty.tools.io._
import scala.io.Codec
import util._
import Tokens._, Scanners._
import org.junit.Test

class DottyScannerTest extends ScannerTest {

val blackList = List(
"/scaladoc/scala/tools/nsc/doc/html/page/Index.scala",
"/scaladoc/scala/tools/nsc/doc/html/page/Template.scala"
)

def scanDir(path: String): Unit = scanDir(Directory(path))

def scanDir(dir: Directory): Unit = {
if (blackList exists (dir.jpath.toString endsWith _))
println(s"blacklisted package: ${dir.toAbsolute.jpath}")
else
for (f <- dir.files)
if (f.name.endsWith(".scala"))
if (blackList exists (f.jpath.toString endsWith _))
println(s"blacklisted file: ${f.toAbsolute.jpath}")
else
scan(new PlainFile(f))
for (d <- dir.dirs)
scanDir(d.path)
}

@Test
def scanList() = {
println(System.getProperty("user.dir"))
scan("compiler/src/dotty/tools/dotc/core/Symbols.scala")
scan("compiler/src/dotty/tools/dotc/core/Symbols.scala")
}

@Test
def scanDotty() = {
scanDir("compiler/src")
}
}
61 changes: 61 additions & 0 deletions compiler/test/dotty/tools/dotc/parsing/IndentTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package dotty.tools
package dotc
package parsing

import org.junit.Test

class IndentTest extends ParserTest:

@Test
def parseBraces: Unit =
val code = s"""
|class A {
| val x = 1
| val y = 2
|}""".stripMargin
assert(parseTextEither(code).isRight)

@Test
def parseIndents: Unit =
val code = s"""
|class A:
| val x = 1
| val y = 2
|""".stripMargin
assert(parseTextEither(code).isRight)

@Test
def innerClassIndents: Unit =
val code = s"""
|class A:
| class B:
| val x = 1
|""".stripMargin
assert(parseTextEither(code).isRight)

@Test
def extendsClassIndents: Unit =
val code = s"""
|class A extends B:
| override def hasUnreportedErrors: Boolean =
| infos.exists(_.isInstanceOf[Error])
|""".stripMargin
assert(parseTextEither(code).isRight)

@Test
def superfluousIndents: Unit =
val code = s"""
|class A:
| val x = 1
| val y = 2
|""".stripMargin
assert(parseTextEither(code).isLeft)

@Test
def superfluousIndents2: Unit =
val code = s"""
|class Test:
| test("hello")
| assert(1 == 1)
|""".stripMargin
assert(parseTextEither(code).isLeft)
18 changes: 18 additions & 0 deletions compiler/test/dotty/tools/dotc/parsing/IndentWidthTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dotty.tools
package dotc
package parsing

import org.junit.Test

class IndentWidthTest extends ScannerTest:

@Test
def innerObjectIndents: Unit =
val code = s"""
|object A:
| object B
| end B
|
| object C
|""".stripMargin
assert(scanTextEither(code).isRight)
Loading