Skip to content

Commit b311b4e

Browse files
authored
Merge pull request #13442 from som-snytt/issue/13440
Avoid shadowing of name 'name'
2 parents f215817 + fd4be37 commit b311b4e

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,13 @@ object Scanners {
135135
*/
136136
protected def putChar(c: Char): Unit = litBuf.append(c)
137137

138-
/** Clear buffer and set name and token
139-
* If `target` is different from `this`, don't treat identifiers as end tokens
138+
/** Finish an IDENTIFIER with `this.name`. */
139+
inline def finishNamed(): Unit = finishNamedToken(IDENTIFIER, this)
140+
141+
/** Clear buffer and set name and token.
142+
* If `target` is different from `this`, don't treat identifiers as end tokens.
140143
*/
141-
def finishNamed(idtoken: Token = IDENTIFIER, target: TokenData = this): Unit =
144+
def finishNamedToken(idtoken: Token, target: TokenData): Unit =
142145
target.name = termName(litBuf.chars, 0, litBuf.length)
143146
litBuf.clear()
144147
target.token = idtoken
@@ -242,24 +245,18 @@ object Scanners {
242245
/** A buffer for comments */
243246
private val commentBuf = CharBuffer()
244247

245-
private def handleMigration(keyword: Token): Token =
246-
if scala3keywords.contains(keyword) && migrateTo3 then treatAsIdent()
247-
else keyword
248-
249-
private def treatAsIdent(): Token =
250-
val name0 = name // don't capture the `name` var in the message closure, it may be null later
251-
report.errorOrMigrationWarning(
252-
i"$name0 is now a keyword, write `$name0` instead of $name0 to keep it as an identifier",
253-
sourcePos())
254-
patch(source, Span(offset), "`")
255-
patch(source, Span(offset + name.length), "`")
256-
IDENTIFIER
257-
258-
def toToken(name: SimpleName): Token = {
259-
val idx = name.start
248+
def toToken(identifier: SimpleName): Token =
249+
def handleMigration(keyword: Token): Token =
250+
if scala3keywords.contains(keyword) && migrateTo3 then
251+
val what = tokenString(keyword)
252+
report.errorOrMigrationWarning(i"$what is now a keyword, write `$what` instead of $what to keep it as an identifier", sourcePos())
253+
patch(source, Span(offset), "`")
254+
patch(source, Span(offset + identifier.length), "`")
255+
IDENTIFIER
256+
else keyword
257+
val idx = identifier.start
260258
if (idx >= 0 && idx <= lastKeywordStart) handleMigration(kwArray(idx))
261259
else IDENTIFIER
262-
}
263260

264261
def newTokenData: TokenData = new TokenData {}
265262

@@ -1002,7 +999,7 @@ object Scanners {
1002999
getLitChars('`')
10031000
if (ch == '`') {
10041001
nextChar()
1005-
finishNamed(BACKQUOTED_IDENT)
1002+
finishNamedToken(BACKQUOTED_IDENT, target = this)
10061003
if (name.length == 0)
10071004
error("empty quoted identifier")
10081005
else if (name == nme.WILDCARD)
@@ -1168,7 +1165,7 @@ object Scanners {
11681165
nextRawChar()
11691166
ch != SU && Character.isUnicodeIdentifierPart(ch)
11701167
do ()
1171-
finishNamed(target = next)
1168+
finishNamedToken(IDENTIFIER, target = next)
11721169
}
11731170
else
11741171
error("invalid string interpolation: `$$`, `$\"`, `$`ident or `$`BlockExpr expected")
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
-- Error: tests/neg-custom-args/fatal-warnings/i13440.scala:3:13 -------------------------------------------------------
2-
3 |case class A(enum: List[Int] = Nil) // error
1+
-- Error: tests/neg-custom-args/fatal-warnings/i13440.scala:3:4 --------------------------------------------------------
2+
3 |def given = 42 // error
3+
| ^
4+
| given is now a keyword, write `given` instead of given to keep it as an identifier
5+
-- Error: tests/neg-custom-args/fatal-warnings/i13440.scala:5:13 -------------------------------------------------------
6+
5 |case class C(enum: List[Int] = Nil) { // error
37
| ^
48
| enum is now a keyword, write `enum` instead of enum to keep it as an identifier
9+
-- Error: tests/neg-custom-args/fatal-warnings/i13440.scala:6:11 -------------------------------------------------------
10+
6 | val s = s"$enum" // error
11+
| ^
12+
| enum is now a keyword, write `enum` instead of enum to keep it as an identifier
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import language.`3.0-migration`
22

3-
case class A(enum: List[Int] = Nil) // error
3+
def given = 42 // error
4+
5+
case class C(enum: List[Int] = Nil) { // error
6+
val s = s"$enum" // error
7+
}

0 commit comments

Comments
 (0)