Skip to content

Commit 5b1b747

Browse files
authored
Merge pull request #3907 from dotty-staging/fix-#1961
Fix #1961: Allow `*` as an infix operator
2 parents 78bd3dd + d3b6a98 commit 5b1b747

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,11 @@ object Parsers {
470470
def infixOps(
471471
first: Tree, canStartOperand: Token => Boolean, operand: () => Tree,
472472
isType: Boolean = false,
473-
notAnOperator: Name = nme.EMPTY,
473+
isOperator: => Boolean = true,
474474
maybePostfix: Boolean = false): Tree = {
475475
val base = opStack
476476
var top = first
477-
while (isIdent && in.name != notAnOperator) {
477+
while (isIdent && isOperator) {
478478
val op = if (isType) typeIdent() else termIdent()
479479
top = reduceStack(base, top, precedence(op.name), isLeftAssoc(op.name), op.name)
480480
opStack = OpInfo(top, op, in.offset) :: opStack
@@ -797,8 +797,16 @@ object Parsers {
797797
*/
798798
def infixType(): Tree = infixTypeRest(refinedType())
799799

800+
/** Is current ident a `*`, and is it followed by a `)` or `,`? */
801+
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+
}
807+
800808
def infixTypeRest(t: Tree): Tree =
801-
infixOps(t, canStartTypeTokens, refinedType, isType = true, notAnOperator = nme.raw.STAR)
809+
infixOps(t, canStartTypeTokens, refinedType, isType = true, isOperator = !isPostfixStar)
802810

803811
/** RefinedType ::= WithType {Annotation | [nl] Refinement}
804812
*/
@@ -1556,7 +1564,7 @@ object Parsers {
15561564
/** InfixPattern ::= SimplePattern {id [nl] SimplePattern}
15571565
*/
15581566
def infixPattern(): Tree =
1559-
infixOps(simplePattern(), canStartExpressionTokens, simplePattern, notAnOperator = nme.raw.BAR)
1567+
infixOps(simplePattern(), canStartExpressionTokens, simplePattern, isOperator = in.name != nme.raw.BAR)
15601568

15611569
/** SimplePattern ::= PatVar
15621570
* | Literal

tests/pos/i1961.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object InfixType {
2+
trait *[N1, N2]
3+
type Result = Int * Int
4+
}

0 commit comments

Comments
 (0)