Skip to content

Fix #3909: Allow ~ as a standalone type identifier #3923

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 2 commits into from
Jan 26, 2018
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
17 changes: 10 additions & 7 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,13 @@ object Parsers {

def commaSeparated[T](part: () => T): List[T] = tokenSeparated(COMMA, part)

/** Is the token following the current one in `tokens`? */
def lookaheadIn(tokens: Token*): Boolean = {
val lookahead = in.lookaheadScanner
lookahead.nextToken()
tokens.contains(lookahead.token)
}

/* --------- OPERAND/OPERATOR STACK --------------------------------------- */

var opStack: List[OpInfo] = Nil
Expand Down Expand Up @@ -799,11 +806,7 @@ object Parsers {

/** Is current ident a `*`, and is it followed by a `)` or `,`? */
def isPostfixStar: Boolean =
in.name == nme.raw.STAR && {
val lookahead = in.lookaheadScanner
lookahead.nextToken()
(lookahead.token == RPAREN || lookahead.token == COMMA)
}
in.name == nme.raw.STAR && lookaheadIn(RPAREN, COMMA)

def infixTypeRest(t: Tree): Tree =
infixOps(t, canStartTypeTokens, refinedType, isType = true, isOperator = !isPostfixStar)
Expand Down Expand Up @@ -842,7 +845,7 @@ object Parsers {
/** SimpleType ::= SimpleType TypeArgs
* | SimpleType `#' id
* | StableId
* | [‘-’ | ‘+’ | ‘~’ | ‘!’] StableId
* | ['~'] StableId
* | Path `.' type
* | `(' ArgTypes `)'
* | `_' TypeBounds
Expand All @@ -861,7 +864,7 @@ object Parsers {
val start = in.skipToken()
typeBounds().withPos(Position(start, in.lastOffset, start))
}
else if (isIdent && nme.raw.isUnary(in.name))
else if (isIdent(nme.raw.TILDE) && lookaheadIn(IDENTIFIER, BACKQUOTED_IDENT))
atPos(in.offset) { PrefixOp(typeIdent(), path(thisOK = true)) }
else path(thisOK = false, handleSingletonType) match {
case r @ SingletonTypeTree(_) => r
Expand Down
8 changes: 8 additions & 0 deletions tests/pos/i3909.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ~(x: Int)
object Test {
new ~(1) // Syntax error
new `~`(1) // Syntax error

def ~(x: Int) = 1
~(1) // OK
}