Skip to content

Commit 8fb14ff

Browse files
committed
Fix #3909: Allow ~ is a standalone type identifier
We previously always parsed as a prefix operator. Another change is that we now accept only ~ as type prefix operator. That's in preparation of giving this a special syntax and not allowing it as a user-defined operator.
1 parent 5b1b747 commit 8fb14ff

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ object Parsers {
432432

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

435+
def lookaheadIn(tokens: Token*): Boolean =
436+
tokens.contains(in.lookaheadScanner.nextToken())
437+
435438
/* --------- OPERAND/OPERATOR STACK --------------------------------------- */
436439

437440
var opStack: List[OpInfo] = Nil
@@ -799,11 +802,7 @@ object Parsers {
799802

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

808807
def infixTypeRest(t: Tree): Tree =
809808
infixOps(t, canStartTypeTokens, refinedType, isType = true, isOperator = !isPostfixStar)
@@ -842,7 +841,7 @@ object Parsers {
842841
/** SimpleType ::= SimpleType TypeArgs
843842
* | SimpleType `#' id
844843
* | StableId
845-
* | [‘-’ | ‘+’ | ‘~’ | ‘!’] StableId
844+
* | ['~'] StableId
846845
* | Path `.' type
847846
* | `(' ArgTypes `)'
848847
* | `_' TypeBounds
@@ -861,7 +860,7 @@ object Parsers {
861860
val start = in.skipToken()
862861
typeBounds().withPos(Position(start, in.lastOffset, start))
863862
}
864-
else if (isIdent && nme.raw.isUnary(in.name))
863+
else if (isIdent(nme.raw.TILDE) && lookaheadIn(IDENTIFIER, BACKQUOTED_IDENT))
865864
atPos(in.offset) { PrefixOp(typeIdent(), path(thisOK = true)) }
866865
else path(thisOK = false, handleSingletonType) match {
867866
case r @ SingletonTypeTree(_) => r

tests/pos/i3909.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class ~(x: Int)
2+
object Test {
3+
new ~(1) // Syntax error
4+
new `~`(1) // Syntax error
5+
6+
def ~(x: Int) = 1
7+
~(1) // OK
8+
}

0 commit comments

Comments
 (0)