Skip to content

Fix #1961: Allow * as an infix operator #3907

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 1 commit into from
Jan 25, 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
16 changes: 12 additions & 4 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,11 @@ object Parsers {
def infixOps(
first: Tree, canStartOperand: Token => Boolean, operand: () => Tree,
isType: Boolean = false,
notAnOperator: Name = nme.EMPTY,
isOperator: => Boolean = true,
maybePostfix: Boolean = false): Tree = {
val base = opStack
var top = first
while (isIdent && in.name != notAnOperator) {
while (isIdent && isOperator) {
val op = if (isType) typeIdent() else termIdent()
top = reduceStack(base, top, precedence(op.name), isLeftAssoc(op.name), op.name)
opStack = OpInfo(top, op, in.offset) :: opStack
Expand Down Expand Up @@ -797,8 +797,16 @@ object Parsers {
*/
def infixType(): Tree = infixTypeRest(refinedType())

/** 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)
}

def infixTypeRest(t: Tree): Tree =
infixOps(t, canStartTypeTokens, refinedType, isType = true, notAnOperator = nme.raw.STAR)
infixOps(t, canStartTypeTokens, refinedType, isType = true, isOperator = !isPostfixStar)

/** RefinedType ::= WithType {Annotation | [nl] Refinement}
*/
Expand Down Expand Up @@ -1556,7 +1564,7 @@ object Parsers {
/** InfixPattern ::= SimplePattern {id [nl] SimplePattern}
*/
def infixPattern(): Tree =
infixOps(simplePattern(), canStartExpressionTokens, simplePattern, notAnOperator = nme.raw.BAR)
infixOps(simplePattern(), canStartExpressionTokens, simplePattern, isOperator = in.name != nme.raw.BAR)

/** SimplePattern ::= PatVar
* | Literal
Expand Down
4 changes: 4 additions & 0 deletions tests/pos/i1961.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object InfixType {
trait *[N1, N2]
type Result = Int * Int
}