Skip to content

Commit

Permalink
Merge pull request #14441 from som-snytt/issue/14386
Browse files Browse the repository at this point in the history
Don't compute indents when in a string
  • Loading branch information
smarter authored Feb 19, 2022
2 parents 7b34af3 + 5c70a0c commit 62d54fe
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
11 changes: 6 additions & 5 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ object Scanners {
val next = newTokenData
private val prev = newTokenData

/** The current region. This is initially an Indented region with indentation width. */
/** The current region. This is initially an Indented region with zero indentation width. */
var currentRegion: Region = Indented(IndentWidth.Zero, Set(), EMPTY, null)

// Get next token ------------------------------------------------------------
Expand Down Expand Up @@ -434,8 +434,8 @@ object Scanners {
&& !migrateTo3
&& !noindentSyntax

/** The indentation width of the given offset */
def indentWidth(offset: Offset): IndentWidth = {
/** The indentation width of the given offset. */
def indentWidth(offset: Offset): IndentWidth =
import IndentWidth.{Run, Conc}
def recur(idx: Int, ch: Char, n: Int, k: IndentWidth => IndentWidth): IndentWidth =
if (idx < 0) k(Run(ch, n))
Expand All @@ -452,7 +452,7 @@ object Scanners {
else recur(idx - 1, ' ', 0, identity)
}
recur(offset - 1, ' ', 0, identity)
}
end indentWidth

/** Handle newlines, possibly inserting an INDENT, OUTDENT, NEWLINE, or NEWLINES token
* in front of the current token. This depends on whether indentation is significant or not.
Expand Down Expand Up @@ -521,6 +521,7 @@ object Scanners {
lastWidth = r.width
newlineIsSeparating = lastWidth <= nextWidth || r.isOutermost
indentPrefix = r.prefix
case _: InString => ()
case r =>
indentIsSignificant = indentSyntax
r.proposeKnownWidth(nextWidth, lastToken)
Expand Down Expand Up @@ -1422,7 +1423,7 @@ object Scanners {
nextToken()
currentRegion = topLevelRegion(indentWidth(offset))
}
// end Scanner
end Scanner

/** A Region indicates what encloses the current token. It can be one of the following
*
Expand Down
48 changes: 48 additions & 0 deletions tests/neg/i14386.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// scalac: -Werror
def logLevelDetail(level: Int): String =
s"""$level

// the following line is indented using [tab][tab]
Sets the global logging level to $level.
"""
/* was 2 errors with carets as shown
| ^ ^
| Incompatible combinations of tabs and spaces in indentation prefixes.
| Previous indent : 3 spaces
| Latest indent : 2 tabs
*/

def log(level: Int, msg: String): String =
s"""
$level
prefixed $level suffixed
"""
/*
^ ^
Incompatible combinations of tabs and spaces in indentation prefixes.
Previous indent : 2 tabs
Latest indent : 2 space
*/

// normal mixed tabs errors as a baseline
def g =
42
+ 17 // error

def p() =
println("hello")
println("world") // error
/*
| Incompatible combinations of tabs and spaces in indentation prefixes.
| Previous indent : 4 spaces
| Latest indent : 1 tab
*/

def braced() =
s"""begin
${
val level = 10
val msg = "hello, world" // error he lets me off with a warning
log(level, msg) // error
}
end"""

0 comments on commit 62d54fe

Please sign in to comment.