Skip to content

Don't compute indents when in a string #14441

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 1 commit into from
Feb 19, 2022
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
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 @@ -1424,7 +1425,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"""