Skip to content
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

Avoid shadowing of name 'name' #13442

Merged
merged 4 commits into from Nov 16, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 18 additions & 21 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Expand Up @@ -135,10 +135,13 @@ object Scanners {
*/
protected def putChar(c: Char): Unit = litBuf.append(c)

/** Clear buffer and set name and token
* If `target` is different from `this`, don't treat identifiers as end tokens
/** Finish an IDENTIFIER with `this.name`. */
inline def finishNamed(): Unit = finishNamedToken(IDENTIFIER, this)

/** Clear buffer and set name and token.
* If `target` is different from `this`, don't treat identifiers as end tokens.
*/
def finishNamed(idtoken: Token = IDENTIFIER, target: TokenData = this): Unit =
def finishNamedToken(idtoken: Token, target: TokenData): Unit =
target.name = termName(litBuf.chars, 0, litBuf.length)
litBuf.clear()
target.token = idtoken
Expand Down Expand Up @@ -242,24 +245,18 @@ object Scanners {
/** A buffer for comments */
private val commentBuf = CharBuffer()

private def handleMigration(keyword: Token): Token =
if scala3keywords.contains(keyword) && migrateTo3 then treatAsIdent()
else keyword

private def treatAsIdent(): Token =
val name0 = name // don't capture the `name` var in the message closure, it may be null later
report.errorOrMigrationWarning(
i"$name0 is now a keyword, write `$name0` instead of $name0 to keep it as an identifier",
sourcePos())
patch(source, Span(offset), "`")
patch(source, Span(offset + name.length), "`")
IDENTIFIER

def toToken(name: SimpleName): Token = {
val idx = name.start
def toToken(identifier: SimpleName): Token =
def handleMigration(keyword: Token): Token =
if scala3keywords.contains(keyword) && migrateTo3 then
val what = tokenString(keyword)
report.errorOrMigrationWarning(i"$what is now a keyword, write `$what` instead of $what to keep it as an identifier", sourcePos())
patch(source, Span(offset), "`")
patch(source, Span(offset + identifier.length), "`")
IDENTIFIER
else keyword
val idx = identifier.start
if (idx >= 0 && idx <= lastKeywordStart) handleMigration(kwArray(idx))
else IDENTIFIER
}

def newTokenData: TokenData = new TokenData {}

Expand Down Expand Up @@ -1002,7 +999,7 @@ object Scanners {
getLitChars('`')
if (ch == '`') {
nextChar()
finishNamed(BACKQUOTED_IDENT)
finishNamedToken(BACKQUOTED_IDENT, target = this)
if (name.length == 0)
error("empty quoted identifier")
else if (name == nme.WILDCARD)
Expand Down Expand Up @@ -1168,7 +1165,7 @@ object Scanners {
nextRawChar()
ch != SU && Character.isUnicodeIdentifierPart(ch)
do ()
finishNamed(target = next)
finishNamedToken(IDENTIFIER, target = next)
}
else
error("invalid string interpolation: `$$`, `$\"`, `$`ident or `$`BlockExpr expected")
Expand Down
12 changes: 10 additions & 2 deletions tests/neg-custom-args/fatal-warnings/i13440.check
@@ -1,4 +1,12 @@
-- Error: tests/neg-custom-args/fatal-warnings/i13440.scala:3:13 -------------------------------------------------------
3 |case class A(enum: List[Int] = Nil) // error
-- Error: tests/neg-custom-args/fatal-warnings/i13440.scala:3:4 --------------------------------------------------------
3 |def given = 42 // error
| ^
| given is now a keyword, write `given` instead of given to keep it as an identifier
-- Error: tests/neg-custom-args/fatal-warnings/i13440.scala:5:13 -------------------------------------------------------
5 |case class C(enum: List[Int] = Nil) { // error
| ^
| enum is now a keyword, write `enum` instead of enum to keep it as an identifier
-- Error: tests/neg-custom-args/fatal-warnings/i13440.scala:6:11 -------------------------------------------------------
6 | val s = s"$enum" // error
| ^
| enum is now a keyword, write `enum` instead of enum to keep it as an identifier
6 changes: 5 additions & 1 deletion tests/neg-custom-args/fatal-warnings/i13440.scala
@@ -1,3 +1,7 @@
import language.`3.0-migration`

case class A(enum: List[Int] = Nil) // error
def given = 42 // error

case class C(enum: List[Int] = Nil) { // error
val s = s"$enum" // error
}