Skip to content

Commit 62d54fe

Browse files
authored
Merge pull request #14441 from som-snytt/issue/14386
Don't compute indents when in a string
2 parents 7b34af3 + 5c70a0c commit 62d54fe

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ object Scanners {
265265
val next = newTokenData
266266
private val prev = newTokenData
267267

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

271271
// Get next token ------------------------------------------------------------
@@ -434,8 +434,8 @@ object Scanners {
434434
&& !migrateTo3
435435
&& !noindentSyntax
436436

437-
/** The indentation width of the given offset */
438-
def indentWidth(offset: Offset): IndentWidth = {
437+
/** The indentation width of the given offset. */
438+
def indentWidth(offset: Offset): IndentWidth =
439439
import IndentWidth.{Run, Conc}
440440
def recur(idx: Int, ch: Char, n: Int, k: IndentWidth => IndentWidth): IndentWidth =
441441
if (idx < 0) k(Run(ch, n))
@@ -452,7 +452,7 @@ object Scanners {
452452
else recur(idx - 1, ' ', 0, identity)
453453
}
454454
recur(offset - 1, ' ', 0, identity)
455-
}
455+
end indentWidth
456456

457457
/** Handle newlines, possibly inserting an INDENT, OUTDENT, NEWLINE, or NEWLINES token
458458
* in front of the current token. This depends on whether indentation is significant or not.
@@ -521,6 +521,7 @@ object Scanners {
521521
lastWidth = r.width
522522
newlineIsSeparating = lastWidth <= nextWidth || r.isOutermost
523523
indentPrefix = r.prefix
524+
case _: InString => ()
524525
case r =>
525526
indentIsSignificant = indentSyntax
526527
r.proposeKnownWidth(nextWidth, lastToken)
@@ -1422,7 +1423,7 @@ object Scanners {
14221423
nextToken()
14231424
currentRegion = topLevelRegion(indentWidth(offset))
14241425
}
1425-
// end Scanner
1426+
end Scanner
14261427

14271428
/** A Region indicates what encloses the current token. It can be one of the following
14281429
*

tests/neg/i14386.scala

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// scalac: -Werror
2+
def logLevelDetail(level: Int): String =
3+
s"""$level
4+
5+
// the following line is indented using [tab][tab]
6+
Sets the global logging level to $level.
7+
"""
8+
/* was 2 errors with carets as shown
9+
| ^ ^
10+
| Incompatible combinations of tabs and spaces in indentation prefixes.
11+
| Previous indent : 3 spaces
12+
| Latest indent : 2 tabs
13+
*/
14+
15+
def log(level: Int, msg: String): String =
16+
s"""
17+
$level
18+
prefixed $level suffixed
19+
"""
20+
/*
21+
^ ^
22+
Incompatible combinations of tabs and spaces in indentation prefixes.
23+
Previous indent : 2 tabs
24+
Latest indent : 2 space
25+
*/
26+
27+
// normal mixed tabs errors as a baseline
28+
def g =
29+
42
30+
+ 17 // error
31+
32+
def p() =
33+
println("hello")
34+
println("world") // error
35+
/*
36+
| Incompatible combinations of tabs and spaces in indentation prefixes.
37+
| Previous indent : 4 spaces
38+
| Latest indent : 1 tab
39+
*/
40+
41+
def braced() =
42+
s"""begin
43+
${
44+
val level = 10
45+
val msg = "hello, world" // error he lets me off with a warning
46+
log(level, msg) // error
47+
}
48+
end"""

0 commit comments

Comments
 (0)