Skip to content

Commit ea18e25

Browse files
authored
Merge pull request #3923 from dotty-staging/fix-#3909
Fix #3909: Allow ~ as a standalone type identifier
2 parents e7f27a1 + 3b42639 commit ea18e25

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

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

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

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

435+
/** Is the token following the current one in `tokens`? */
436+
def lookaheadIn(tokens: Token*): Boolean = {
437+
val lookahead = in.lookaheadScanner
438+
lookahead.nextToken()
439+
tokens.contains(lookahead.token)
440+
}
441+
435442
/* --------- OPERAND/OPERATOR STACK --------------------------------------- */
436443

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

800807
/** Is current ident a `*`, and is it followed by a `)` or `,`? */
801808
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-
}
809+
in.name == nme.raw.STAR && lookaheadIn(RPAREN, COMMA)
807810

808811
def infixTypeRest(t: Tree): Tree =
809812
infixOps(t, canStartTypeTokens, refinedType, isType = true, isOperator = !isPostfixStar)
@@ -842,7 +845,7 @@ object Parsers {
842845
/** SimpleType ::= SimpleType TypeArgs
843846
* | SimpleType `#' id
844847
* | StableId
845-
* | [‘-’ | ‘+’ | ‘~’ | ‘!’] StableId
848+
* | ['~'] StableId
846849
* | Path `.' type
847850
* | `(' ArgTypes `)'
848851
* | `_' TypeBounds
@@ -861,7 +864,7 @@ object Parsers {
861864
val start = in.skipToken()
862865
typeBounds().withPos(Position(start, in.lastOffset, start))
863866
}
864-
else if (isIdent && nme.raw.isUnary(in.name))
867+
else if (isIdent(nme.raw.TILDE) && lookaheadIn(IDENTIFIER, BACKQUOTED_IDENT))
865868
atPos(in.offset) { PrefixOp(typeIdent(), path(thisOK = true)) }
866869
else path(thisOK = false, handleSingletonType) match {
867870
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)